Code test

Prompt

You are reviewing React 19 + TypeScript code. It animates an AI agent that rewrites the lines of a rΓ©sumΓ©, then places job keywords. **Intended behaviour:** - **Agent tag.** It sits right of the rΓ©sumΓ© page when there is room, otherwise a frame goes around the line. The page is inside a scrolling column; from 1280 px a score column sits to the right of that column. The tag must never be hidden under the score column. - **Rewriting a line.** The component that renders `live.typing` strikes the old text (`STRIKE_MS`), types the new text (`typeMs`), then flashes lime for `FLASH_MS`. It unmounts as soon as `typing` changes. - **Keyword chips** (pending, placed, missing) show during every run, including a resumed one. - **Resume.** `run(from, done)` continues a stopped run: `from` is the step index (0 read, 1 lines, 2 keywords), and `done` lists the lines already rewritten. - **Stop, the end of a run and the end of a replay** all return to rest: no tag, no flight, no replay state. - **Replay** stops on any scroll, click or key, and its own button stops it. - **Reduced motion:** no flight. A stop or an unmount leaves nothing on screen. ```tsx import { useCallback, useEffect, useRef, useState } from "react"; type Chip = { term: string; state: "pending" | "placed" | "missing" }; type Live = { agentId?: string; task?: string; typing?: string; flying?: string; chips?: Chip[]; placed: string[]; replaying?: boolean }; const STRIKE_MS = 300, FLASH_MS = 1200, FLIGHT_MS = 700; const typeMs = (t: string) => Math.min(t.length * 6, 700); const IDLE: Live = { placed: [] }; function useTimers() { const ids = useRef<number[]>([]); const clear = useCallback(() => { ids.current.forEach(clearTimeout); ids.current = []; }, []); useEffect(() => clear, [clear]); return { ids, clear }; } export function useRun(lines: Record<string, string>, rewritten: string[], added: string[], missing: string[]) { const [live, setLive] = useState<Live>(IDLE); const timers = useTimers(); const patch = (p: Partial<Live>) => setLive((s) => ({ ...s, ...p })); const chips = (placed: string[]): Chip[] => [ ...added.map((term) => ({ term, state: placed.includes(term) ? ("placed" as const) : ("pending" as const) })), ...missing.map((term) => ({ term, state: "missing" as const })), ]; // Each step: [wait after it, what it does]. Steps run in order. function schedule(steps: [number, () => void][]) { let at = 0; for (const [ms, fn] of steps) { timers.ids.current.push(window.setTimeout(fn, at)); at += ms; } } function run(from = 0, done: string[] = []) { const steps: [number, () => void][] = []; if (from < 1) { steps.push([0, () => patch({ chips: chips([]) })]); steps.push([700, () => patch({ agentId: "summary", task: "read" })]); } for (const id of rewritten.filter((x) => !done.includes(x))) { steps.push([STRIKE_MS + typeMs(lines[id]), () => patch({ agentId: id, task: "rewrite", typing: id })]); } const placed: string[] = []; for (const term of added) { steps.push([0, () => patch({ task: "keywords", flying: term })]); steps.push([FLIGHT_MS + 250, () => { placed.push(term); patch({ flying: undefined, placed: [...placed], chips: chips([...placed]) }); }]); } steps.push([300, () => setLive((s) => ({ ...s, ...IDLE }))]); schedule(steps); } function stop() { timers.clear(); setLive((s) => ({ ...s, ...IDLE })); } function replay() { const steps: [number, () => void][] = rewritten.map((id) => [STRIKE_MS + typeMs(lines[id]) + 400, () => patch({ agentId: id, task: "replay", typing: id, replaying: true })]); steps.push([0, () => setLive((s) => ({ ...s, ...IDLE }))]); schedule(steps); } return { live, run, stop, replay }; } export function useStopReplayOnInput(main: HTMLElement | null, replaying: boolean, stop: () => void) { useEffect(() => { if (!main || !replaying) return; main.addEventListener("wheel", stop, { passive: true }); main.addEventListener("pointerdown", stop); window.addEventListener("keydown", stop); return () => { main.removeEventListener("wheel", stop); main.removeEventListener("pointerdown", stop); window.removeEventListener("keydown", stop); }; }, [main, replaying, stop]); } export function ReplayButton({ playing, onPlay, onStop }: { playing: boolean; onPlay: () => void; onStop: () => void }) { return playing ? <button onClick={onStop}>Stop</button> : <button onClick={onPlay}>Replay changes</button>; } export function useKeywordFlight(flying: string | undefined, scope: HTMLElement | null) { useEffect(() => { if (!flying || !scope) return; const from = document.querySelector<HTMLElement>(`[data-chip="${flying}"]`); const to = findText(scope, flying); // returns a Range or null if (!from || !to) return; const a = from.getBoundingClientRect(), b = to.getBoundingClientRect(); const chip = document.createElement("span"); chip.textContent = flying; chip.className = "fixed z-50 rounded-full bg-lime px-2 text-xs"; chip.style.left = `${a.left}px`; chip.style.top = `${a.top}px`; document.body.appendChild(chip); const anim = chip.animate([{ transform: "none" }, { transform: `translate(${b.left - a.left}px, ${b.top - a.top}px)` }], { duration: FLIGHT_MS, easing: "cubic-bezier(.22,1,.36,1)" }); anim.onfinish = () => chip.remove(); }, [flying, scope]); } export function tagSide(line: HTMLElement): "right" | "frame" { const page = line.closest<HTMLElement>("[data-template]")!.getBoundingClientRect(); return window.innerWidth - page.right - 24 >= 200 ? "right" : "frame"; } declare function findText(scope: HTMLElement, term: string): Range | null; ``` **Find the bugs that break the intended behaviour.** Answer in 10 lines at most, one bug per line: - the function; - what the person sees go wrong; - the fix, in a few words. No rewritten code and no style remarks. Order the bugs from the most visible to the least.

Response not available

Drag to resize
Drag to resize
Drag to resize
Drag to resize
Alibaba logoAlibaba
Qwen4 Max
Drag to resize