({
projects,
collapsedProjectIds,
activeView,
loadProjectThreads,
loadArchivedThreads,
dispatch,
setArchivedThreads,
}: UseProjectShellSyncInput)
| 15 | } |
| 16 | |
| 17 | export function useProjectShellSync({ |
| 18 | projects, |
| 19 | collapsedProjectIds, |
| 20 | activeView, |
| 21 | loadProjectThreads, |
| 22 | loadArchivedThreads, |
| 23 | dispatch, |
| 24 | setArchivedThreads, |
| 25 | }: UseProjectShellSyncInput) { |
| 26 | useEffect(() => { |
| 27 | if (projects.length === 0) { |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | dispatch({ type: 'sync-projects', projects }) |
| 32 | }, [dispatch, projects]) |
| 33 | |
| 34 | useEffect(() => { |
| 35 | const threadsScope = activeView === 'chat' ? 'chat' : 'code' |
| 36 | const expandedProjects = projects.filter( |
| 37 | (project) => |
| 38 | !collapsedProjectIds[project.id] && |
| 39 | (!project.threadsLoaded || project.threadsScope !== threadsScope), |
| 40 | ) |
| 41 | |
| 42 | for (const project of expandedProjects) { |
| 43 | void loadProjectThreads(project.id, { chat: activeView === 'chat' }) |
| 44 | } |
| 45 | }, [activeView, collapsedProjectIds, loadProjectThreads, projects]) |
| 46 | |
| 47 | useEffect(() => { |
| 48 | if (activeView !== 'archived') { |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | let cancelled = false |
| 53 | |
| 54 | const syncArchivedThreads = async () => { |
| 55 | const nextArchivedThreads = await loadArchivedThreads() |
| 56 | if (!cancelled) { |
| 57 | setArchivedThreads(nextArchivedThreads) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void syncArchivedThreads() |
| 62 | |
| 63 | return () => { |
| 64 | cancelled = true |
| 65 | } |
| 66 | }, [activeView, loadArchivedThreads, setArchivedThreads]) |
| 67 | } |
no test coverage detected