fix(web): stale-closure + focus trap + 去边界强转 + a11y/性能打磨 + 类型化客户端
P1-6 useStyleLearn/useKimiOauth 修 stale-closure 依赖。 P1-7 CommandPalette/Drawer 加 focus trap(新 lib/a11y/focusTrap)。 P1-8 SSE 解析与 server.ts 去 as 强转改类型守卫/运行时校验。 P2 删冗余强转、开 noUncheckedIndexedAccess、Toast 清理+稳定 key、列表 key 稳定化、 rel=noopener、useMemo 大文本、ConflictCard role=group、useInjection 加锁、 client.test 真断言。 codegen 重生成 schema.d.ts + 消费 DimensionEntry/ReviewConflictView 强类型。
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
import { useEffect, useRef, type ReactNode } from "react";
|
||||
|
||||
import { handleTabTrap } from "@/lib/a11y/focusTrap";
|
||||
|
||||
interface DrawerProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
@@ -49,6 +51,10 @@ export function Drawer({
|
||||
aria-modal="true"
|
||||
aria-label={label}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
onKeyDown={(e) => {
|
||||
// focus trap:Tab/Shift+Tab 在抽屉内循环,不逃逸到背景(WCAG 2.1.2)。
|
||||
if (panelRef.current) handleTabTrap(panelRef.current, e);
|
||||
}}
|
||||
className={`absolute top-0 h-full w-64 max-w-[80vw] overflow-auto border-line bg-panel py-4 shadow-paper outline-none ${sideClass}`}
|
||||
>
|
||||
{children}
|
||||
|
||||
@@ -4,7 +4,9 @@ import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
@@ -25,13 +27,28 @@ const TOAST_TTL_MS = 4000;
|
||||
|
||||
export function ToastProvider({ children }: { children: ReactNode }) {
|
||||
const [items, setItems] = useState<ToastItem[]>([]);
|
||||
// 单调计数器作 id(不再用 Date.now()+random,避免极小概率碰撞且稳定可测)。
|
||||
const nextIdRef = useRef(0);
|
||||
// 在途定时器集合:卸载时统一清理,避免 setState-after-unmount 泄漏。
|
||||
const timersRef = useRef<Set<ReturnType<typeof setTimeout>>>(new Set());
|
||||
|
||||
const show = useCallback<ShowToast>((message, kind = "info") => {
|
||||
const id = Date.now() + Math.random();
|
||||
const id = nextIdRef.current++;
|
||||
setItems((prev) => [...prev, { id, message, kind }]);
|
||||
setTimeout(() => {
|
||||
const timer = setTimeout(() => {
|
||||
timersRef.current.delete(timer);
|
||||
setItems((prev) => prev.filter((t) => t.id !== id));
|
||||
}, TOAST_TTL_MS);
|
||||
timersRef.current.add(timer);
|
||||
}, []);
|
||||
|
||||
// 卸载清理所有在途定时器。
|
||||
useEffect(() => {
|
||||
const timers = timersRef.current;
|
||||
return () => {
|
||||
timers.forEach((t) => clearTimeout(t));
|
||||
timers.clear();
|
||||
};
|
||||
}, []);
|
||||
|
||||
const value = useMemo(() => show, [show]);
|
||||
|
||||
@@ -10,11 +10,12 @@ import {
|
||||
projectCommands,
|
||||
type Command,
|
||||
} from "@/lib/command/palette";
|
||||
import { handleTabTrap } from "@/lib/a11y/focusTrap";
|
||||
|
||||
// 从 pathname 抽取当前 projectId(/projects/<id>/...)。
|
||||
function projectIdFromPath(pathname: string): string | null {
|
||||
const m = pathname.match(/^\/projects\/([^/]+)/);
|
||||
return m ? m[1] : null;
|
||||
return m?.[1] ?? null;
|
||||
}
|
||||
|
||||
interface CommandPaletteProps {
|
||||
@@ -29,6 +30,7 @@ export function CommandPalette({ pathname }: CommandPaletteProps) {
|
||||
const [query, setQuery] = useState("");
|
||||
const [highlight, setHighlight] = useState(0);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const dialogRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const projectId = projectIdFromPath(pathname);
|
||||
const commands = useMemo<Command[]>(
|
||||
@@ -102,11 +104,16 @@ export function CommandPalette({ pathname }: CommandPaletteProps) {
|
||||
onClick={close}
|
||||
>
|
||||
<div
|
||||
ref={dialogRef}
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label="命令面板"
|
||||
className="w-full max-w-lg overflow-hidden rounded-lg border border-line bg-panel shadow-paper"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
onKeyDown={(e) => {
|
||||
// focus trap:Tab/Shift+Tab 在对话框内循环,不逃逸到背景(WCAG 2.1.2)。
|
||||
if (dialogRef.current) handleTabTrap(dialogRef.current, e);
|
||||
}}
|
||||
>
|
||||
<input
|
||||
ref={inputRef}
|
||||
|
||||
@@ -32,7 +32,10 @@ export function ConflictAdjudication({
|
||||
</p>
|
||||
<ul className="mb-3 flex flex-col gap-2">
|
||||
{conflicts.conflicts.map((c, i) => (
|
||||
<li key={i} className="rounded border border-line bg-bg p-2 text-sm">
|
||||
<li
|
||||
key={`${c.type}-${c.where}-${i}`}
|
||||
className="rounded border border-line bg-bg p-2 text-sm"
|
||||
>
|
||||
<div className="mb-1 flex items-center gap-2">
|
||||
<span className="rounded bg-conflict/10 px-2 py-0.5 text-xs text-conflict">
|
||||
{c.type}
|
||||
|
||||
@@ -26,7 +26,7 @@ export function AnnotatedText({
|
||||
<div className="mb-3 flex flex-wrap gap-2 border-b border-line pb-3">
|
||||
{conflicts.map((c, i) => (
|
||||
<button
|
||||
key={i}
|
||||
key={`${c.type}-${c.where}-${i}`}
|
||||
type="button"
|
||||
id={`anchor-${i}`}
|
||||
onClick={() => onAnchorClick(i)}
|
||||
|
||||
@@ -89,8 +89,10 @@ export function ConflictCard({
|
||||
))}
|
||||
</div>
|
||||
|
||||
<fieldset className="mt-3">
|
||||
<legend className="sr-only">冲突 {index + 1} 裁决</legend>
|
||||
<div role="group" aria-labelledby={`verdict-label-${index}`} className="mt-3">
|
||||
<span id={`verdict-label-${index}`} className="sr-only">
|
||||
冲突 {index + 1} 裁决
|
||||
</span>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
{VERDICT_OPTIONS.map((opt) => {
|
||||
const active = draft.verdict === opt.value;
|
||||
@@ -132,7 +134,7 @@ export function ConflictCard({
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
</fieldset>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import Link from "next/link";
|
||||
|
||||
import { AppShell } from "@/components/AppShell";
|
||||
import type { ProjectResponse, ReviewHistoryItem } from "@/lib/api/types";
|
||||
@@ -185,11 +186,10 @@ export function ReviewReport({
|
||||
}
|
||||
};
|
||||
|
||||
// 漂移段 idx → 终稿对应段正文(按空行切段;越界则空串)。
|
||||
const segmentText = (idx: number): string => {
|
||||
const paras = finalText.split(/\n{2,}/);
|
||||
return paras[idx]?.trim() ?? "";
|
||||
};
|
||||
// 终稿按空行切段(大文本:仅在 finalText 变化时重算,不在每次 render split)。
|
||||
const finalParas = useMemo(() => finalText.split(/\n{2,}/), [finalText]);
|
||||
// 漂移段 idx → 终稿对应段正文(越界则空串)。
|
||||
const segmentText = (idx: number): string => finalParas[idx]?.trim() ?? "";
|
||||
|
||||
// 采纳:把重写段替换终稿中的原段(首个匹配),合入终稿(经既有 draft 路径)。
|
||||
const onAdopt = (original: string, refined: string): void => {
|
||||
@@ -407,12 +407,12 @@ function ReviewErrorNote({
|
||||
{friendly.actionHref ? (
|
||||
<>
|
||||
{" "}
|
||||
<a
|
||||
<Link
|
||||
href={friendly.actionHref}
|
||||
className="underline hover:text-cinnabar"
|
||||
>
|
||||
{friendly.actionLabel}
|
||||
</a>
|
||||
</Link>
|
||||
</>
|
||||
) : null}
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user