import React, { useEffect, useRef, useState } from 'react'; import { DiffOptionsPopover } from './DiffOptionsPopover'; interface FileHeaderProps { filePath: string; patch: string; isViewed?: boolean; onToggleViewed?: () => void; isStaged?: boolean; isStaging?: boolean; onStage?: () => void; canStage?: boolean; stageError?: string | null; onFileComment?: (anchorEl: HTMLElement) => void; collapseToggle?: React.ReactNode; onCollapseToggle?: () => void; } function splitFilePath(filePath: string): { directory: string; name: string } { const lastSlash = filePath.lastIndexOf('/'); if (lastSlash === -1) { return { directory: '', name: filePath }; } return { directory: filePath.slice(0, lastSlash + 1), name: filePath.slice(lastSlash + 1), }; } function frontEllipsize(text: string, visibleChars: number): string { if (text.length <= visibleChars) return text; return `...${text.slice(-visibleChars)}`; } /** Sticky file header with file path, Viewed toggle, Git Add, and Copy Diff button */ export const FileHeader: React.FC = ({ filePath, patch, isViewed = false, onToggleViewed, isStaged = false, isStaging = false, onStage, canStage = false, stageError, onFileComment, collapseToggle, onCollapseToggle, }) => { const [copied, setCopied] = useState(false); const [headerWidth, setHeaderWidth] = useState(0); const headerRef = useRef(null); const fileCommentRef = useRef(null); const { directory, name } = splitFilePath(filePath); const isCompact = headerWidth > 0 && headerWidth < 760; const isTight = headerWidth > 0 && headerWidth < 600; const isVeryTight = headerWidth > 0 && headerWidth < 480; const showFilenameOnly = headerWidth > 0 && headerWidth < 560; const truncatedName = showFilenameOnly ? frontEllipsize( name, headerWidth < 360 ? 14 : headerWidth < 420 ? 18 : headerWidth < 500 ? 24 : 32, ) : name; useEffect(() => { if (!headerRef.current || typeof ResizeObserver === 'undefined') return; const node = headerRef.current; const observer = new ResizeObserver(([entry]) => { setHeaderWidth(entry.contentRect.width); }); observer.observe(node); return () => observer.disconnect(); }, []); const stageLabel = isVeryTight ? '' : isCompact ? (isStaging ? 'Adding' : isStaged ? 'Added' : 'Add') : (isStaging ? 'Adding...' : isStaged ? 'Added' : 'Git Add'); const commentLabel = isVeryTight ? '' : isCompact ? 'Comment' : 'File Comment'; const copyLabel = isTight ? '' : isCompact ? 'Copy' : copied ? 'Copied!' : 'Copy Diff'; const viewedLabel = isVeryTight ? '' : 'Viewed'; return (
{collapseToggle} {!showFilenameOnly && directory && ( {directory} )} {truncatedName}
{onToggleViewed && ( )} {canStage && onStage && ( )} {stageError && ( {stageError} )} {onFileComment && ( )}
); };