@@ -264,14 +264,20 @@ export function GitVisualizer({
264264 if ( ! cy ) return ;
265265 try {
266266 cy . resize ( ) ;
267- cy . fit ( undefined , 30 ) ;
267+ // Fit to the last 5 commits (most recent). If fewer than 5, fit all.
268+ const recentIds = commits . slice ( undefined , 10 ) . map ( ( c ) => c . id ) ;
269+ const recentNodes = recentIds . length > 0 ? cy . getElementById ( recentIds [ 0 ] ) : cy . collection ( ) ;
270+ for ( let i = 1 ; i < recentIds . length ; i ++ ) {
271+ recentNodes . merge ( cy . getElementById ( recentIds [ i ] ) ) ;
272+ }
273+ cy . fit ( recentNodes . nonempty ( ) ? recentNodes : undefined , 30 ) ;
268274 } catch ( error ) {
269275 console . warn ( "Resize error:" , error ) ;
270276 }
271277 } ;
272278 window . addEventListener ( "resize" , onResize ) ;
273279 return ( ) => window . removeEventListener ( "resize" , onResize ) ;
274- } , [ ] ) ;
280+ } , [ commits ] ) ;
275281
276282 // Re-run layout when commit set changes size to avoid race conditions during rapid branch switches
277283 const lastElementCountRef = useRef < number > ( 0 ) ;
@@ -295,7 +301,13 @@ export function GitVisualizer({
295301 l . run ( ) ;
296302 // Fit with padding if still mounted
297303 if ( cyRef . current === liveCy ) {
298- liveCy . fit ( undefined , 30 ) ;
304+ // After layout, fit to last 5 commits for focused view
305+ const recentIds = commits . slice ( undefined , 10 ) . map ( ( c ) => c . id ) ;
306+ const recentNodes = recentIds . length > 0 ? liveCy . getElementById ( recentIds [ 0 ] ) : liveCy . collection ( ) ;
307+ for ( let i = 1 ; i < recentIds . length ; i ++ ) {
308+ recentNodes . merge ( liveCy . getElementById ( recentIds [ i ] ) ) ;
309+ }
310+ liveCy . fit ( recentNodes . nonempty ( ) ? recentNodes : undefined , 30 ) ;
299311 }
300312 }
301313 } catch {
0 commit comments