import { memo, useCallback, useEffect, useMemo, useState } from "react"; import { ChevronDown, ChevronUp } from "lucide-react"; import { MultiFileDiff } from "@pierre/diffs/react"; import type { ChangedFileSummaryItem } from "./types"; import { useDiffOptions, warmDiffHighlighter } from "@/features/agents/utils/diffUtils"; function stripProjectPathForDisplay(path: string, projectPath?: string): string { if (!projectPath) return path; const normalizedPath = path.replace(/\\/g, "/"); const normalizedProjectPath = projectPath.replace(/\\/g, "/").replace(/\/+$/, ""); if (!normalizedPath.startsWith(`${normalizedProjectPath}/`)) return path; return normalizedPath.slice(normalizedProjectPath.length + 1); } const ChangedFileRow = memo(function ChangedFileRow({ file, projectPath, open, diffReady, onToggle, }: { file: ChangedFileSummaryItem; projectPath?: string; open: boolean; diffReady: boolean; onToggle: (filePath: string) => void; }) { const diffOptions = useDiffOptions(); const displayPath = useMemo( () => stripProjectPathForDisplay(file.filePath, projectPath), [file.filePath, projectPath], ); const oldFile = useMemo( () => ({ name: displayPath, contents: file.originalContent }), [displayPath, file.originalContent], ); const newFile = useMemo( () => ({ name: displayPath, contents: file.modifiedContent }), [displayPath, file.modifiedContent], ); return (
{open && (
{diffReady ? (
) : (
Loading diff…
)}
)}
); }); export const TurnChangedFilesCard = memo(function TurnChangedFilesCard({ files, totals, projectPath, }: { files: Array; totals: { additions: number; deletions: number }; projectPath?: string; }) { const [expandedByPath, setExpandedByPath] = useState>({}); const [diffReady, setDiffReady] = useState(false); useEffect(() => { let active = true; warmDiffHighlighter().finally(() => { if (active) setDiffReady(true); }); return () => { active = false; }; }, []); const toggleFile = useCallback((filePath: string) => { setExpandedByPath((prev) => ({ ...prev, [filePath]: !prev[filePath] })); }, []); return (
{files.length} file{files.length === 1 ? "" : "s"} changed +{totals.additions} -{totals.deletions}
{files.map((file) => ( ))}
); });