@@ -21,7 +21,7 @@ import {
2121import { ChevronDown , ChevronRight , ChevronsDownUp , ChevronsUpDown } from "lucide" ;
2222import * as Schema from "effect/Schema" ;
2323import * as DateTime from "effect/DateTime" ;
24- import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
24+ import { memo , useCallback , useEffect , useMemo , useRef , useState } from "react" ;
2525import { useCodeViewFileReveal } from "./diffs/useCodeViewFileReveal" ;
2626import { useOpenInPreferredEditor } from "../editorPreferences" ;
2727import { useFileContextMenuHandler } from "../fileContextMenu" ;
@@ -118,6 +118,85 @@ interface CollapsedDiffFilesState {
118118
119119const EMPTY_COLLAPSED_DIFF_FILE_KEYS : ReadonlySet < string > = new Set ( ) ;
120120
121+ // Header content re-renders only when its own file changes.
122+ const DiffFileCollapseToggle = memo ( function DiffFileCollapseToggle ( {
123+ filePath,
124+ fileKey,
125+ collapsed,
126+ unavailable,
127+ iconClassName,
128+ onToggle,
129+ } : {
130+ filePath : string ;
131+ fileKey : string ;
132+ collapsed : boolean ;
133+ unavailable : boolean ;
134+ iconClassName : string ;
135+ onToggle : ( fileKey : string ) => void ;
136+ } ) {
137+ return (
138+ < Tooltip >
139+ < TooltipTrigger
140+ render = {
141+ < Button
142+ size = "icon-micro"
143+ variant = "ghost"
144+ className = "-ms-0.5"
145+ aria-label = { collapsed ? `Expand ${ filePath } ` : `Collapse ${ filePath } ` }
146+ aria-expanded = { ! collapsed }
147+ disabled = { unavailable }
148+ onClick = { ( event ) => {
149+ event . stopPropagation ( ) ;
150+ onToggle ( fileKey ) ;
151+ } }
152+ />
153+ }
154+ >
155+ < MorphIcon
156+ className = { cn ( "size-4" , iconClassName ) }
157+ icon = { collapsed ? ChevronRight : ChevronDown }
158+ />
159+ </ TooltipTrigger >
160+ < TooltipPopup side = "top" > { collapsed ? "Expand diff" : "Collapse diff" } </ TooltipPopup >
161+ </ Tooltip >
162+ ) ;
163+ } ) ;
164+
165+ // Header content re-renders only when its own file changes.
166+ const DiffFileHeaderSuffix = memo ( function DiffFileHeaderSuffix ( {
167+ filePath,
168+ hasStat,
169+ error,
170+ truncated,
171+ onRetry,
172+ } : {
173+ filePath : string ;
174+ hasStat : boolean ;
175+ error : boolean ;
176+ truncated : boolean ;
177+ onRetry : ( path : string ) => void ;
178+ } ) {
179+ return (
180+ < >
181+ < DiffFilePathCopyButton filePath = { filePath } />
182+ { hasStat ? (
183+ < DiffFileStatus error = { error } truncated = { truncated } retry = { ( ) => onRetry ( filePath ) } />
184+ ) : null }
185+ </ >
186+ ) ;
187+ } ) ;
188+
189+ // Header content re-renders only when its own file changes.
190+ const DiffFileHeaderStat = memo ( function DiffFileHeaderStat ( {
191+ additions,
192+ deletions,
193+ } : {
194+ additions : number ;
195+ deletions : number ;
196+ } ) {
197+ return < DiffStatLabel additions = { additions } deletions = { deletions } /> ;
198+ } ) ;
199+
121200interface DiffPanelProps {
122201 mode ?: DiffPanelMode ;
123202 composerDraftTarget : ScopedThreadRef | DraftId ;
@@ -598,22 +677,22 @@ export default function DiffPanel({
598677 } ,
599678 [ activeCwd , activeRepositoryRoot , openInPreferredEditor , routeThreadRef ] ,
600679 ) ;
601- const toggleDiffFileCollapsed = useCallback (
602- ( fileKey : string ) => {
603- setCollapsedDiffFiles ( ( current ) => {
604- const next = new Set (
605- current . scopeKey === collapseScopeKey ? current . fileKeys : defaultCollapsedDiffFileKeys ,
606- ) ;
607- if ( next . has ( fileKey ) ) {
608- next . delete ( fileKey ) ;
609- } else {
610- next . add ( fileKey ) ;
611- }
612- return { scopeKey : collapseScopeKey , fileKeys : next } ;
613- } ) ;
614- } ,
615- [ collapseScopeKey , defaultCollapsedDiffFileKeys ] ,
616- ) ;
680+ const collapseDefaultsRef = useRef ( { collapseScopeKey , defaultCollapsedDiffFileKeys } ) ;
681+ collapseDefaultsRef . current = { collapseScopeKey , defaultCollapsedDiffFileKeys } ;
682+ const toggleDiffFileCollapsed = useCallback ( ( fileKey : string ) => {
683+ const { collapseScopeKey , defaultCollapsedDiffFileKeys } = collapseDefaultsRef . current ;
684+ setCollapsedDiffFiles ( ( current ) => {
685+ const next = new Set (
686+ current . scopeKey === collapseScopeKey ? current . fileKeys : defaultCollapsedDiffFileKeys ,
687+ ) ;
688+ if ( next . has ( fileKey ) ) {
689+ next . delete ( fileKey ) ;
690+ } else {
691+ next . add ( fileKey ) ;
692+ }
693+ return { scopeKey : collapseScopeKey , fileKeys : next } ;
694+ } ) ;
695+ } , [ ] ) ;
617696
618697 const toggleDiffFileCollapse = useCallback ( ( ) => {
619698 setCodeViewRevision ( ( current ) => current + 1 ) ;
@@ -1082,14 +1161,15 @@ export default function DiffPanel({
10821161 composerDraftTarget = { composerDraftTarget }
10831162 renderHeaderFilenameSuffix = { ( fileDiff ) => {
10841163 const path = resolveFileDiffPath ( fileDiff ) ;
1085- const stat = fileStats . get ( path ) ;
1164+ const state = fileStates . get ( path ) ;
10861165 return (
1087- < >
1088- < DiffFilePathCopyButton filePath = { path } />
1089- { stat ? (
1090- < DiffFileStatus { ...fileStates . get ( path ) } retry = { ( ) => retry ( path ) } />
1091- ) : null }
1092- </ >
1166+ < DiffFileHeaderSuffix
1167+ filePath = { path }
1168+ hasStat = { fileStats . has ( path ) }
1169+ error = { state ?. error ?? false }
1170+ truncated = { state ?. truncated ?? false }
1171+ onRetry = { retry }
1172+ />
10931173 ) ;
10941174 } }
10951175 { ...( lazySource
@@ -1099,7 +1179,7 @@ export default function DiffPanel({
10991179 renderHeaderMetadata : ( fileDiff : FileDiffMetadata ) => {
11001180 const stat = fileStats . get ( resolveFileDiffPath ( fileDiff ) ) ;
11011181 return stat ? (
1102- < DiffStatLabel
1182+ < DiffFileHeaderStat
11031183 additions = { stat . additions }
11041184 deletions = { stat . deletions }
11051185 />
@@ -1109,37 +1189,15 @@ export default function DiffPanel({
11091189 : { } ) }
11101190 renderHeaderPrefix = { ( fileDiff , fileKey ) => {
11111191 const unavailable = fileDiff . cacheKey ?. endsWith ( ":pending" ) === true ;
1112- const collapsed = unavailable || collapsedDiffFileKeys . has ( fileKey ) ;
1113- const filePath = resolveFileDiffPath ( fileDiff ) ;
11141192 return (
1115- < Tooltip >
1116- < TooltipTrigger
1117- render = {
1118- < Button
1119- size = "icon-micro"
1120- variant = "ghost"
1121- className = "-ms-0.5"
1122- aria-label = {
1123- collapsed ? `Expand ${ filePath } ` : `Collapse ${ filePath } `
1124- }
1125- aria-expanded = { ! collapsed }
1126- disabled = { unavailable }
1127- onClick = { ( event ) => {
1128- event . stopPropagation ( ) ;
1129- toggleDiffFileCollapsed ( fileKey ) ;
1130- } }
1131- />
1132- }
1133- >
1134- < MorphIcon
1135- className = { cn ( "size-4" , getDiffCollapseIconClassName ( fileDiff ) ) }
1136- icon = { collapsed ? ChevronRight : ChevronDown }
1137- />
1138- </ TooltipTrigger >
1139- < TooltipPopup side = "top" >
1140- { collapsed ? "Expand diff" : "Collapse diff" }
1141- </ TooltipPopup >
1142- </ Tooltip >
1193+ < DiffFileCollapseToggle
1194+ filePath = { resolveFileDiffPath ( fileDiff ) }
1195+ fileKey = { fileKey }
1196+ collapsed = { unavailable || collapsedDiffFileKeys . has ( fileKey ) }
1197+ unavailable = { unavailable }
1198+ iconClassName = { getDiffCollapseIconClassName ( fileDiff ) }
1199+ onToggle = { toggleDiffFileCollapsed }
1200+ />
11431201 ) ;
11441202 } }
11451203 options = { {
0 commit comments