@@ -342,36 +342,54 @@ export function TodoBoards({ projectId }: { projectId: string }) {
342342 [ projectId ] ,
343343 ) ;
344344
345- // TODO: not sure if this is necessary
346- const [ projectIds , setProjectIds ] = useState < string [ ] > ( [ ] ) ;
345+ // Track projects that have completed loading (not just started)
346+ const loadedProjectsRef = useRef < Set < string > > ( new Set ( ) ) ;
347+ const unsubscribeRef = useRef < ( ( ) => void ) | undefined > ( undefined ) ;
347348
348349 const [ showTodoItemsLoading , setShowTodoItemsLoading ] =
349350 useState < boolean > ( false ) ;
350351
351352 useEffect ( ( ) => {
352- let unsubscribe : ( ( ) => void ) | undefined ;
353+ // Clean up previous subscription synchronously before creating a new one
354+ if ( unsubscribeRef . current ) {
355+ unsubscribeRef . current ( ) ;
356+ unsubscribeRef . current = undefined ;
357+ }
358+
359+ // If project already loaded, no need to show loading or subscribe
360+ if ( loadedProjectsRef . current . has ( projectId ) ) {
361+ setShowTodoItemsLoading ( false ) ;
362+ return ;
363+ }
364+
365+ // If we have items or not loading, mark as loaded
366+ if ( allTodoItems . length > 0 || ! todoItemsCollection . isLoadingSubset ) {
367+ loadedProjectsRef . current . add ( projectId ) ;
368+ setShowTodoItemsLoading ( false ) ;
369+ return ;
370+ }
353371
354- if (
355- allTodoItems . length === 0 &&
356- todoItemsCollection . isLoadingSubset &&
357- ! projectIds . includes ( projectId )
358- ) {
359- setShowTodoItemsLoading ( true ) ;
360- setProjectIds ( ( prev ) => [ ...prev , projectId ] ) ;
372+ // Items are empty and still loading - show skeleton and subscribe
373+ setShowTodoItemsLoading ( true ) ;
361374
362- unsubscribe = todoItemsCollection . on ( "loadingSubset:change" , ( event ) => {
375+ const currentProjectId = projectId ;
376+ unsubscribeRef . current = todoItemsCollection . on (
377+ "loadingSubset:change" ,
378+ ( event ) => {
363379 if ( event . loadingSubsetTransition === "end" ) {
380+ loadedProjectsRef . current . add ( currentProjectId ) ;
364381 setShowTodoItemsLoading ( false ) ;
365382 }
366- } ) ;
367- } else {
368- setShowTodoItemsLoading ( false ) ;
369- }
383+ } ,
384+ ) ;
370385
371386 return ( ) => {
372- unsubscribe ?.( ) ;
387+ if ( unsubscribeRef . current ) {
388+ unsubscribeRef . current ( ) ;
389+ unsubscribeRef . current = undefined ;
390+ }
373391 } ;
374- } , [ projectId , projectIds , allTodoItems . length ] ) ;
392+ } , [ projectId , allTodoItems . length ] ) ;
375393
376394 // Get active todo item from already-loaded collection data
377395 // instead of making a separate query
0 commit comments