(activeWorkspace: WorkspaceInfo | null)
| 13 | }; |
| 14 | |
| 15 | export function useGitRemote(activeWorkspace: WorkspaceInfo | null) { |
| 16 | const [state, setState] = useState<GitRemoteState>(emptyState); |
| 17 | const requestIdRef = useRef(0); |
| 18 | const workspaceIdRef = useRef<string | null>(activeWorkspace?.id ?? null); |
| 19 | const workspaceId = activeWorkspace?.id ?? null; |
| 20 | |
| 21 | const refresh = useCallback(() => { |
| 22 | if (!workspaceId) { |
| 23 | setState(emptyState); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | const requestId = requestIdRef.current + 1; |
| 28 | requestIdRef.current = requestId; |
| 29 | |
| 30 | return getGitRemote(workspaceId) |
| 31 | .then((remote) => { |
| 32 | if ( |
| 33 | requestIdRef.current !== requestId || |
| 34 | workspaceIdRef.current !== workspaceId |
| 35 | ) { |
| 36 | return; |
| 37 | } |
| 38 | setState({ remote, error: null }); |
| 39 | }) |
| 40 | .catch((error) => { |
| 41 | if ( |
| 42 | requestIdRef.current !== requestId || |
| 43 | workspaceIdRef.current !== workspaceId |
| 44 | ) { |
| 45 | return; |
| 46 | } |
| 47 | setState({ |
| 48 | remote: null, |
| 49 | error: error instanceof Error ? error.message : String(error), |
| 50 | }); |
| 51 | }); |
| 52 | }, [workspaceId]); |
| 53 | |
| 54 | useEffect(() => { |
| 55 | if (workspaceIdRef.current !== workspaceId) { |
| 56 | workspaceIdRef.current = workspaceId; |
| 57 | requestIdRef.current += 1; |
| 58 | setState(emptyState); |
| 59 | } |
| 60 | |
| 61 | if (!workspaceId) { |
| 62 | setState(emptyState); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | refresh()?.catch(() => {}); |
| 67 | }, [refresh, workspaceId]); |
| 68 | |
| 69 | return { ...state, refresh }; |
| 70 | } |
no test coverage detected