diff --git a/src/renderer/src/components/workspace-cleanup/WorkspaceCleanupDialog.tsx b/src/renderer/src/components/workspace-cleanup/WorkspaceCleanupDialog.tsx index f96a5befb25..9d8cf8242f7 100644 --- a/src/renderer/src/components/workspace-cleanup/WorkspaceCleanupDialog.tsx +++ b/src/renderer/src/components/workspace-cleanup/WorkspaceCleanupDialog.tsx @@ -38,6 +38,11 @@ import { type WorkspaceCleanupScanError, type WorkspaceCleanupTier } from '../../../../shared/workspace-cleanup' +import { + resolveWorkspaceCleanupActiveView, + type WorkspaceCleanupView, + type WorkspaceCleanupViewCounts +} from './workspace-cleanup-view-selection' const TIER_LABELS: Record = { ready: 'Suggested cleanup', @@ -45,8 +50,6 @@ const TIER_LABELS: Record = { protected: 'Not suggested for cleanup' } -type CleanupView = WorkspaceCleanupTier | 'hidden' - const BLOCKER_LABELS: Record = { 'main-worktree': 'Main workspace', 'folder-repo': 'Folder project', @@ -185,7 +188,7 @@ export default function WorkspaceCleanupDialog(): React.JSX.Element { const open = activeModal === 'workspace-cleanup' const [selectedIds, setSelectedIds] = useState>(() => new Set()) - const [activeView, setActiveView] = useState('ready') + const [activeView, setActiveView] = useState('ready') const [confirming, setConfirming] = useState(false) const [removing, setRemoving] = useState(false) const [rowFailures, setRowFailures] = useState>({}) @@ -278,6 +281,22 @@ export default function WorkspaceCleanupDialog(): React.JSX.Element { const hiddenByKeepCount = filteredCandidates.filter((candidate) => candidate.blockers.includes('dismissed') ).length + const cleanupViewCounts = useMemo( + () => ({ + ready: groups.ready.length, + review: groups.review.length, + protected: groups.protected.length, + hidden: hiddenCandidates.length + }), + [groups.protected.length, groups.ready.length, groups.review.length, hiddenCandidates.length] + ) + const resolvedActiveView = resolveWorkspaceCleanupActiveView({ + requestedView: activeView, + counts: cleanupViewCounts, + open, + loading, + hasScan: scan != null + }) const repoNameById = useMemo( () => new Map(repos.map((repo) => [repo.id, repo.displayName || repo.path])), [repos] @@ -295,7 +314,7 @@ export default function WorkspaceCleanupDialog(): React.JSX.Element { const inactiveCount = filteredCandidates.length const hasAnyCandidates = candidates.length > 0 const initialLoading = loading && !scan - const activeRows = activeView === 'hidden' ? hiddenCandidates : groups[activeView] + const activeRows = resolvedActiveView === 'hidden' ? hiddenCandidates : groups[resolvedActiveView] const activeQueueableRows = useMemo( () => activeRows.filter(canQueueWorkspaceCleanupCandidate), [activeRows] @@ -313,33 +332,6 @@ export default function WorkspaceCleanupDialog(): React.JSX.Element { ? 'mixed' : 'unchecked' - useEffect(() => { - if (!open || loading || !scan) { - return - } - if (activeRows.length > 0) { - return - } - if (readyCount > 0) { - setActiveView('ready') - } else if (groups.review.length > 0) { - setActiveView('review') - } else if (groups.protected.length > 0) { - setActiveView('protected') - } else if (hiddenCandidates.length > 0) { - setActiveView('hidden') - } - }, [ - activeRows.length, - groups.protected.length, - groups.review.length, - hiddenCandidates.length, - loading, - open, - readyCount, - scan - ]) - const handleOpenChange = useCallback( (nextOpen: boolean) => { if (!nextOpen && !removing) { @@ -567,19 +559,14 @@ export default function WorkspaceCleanupDialog(): React.JSX.Element {
- {activeView !== 'hidden' && activeQueueableRows.length > 0 ? ( + {resolvedActiveView !== 'hidden' && activeQueueableRows.length > 0 ? (
- {activeView === 'hidden' && hiddenByKeepCount > 0 ? ( + {resolvedActiveView === 'hidden' && hiddenByKeepCount > 0 ? (