feat: Git status tracking refactor with persistent snapshots and commit history (#86)
This commit is contained in:
@@ -12,37 +12,39 @@ import { FILE_EXTENSION_COLORS } from '@/lib/types/backend';
|
||||
import { useAppStore } from '@/components/state-provider';
|
||||
import { SessionTabs, type SessionTab } from '@/components/session-tabs';
|
||||
|
||||
import type { SelectedDiff } from './task-layout';
|
||||
|
||||
type TaskCenterPanelProps = {
|
||||
selectedDiffPath: string | null;
|
||||
selectedDiff: SelectedDiff | null;
|
||||
openFileRequest: OpenFileTab | null;
|
||||
onDiffPathHandled: () => void;
|
||||
onDiffHandled: () => void;
|
||||
onFileOpenHandled: () => void;
|
||||
sessionId?: string | null;
|
||||
};
|
||||
|
||||
export const TaskCenterPanel = memo(function TaskCenterPanel({
|
||||
selectedDiffPath: externalSelectedDiffPath,
|
||||
selectedDiff: externalSelectedDiff,
|
||||
openFileRequest,
|
||||
onDiffPathHandled,
|
||||
onDiffHandled,
|
||||
onFileOpenHandled,
|
||||
sessionId = null,
|
||||
}: TaskCenterPanelProps) {
|
||||
const activeTaskId = useAppStore((state) => state.tasks.activeTaskId);
|
||||
const [leftTab, setLeftTab] = useState('chat');
|
||||
const [selectedDiffPath, setSelectedDiffPath] = useState<string | null>(null);
|
||||
const [selectedDiff, setSelectedDiff] = useState<SelectedDiff | null>(null);
|
||||
const [notes, setNotes] = useState('');
|
||||
const [openFileTabs, setOpenFileTabs] = useState<OpenFileTab[]>([]);
|
||||
|
||||
// Handle external diff path selection
|
||||
// Handle external diff selection
|
||||
useEffect(() => {
|
||||
if (externalSelectedDiffPath) {
|
||||
if (externalSelectedDiff) {
|
||||
queueMicrotask(() => {
|
||||
setSelectedDiffPath(externalSelectedDiffPath);
|
||||
setSelectedDiff(externalSelectedDiff);
|
||||
setLeftTab('changes');
|
||||
onDiffPathHandled();
|
||||
onDiffHandled();
|
||||
});
|
||||
}
|
||||
}, [externalSelectedDiffPath, onDiffPathHandled]);
|
||||
}, [externalSelectedDiff, onDiffHandled]);
|
||||
|
||||
// Handle external file open request
|
||||
useEffect(() => {
|
||||
@@ -119,8 +121,8 @@ export const TaskCenterPanel = memo(function TaskCenterPanel({
|
||||
|
||||
<TabsContent value="changes" className="flex-1 min-h-0">
|
||||
<TaskChangesPanel
|
||||
selectedDiffPath={selectedDiffPath}
|
||||
onClearSelected={() => setSelectedDiffPath(null)}
|
||||
selectedDiff={selectedDiff}
|
||||
onClearSelected={() => setSelectedDiff(null)}
|
||||
/>
|
||||
</TabsContent>
|
||||
|
||||
|
||||
@@ -2,7 +2,12 @@
|
||||
|
||||
import { memo, useMemo, useState } from 'react';
|
||||
import { DiffModeEnum, DiffView } from '@git-diff-view/react';
|
||||
import { IconArrowBackUp, IconCopy, IconLayoutColumns, IconLayoutRows } from '@tabler/icons-react';
|
||||
import {
|
||||
IconArrowBackUp,
|
||||
IconCopy,
|
||||
IconLayoutColumns,
|
||||
IconLayoutRows,
|
||||
} from '@tabler/icons-react';
|
||||
import { useTheme } from 'next-themes';
|
||||
import { Badge } from '@kandev/ui/badge';
|
||||
import { Button } from '@kandev/ui/button';
|
||||
@@ -12,49 +17,129 @@ import { cn } from '@/lib/utils';
|
||||
import { getLocalStorage, setLocalStorage } from '@/lib/local-storage';
|
||||
import { useAppStore } from '@/components/state-provider';
|
||||
import { useSessionGitStatus } from '@/hooks/domains/session/use-session-git-status';
|
||||
import { useCumulativeDiff } from '@/hooks/domains/session/use-cumulative-diff';
|
||||
import type { FileInfo } from '@/lib/state/slices';
|
||||
import type { SelectedDiff } from './task-layout';
|
||||
|
||||
type TaskChangesPanelProps = {
|
||||
selectedDiffPath: string | null;
|
||||
selectedDiff: SelectedDiff | null;
|
||||
onClearSelected: () => void;
|
||||
};
|
||||
|
||||
const DEFAULT_DIFF_MODE: 'unified' | 'split' = 'unified';
|
||||
|
||||
/**
|
||||
* Normalize diff content for the @git-diff-view/react library.
|
||||
*
|
||||
* Handles two edge cases from the backend:
|
||||
* 1. Multiple diffs concatenated with "--- Staged changes ---" separator
|
||||
* (we take the staged diff as it's what matters for the commit)
|
||||
* 2. Diffs missing the "diff --git" header line (for untracked files)
|
||||
*
|
||||
* The library expects each hunk to be a complete diff string with headers.
|
||||
*/
|
||||
function parseDiffIntoHunks(diffContent: string, filePath?: string): string[] {
|
||||
if (!diffContent || typeof diffContent !== 'string') return [];
|
||||
|
||||
let trimmed = diffContent.trim();
|
||||
if (!trimmed) return [];
|
||||
|
||||
// Handle concatenated diffs with "--- Staged changes ---" separator
|
||||
// Take the staged diff (after the separator) as it's the authoritative one
|
||||
const stagedSeparator = '--- Staged changes ---';
|
||||
if (trimmed.includes(stagedSeparator)) {
|
||||
const parts = trimmed.split(stagedSeparator);
|
||||
// Use the staged diff (second part) if available
|
||||
trimmed = (parts[1] || parts[0]).trim();
|
||||
}
|
||||
|
||||
// If diff is missing the "diff --git" header, add it
|
||||
// This happens for untracked files where backend generates incomplete diff
|
||||
if (!trimmed.startsWith('diff --git') && trimmed.includes('@@')) {
|
||||
const path = filePath || 'file';
|
||||
const header = `diff --git a/${path} b/${path}\nnew file mode 100644\nindex 0000000..0000000\n`;
|
||||
trimmed = header + trimmed;
|
||||
}
|
||||
|
||||
// Return as single hunk - the library handles parsing internally
|
||||
return [trimmed];
|
||||
}
|
||||
|
||||
const TaskChangesPanel = memo(function TaskChangesPanel({
|
||||
selectedDiffPath,
|
||||
selectedDiff,
|
||||
onClearSelected,
|
||||
}: TaskChangesPanelProps) {
|
||||
const [diffViewMode, setDiffViewMode] = useState<'unified' | 'split'>(() =>
|
||||
getLocalStorage('task-diff-view-mode', DEFAULT_DIFF_MODE)
|
||||
);
|
||||
|
||||
const { resolvedTheme } = useTheme();
|
||||
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
|
||||
const gitStatus = useSessionGitStatus(activeSessionId);
|
||||
// Fetch cumulative diff (all commits combined) when no specific file is selected
|
||||
const { diff: cumulativeDiff, loading: cumulativeLoading } = useCumulativeDiff(
|
||||
selectedDiff ? null : activeSessionId
|
||||
);
|
||||
|
||||
// Convert git status files to array for display
|
||||
const changedFiles = useMemo<FileInfo[]>(() => {
|
||||
const selectedDiffPath = selectedDiff?.path ?? null;
|
||||
// Use provided diff content (from commit or snapshot) or look up from current git status
|
||||
const hasProvidedContent = Boolean(selectedDiff?.content);
|
||||
|
||||
// Convert git status files to array for display (uncommitted changes)
|
||||
const uncommittedFiles = useMemo<FileInfo[]>(() => {
|
||||
if (!gitStatus?.files || Object.keys(gitStatus.files).length === 0) {
|
||||
return [];
|
||||
}
|
||||
return Object.values(gitStatus.files) as FileInfo[];
|
||||
}, [gitStatus]);
|
||||
const diffTargets = useMemo(
|
||||
() => (selectedDiffPath ? [selectedDiffPath] : changedFiles.map((file: FileInfo) => file.path)),
|
||||
[selectedDiffPath, changedFiles]
|
||||
);
|
||||
|
||||
// Convert cumulative diff files to array (committed changes)
|
||||
const committedFiles = useMemo<Array<{ path: string; diff: string; additions: number; deletions: number }>>(() => {
|
||||
if (!cumulativeDiff?.files || Object.keys(cumulativeDiff.files).length === 0) {
|
||||
return [];
|
||||
}
|
||||
return Object.entries(cumulativeDiff.files)
|
||||
.filter(([, file]) => file.diff) // Only include files with diffs
|
||||
.map(([path, file]) => ({
|
||||
path,
|
||||
diff: file.diff!,
|
||||
additions: file.additions ?? 0,
|
||||
deletions: file.deletions ?? 0,
|
||||
}));
|
||||
}, [cumulativeDiff]);
|
||||
|
||||
const diffModeEnum = diffViewMode === 'split' ? DiffModeEnum.Split : DiffModeEnum.Unified;
|
||||
const diffTheme = resolvedTheme === 'dark' ? 'dark' : 'light';
|
||||
const selectedFile = selectedDiffPath && gitStatus ? gitStatus.files[selectedDiffPath] : null;
|
||||
const selectedDiffContent = selectedFile?.diff ?? '';
|
||||
const isSingleDiffSelected = Boolean(selectedDiffPath && selectedFile);
|
||||
const selectedFile = selectedDiffPath && gitStatus?.files ? gitStatus.files[selectedDiffPath] : null;
|
||||
// Use provided content if available, otherwise fall back to git status
|
||||
const selectedDiffContent = selectedDiff?.content ?? selectedFile?.diff ?? '';
|
||||
const isSingleDiffSelected = Boolean(selectedDiffPath && (hasProvidedContent || selectedFile));
|
||||
|
||||
// Helper to get file language from extension
|
||||
const getFileLang = (path: string) => {
|
||||
const ext = path.split('.').pop() || '';
|
||||
const langMap: Record<string, string> = {
|
||||
ts: 'typescript', tsx: 'tsx', js: 'javascript', jsx: 'jsx',
|
||||
py: 'python', go: 'go', rs: 'rust', java: 'java',
|
||||
cpp: 'cpp', c: 'c', css: 'css', html: 'html',
|
||||
json: 'json', md: 'markdown', yaml: 'yaml', yml: 'yaml',
|
||||
};
|
||||
return langMap[ext] || 'plaintext';
|
||||
};
|
||||
|
||||
const hasUncommitted = uncommittedFiles.length > 0;
|
||||
const hasCommitted = committedFiles.length > 0;
|
||||
const hasAnyChanges = hasUncommitted || hasCommitted;
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2 h-full">
|
||||
{/* Header toolbar */}
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<Badge variant="secondary" className="rounded-full text-xs">
|
||||
{selectedDiffPath ?? 'All files'}
|
||||
</Badge>
|
||||
<div className="flex items-center gap-2">
|
||||
<Badge variant="secondary" className="rounded-full text-xs">
|
||||
{selectedDiffPath ?? 'All files'}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5">
|
||||
{selectedDiffPath && (
|
||||
<Tooltip>
|
||||
@@ -66,10 +151,10 @@ const TaskChangesPanel = memo(function TaskChangesPanel({
|
||||
className="h-7 px-2 text-xs cursor-pointer"
|
||||
onClick={onClearSelected}
|
||||
>
|
||||
All changes
|
||||
Clear
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Show all changes</TooltipContent>
|
||||
<TooltipContent>Show all uncommitted changes</TooltipContent>
|
||||
</Tooltip>
|
||||
)}
|
||||
<Tooltip>
|
||||
@@ -90,7 +175,7 @@ const TaskChangesPanel = memo(function TaskChangesPanel({
|
||||
</Button>
|
||||
</span>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Copy file contents</TooltipContent>
|
||||
<TooltipContent>Copy diff</TooltipContent>
|
||||
</Tooltip>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
@@ -156,62 +241,103 @@ const TaskChangesPanel = memo(function TaskChangesPanel({
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Diff content */}
|
||||
<SessionPanelContent>
|
||||
{changedFiles.length === 0 ? (
|
||||
{isSingleDiffSelected && selectedDiffContent ? (
|
||||
// Render single file diff (from commit, snapshot, or uncommitted)
|
||||
<div className="space-y-4">
|
||||
<DiffView
|
||||
data={{
|
||||
hunks: parseDiffIntoHunks(selectedDiffContent, selectedDiffPath!),
|
||||
oldFile: { fileName: selectedDiffPath!, fileLang: getFileLang(selectedDiffPath!) },
|
||||
newFile: { fileName: selectedDiffPath!, fileLang: getFileLang(selectedDiffPath!) },
|
||||
}}
|
||||
diffViewMode={diffModeEnum}
|
||||
diffViewTheme={diffTheme}
|
||||
/>
|
||||
</div>
|
||||
) : cumulativeLoading ? (
|
||||
<div className="flex items-center justify-center h-full text-muted-foreground text-sm">
|
||||
No changes detected
|
||||
Loading changes...
|
||||
</div>
|
||||
) : !hasAnyChanges ? (
|
||||
<div className="flex items-center justify-center h-full text-muted-foreground text-sm">
|
||||
No changes
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
{diffTargets.map((path) => {
|
||||
const file = gitStatus?.files?.[path];
|
||||
if (!file || !file.diff) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Detect file language from extension
|
||||
const ext = path.split('.').pop() || '';
|
||||
const langMap: Record<string, string> = {
|
||||
ts: 'typescript',
|
||||
tsx: 'tsx',
|
||||
js: 'javascript',
|
||||
jsx: 'jsx',
|
||||
py: 'python',
|
||||
go: 'go',
|
||||
rs: 'rust',
|
||||
java: 'java',
|
||||
cpp: 'cpp',
|
||||
c: 'c',
|
||||
css: 'css',
|
||||
html: 'html',
|
||||
json: 'json',
|
||||
md: 'markdown',
|
||||
yaml: 'yaml',
|
||||
yml: 'yaml',
|
||||
};
|
||||
const fileLang = langMap[ext] || 'plaintext';
|
||||
|
||||
return (
|
||||
<div key={path} className="space-y-2">
|
||||
{!selectedDiffPath && (
|
||||
<div className="flex items-center justify-between">
|
||||
<Badge variant="secondary" className="rounded-full text-xs">
|
||||
{path}
|
||||
</Badge>
|
||||
</div>
|
||||
)}
|
||||
<DiffView
|
||||
data={{
|
||||
hunks: [file.diff],
|
||||
oldFile: { fileName: path, fileLang },
|
||||
newFile: { fileName: path, fileLang },
|
||||
}}
|
||||
diffViewMode={diffModeEnum}
|
||||
diffViewTheme={diffTheme}
|
||||
/>
|
||||
<div className="space-y-6">
|
||||
{/* Uncommitted changes section */}
|
||||
{hasUncommitted && (
|
||||
<div className="space-y-4">
|
||||
<div className="text-xs font-medium text-muted-foreground uppercase tracking-wide">
|
||||
Uncommitted ({uncommittedFiles.length})
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{uncommittedFiles.map((file) => {
|
||||
if (!file.diff) return null;
|
||||
const fileLang = getFileLang(file.path);
|
||||
return (
|
||||
<div key={`uncommitted-${file.path}`} className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Badge variant="secondary" className="rounded-full text-xs">
|
||||
{file.path}
|
||||
</Badge>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
<span className="text-emerald-500">+{file.additions}</span>
|
||||
{' / '}
|
||||
<span className="text-rose-500">-{file.deletions}</span>
|
||||
</span>
|
||||
</div>
|
||||
<DiffView
|
||||
data={{
|
||||
hunks: parseDiffIntoHunks(file.diff, file.path),
|
||||
oldFile: { fileName: file.path, fileLang },
|
||||
newFile: { fileName: file.path, fileLang },
|
||||
}}
|
||||
diffViewMode={diffModeEnum}
|
||||
diffViewTheme={diffTheme}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Committed changes section */}
|
||||
{hasCommitted && (
|
||||
<div className="space-y-4">
|
||||
<div className="text-xs font-medium text-muted-foreground uppercase tracking-wide">
|
||||
Committed ({committedFiles.length})
|
||||
</div>
|
||||
{committedFiles.map((file) => {
|
||||
if (!file.diff) return null;
|
||||
const fileLang = getFileLang(file.path);
|
||||
return (
|
||||
<div key={`committed-${file.path}`} className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Badge variant="secondary" className="rounded-full text-xs">
|
||||
{file.path}
|
||||
</Badge>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
<span className="text-emerald-500">+{file.additions}</span>
|
||||
{' / '}
|
||||
<span className="text-rose-500">-{file.deletions}</span>
|
||||
</span>
|
||||
</div>
|
||||
<DiffView
|
||||
data={{
|
||||
hunks: parseDiffIntoHunks(file.diff, file.path),
|
||||
oldFile: { fileName: file.path, fileLang },
|
||||
newFile: { fileName: file.path, fileLang },
|
||||
}}
|
||||
diffViewMode={diffModeEnum}
|
||||
diffViewTheme={diffTheme}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</SessionPanelContent>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { memo, useMemo, useState } from 'react';
|
||||
import { memo, useMemo, useState, useCallback } from 'react';
|
||||
import {
|
||||
IconArrowBackUp,
|
||||
IconExternalLink,
|
||||
@@ -8,19 +8,26 @@ import {
|
||||
IconCircleFilled,
|
||||
IconMinus,
|
||||
IconCheck,
|
||||
IconChevronRight,
|
||||
IconGitCommit,
|
||||
IconLoader2,
|
||||
} from '@tabler/icons-react';
|
||||
|
||||
import { TabsContent } from '@kandev/ui/tabs';
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from '@kandev/ui/tooltip';
|
||||
import { SessionPanel, SessionPanelContent } from '@kandev/ui/pannel-session';
|
||||
import { LineStat } from '@/components/diff-stat';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useAppStore } from '@/components/state-provider';
|
||||
import { useOpenSessionInEditor } from '@/hooks/use-open-session-in-editor';
|
||||
import { useSessionGitStatus } from '@/hooks/domains/session/use-session-git-status';
|
||||
import { useSessionCommits } from '@/hooks/domains/session/use-session-commits';
|
||||
import { useGitOperations } from '@/hooks/use-git-operations';
|
||||
import { getWebSocketClient } from '@/lib/ws/connection';
|
||||
import type { FileInfo } from '@/lib/state/store';
|
||||
import { FileBrowser } from '@/components/task/file-browser';
|
||||
import { SessionTabs, type SessionTab } from '@/components/session-tabs';
|
||||
import { useToast } from '@/components/toast-provider';
|
||||
|
||||
type OpenFileTab = {
|
||||
path: string;
|
||||
@@ -29,7 +36,7 @@ type OpenFileTab = {
|
||||
};
|
||||
|
||||
type TaskFilesPanelProps = {
|
||||
onSelectDiffPath: (path: string) => void;
|
||||
onSelectDiff: (path: string, content?: string) => void;
|
||||
onOpenFile: (file: OpenFileTab) => void;
|
||||
};
|
||||
|
||||
@@ -80,12 +87,56 @@ const splitPath = (path: string) => {
|
||||
};
|
||||
};
|
||||
|
||||
const TaskFilesPanel = memo(function TaskFilesPanel({ onSelectDiffPath, onOpenFile }: TaskFilesPanelProps) {
|
||||
const TaskFilesPanel = memo(function TaskFilesPanel({ onSelectDiff, onOpenFile }: TaskFilesPanelProps) {
|
||||
const [topTab, setTopTab] = useState<'diff' | 'files'>('diff');
|
||||
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
|
||||
const gitStatus = useSessionGitStatus(activeSessionId);
|
||||
const { commits } = useSessionCommits(activeSessionId ?? null);
|
||||
const openEditor = useOpenSessionInEditor(activeSessionId ?? null);
|
||||
const gitOps = useGitOperations(activeSessionId ?? null);
|
||||
const { toast } = useToast();
|
||||
|
||||
// State for commit diffs
|
||||
const [expandedCommit, setExpandedCommit] = useState<string | null>(null);
|
||||
const [commitDiffs, setCommitDiffs] = useState<Record<string, Record<string, FileInfo>>>({});
|
||||
const [loadingCommitSha, setLoadingCommitSha] = useState<string | null>(null);
|
||||
|
||||
// Fetch commit diff
|
||||
const fetchCommitDiff = useCallback(async (commitSha: string) => {
|
||||
if (!activeSessionId || commitDiffs[commitSha]) return;
|
||||
|
||||
setLoadingCommitSha(commitSha);
|
||||
try {
|
||||
const ws = getWebSocketClient();
|
||||
if (!ws) return;
|
||||
const response = await ws.request<{ success: boolean; files: Record<string, FileInfo> }>(
|
||||
'session.commit_diff',
|
||||
{ session_id: activeSessionId, commit_sha: commitSha },
|
||||
10000
|
||||
);
|
||||
if (response?.success && response.files) {
|
||||
setCommitDiffs((prev) => ({ ...prev, [commitSha]: response.files }));
|
||||
}
|
||||
} catch (err) {
|
||||
toast({
|
||||
title: 'Failed to load commit diff',
|
||||
description: err instanceof Error ? err.message : 'An unexpected error occurred',
|
||||
variant: 'error',
|
||||
});
|
||||
} finally {
|
||||
setLoadingCommitSha(null);
|
||||
}
|
||||
}, [activeSessionId, commitDiffs, toast]);
|
||||
|
||||
// Toggle commit expansion
|
||||
const toggleCommit = useCallback((commitSha: string) => {
|
||||
if (expandedCommit === commitSha) {
|
||||
setExpandedCommit(null);
|
||||
} else {
|
||||
setExpandedCommit(commitSha);
|
||||
void fetchCommitDiff(commitSha);
|
||||
}
|
||||
}, [expandedCommit, fetchCommitDiff]);
|
||||
|
||||
// Convert git status files to array for display
|
||||
const changedFiles = useMemo(() => {
|
||||
@@ -123,101 +174,179 @@ const TaskFilesPanel = memo(function TaskFilesPanel({ onSelectDiffPath, onOpenFi
|
||||
>
|
||||
<TabsContent value="diff" className="flex-1 min-h-0">
|
||||
<SessionPanelContent>
|
||||
{changedFiles.length === 0 ? (
|
||||
<div className="flex items-center justify-center h-full text-muted-foreground text-sm">
|
||||
No changes detected
|
||||
</div>
|
||||
) : (
|
||||
<ul className="space-y-2">
|
||||
{changedFiles.map((file) => {
|
||||
const { folder, file: name } = splitPath(file.path);
|
||||
return (
|
||||
<li
|
||||
key={file.path}
|
||||
className="group flex items-center justify-between gap-3 text-sm rounded-md px-1 py-0.5 -mx-1 hover:bg-muted/60 cursor-pointer"
|
||||
onClick={() => onSelectDiffPath(file.path)}
|
||||
>
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
{/* Staged indicator */}
|
||||
{file.staged ? (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<div className="flex items-center justify-center h-4 w-4 rounded bg-emerald-500/20 text-emerald-600">
|
||||
<IconCheck className="h-3 w-3" />
|
||||
</div>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Staged for commit</TooltipContent>
|
||||
</Tooltip>
|
||||
) : (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="flex items-center justify-center h-4 w-4 rounded border border-dashed border-muted-foreground/50 text-muted-foreground hover:border-emerald-500 hover:text-emerald-500 hover:bg-emerald-500/10"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
void gitOps.stage([file.path]);
|
||||
}}
|
||||
>
|
||||
<IconPlus className="h-2.5 w-2.5" />
|
||||
</button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Stage file</TooltipContent>
|
||||
</Tooltip>
|
||||
)}
|
||||
<button type="button" className="min-w-0 text-left cursor-pointer">
|
||||
<p className="truncate text-foreground">
|
||||
<span className="text-foreground/60">{folder}/</span>
|
||||
<span className="font-medium text-foreground">{name}</span>
|
||||
</p>
|
||||
{file.oldPath && (
|
||||
<p className="truncate text-xs text-muted-foreground">
|
||||
Renamed from: {file.oldPath}
|
||||
</p>
|
||||
<div className="space-y-4">
|
||||
{/* Uncommitted changes section */}
|
||||
{changedFiles.length > 0 && (
|
||||
<div>
|
||||
<div className="text-xs font-medium text-muted-foreground mb-2">
|
||||
Uncommitted ({changedFiles.length})
|
||||
</div>
|
||||
<ul className="space-y-1">
|
||||
{changedFiles.map((file) => {
|
||||
const { folder, file: name } = splitPath(file.path);
|
||||
return (
|
||||
<li
|
||||
key={file.path}
|
||||
className="group flex items-center justify-between gap-2 text-sm rounded-md px-1 py-0.5 -mx-1 hover:bg-muted/60 cursor-pointer"
|
||||
onClick={() => onSelectDiff(file.path)}
|
||||
>
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
{file.staged ? (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<div className="flex items-center justify-center h-4 w-4 rounded bg-emerald-500/20 text-emerald-600">
|
||||
<IconCheck className="h-3 w-3" />
|
||||
</div>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Staged for commit</TooltipContent>
|
||||
</Tooltip>
|
||||
) : (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="flex items-center justify-center h-4 w-4 rounded border border-dashed border-muted-foreground/50 text-muted-foreground hover:border-emerald-500 hover:text-emerald-500 hover:bg-emerald-500/10"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
void gitOps.stage([file.path]);
|
||||
}}
|
||||
>
|
||||
<IconPlus className="h-2.5 w-2.5" />
|
||||
</button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Stage file</TooltipContent>
|
||||
</Tooltip>
|
||||
)}
|
||||
<button type="button" className="min-w-0 text-left cursor-pointer">
|
||||
<p className="truncate text-foreground text-xs">
|
||||
<span className="text-foreground/60">{folder}/</span>
|
||||
<span className="font-medium text-foreground">{name}</span>
|
||||
</p>
|
||||
</button>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100">
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="text-muted-foreground hover:text-foreground"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
}}
|
||||
>
|
||||
<IconArrowBackUp className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Discard changes</TooltipContent>
|
||||
</Tooltip>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="text-muted-foreground hover:text-foreground"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
if (!activeSessionId) return;
|
||||
void openEditor.open({ filePath: file.path });
|
||||
}}
|
||||
>
|
||||
<IconExternalLink className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Open in editor</TooltipContent>
|
||||
</Tooltip>
|
||||
</div>
|
||||
<LineStat added={file.plus} removed={file.minus} />
|
||||
<StatusIcon status={file.status} />
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Commits section */}
|
||||
{commits.length > 0 && (
|
||||
<div>
|
||||
<div className="text-xs font-medium text-muted-foreground mb-2">
|
||||
Commits ({commits.length})
|
||||
</div>
|
||||
<ul className="space-y-1">
|
||||
{commits.map((commit) => {
|
||||
const isExpanded = expandedCommit === commit.commit_sha;
|
||||
const files = commitDiffs[commit.commit_sha];
|
||||
const isLoading = loadingCommitSha === commit.commit_sha;
|
||||
return (
|
||||
<li key={commit.commit_sha}>
|
||||
<button
|
||||
type="button"
|
||||
className="w-full flex items-center gap-2 text-left rounded-md px-1 py-1 -mx-1 hover:bg-muted/60 cursor-pointer"
|
||||
onClick={() => toggleCommit(commit.commit_sha)}
|
||||
>
|
||||
<IconChevronRight
|
||||
className={cn(
|
||||
'h-3 w-3 text-muted-foreground transition-transform shrink-0',
|
||||
isExpanded && 'rotate-90'
|
||||
)}
|
||||
/>
|
||||
<IconGitCommit className="h-3.5 w-3.5 text-emerald-500 shrink-0" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-xs truncate">
|
||||
<code className="font-mono text-muted-foreground">{commit.commit_sha.slice(0, 7)}</code>
|
||||
{' '}
|
||||
<span className="text-foreground">{commit.commit_message}</span>
|
||||
</p>
|
||||
</div>
|
||||
<span className="text-xs shrink-0">
|
||||
<span className="text-emerald-500">+{commit.insertions}</span>
|
||||
{'/'}
|
||||
<span className="text-rose-500">-{commit.deletions}</span>
|
||||
</span>
|
||||
{isLoading && (
|
||||
<IconLoader2 className="h-3 w-3 animate-spin text-muted-foreground" />
|
||||
)}
|
||||
</button>
|
||||
{isExpanded && files && (
|
||||
<ul className="ml-6 mt-1 space-y-0.5">
|
||||
{Object.entries(files).map(([path, file]) => {
|
||||
const { folder, file: fileName } = splitPath(path);
|
||||
return (
|
||||
<li
|
||||
key={path}
|
||||
className="flex items-center gap-2 text-xs rounded px-1 py-0.5 hover:bg-muted/50 cursor-pointer"
|
||||
onClick={() => onSelectDiff(path, file.diff)}
|
||||
>
|
||||
<StatusIcon status={file.status} />
|
||||
<span className="truncate flex-1">
|
||||
<span className="text-foreground/60">{folder}/</span>
|
||||
<span className="text-foreground">{fileName}</span>
|
||||
</span>
|
||||
<span className="shrink-0 text-xs">
|
||||
<span className="text-emerald-500">+{file.additions ?? 0}</span>
|
||||
{'/'}
|
||||
<span className="text-rose-500">-{file.deletions ?? 0}</span>
|
||||
</span>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100">
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="text-muted-foreground hover:text-foreground"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
}}
|
||||
>
|
||||
<IconArrowBackUp className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Discard changes</TooltipContent>
|
||||
</Tooltip>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="text-muted-foreground hover:text-foreground"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
if (!activeSessionId) return;
|
||||
void openEditor.open({ filePath: file.path });
|
||||
}}
|
||||
>
|
||||
<IconExternalLink className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Open in editor</TooltipContent>
|
||||
</Tooltip>
|
||||
</div>
|
||||
<LineStat added={file.plus} removed={file.minus} />
|
||||
<StatusIcon status={file.status} />
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Empty state */}
|
||||
{changedFiles.length === 0 && commits.length === 0 && (
|
||||
<div className="flex items-center justify-center h-full text-muted-foreground text-sm">
|
||||
No changes detected
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</SessionPanelContent>
|
||||
</TabsContent>
|
||||
<TabsContent value="files" className="mt-3 flex-1 min-h-0">
|
||||
|
||||
@@ -34,6 +34,11 @@ type TaskLayoutProps = {
|
||||
defaultLayouts?: Record<string, Layout>;
|
||||
};
|
||||
|
||||
export type SelectedDiff = {
|
||||
path: string;
|
||||
content?: string; // Optional: if provided, use this instead of looking up from git status
|
||||
};
|
||||
|
||||
export const TaskLayout = memo(function TaskLayout({
|
||||
workspaceId,
|
||||
boardId,
|
||||
@@ -41,7 +46,7 @@ export const TaskLayout = memo(function TaskLayout({
|
||||
repository = null,
|
||||
defaultLayouts = {},
|
||||
}: TaskLayoutProps) {
|
||||
const [selectedDiffPath, setSelectedDiffPath] = useState<string | null>(null);
|
||||
const [selectedDiff, setSelectedDiff] = useState<SelectedDiff | null>(null);
|
||||
const [openFileRequest, setOpenFileRequest] = useState<OpenFileTab | null>(null);
|
||||
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
|
||||
const layoutBySession = useLayoutStore((state) => state.columnsBySessionId);
|
||||
@@ -52,8 +57,8 @@ export const TaskLayout = memo(function TaskLayout({
|
||||
);
|
||||
const hasDevScript = Boolean(repository?.dev_script?.trim());
|
||||
|
||||
const handleSelectDiffPath = (path: string) => {
|
||||
setSelectedDiffPath(path);
|
||||
const handleSelectDiff = (path: string, content?: string) => {
|
||||
setSelectedDiff({ path, content });
|
||||
};
|
||||
|
||||
const handleOpenFile = (file: OpenFileTab) => {
|
||||
@@ -62,7 +67,7 @@ export const TaskLayout = memo(function TaskLayout({
|
||||
|
||||
const topFilesPanel = (
|
||||
<TaskFilesPanel
|
||||
onSelectDiffPath={handleSelectDiffPath}
|
||||
onSelectDiff={handleSelectDiff}
|
||||
onOpenFile={handleOpenFile}
|
||||
/>
|
||||
);
|
||||
@@ -127,9 +132,9 @@ export const TaskLayout = memo(function TaskLayout({
|
||||
>
|
||||
<Panel id="chat" minSize="400px" className="min-h-0 min-w-0">
|
||||
<TaskCenterPanel
|
||||
selectedDiffPath={selectedDiffPath}
|
||||
selectedDiff={selectedDiff}
|
||||
openFileRequest={openFileRequest}
|
||||
onDiffPathHandled={() => setSelectedDiffPath(null)}
|
||||
onDiffHandled={() => setSelectedDiff(null)}
|
||||
onFileOpenHandled={() => setOpenFileRequest(null)}
|
||||
/>
|
||||
</Panel>
|
||||
@@ -141,9 +146,9 @@ export const TaskLayout = memo(function TaskLayout({
|
||||
) : (
|
||||
<Panel id="chat" minSize="400px" className="min-h-0 min-w-0">
|
||||
<TaskCenterPanel
|
||||
selectedDiffPath={selectedDiffPath}
|
||||
selectedDiff={selectedDiff}
|
||||
openFileRequest={openFileRequest}
|
||||
onDiffPathHandled={() => setSelectedDiffPath(null)}
|
||||
onDiffHandled={() => setSelectedDiff(null)}
|
||||
onFileOpenHandled={() => setOpenFileRequest(null)}
|
||||
sessionId={sessionId}
|
||||
/>
|
||||
|
||||
@@ -41,6 +41,7 @@ import { Checkbox } from '@kandev/ui/checkbox';
|
||||
import { Label } from '@kandev/ui/label';
|
||||
import { CommitStatBadge, LineStat } from '@/components/diff-stat';
|
||||
import { useSessionGitStatus } from '@/hooks/domains/session/use-session-git-status';
|
||||
import { useSessionCommits } from '@/hooks/domains/session/use-session-commits';
|
||||
import { useGitOperations } from '@/hooks/use-git-operations';
|
||||
import type { FileInfo } from '@/lib/state/slices';
|
||||
import { formatUserHomePath } from '@/lib/utils';
|
||||
@@ -88,21 +89,30 @@ const TaskTopBar = memo(function TaskTopBar({
|
||||
|
||||
const { toast } = useToast();
|
||||
const gitStatus = useSessionGitStatus(activeSessionId ?? null);
|
||||
const { commits } = useSessionCommits(activeSessionId ?? null);
|
||||
const { pull, push, rebase, merge, commit, createPR, isLoading: isGitLoading } = useGitOperations(activeSessionId ?? null);
|
||||
|
||||
// Use worktree branch if available, otherwise fall back to base branch
|
||||
const displayBranch = worktreeBranch || baseBranch;
|
||||
|
||||
// Calculate total additions and deletions from all files.
|
||||
let totalAdditions = 0;
|
||||
let totalDeletions = 0;
|
||||
// Calculate total additions and deletions from uncommitted files.
|
||||
let uncommittedAdditions = 0;
|
||||
let uncommittedDeletions = 0;
|
||||
if (gitStatus?.files && Object.keys(gitStatus.files).length > 0) {
|
||||
for (const file of Object.values(gitStatus.files) as FileInfo[]) {
|
||||
totalAdditions += file.additions || 0;
|
||||
totalDeletions += file.deletions || 0;
|
||||
uncommittedAdditions += file.additions || 0;
|
||||
uncommittedDeletions += file.deletions || 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate cumulative additions and deletions from commits.
|
||||
const commitAdditions = commits.reduce((sum, c) => sum + c.insertions, 0);
|
||||
const commitDeletions = commits.reduce((sum, c) => sum + c.deletions, 0);
|
||||
|
||||
// Combined total (uncommitted + commits)
|
||||
const totalAdditions = uncommittedAdditions + commitAdditions;
|
||||
const totalDeletions = uncommittedDeletions + commitDeletions;
|
||||
|
||||
const handleGitOperation = useCallback(async (
|
||||
operation: () => Promise<{ success: boolean; output: string; error?: string; conflict_files?: string[] }>,
|
||||
operationName: string
|
||||
@@ -121,9 +131,7 @@ const TaskTopBar = memo(function TaskTopBar({
|
||||
description: result.error || 'An error occurred',
|
||||
variant: 'error',
|
||||
});
|
||||
if (result.conflict_files && result.conflict_files.length > 0) {
|
||||
console.log('Conflict files:', result.conflict_files);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
@@ -358,7 +366,7 @@ const TaskTopBar = memo(function TaskTopBar({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Git Status: Lines Changed */}
|
||||
{/* Git Status: Total Lines Changed (uncommitted + commits) */}
|
||||
{(totalAdditions > 0 || totalDeletions > 0) && (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
@@ -366,7 +374,9 @@ const TaskTopBar = memo(function TaskTopBar({
|
||||
<LineStat added={totalAdditions} removed={totalDeletions} />
|
||||
</span>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>Lines changed</TooltipContent>
|
||||
<TooltipContent>
|
||||
Total changes{commits.length > 0 ? ` (${commits.length} commit${commits.length !== 1 ? 's' : ''}${uncommittedAdditions > 0 || uncommittedDeletions > 0 ? ' + uncommitted' : ''})` : ''}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
)}
|
||||
|
||||
@@ -494,17 +504,18 @@ const TaskTopBar = memo(function TaskTopBar({
|
||||
<IconCopy className="h-4 w-4" />
|
||||
Copy workspace path
|
||||
</DropdownMenuItem>
|
||||
{/* TODO: Implement open workspace folder
|
||||
<DropdownMenuItem
|
||||
className="cursor-pointer"
|
||||
onClick={() => {
|
||||
// TODO: Implement open workspace folder
|
||||
console.log('Open workspace folder:', worktreePath);
|
||||
// Open workspace folder in system file manager
|
||||
}}
|
||||
disabled={!worktreePath}
|
||||
>
|
||||
<IconFolderOpen className="h-4 w-4" />
|
||||
Open workspace folder
|
||||
</DropdownMenuItem>
|
||||
*/}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
@@ -523,11 +534,11 @@ const TaskTopBar = memo(function TaskTopBar({
|
||||
{gitStatus?.files && Object.keys(gitStatus.files).length > 0 ? (
|
||||
<span>
|
||||
<span className="font-medium text-foreground">{Object.keys(gitStatus.files).length}</span> file{Object.keys(gitStatus.files).length !== 1 ? 's' : ''} changed
|
||||
{(totalAdditions > 0 || totalDeletions > 0) && (
|
||||
{(uncommittedAdditions > 0 || uncommittedDeletions > 0) && (
|
||||
<span className="ml-2">
|
||||
(<span className="text-green-600">+{totalAdditions}</span>
|
||||
(<span className="text-green-600">+{uncommittedAdditions}</span>
|
||||
{' / '}
|
||||
<span className="text-red-600">-{totalDeletions}</span>)
|
||||
<span className="text-red-600">-{uncommittedDeletions}</span>)
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
|
||||
Reference in New Issue
Block a user