'use client'; import { useState } from 'react'; import { api } from '~/trpc/react'; interface DiffViewerProps { scriptSlug: string; filePath: string; isOpen: boolean; onClose: () => void; } export function DiffViewer({ scriptSlug, filePath, isOpen, onClose }: DiffViewerProps) { const [isLoading, setIsLoading] = useState(false); // Get diff content const { data: diffData, refetch } = api.scripts.getScriptDiff.useQuery( { slug: scriptSlug, filePath }, { enabled: isOpen && !!scriptSlug && !!filePath } ); const handleBackdropClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget) { onClose(); } }; const handleRefresh = async () => { setIsLoading(true); await refetch(); setIsLoading(false); }; if (!isOpen) return null; const renderDiffLine = (line: string, index: number) => { const lineNumberMatch = /^([+-]?\d+):/.exec(line); const lineNumber = lineNumberMatch?.[1]; const content = line.replace(/^[+-]?\d+:\s*/, ''); const isAdded = line.startsWith('+'); const isRemoved = line.startsWith('-'); return (
{lineNumber}
{isAdded ? '+' : isRemoved ? '-' : ' '} {content}
); }; return (
{/* Header */}

Script Diff

{filePath}

{/* Legend */}
Added (Remote)
Removed (Local)
Unchanged
{/* Diff Content */}
{diffData?.success ? ( diffData.diff ? (
{diffData.diff.split('\n').map((line, index) => line.trim() ? renderDiffLine(line, index) : null )}
) : (

No differences found

The local and remote files are identical

) ) : diffData?.error ? (

Error loading diff

{diffData.error}

) : (

Loading diff...

)}
); }