mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
* perf(studio): the lint error badge stops pulsing after three beats The badge pulsed forever, so a Studio with a lint error painted a frame on every display refresh, idle or paused. It now pulses three times and holds. * fix(studio): the lint badge counts findings, never files The badge showed the manual result's finding count while its dialog was open and the number of files with findings once it closed, while its screen-reader label always said findings. The hook now owns one count, the findings in the list the error pulse also reads, and the badge shows it in both states. * fix(studio): the lint badge pulses again when its finding count changes A CSS animation does not restart on a text change, so after its three pulses a background lint that found more problems left the badge still. The badge is keyed on its count, so a new count mounts a new element and pulses three more times. * refactor(studio): the lint badge names its pulse count, and App reads the hook as one object The pulse count moves from a comment into a constant name, so the file's comment share does not rise. App takes useLintModal as one object instead of a nine-line destructure, which keeps App.tsx under 600 lines once merged with main.
124 lines
4.1 KiB
TypeScript
124 lines
4.1 KiB
TypeScript
import { buildProjectApiPath } from "../utils/projectRouting";
|
|
import { useState, useCallback, useEffect, useRef, useMemo } from "react";
|
|
import type { LintFinding } from "../components/LintModal";
|
|
import { usePlayerStore } from "../player";
|
|
import { isPreviewBooted, whenPreviewBooted } from "../player/store/playerStore";
|
|
|
|
interface RawFinding {
|
|
severity?: string;
|
|
message?: string;
|
|
file?: string;
|
|
fixHint?: string;
|
|
elementId?: string;
|
|
selector?: string;
|
|
code?: string;
|
|
}
|
|
|
|
function parseFinding(f: RawFinding): LintFinding & { elementId?: string; file?: string } {
|
|
return {
|
|
severity: f.severity === "error" ? ("error" as const) : ("warning" as const),
|
|
message: f.message ?? "",
|
|
file: f.file,
|
|
fixHint: f.fixHint,
|
|
elementId: f.elementId,
|
|
};
|
|
}
|
|
|
|
async function fetchLintFindings(projectId: string) {
|
|
const res = await fetch(buildProjectApiPath(projectId, `/lint`));
|
|
const data = await res.json();
|
|
return ((data.findings ?? []) as RawFinding[]).map(parseFinding);
|
|
}
|
|
|
|
export function useLintModal(projectId: string | null, refreshKey?: number) {
|
|
const [lintModal, setLintModal] = useState<LintFinding[] | null>(null);
|
|
const [linting, setLinting] = useState(false);
|
|
const [backgroundFindings, setBackgroundFindings] = useState<
|
|
Array<LintFinding & { elementId?: string; file?: string }>
|
|
>([]);
|
|
const autoLintRanRef = useRef(false);
|
|
|
|
const runBackgroundLint = useCallback(async () => {
|
|
if (!projectId) return;
|
|
if (!isPreviewBooted(projectId) && !(await whenPreviewBooted(projectId))) return;
|
|
try {
|
|
setBackgroundFindings(await fetchLintFindings(projectId));
|
|
} catch {}
|
|
}, [projectId]);
|
|
|
|
const handleLint = useCallback(async () => {
|
|
if (!projectId) return;
|
|
setLinting(true);
|
|
try {
|
|
const parsed = await fetchLintFindings(projectId);
|
|
setLintModal(parsed);
|
|
setBackgroundFindings(parsed);
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
setLintModal([{ severity: "error", message: `Failed to run lint: ${msg}` }]);
|
|
} finally {
|
|
setLinting(false);
|
|
}
|
|
}, [projectId]);
|
|
|
|
const prevProjectIdRef = useRef(projectId);
|
|
useEffect(() => {
|
|
if (projectId !== prevProjectIdRef.current) {
|
|
autoLintRanRef.current = false;
|
|
prevProjectIdRef.current = projectId;
|
|
}
|
|
if (!projectId || autoLintRanRef.current) return;
|
|
autoLintRanRef.current = true;
|
|
void runBackgroundLint();
|
|
}, [projectId, runBackgroundLint]);
|
|
|
|
useEffect(() => {
|
|
if (!projectId || !refreshKey) return;
|
|
const timer = setTimeout(() => void runBackgroundLint(), 1000);
|
|
return () => clearTimeout(timer);
|
|
}, [projectId, refreshKey, runBackgroundLint]);
|
|
|
|
const closeLintModal = useCallback(() => setLintModal(null), []);
|
|
|
|
const groupFindings = useCallback(
|
|
(keyFn: (f: (typeof backgroundFindings)[0]) => string | undefined) => {
|
|
const map = new Map<string, { count: number; messages: string[] }>();
|
|
for (const f of backgroundFindings) {
|
|
const key = keyFn(f);
|
|
if (!key) continue;
|
|
const prev = map.get(key) ?? { count: 0, messages: [] };
|
|
prev.count += 1;
|
|
prev.messages.push(f.message);
|
|
map.set(key, prev);
|
|
}
|
|
return map;
|
|
},
|
|
[backgroundFindings],
|
|
);
|
|
|
|
const findingsByElement = useMemo(() => groupFindings((f) => f.elementId), [groupFindings]);
|
|
const findingsByFile = useMemo(() => groupFindings((f) => f.file), [groupFindings]);
|
|
// The badge counts findings, not files: the manual result when present, else the background one.
|
|
const badgeFindings = lintModal ?? backgroundFindings;
|
|
const lintFindingCount = badgeFindings.length;
|
|
const hasLintError = badgeFindings.some((f) => f.severity === "error");
|
|
|
|
// Sync lint findings directly to the player store — eliminates the
|
|
// mirroring useEffect that was previously in App.tsx.
|
|
useEffect(() => {
|
|
usePlayerStore.getState().setLintFindingsByElement(findingsByElement);
|
|
}, [findingsByElement]);
|
|
|
|
return {
|
|
lintModal,
|
|
linting,
|
|
handleLint,
|
|
closeLintModal,
|
|
backgroundFindings,
|
|
findingsByElement,
|
|
findingsByFile,
|
|
lintFindingCount,
|
|
hasLintError,
|
|
};
|
|
}
|