From 6caa08fc79ed2819926a86c94b77b603a3802a5b Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sat, 4 Apr 2026 22:19:51 -0700 Subject: [PATCH] fix: address review findings (#302) - Rename worktreePath prop to worktreeRoot for clarity (it's a directory, not a file) - Remove unused imports (cn, basename, dirname, Layers) - Remove unused variables (fileName, dirPath) --- .../components/editor/CombinedDiffViewer.tsx | 2 + .../src/components/editor/DiffSectionItem.tsx | 139 +++++++++++++----- .../right-sidebar/SourceControl.tsx | 29 ++-- 3 files changed, 125 insertions(+), 45 deletions(-) diff --git a/src/renderer/src/components/editor/CombinedDiffViewer.tsx b/src/renderer/src/components/editor/CombinedDiffViewer.tsx index 65ecad9e2..97c51428e 100644 --- a/src/renderer/src/components/editor/CombinedDiffViewer.tsx +++ b/src/renderer/src/components/editor/CombinedDiffViewer.tsx @@ -342,6 +342,8 @@ export default function CombinedDiffViewer({ file }: { file: OpenFile }): React. isDark={isDark} settings={settings} sectionHeight={sectionHeights[index]} + worktreeId={file.worktreeId} + worktreeRoot={file.filePath} loadSection={loadSection} toggleSection={toggleSection} setSectionHeights={setSectionHeights} diff --git a/src/renderer/src/components/editor/DiffSectionItem.tsx b/src/renderer/src/components/editor/DiffSectionItem.tsx index 8bd954669..c463db7a2 100644 --- a/src/renderer/src/components/editor/DiffSectionItem.tsx +++ b/src/renderer/src/components/editor/DiffSectionItem.tsx @@ -1,15 +1,55 @@ -import React, { lazy, type MutableRefObject } from 'react' +import React, { lazy, useMemo, type MutableRefObject } from 'react' import { LazySection } from './LazySection' -import { ChevronDown, ChevronRight } from 'lucide-react' +import { ChevronDown, ChevronRight, ExternalLink } from 'lucide-react' import { DiffEditor, type DiffOnMount } from '@monaco-editor/react' import type { editor as monacoEditor } from 'monaco-editor' -import { basename, dirname } from '@/lib/path' +import { joinPath } from '@/lib/path' import { detectLanguage } from '@/lib/language-detect' -import { cn } from '@/lib/utils' +import { useAppStore } from '@/store' import type { GitDiffResult } from '../../../../shared/types' const ImageDiffViewer = lazy(() => import('./ImageDiffViewer')) +/** + * Compute approximate added/removed line counts by matching lines + * between original and modified content using a multiset approach. + * Not a true Myers diff, but fast and accurate enough for stat display. + */ +function computeLineStats( + original: string, + modified: string, + status: string +): { added: number; removed: number } { + if (status === 'added') { + return { added: modified ? modified.split('\n').length : 0, removed: 0 } + } + if (status === 'deleted') { + return { added: 0, removed: original ? original.split('\n').length : 0 } + } + + const origLines = original.split('\n') + const modLines = modified.split('\n') + + const origMap = new Map() + for (const line of origLines) { + origMap.set(line, (origMap.get(line) ?? 0) + 1) + } + + let matched = 0 + for (const line of modLines) { + const count = origMap.get(line) ?? 0 + if (count > 0) { + origMap.set(line, count - 1) + matched++ + } + } + + return { + added: modLines.length - matched, + removed: origLines.length - matched + } +} + type DiffSection = { key: string path: string @@ -32,6 +72,8 @@ export function DiffSectionItem({ isDark, settings, sectionHeight, + worktreeId, + worktreeRoot, loadSection, toggleSection, setSectionHeights, @@ -46,6 +88,9 @@ export function DiffSectionItem({ isDark: boolean settings: { terminalFontSize?: number; terminalFontFamily?: string } | null sectionHeight: number | undefined + worktreeId: string + /** The worktree root directory — not a file path; used to resolve absolute paths for opening files. */ + worktreeRoot: string loadSection: (index: number) => void toggleSection: (index: number) => void setSectionHeights: React.Dispatch>> @@ -53,12 +98,30 @@ export function DiffSectionItem({ modifiedEditorsRef: MutableRefObject> handleSectionSaveRef: MutableRefObject<(index: number) => Promise> }): React.JSX.Element { + const openFile = useAppStore((s) => s.openFile) const language = detectLanguage(section.path) - const fileName = basename(section.path) - const parentDir = dirname(section.path) - const dirPath = parentDir === '.' ? '' : parentDir const isEditable = section.area === 'unstaged' + const lineStats = useMemo( + () => + section.loading + ? null + : computeLineStats(section.originalContent, section.modifiedContent, section.status), + [section.loading, section.originalContent, section.modifiedContent, section.status] + ) + + const handleOpenInEditor = (e: React.MouseEvent): void => { + e.stopPropagation() + const absolutePath = joinPath(worktreeRoot, section.path) + openFile({ + filePath: absolutePath, + relativePath: section.path, + worktreeId, + language, + mode: 'edit' + }) + } + const handleMount: DiffOnMount = (editor, monaco) => { const modifiedEditor = editor.getModifiedEditor() @@ -92,37 +155,45 @@ export function DiffSectionItem({ return ( - +
+ + {section.collapsed ? ( + + ) : ( + + )} +
+ {!section.collapsed && (
= { staged: 'Staged Changes', unstaged: 'Changes', @@ -533,19 +532,23 @@ export default function SourceControl(): React.JSX.Element { } }} > - Open non-conflict diffs + View all ) : ( - { e.stopPropagation() if (activeWorktreeId && worktreePath) { openAllDiffs(activeWorktreeId, worktreePath, undefined, area) } }} - /> + > + View all + ) } /> @@ -589,16 +592,20 @@ export default function SourceControl(): React.JSX.Element { isCollapsed={collapsedSections.has('branch')} onToggle={() => toggleSection('branch')} actions={ - { e.stopPropagation() if (activeWorktreeId && worktreePath && branchSummary) { openBranchAllDiffs(activeWorktreeId, worktreePath, branchSummary) } }} - /> + > + View all + } /> {!collapsedSections.has('branch') &&