Open History to see the last commit
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;
15 /** Reads the folders again; a repository gets its first commit outside the app. */
16 readonly refresh: () => void;
17}
18
19const NO_TRACKED_FOLDERS: WorktreeRepositoryChoices = Object.freeze({
20 choices: Object.freeze([]),
21 tracksGitRepositories: false,
22 refresh: () => {},
23});
24
25/**
26 * The tracked Git folders a new worktree thread of this project can take. A project that tracks
27 * no folders asks the server nothing.
28 */
29export function useWorktreeRepositoryChoices(
30 project: Pick<
31 EnvironmentProject,
32 "environmentId" | "id" | "workspaceRoot" | "trackedFolders"
33 > | null,
34): WorktreeRepositoryChoices {
35 const tracksFolders = (project?.trackedFolders?.length ?? 0) > 0;
36 const folders = useEnvironmentQuery(
37 project !== null && tracksFolders
38 ? filesystemEnvironment.listFolders({
39 environmentId: project.environmentId,
40 input: { projectId: project.id },
41 })
42 : null,
43 );
44 if (project === null || !tracksFolders) return NO_TRACKED_FOLDERS;
45 if (folders.data === null) {
46 // Unknown, whether loading or unreadable: the server decides, so a worktree send it cannot
47 // serve fails there and never runs in the original checkouts.
48 return { choices: null, tracksGitRepositories: true, refresh: folders.refresh };
49 }
50 const choices = worktreeRepositoryChoices({
51 folders: folders.data.folders,
52 projectRoot: project.workspaceRoot,
53 });
54 return {
55 choices,
56 // A repository with no commit yet is no choice, but the worktree is still the server's to plan.
57 tracksGitRepositories: folders.data.folders.some((folder) => folder.state === "git"),
58 refresh: folders.refresh,
59 };
60}