import { useFileDiff, useCommitFileDiff } from "@/api/git"; import { Loader2, FileText, FilePlus, FileX, FileEdit, File, ArrowLeft, ExternalLink, X, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { CopyButton } from "@/components/ui/copy-button"; import { cn } from "@/lib/utils"; import type { GitFileStatusType } from "@/types/git"; import { GIT_STATUS_COLORS, GIT_STATUS_LABELS } from "@/lib/git-status-styles"; import { DiffLines } from "./DiffLines"; interface FileDiffViewProps { repoId: number; filePath: string; includeStaged?: boolean; commitHash?: string; onBack?: () => void; onClose?: () => void; onOpenFile?: (path: string, lineNumber?: number) => void; isMobile?: boolean; } export interface FileDiffContentProps { filePath: string; diff: string | null; status: GitFileStatusType; additions: number; deletions: number; isBinary?: boolean; onBack?: () => void; onClose?: () => void; onOpenFile?: (path: string, lineNumber?: number) => void; isMobile?: boolean; } const statusConfig: Record< GitFileStatusType, { icon: typeof FileText; color: string; bgColor: string; label: string } > = { modified: { icon: FileEdit, color: GIT_STATUS_COLORS.modified, bgColor: "bg-warning/10", label: GIT_STATUS_LABELS.modified, }, added: { icon: FilePlus, color: GIT_STATUS_COLORS.added, bgColor: "bg-success/10", label: GIT_STATUS_LABELS.added, }, deleted: { icon: FileX, color: GIT_STATUS_COLORS.deleted, bgColor: "bg-destructive/10", label: GIT_STATUS_LABELS.deleted, }, renamed: { icon: FileText, color: GIT_STATUS_COLORS.renamed, bgColor: "bg-info/10", label: GIT_STATUS_LABELS.renamed, }, untracked: { icon: File, color: GIT_STATUS_COLORS.untracked, bgColor: "bg-muted/50", label: GIT_STATUS_LABELS.untracked, }, copied: { icon: FileText, color: GIT_STATUS_COLORS.copied, bgColor: "bg-success/10", label: GIT_STATUS_LABELS.copied, }, }; export function FileDiffContent({ filePath, diff, status, additions, deletions, isBinary = false, onBack, onClose, onOpenFile, isMobile = false, }: FileDiffContentProps) { const fileName = filePath.split("/").pop() || filePath; const dirPath = filePath.includes("/") ? filePath.substring(0, filePath.lastIndexOf("/")) : ""; const config = statusConfig[status]; const Icon = config.icon; return (
{onBack && ( )}
{onOpenFile ? ( ) : ( <>
{fileName}
{dirPath && (
{dirPath}
)} )}
{config.label} {!isBinary && ( <> +{additions} -{deletions} )} {diff && ( )} {onClose && ( )}
{isBinary ? (

Binary file - cannot display diff

) : !diff ? (

No changes to display

) : (
onOpenFile(filePath, lineNum) : undefined } />
)}
); } export function FileDiffView({ repoId, filePath, includeStaged, commitHash, onBack, onClose, onOpenFile, isMobile = false, }: FileDiffViewProps) { const workingDiff = useFileDiff(repoId, filePath, includeStaged); const commitDiff = useCommitFileDiff(repoId, commitHash, filePath); const { data: diffData, isLoading, error } = commitHash ? commitDiff : workingDiff; if (isLoading) { return (
); } if (error) { return (

Failed to load diff

{error.message}

{onBack && ( )}
); } if (!diffData) { return null; } return ( ); }