"use client"; import { memo, useMemo, useState, useCallback, useEffect } from "react"; import { PanelRoot, PanelBody } from "./panel-primitives"; import { useAppStore } from "@/components/state-provider"; 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 { useSessionFileReviews } from "@/hooks/use-session-file-reviews"; import { useCumulativeDiff } from "@/hooks/domains/session/use-cumulative-diff"; import { hashDiff, normalizeDiffContent } from "@/components/review/types"; import type { FileInfo } from "@/lib/state/store"; import { useToast } from "@/components/toast-provider"; import { useIsTaskArchived, ArchivedPanelPlaceholder } from "./task-archived-context"; import { DiscardDialog, CommitDialog, PRDialog } from "./changes-panel-dialogs"; import { ChangesPanelHeader } from "./changes-panel-header"; import { FileListSection, CommitsSection, ActionButtonsSection, ReviewProgressBar, } from "./changes-panel-timeline"; type ChangesPanelProps = { onOpenDiffFile: (path: string) => void; onEditFile: (path: string) => void; onOpenCommitDetail?: (sha: string) => void; onOpenDiffAll?: () => void; onOpenReview?: () => void; }; type CumulativeDiffFiles = Record< string, { diff?: string; status?: string; additions?: number; deletions?: number } >; function collectReviewPaths( gitStatus: ReturnType, cumulativeDiffFiles: CumulativeDiffFiles | undefined, ): Set { const paths = new Set(); if (gitStatus?.files) { for (const [path, file] of Object.entries(gitStatus.files)) { if (file.diff && normalizeDiffContent(file.diff)) paths.add(path); } } if (cumulativeDiffFiles) { for (const [path, file] of Object.entries(cumulativeDiffFiles)) { if (paths.has(path)) continue; if (file.diff && normalizeDiffContent(file.diff)) paths.add(path); } } return paths; } function getDiffContentForPath( path: string, gitStatus: ReturnType, cumulativeDiffFiles: CumulativeDiffFiles | undefined, ): string { const uncommitted = gitStatus?.files?.[path]; if (uncommitted?.diff) return normalizeDiffContent(uncommitted.diff); const cumDiff = cumulativeDiffFiles?.[path]?.diff; if (cumDiff) return normalizeDiffContent(cumDiff); return ""; } function isFileReviewStale(diffContent: string, diffHash: string | undefined): boolean { return !!(diffContent && diffHash && diffHash !== hashDiff(diffContent)); } /** Compute review progress across uncommitted + committed files */ function computeReviewProgress( gitStatus: ReturnType, cumulativeDiff: { files?: CumulativeDiffFiles } | null, reviews: Map, ) { const cumulativeDiffFiles = cumulativeDiff?.files; const paths = collectReviewPaths(gitStatus, cumulativeDiffFiles); let reviewed = 0; for (const path of paths) { const state = reviews.get(path); if (!state?.reviewed) continue; const diffContent = getDiffContentForPath(path, gitStatus, cumulativeDiffFiles); if (isFileReviewStale(diffContent, state.diffHash)) continue; reviewed++; } return { reviewedCount: reviewed, totalFileCount: paths.size }; } function computeHasAnything( hasUnstaged: boolean, hasStaged: boolean, hasCommits: boolean, ): boolean { return hasUnstaged || hasStaged || hasCommits; } /** Determine the last timeline section for isLast logic */ function getLastTimelineSection(hasCommits: boolean, hasStaged: boolean): string { if (hasCommits) return "action"; if (hasStaged) return "staged"; return "unstaged"; } function useChangesGitHandlers( gitOps: ReturnType, toast: ReturnType["toast"], baseBranch: string | undefined, ) { const handleGitOperation = useCallback( async ( operation: () => Promise<{ success: boolean; output: string; error?: string }>, operationName: string, ) => { try { const result = await operation(); const variant = result.success ? "success" : "error"; const title = result.success ? `${operationName} successful` : `${operationName} failed`; const description = result.success ? result.output.slice(0, 200) || `${operationName} completed` : result.error || "An error occurred"; toast({ title, description, variant }); } catch (e) { toast({ title: `${operationName} failed`, description: e instanceof Error ? e.message : "An unexpected error occurred", variant: "error", }); } }, [toast], ); const handlePull = useCallback(() => { handleGitOperation(() => gitOps.pull(), "Pull"); }, [handleGitOperation, gitOps]); const handleRebase = useCallback(() => { const targetBranch = baseBranch?.replace(/^origin\//, "") || "main"; handleGitOperation(() => gitOps.rebase(targetBranch), "Rebase"); }, [handleGitOperation, gitOps, baseBranch]); const handlePush = useCallback(() => { handleGitOperation(() => gitOps.push(), "Push"); }, [handleGitOperation, gitOps]); return { handleGitOperation, handlePull, handleRebase, handlePush }; } function useChangesStageHandlers( gitOps: ReturnType, changedFiles: unknown[], ) { const [pendingStageFiles, setPendingStageFiles] = useState>(new Set()); useEffect(() => { if (pendingStageFiles.size > 0) setPendingStageFiles(new Set()); // eslint-disable-next-line react-hooks/exhaustive-deps }, [changedFiles]); const handleStageAll = useCallback(async () => { try { await gitOps.stage(); } catch { /* handled by gitOps */ } }, [gitOps]); const handleStage = useCallback( async (path: string) => { setPendingStageFiles((prev) => new Set(prev).add(path)); try { await gitOps.stage([path]); } catch { setPendingStageFiles((prev) => { const next = new Set(prev); next.delete(path); return next; }); } }, [gitOps], ); const handleUnstage = useCallback( async (path: string) => { setPendingStageFiles((prev) => new Set(prev).add(path)); try { await gitOps.unstage([path]); } catch { setPendingStageFiles((prev) => { const next = new Set(prev); next.delete(path); return next; }); } }, [gitOps], ); return { pendingStageFiles, handleStageAll, handleStage, handleUnstage }; } function useChangesDiscardCommitHandlers( gitOps: ReturnType, toast: ReturnType["toast"], handleGitOperation: ( op: () => Promise<{ success: boolean; output: string; error?: string }>, name: string, ) => Promise, ) { const [showDiscardDialog, setShowDiscardDialog] = useState(false); const [fileToDiscard, setFileToDiscard] = useState(null); const [commitDialogOpen, setCommitDialogOpen] = useState(false); const [commitMessage, setCommitMessage] = useState(""); const handleDiscardClick = useCallback((filePath: string) => { setFileToDiscard(filePath); setShowDiscardDialog(true); }, []); const handleDiscardConfirm = useCallback(async () => { if (!fileToDiscard) return; try { const result = await gitOps.discard([fileToDiscard]); if (!result.success) toast({ title: "Failed to discard changes", description: result.error || "An unknown error occurred", variant: "error", }); } catch (error) { toast({ title: "Failed to discard changes", description: error instanceof Error ? error.message : "An unknown error occurred", variant: "error", }); } finally { setShowDiscardDialog(false); setFileToDiscard(null); } }, [fileToDiscard, gitOps, toast]); const handleOpenCommitDialog = useCallback(() => { setCommitMessage(""); setCommitDialogOpen(true); }, []); const handleCommit = useCallback(async () => { if (!commitMessage.trim()) return; setCommitDialogOpen(false); await handleGitOperation(() => gitOps.commit(commitMessage.trim(), false), "Commit"); setCommitMessage(""); }, [commitMessage, handleGitOperation, gitOps]); return { showDiscardDialog, setShowDiscardDialog, fileToDiscard, commitDialogOpen, setCommitDialogOpen, commitMessage, setCommitMessage, handleDiscardClick, handleDiscardConfirm, handleOpenCommitDialog, handleCommit, }; } function useChangesPRHandlers( gitOps: ReturnType, toast: ReturnType["toast"], taskTitle: string | undefined, baseBranch: string | undefined, ) { const [prDialogOpen, setPrDialogOpen] = useState(false); const [prTitle, setPrTitle] = useState(""); const [prBody, setPrBody] = useState(""); const [prDraft, setPrDraft] = useState(true); const handleOpenPRDialog = useCallback(() => { setPrTitle(taskTitle || ""); setPrBody(""); setPrDialogOpen(true); }, [taskTitle]); const handleCreatePR = useCallback(async () => { if (!prTitle.trim()) return; setPrDialogOpen(false); try { const result = await gitOps.createPR(prTitle.trim(), prBody.trim(), baseBranch, prDraft); if (result.success) { toast({ title: prDraft ? "Draft PR created" : "PR created", description: result.pr_url || "PR created successfully", variant: "success", }); if (result.pr_url) window.open(result.pr_url, "_blank"); } else { toast({ title: "Create PR failed", description: result.error || "An error occurred", variant: "error" }); } } catch (e) { toast({ title: "Create PR failed", description: e instanceof Error ? e.message : "An error occurred", variant: "error" }); } setPrTitle(""); setPrBody(""); }, [prTitle, prBody, baseBranch, prDraft, gitOps, toast]); return { prDialogOpen, setPrDialogOpen, prTitle, setPrTitle, prBody, setPrBody, prDraft, setPrDraft, handleOpenPRDialog, handleCreatePR }; } function useChangesDialogHandlers( gitOps: ReturnType, toast: ReturnType["toast"], handleGitOperation: ( op: () => Promise<{ success: boolean; output: string; error?: string }>, name: string, ) => Promise, taskTitle: string | undefined, baseBranch: string | undefined, ) { const discardCommit = useChangesDiscardCommitHandlers(gitOps, toast, handleGitOperation); const pr = useChangesPRHandlers(gitOps, toast, taskTitle, baseBranch); return { ...discardCommit, ...pr }; } function getBaseBranchDisplay(baseBranch: string | undefined): string { return baseBranch ? baseBranch.replace(/^origin\//, "") : "main"; } function mapChangedFiles(gitStatus: ReturnType) { if (!gitStatus?.files || Object.keys(gitStatus.files).length === 0) return []; return (Object.values(gitStatus.files) as FileInfo[]).map((file: FileInfo) => ({ path: file.path, status: file.status, staged: file.staged, plus: file.additions, minus: file.deletions, oldPath: file.old_path, })); } function computeStagedStats( gitStatus: ReturnType, stagedFiles: unknown[], ) { let additions = 0; let deletions = 0; const count = stagedFiles.length; if (gitStatus?.files && count > 0) { for (const file of Object.values(gitStatus.files) as FileInfo[]) { if (file.staged) { additions += file.additions || 0; deletions += file.deletions || 0; } } } return { stagedFileCount: count, stagedAdditions: additions, stagedDeletions: deletions }; } function useChangesPanelStoreData() { const activeSessionId = useAppStore((state) => state.tasks.activeSessionId); const taskTitle = useAppStore((state) => { if (!state.tasks.activeTaskId) return undefined; return state.kanban.tasks.find((t: { id: string }) => t.id === state.tasks.activeTaskId)?.title; }); const baseBranch = useAppStore((state) => activeSessionId ? state.taskSessions.items[activeSessionId]?.base_branch : undefined, ); const displayBranch = useAppStore((state) => activeSessionId ? (state.gitStatus.bySessionId[activeSessionId]?.branch ?? null) : null, ); return { activeSessionId, taskTitle, baseBranch, displayBranch }; } type ChangesPanelBodyProps = { hasAnything: boolean; hasUnstaged: boolean; hasStaged: boolean; hasCommits: boolean; unstagedFiles: ReturnType; stagedFiles: ReturnType; commits: ReturnType["commits"]; pendingStageFiles: Set; lastTimelineSection: string; reviewedCount: number; totalFileCount: number; aheadCount: number; isLoading: boolean; dialogs: ReturnType; onOpenDiffFile: (path: string) => void; onEditFile: (path: string) => void; onOpenCommitDetail?: (sha: string) => void; onOpenReview?: () => void; onStageAll: () => void; onStage: (path: string) => Promise; onUnstage: (path: string) => Promise; onPush: () => void; stagedFileCount: number; stagedAdditions: number; stagedDeletions: number; displayBranch: string | null; baseBranch: string | undefined; }; function ChangesPanelBody({ hasAnything, hasUnstaged, hasStaged, hasCommits, unstagedFiles, stagedFiles, commits, pendingStageFiles, lastTimelineSection, reviewedCount, totalFileCount, aheadCount, isLoading, dialogs, onOpenDiffFile, onEditFile, onOpenCommitDetail, onOpenReview, onStageAll, onStage, onUnstage, onPush, stagedFileCount, stagedAdditions, stagedDeletions, displayBranch, baseBranch, }: ChangesPanelBodyProps) { return (
{!hasAnything && (
Your changed files will appear here
)} {hasAnything && (
{hasUnstaged && ( )} {hasStaged && ( )} {hasCommits && ( )} {hasCommits && ( )}
)}
); } const ChangesPanel = memo(function ChangesPanel({ onOpenDiffFile, onEditFile, onOpenCommitDetail, onOpenDiffAll, onOpenReview, }: ChangesPanelProps) { const isArchived = useIsTaskArchived(); const { activeSessionId, taskTitle, baseBranch, displayBranch } = useChangesPanelStoreData(); const gitStatus = useSessionGitStatus(activeSessionId); const { commits } = useSessionCommits(activeSessionId ?? null); const gitOps = useGitOperations(activeSessionId ?? null); const { toast } = useToast(); const { reviews } = useSessionFileReviews(activeSessionId); const { diff: cumulativeDiff } = useCumulativeDiff(activeSessionId); const aheadCount = gitStatus?.ahead ?? 0; const behindCount = gitStatus?.behind ?? 0; const baseBranchDisplay = useMemo(() => getBaseBranchDisplay(baseBranch), [baseBranch]); const changedFiles = useMemo(() => mapChangedFiles(gitStatus), [gitStatus]); const unstagedFiles = useMemo(() => changedFiles.filter((f) => !f.staged), [changedFiles]); const stagedFiles = useMemo(() => changedFiles.filter((f) => f.staged), [changedFiles]); const { reviewedCount, totalFileCount } = useMemo( () => computeReviewProgress(gitStatus, cumulativeDiff, reviews), [gitStatus, cumulativeDiff, reviews], ); const { stagedFileCount, stagedAdditions, stagedDeletions } = useMemo( () => computeStagedStats(gitStatus, stagedFiles), [gitStatus, stagedFiles], ); const { handleGitOperation, handlePull, handleRebase, handlePush } = useChangesGitHandlers(gitOps, toast, baseBranch); const { pendingStageFiles, handleStageAll, handleStage, handleUnstage } = useChangesStageHandlers(gitOps, changedFiles); const dialogs = useChangesDialogHandlers(gitOps, toast, handleGitOperation, taskTitle, baseBranch); const hasUnstaged = unstagedFiles.length > 0; const hasStaged = stagedFiles.length > 0; const hasCommits = commits.length > 0; const hasChanges = changedFiles.length > 0; const hasAnything = computeHasAnything(hasUnstaged, hasStaged, hasCommits); const lastTimelineSection = getLastTimelineSection(hasCommits, hasStaged); if (isArchived) return ; return ( ); }); export { ChangesPanel };