import React, { useMemo, useCallback, useRef, useState, useEffect } from 'react'; import { type DiffLineAnnotation } from '@pierre/diffs/react'; import { getSingularPatch } from '@pierre/diffs'; import { CodeAnnotation, CodeAnnotationType, SelectedLineRange, DiffAnnotationMetadata, TokenAnnotationMeta, ConventionalLabel, ConventionalDecoration } from '@plannotator/ui/types'; import type { DiffTokenEventBaseProps } from '@pierre/diffs'; import { buildCodeNavRequest } from '../utils/buildCodeNavRequest'; import { usePierreTheme } from '../hooks/usePierreTheme'; import { LazyFileDiff } from './LazyFileDiff'; import { ToolbarHost, type ToolbarHostHandle } from './ToolbarHost'; import { InlineAnnotation } from './InlineAnnotation'; import { FileHeader } from './FileHeader'; import { CommentPopover } from '@plannotator/ui/components/CommentPopover'; import { detectLanguage } from '../utils/detectLanguage'; import type { DiffFile } from '../types'; import { buildFileTree, getVisualFileOrder } from '../utils/buildFileTree'; import { getLineNumberFromNode, getSideFromNode, getDiffSelection } from '../utils/diffSelection'; import type { AIChatEntry } from '../hooks/useAIChat'; interface AllFilesDiffViewProps { files: DiffFile[]; annotations: CodeAnnotation[]; selectedAnnotationId: string | null; pendingSelection: SelectedLineRange | null; onLineSelection: (range: SelectedLineRange | null) => void; onAddAnnotation: (filePath: string, type: CodeAnnotationType, text?: string, suggestedCode?: string, originalCode?: string, conventionalLabel?: ConventionalLabel, decorations?: ConventionalDecoration[], tokenMeta?: TokenAnnotationMeta) => void; onAddFileComment?: (filePath: string, text: string) => void; onEditAnnotation: (id: string, text?: string, suggestedCode?: string, originalCode?: string, conventionalLabel?: ConventionalLabel | null, decorations?: ConventionalDecoration[]) => void; onSelectAnnotation: (id: string | null) => void; onDeleteAnnotation: (id: string) => void; diffStyle: 'split' | 'unified'; diffOverflow?: 'scroll' | 'wrap'; diffIndicators?: 'bars' | 'classic' | 'none'; lineDiffType?: 'word-alt' | 'word' | 'char' | 'none'; disableLineNumbers?: boolean; disableBackground?: boolean; fontFamily?: string; fontSize?: string; viewedFiles: Set; onToggleViewed?: (filePath: string) => void; stagedFiles?: Set; onStage?: (filePath: string) => void; canStageFiles?: boolean; stagingFile?: string | null; stageError?: string | null; reviewBase?: string; prUrl?: string; prDiffScope?: string; onVisibleFileChange?: (filePath: string | null) => void; isActive?: boolean; // AI props aiAvailable?: boolean; onAskAI?: (question: string) => void; isAILoading?: boolean; onViewAIResponse?: (questionId?: string) => void; aiHistoryForSelection?: AIChatEntry[]; onCodeNavRequest?: (request: import('@plannotator/shared/code-nav').CodeNavRequest) => void; } export const AllFilesDiffView: React.FC = ({ files, annotations, selectedAnnotationId, pendingSelection, onLineSelection, onAddAnnotation, onAddFileComment, onEditAnnotation, onSelectAnnotation, onDeleteAnnotation, diffStyle, diffOverflow, diffIndicators, lineDiffType, disableLineNumbers, disableBackground, fontFamily, fontSize, viewedFiles, onToggleViewed, stagedFiles, onStage, canStageFiles, stagingFile, stageError, reviewBase, prUrl, prDiffScope, onVisibleFileChange, isActive = true, aiAvailable = false, onAskAI, isAILoading = false, onViewAIResponse, aiHistoryForSelection = [], onCodeNavRequest, }) => { const pierreTheme = usePierreTheme({ fontFamily, fontSize }); const [activeFilePath, setActiveFilePath] = useState(null); const pendingToolbarRange = useRef(null); const [collapsedFiles, setCollapsedFiles] = useState>(new Set()); const collapseHistory = useRef([]); const [fileCommentAnchor, setFileCommentAnchor] = useState<{ el: HTMLElement; filePath: string } | null>(null); useEffect(() => { setActiveFilePath(null); setCollapsedFiles(new Set()); collapseHistory.current = []; setFileCommentAnchor(null); }, [files]); const scrollRef = useRef(null); const headerRefs = useRef>(new Map()); const activePatch = useMemo( () => files.find(f => f.path === activeFilePath)?.patch ?? '', [files, activeFilePath], ); const handleAddAnnotation = useCallback(( type: CodeAnnotationType, text?: string, suggestedCode?: string, originalCode?: string, conventionalLabel?: ConventionalLabel, decorations?: ConventionalDecoration[], tokenMeta?: TokenAnnotationMeta, ) => { if (!activeFilePath) return; onAddAnnotation(activeFilePath, type, text, suggestedCode, originalCode, conventionalLabel, decorations, tokenMeta); }, [activeFilePath, onAddAnnotation]); const toolbarHostRef = useRef(null); useEffect(() => { if (pendingToolbarRange.current && activePatch) { toolbarHostRef.current?.handleLineSelectionEnd(pendingToolbarRange.current); pendingToolbarRange.current = null; } }, [activePatch]); const handleEdit = useCallback((id: string) => { const ann = annotations.find(a => a.id === id); if (!ann) return; toolbarHostRef.current?.startEdit(ann); }, [annotations]); const visualOrder = useMemo(() => { const tree = buildFileTree(files); return getVisualFileOrder(tree); }, [files]); const sortedFiles = useMemo(() => visualOrder.map(i => files[i]), [files, visualOrder]); const baseDiffs = useMemo(() => { const map = new Map>(); for (const file of files) map.set(file.path, getSingularPatch(file.patch)); return map; }, [files]); const toggleCollapse = useCallback((filePath: string) => { setCollapsedFiles(prev => { const next = new Set(prev); if (next.has(filePath)) { next.delete(filePath); } else { next.add(filePath); collapseHistory.current.push(filePath); if (activeFilePath === filePath) setActiveFilePath(null); } return next; }); }, [activeFilePath]); const collapseFile = useCallback((filePath: string) => { if (!collapsedFiles.has(filePath)) { collapseHistory.current.push(filePath); setCollapsedFiles(prev => { const next = new Set(prev); next.add(filePath); return next; }); } if (activeFilePath === filePath) setActiveFilePath(null); }, [activeFilePath, collapsedFiles]); const toggleViewedAndCollapse = useCallback((filePath: string) => { const isCurrentlyViewed = viewedFiles.has(filePath); onToggleViewed?.(filePath); if (!isCurrentlyViewed) collapseFile(filePath); }, [collapseFile, onToggleViewed, viewedFiles]); const setHeaderRef = useCallback((path: string, el: HTMLDivElement | null) => { if (el) headerRefs.current.set(path, el); else headerRefs.current.delete(path); }, []); // Scroll tracking for file tree highlight const visibleFileRef = useRef(null); useEffect(() => { if (!onVisibleFileChange) return; const container = scrollRef.current; if (!container) return; const onScroll = () => { const containerTop = container.getBoundingClientRect().top; const expandedFiles = sortedFiles.filter(f => !collapsedFiles.has(f.path)); let bestPath: string | null = expandedFiles[0]?.path ?? null; const atBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 20; if (atBottom && expandedFiles.length > 0) { bestPath = expandedFiles[expandedFiles.length - 1].path; } else { for (const file of expandedFiles) { const header = headerRefs.current.get(file.path); if (!header) continue; const rect = header.getBoundingClientRect(); if (rect.top <= containerTop + 50) bestPath = file.path; else break; } } if (bestPath !== visibleFileRef.current) { visibleFileRef.current = bestPath; onVisibleFileChange(bestPath); } }; onScroll(); container.addEventListener('scroll', onScroll, { passive: true }); return () => container.removeEventListener('scroll', onScroll); }, [onVisibleFileChange, sortedFiles, collapsedFiles]); // Keyboard shortcuts for all-files mode: [/] scroll, v viewed+collapse, a stage, c collapse useEffect(() => { if (!isActive) return; const handler = (e: KeyboardEvent) => { const el = e.target as HTMLElement | null; if (el && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.isContentEditable)) return; if (e.metaKey || e.ctrlKey || e.shiftKey) return; const currentPath = activeFilePath || visibleFileRef.current; if (e.key === 'z') { // Find the most recent entry that's actually still collapsed let last: string | undefined; while (collapseHistory.current.length > 0) { const candidate = collapseHistory.current.pop()!; if (collapsedFiles.has(candidate)) { last = candidate; break; } } if (!last) return; e.preventDefault(); setCollapsedFiles(prev => { const next = new Set(prev); next.delete(last!); return next; }); setActiveFilePath(last); setTimeout(() => { const header = headerRefs.current.get(last!); header?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); }, 50); return; } if (e.key === 'x' && currentPath) { e.preventDefault(); toggleCollapse(currentPath); return; } if (e.key === 'c' && currentPath && onAddFileComment) { e.preventDefault(); const header = headerRefs.current.get(currentPath); if (header) { const btn = header.querySelector('[title="Add file-scoped comment"]'); if (btn) { setFileCommentAnchor({ el: btn, filePath: currentPath }); } } return; } if (e.key === 'v' && currentPath) { e.preventDefault(); toggleViewedAndCollapse(currentPath); return; } if (e.key === 'a' && currentPath && canStageFiles) { e.preventDefault(); onStage?.(currentPath); return; } if (e.key !== '[' && e.key !== ']') return; e.preventDefault(); const expandedFiles = sortedFiles.filter(f => !collapsedFiles.has(f.path)); if (expandedFiles.length === 0) return; const currentIdx = currentPath ? expandedFiles.findIndex(f => f.path === currentPath) : -1; let targetIdx: number; if (e.key === ']') { targetIdx = currentIdx < expandedFiles.length - 1 ? currentIdx + 1 : expandedFiles.length - 1; } else { targetIdx = currentIdx > 0 ? currentIdx - 1 : 0; } const targetFile = expandedFiles[targetIdx]; const header = headerRefs.current.get(targetFile.path); if (header) { header.scrollIntoView({ behavior: 'smooth', block: 'start' }); setActiveFilePath(targetFile.path); } }; window.addEventListener('keydown', handler); return () => window.removeEventListener('keydown', handler); }, [isActive, sortedFiles, collapsedFiles, activeFilePath, toggleCollapse, toggleViewedAndCollapse, canStageFiles, onStage, onAddFileComment]); // Click-and-drag line selection in diff content useEffect(() => { const root = scrollRef.current; if (!root) return; const handler = () => { requestAnimationFrame(() => { const selection = getDiffSelection(root); if (!selection || selection.isCollapsed || !selection.toString().trim()) return; const anchorLine = getLineNumberFromNode(selection.anchorNode); const focusLine = getLineNumberFromNode(selection.focusNode); if (anchorLine == null || focusLine == null) return; if (anchorLine === focusLine) return; const side = getSideFromNode(selection.anchorNode); // Determine which file the selection is in by checking header positions const anchorRect = selection.getRangeAt(0).getBoundingClientRect(); let closestFile: string | null = null; for (const file of sortedFiles) { const header = headerRefs.current.get(file.path); if (header && header.getBoundingClientRect().top <= anchorRect.top) { closestFile = file.path; } } if (closestFile) { if (activeFilePath !== closestFile) { pendingToolbarRange.current = { start: Math.min(anchorLine, focusLine), end: Math.max(anchorLine, focusLine), side, }; setActiveFilePath(closestFile); selection.removeAllRanges(); return; } setActiveFilePath(closestFile); } toolbarHostRef.current?.handleLineSelectionEnd({ start: Math.min(anchorLine, focusLine), end: Math.max(anchorLine, focusLine), side, }); selection.removeAllRanges(); }); }; root.addEventListener('mouseup', handler, true); return () => root.removeEventListener('mouseup', handler, true); }, [sortedFiles, activeFilePath]); // Scroll to selected annotation — auto-expand collapsed file useEffect(() => { if (!selectedAnnotationId) return; const ann = annotations.find(a => a.id === selectedAnnotationId); if (!ann) return; setCollapsedFiles(prev => { if (!prev.has(ann.filePath)) return prev; const next = new Set(prev); next.delete(ann.filePath); return next; }); requestAnimationFrame(() => { const header = headerRefs.current.get(ann.filePath); header?.scrollIntoView({ behavior: 'smooth', block: 'start' }); }); }, [selectedAnnotationId, annotations]); return (
{sortedFiles.map(file => { const isCollapsed = collapsedFiles.has(file.path); const fileAnnotations = annotations .filter(a => a.filePath === file.path && (a.scope ?? 'line') === 'line' && (!a.prUrl || !prUrl || a.prUrl === prUrl) && (!a.diffScope || !prDiffScope || a.diffScope === prDiffScope) ) .map(ann => ({ side: ann.side === 'new' ? 'additions' as const : 'deletions' as const, lineNumber: ann.lineEnd, metadata: { annotationId: ann.id, type: ann.type, text: ann.text, suggestedCode: ann.suggestedCode, originalCode: ann.originalCode, author: ann.author, severity: ann.severity, reasoning: ann.reasoning, conventionalLabel: ann.conventionalLabel, decorations: ann.decorations, } as DiffAnnotationMetadata, })); return (
setHeaderRef(file.path, el)} > toggleViewedAndCollapse(file.path) : undefined} isStaged={stagedFiles?.has(file.path)} isStaging={stagingFile === file.path} onStage={onStage ? () => onStage(file.path) : undefined} canStage={canStageFiles} stageError={stagingFile === file.path ? stageError : null} collapseToggle={ } onCollapseToggle={() => toggleCollapse(file.path)} onFileComment={onAddFileComment ? (anchorEl) => setFileCommentAnchor({ el: anchorEl, filePath: file.path }) : undefined} />
{!isCollapsed && ( a.metadata.annotationId === selectedAnnotationId)} options={{ themeType: pierreTheme.type, unsafeCSS: pierreTheme.css, ...(pierreTheme.syntaxTheme && { theme: pierreTheme.syntaxTheme }), diffStyle, overflow: diffOverflow, diffIndicators, lineDiffType, disableLineNumbers, disableBackground, hunkSeparators: 'line-info', enableLineSelection: true, enableGutterUtility: true, onGutterUtilityClick: (range: SelectedLineRange) => { if (activeFilePath === file.path) { toolbarHostRef.current?.handleLineSelectionEnd(range); } else { pendingToolbarRange.current = range; setActiveFilePath(file.path); } }, onLineSelectionEnd: (range: SelectedLineRange | null) => { if (range) { if (activeFilePath === file.path) { toolbarHostRef.current?.handleLineSelectionEnd(range); } else { pendingToolbarRange.current = range; setActiveFilePath(file.path); } } onLineSelection(range); }, ...(onCodeNavRequest && { onTokenClick: (props: DiffTokenEventBaseProps, event: MouseEvent) => { if (event.metaKey || event.ctrlKey) { onCodeNavRequest(buildCodeNavRequest(props, file.path)); } }, onTokenEnter: (props: DiffTokenEventBaseProps, event: PointerEvent) => { if (event.metaKey || event.ctrlKey) { props.tokenElement.classList.add('pn-token-nav'); } }, onTokenLeave: (props: DiffTokenEventBaseProps) => { props.tokenElement.classList.remove('pn-token-nav'); }, }), }} annotations={fileAnnotations} selectedLines={activeFilePath === file.path ? (pendingSelection || undefined) : undefined} renderAnnotation={(annotation: DiffLineAnnotation) => { if (!annotation.metadata) return null; return ( ); }} /> )}
); })} {fileCommentAnchor && onAddFileComment && ( { onAddFileComment(fileCommentAnchor.filePath, text); setFileCommentAnchor(null); }} onClose={() => setFileCommentAnchor(null)} /> )}
); };