(activeWorkspace: WorkspaceInfo | null)
| 13 | const DEFAULT_DEPTH = 2; |
| 14 | |
| 15 | export function useGitRepoScan(activeWorkspace: WorkspaceInfo | null) { |
| 16 | const [state, setState] = useState<GitRepoScanState>({ |
| 17 | repos: [], |
| 18 | isLoading: false, |
| 19 | error: null, |
| 20 | depth: DEFAULT_DEPTH, |
| 21 | hasScanned: false, |
| 22 | }); |
| 23 | const requestIdRef = useRef(0); |
| 24 | const workspaceIdRef = useRef<string | null>(activeWorkspace?.id ?? null); |
| 25 | |
| 26 | const scan = useCallback(async () => { |
| 27 | if (!activeWorkspace) { |
| 28 | setState((prev) => ({ ...prev, repos: [], isLoading: false })); |
| 29 | return; |
| 30 | } |
| 31 | const workspaceId = activeWorkspace.id; |
| 32 | const requestId = requestIdRef.current + 1; |
| 33 | requestIdRef.current = requestId; |
| 34 | setState((prev) => ({ |
| 35 | ...prev, |
| 36 | isLoading: true, |
| 37 | error: null, |
| 38 | hasScanned: true, |
| 39 | })); |
| 40 | try { |
| 41 | const repos = await listGitRoots(workspaceId, state.depth); |
| 42 | if ( |
| 43 | requestIdRef.current !== requestId || |
| 44 | workspaceIdRef.current !== workspaceId |
| 45 | ) { |
| 46 | return; |
| 47 | } |
| 48 | setState((prev) => ({ |
| 49 | ...prev, |
| 50 | repos, |
| 51 | isLoading: false, |
| 52 | error: null, |
| 53 | })); |
| 54 | } catch (error) { |
| 55 | if ( |
| 56 | requestIdRef.current !== requestId || |
| 57 | workspaceIdRef.current !== workspaceId |
| 58 | ) { |
| 59 | return; |
| 60 | } |
| 61 | setState((prev) => ({ |
| 62 | ...prev, |
| 63 | repos: [], |
| 64 | isLoading: false, |
| 65 | error: error instanceof Error ? error.message : String(error), |
| 66 | })); |
| 67 | } |
| 68 | }, [activeWorkspace, state.depth]); |
| 69 | |
| 70 | const setDepth = useCallback((depth: number) => { |
| 71 | const clamped = Math.min(6, Math.max(1, depth)); |
| 72 | setState((prev) => ({ ...prev, depth: clamped })); |
no test coverage detected