@@ -16,7 +16,7 @@ import {
1616 PlusIcon ,
1717} from "lucide-react" ;
1818import { LegendList } from "@legendapp/list/react" ;
19- import { memo , useCallback , useMemo , useState } from "react" ;
19+ import { memo , useState } from "react" ;
2020
2121import {
2222 COMPOSER_PULL_REQUEST_DRAG_TYPE ,
@@ -100,13 +100,9 @@ function LinkRowImpl({
100100 const openPrLink = useOpenPrLink ( threadRef ) ;
101101 const [ menuOpen , setMenuOpen ] = useState ( false ) ;
102102 const [ menuPosition , setMenuPosition ] = useState < { x : number ; y : number } | null > ( null ) ;
103- const menuAnchor = useMemo (
104- ( ) =>
105- menuPosition
106- ? { getBoundingClientRect : ( ) => new DOMRect ( menuPosition . x , menuPosition . y , 0 , 0 ) }
107- : undefined ,
108- [ menuPosition ] ,
109- ) ;
103+ const menuAnchor = menuPosition
104+ ? { getBoundingClientRect : ( ) => new DOMRect ( menuPosition . x , menuPosition . y , 0 , 0 ) }
105+ : undefined ;
110106 const { link, depth, stack } = line ;
111107 const snapshot = link . snapshot ;
112108 const open = snapshot === null || snapshot . state === "open" ;
@@ -338,6 +334,26 @@ function LinkRowImpl({
338334/** Memoized so a modifier key or a sync of one link re-renders only the rows that changed. */
339335const LinkRow = memo ( LinkRowImpl ) ;
340336
337+ // LegendList re-renders its rows when `data` changes identity. Inline, the compiler
338+ // recomputes these lines whenever a modifier key or row input changes too.
339+ function usePullRequestLines (
340+ pullRequests : Parameters < typeof visibleThreadPullRequests > [ 0 ] | undefined ,
341+ ) {
342+ "use memo" ;
343+ const links = visibleThreadPullRequests ( pullRequests ?? [ ] ) ;
344+ const lines = pullRequestListLines ( resolveThreadPullRequestChains ( links ) ) ;
345+ return { links, lines } ;
346+ }
347+
348+ function latestSyncedAt ( links : ReadonlyArray < ThreadPullRequestLink > ) : string | null {
349+ let latest : string | null = null ;
350+ for ( const link of links ) {
351+ const at = link . snapshot ?. syncedAt ;
352+ if ( at !== undefined && ( latest === null || at > latest ) ) latest = at ;
353+ }
354+ return latest ;
355+ }
356+
341357const lineKey = ( line : PullRequestListLine ) =>
342358 `${ line . link . host } /${ line . link . repository } #${ line . link . number } ` ;
343359
@@ -357,73 +373,51 @@ export function ThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
357373function EnabledThreadPullRequestsPanel ( { threadRef } : { threadRef : ScopedThreadRef } ) {
358374 const thread = useThreadShell ( threadRef ) ;
359375 const projects = useProjects ( ) ;
360- const environmentProjects = useMemo (
361- ( ) =>
362- projects
363- . filter ( ( project ) => project . environmentId === threadRef . environmentId )
364- . toSorted ( ( left , right ) =>
365- left . id === thread ?. projectId ? - 1 : right . id === thread ?. projectId ? 1 : 0 ,
366- ) ,
367- [ projects , threadRef . environmentId , thread ?. projectId ] ,
368- ) ;
376+ const environmentProjects = projects
377+ . filter ( ( project ) => project . environmentId === threadRef . environmentId )
378+ . toSorted ( ( left , right ) =>
379+ left . id === thread ?. projectId ? - 1 : right . id === thread ?. projectId ? 1 : 0 ,
380+ ) ;
369381 const modifiers = useShortcutModifierState ( true ) ;
370382 const speedMode =
371383 modifiers . shiftKey && ! modifiers . metaKey && ! modifiers . ctrlKey && ! modifiers . altKey ;
372- const openLinkDialog = useCallback ( ( ) => openLinkPullRequestDialog ( threadRef ) , [ threadRef ] ) ;
384+ const openLinkDialog = ( ) => openLinkPullRequestDialog ( threadRef ) ;
373385 const unlink = useAtomCommand ( threadEnvironment . unlinkPullRequest , { reportFailure : true } ) ;
374386 const watch = useAtomCommand ( threadEnvironment . watchPullRequest , { reportFailure : true } ) ;
375387 const capabilities = useServerConfigs ( ) . get ( threadRef . environmentId ) ?. environment . capabilities ;
376388 const supportsWatch = capabilities ?. threadPullRequestWatch === true ;
377- const links = useMemo ( ( ) => visibleThreadPullRequests ( thread ?. pullRequests ?? [ ] ) , [ thread ] ) ;
378- const lines = useMemo ( ( ) => pullRequestListLines ( resolveThreadPullRequestChains ( links ) ) , [ links ] ) ;
379- const handleUnlink = useCallback (
380- ( link : ThreadPullRequestLink ) => {
381- void unlink ( {
382- environmentId : threadRef . environmentId ,
383- input : {
384- threadId : threadRef . threadId ,
385- host : link . host ,
386- repository : link . repository ,
387- number : link . number ,
388- } ,
389- } ) ;
390- } ,
391- [ threadRef , unlink ] ,
392- ) ;
393- const handleSetWatching = useCallback (
394- ( link : ThreadPullRequestLink , watching : boolean ) => {
395- void watch ( {
396- environmentId : threadRef . environmentId ,
397- input : {
398- threadId : threadRef . threadId ,
399- host : link . host ,
400- repository : link . repository ,
401- number : link . number ,
402- watching,
403- } ,
404- } ) ;
405- } ,
406- [ threadRef , watch ] ,
407- ) ;
408- const openCount = useMemo (
409- ( ) => links . filter ( ( link ) => link . snapshot === null || link . snapshot . state === "open" ) . length ,
410- [ links ] ,
411- ) ;
412- const lastSynced = useMemo ( ( ) => {
413- let latest : string | null = null ;
414- for ( const link of links ) {
415- const at = link . snapshot ?. syncedAt ;
416- if ( at !== undefined && ( latest === null || at > latest ) ) latest = at ;
417- }
418- return latest ;
419- } , [ links ] ) ;
389+ const { links, lines } = usePullRequestLines ( thread ?. pullRequests ) ;
390+ const handleUnlink = ( link : ThreadPullRequestLink ) => {
391+ void unlink ( {
392+ environmentId : threadRef . environmentId ,
393+ input : {
394+ threadId : threadRef . threadId ,
395+ host : link . host ,
396+ repository : link . repository ,
397+ number : link . number ,
398+ } ,
399+ } ) ;
400+ } ;
401+ const handleSetWatching = ( link : ThreadPullRequestLink , watching : boolean ) => {
402+ void watch ( {
403+ environmentId : threadRef . environmentId ,
404+ input : {
405+ threadId : threadRef . threadId ,
406+ host : link . host ,
407+ repository : link . repository ,
408+ number : link . number ,
409+ watching,
410+ } ,
411+ } ) ;
412+ } ;
413+ const openCount = links . filter (
414+ ( link ) => link . snapshot === null || link . snapshot . state === "open" ,
415+ ) . length ;
416+ const lastSynced = latestSyncedAt ( links ) ;
420417
421418 // Legend re-renders visible rows only when data or extraData changes, so everything the rows
422419 // read from outside their line goes in here.
423- const rowInputs = useMemo (
424- ( ) => ( { speedMode, environmentProjects, capabilities, supportsWatch, thread } ) ,
425- [ speedMode , environmentProjects , capabilities , supportsWatch , thread ] ,
426- ) ;
420+ const rowInputs = { speedMode, environmentProjects, capabilities, supportsWatch, thread } ;
427421
428422 if ( links . length === 0 ) {
429423 return (
0 commit comments