Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useWorktreeRepositoryChoices.ts

52 lines1.9 KB

1import {
2 worktreeRepositoryChoices,
3 type WorktreeRepositoryChoice,
4} from "@t3tools/client-runtime/state/thread-repositories";
5import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
6​
7import { filesystemEnvironment } from "~/state/filesystem";
8import { useEnvironmentQuery } from "~/state/query";
9​
10interface WorktreeRepositoryChoices {
11 /** The project's tracked Git folders. Null while they load or when they cannot be read. */
12 readonly choices: ReadonlyArray<WorktreeRepositoryChoice> | null;
13 /** Whether a worktree thread has a repository to work in besides the project folder. */
14 readonly tracksGitRepositories: boolean;
16​
17const NO_TRACKED_FOLDERS: WorktreeRepositoryChoices = Object.freeze({
18 choices: Object.freeze([]),
19 tracksGitRepositories: false,
20});
21​
22/**
23 * The tracked Git folders a new worktree thread of this project can take. A project that tracks
24 * no folders asks the server nothing.
25 */
26export function useWorktreeRepositoryChoices(
27 project: Pick<
28 EnvironmentProject,
29 "environmentId" | "id" | "workspaceRoot" | "trackedFolders"
30 > | null,
31): WorktreeRepositoryChoices {
32 const tracksFolders = (project?.trackedFolders?.length ?? 0) > 0;
33 const folders = useEnvironmentQuery(
34 project !== null && tracksFolders
35 ? filesystemEnvironment.listFolders({
36 environmentId: project.environmentId,
37 input: { projectId: project.id },
38 })
39 : null,
40 );
41 if (project === null || !tracksFolders) return NO_TRACKED_FOLDERS;
42 if (folders.data === null) {
43 // Unknown, whether loading or unreadable: the server decides, so a worktree send it cannot
44 // serve fails there and never runs in the original checkouts.
45 return { choices: null, tracksGitRepositories: true };
46 }
47 const choices = worktreeRepositoryChoices({
48 folders: folders.data.folders,
49 projectRoot: project.workspaceRoot,
50 });
51 return { choices, tracksGitRepositories: choices.length > 0 };