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";
6import { useEffect, useEffectEvent } from "react";
7
8import { subscribeDocumentReturn } from "~/lib/documentActivity";
9import { filesystemEnvironment } from "~/state/filesystem";
10import { useEnvironmentQuery } from "~/state/query";
11
12interface WorktreeRepositoryChoices {
13 /** The project's tracked Git folders. Null while they load or when they cannot be read. */
14 readonly choices: ReadonlyArray<WorktreeRepositoryChoice> | null;
15 /** Whether a worktree thread has a repository to work in besides the project folder. */
16 readonly tracksGitRepositories: boolean;
17 /** Reads the folders again; a repository gets its first commit outside the app. */
18 readonly refresh: () => void;
19}
20
21// A tab return fires both focus and visibilitychange; one read serves both.
22const RETURN_DEBOUNCE_MS = 250;
23
24const NO_TRACKED_FOLDERS: WorktreeRepositoryChoices = Object.freeze({
25 choices: Object.freeze([]),
26 tracksGitRepositories: false,
27 refresh: () => {},
28});
29
30/**
31 * The tracked Git folders a new worktree thread of this project can take. A project that tracks
32 * no folders asks the server nothing. With `revalidateOnReturn`, the folders are read again when
33 * the user returns while none offers a worktree yet, as one can gain its first commit outside the app.
34 */
35export function useWorktreeRepositoryChoices(
36 project: Pick<
37 EnvironmentProject,
38 "environmentId" | "id" | "workspaceRoot" | "trackedFolders"
39 > | null,
40 options?: { readonly revalidateOnReturn?: boolean },
41): WorktreeRepositoryChoices {
42 const tracksFolders = (project?.trackedFolders?.length ?? 0) > 0;
43 const folders = useEnvironmentQuery(
44 project !== null && tracksFolders
45 ? filesystemEnvironment.listFolders({
46 environmentId: project.environmentId,
47 input: { projectId: project.id },
48 })
49 : null,
50 );
51 // Also while the first read is in flight: its answer may predate the repository.
52 const awaitsChoice =
53 project !== null &&
54 tracksFolders &&
55 options?.revalidateOnReturn === true &&
56 (folders.data === null ||
57 worktreeRepositoryChoices({
58 folders: folders.data.folders,
59 projectRoot: project.workspaceRoot,
60 }).length === 0);
61 const refreshFolders = useEffectEvent(folders.refresh);
62 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the window's return to re-read folders changed outside the app
63 useEffect(() => {
64 if (!awaitsChoice) return;
65 let timer: ReturnType<typeof setTimeout> | undefined;
66 const unsubscribe = subscribeDocumentReturn(() => {
67 clearTimeout(timer);
68 timer = setTimeout(refreshFolders, RETURN_DEBOUNCE_MS);
69 });
70 return () => {
71 unsubscribe();
72 clearTimeout(timer);
73 };
74 }, [awaitsChoice]);
75 if (project === null || !tracksFolders) return NO_TRACKED_FOLDERS;
76 if (folders.data === null) {
77 // Unknown, whether loading or unreadable: the server decides, so a worktree send it cannot
78 // serve fails there and never runs in the original checkouts.
79 return { choices: null, tracksGitRepositories: true, refresh: folders.refresh };
80 }
81 const choices = worktreeRepositoryChoices({
82 folders: folders.data.folders,
83 projectRoot: project.workspaceRoot,
84 });
85 return {
86 choices,
87 // A repository with no commit yet is no choice, but the worktree is still the server's to plan.
88 tracksGitRepositories: folders.data.folders.some((folder) => folder.state === "git"),
89 refresh: folders.refresh,
90 };
91}