({
activeWorkspace,
updateWorkspaceSettings,
clearGitRootCandidates,
refreshGitStatus,
}: UseGitRootSelectionOptions)
| 17 | } |
| 18 | |
| 19 | export function useGitRootSelection({ |
| 20 | activeWorkspace, |
| 21 | updateWorkspaceSettings, |
| 22 | clearGitRootCandidates, |
| 23 | refreshGitStatus, |
| 24 | }: UseGitRootSelectionOptions) { |
| 25 | const activeGitRoot = activeWorkspace?.settings.gitRoot ?? null; |
| 26 | |
| 27 | const handleSetGitRoot = useCallback( |
| 28 | async (path: string | null) => { |
| 29 | if (!activeWorkspace) { |
| 30 | return; |
| 31 | } |
| 32 | await updateWorkspaceSettings(activeWorkspace.id, { |
| 33 | gitRoot: path, |
| 34 | }); |
| 35 | clearGitRootCandidates(); |
| 36 | refreshGitStatus(); |
| 37 | }, |
| 38 | [ |
| 39 | activeWorkspace, |
| 40 | clearGitRootCandidates, |
| 41 | refreshGitStatus, |
| 42 | updateWorkspaceSettings, |
| 43 | ], |
| 44 | ); |
| 45 | |
| 46 | const handlePickGitRoot = useCallback(async () => { |
| 47 | if (!activeWorkspace) { |
| 48 | return; |
| 49 | } |
| 50 | const selection = await pickWorkspacePath(); |
| 51 | if (!selection) { |
| 52 | return; |
| 53 | } |
| 54 | const workspacePath = normalizePath(activeWorkspace.path); |
| 55 | const selectedPath = normalizePath(selection); |
| 56 | let nextRoot: string | null = null; |
| 57 | if (selectedPath === workspacePath) { |
| 58 | nextRoot = null; |
| 59 | } else if (selectedPath.startsWith(`${workspacePath}/`)) { |
| 60 | nextRoot = selectedPath.slice(workspacePath.length + 1); |
| 61 | } else { |
| 62 | nextRoot = selectedPath; |
| 63 | } |
| 64 | await handleSetGitRoot(nextRoot); |
| 65 | }, [activeWorkspace, handleSetGitRoot]); |
| 66 | |
| 67 | return { |
| 68 | activeGitRoot, |
| 69 | handleSetGitRoot, |
| 70 | handlePickGitRoot, |
| 71 | }; |
| 72 | } |
no test coverage detected