Open History to see the last commit
1import {
2 appendTrackedFolders,
3 suggestedTrackedFolders,
4} from "@t3tools/client-runtime/state/projects";
5import {
6 isAtomCommandInterrupted,
7 squashAtomCommandFailure,
8} from "@t3tools/client-runtime/state/runtime";
9import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
10import {
11 AuthFilesystemReadScope,
12 AuthOrchestrationOperateScope,
13 PROJECT_TRACKED_FOLDERS_MAX,
14 type ProjectTrackedFolder,
15 type WorkspaceFolderState,
16} from "@t3tools/contracts";
17import { msg, T, useGT, useMessages } from "gt-react-native";
18import { useState } from "react";
19import { Pressable } from "react-native";
20
21import { AppText as Text } from "../../components/ui/AppText";
22import * as Dialog from "../../components/ui/Dialog";
23import { ListActionRow } from "../../components/ui/ListActionRow";
24import { ListRowLayout } from "../../components/ui/ListRowLayout";
25import { filesystemEnvironment } from "../../state/filesystem";
26import { projectEnvironment } from "../../state/projects";
27import { useEnvironmentQuery } from "../../state/query";
28import { useEnvironmentScope } from "../../state/session";
29import { useAtomCommand } from "../../state/use-atom-command";
30import { SettingsSection } from "./components/SettingsSection";
31
32const FOLDER_STATE_LABELS: Record<WorkspaceFolderState, string> = {
33 git: msg("Git repository"),
34 plain: msg("Folder", { $context: "a plain folder that is not a Git repository" }),
35 missing: msg("Missing", { $context: "folder state: not found on disk" }),
36};
37
38/** Tracked folders are paths on one environment, so each checkout gets its own section. */
39export function ProjectRepositoriesSection(props: {
40 readonly member: EnvironmentProject;
41 readonly title: string;
42}) {
43 const { member } = props;
44 const gt = useGT();
45 const m = useMessages();
46 const tracked = member.trackedFolders ?? [];
47 const canEdit = useEnvironmentScope(member.environmentId, AuthOrchestrationOperateScope);
48 const canReadFiles = useEnvironmentScope(member.environmentId, AuthFilesystemReadScope);
49 // Read from disk: the project's own copy of the list can be a moment behind a clone.
50 const listQuery = useEnvironmentQuery(
51 canReadFiles
52 ? filesystemEnvironment.listFolders({
53 environmentId: member.environmentId,
54 input: { projectId: member.id },
55 })
56 : null,
57 );
58 const folders: ReadonlyArray<ProjectTrackedFolder & { state?: WorkspaceFolderState }> =
59 listQuery.data?.folders ?? tracked;
60 // Only a workspace file's folders are offered: the rest is found without asking.
61 const scanQuery = useEnvironmentQuery(
62 canReadFiles
63 ? filesystemEnvironment.scanFolders({
64 environmentId: member.environmentId,
65 input: { workspaceRoot: member.workspaceRoot },
66 })
67 : null,
68 );
69 const suggested = suggestedTrackedFolders(folders, scanQuery.data?.folders ?? []);
70 const updateProject = useAtomCommand(projectEnvironment.update, { reportFailure: false });
71 const [isSaving, setIsSaving] = useState(false);
72 // A save builds on the listed folders, so it waits for the list that follows the last one.
73 const isBusy = isSaving || listQuery.isPending;
74 const added = folders.filter((folder) => folder.discovered !== true);
75
76 // One folder in or out: the server applies it to its own list, which another client may have
77 // changed while a confirmation was open here.
78 const save = async (
79 change: { readonly track: ProjectTrackedFolder } | { readonly untrack: string },
80 ) => {
81 setIsSaving(true);
82 const result = await updateProject({
83 environmentId: member.environmentId,
84 input: {
85 projectId: member.id,
86 ...("track" in change
87 ? { trackFolders: [{ path: change.track.path, name: change.track.name }] }
88 : { untrackFolders: [change.untrack] }),
89 },
90 });
91 setIsSaving(false);
92 if (result._tag === "Failure") {
93 if (!isAtomCommandInterrupted(result)) {
94 const error = squashAtomCommandFailure(result);
95 Dialog.alert(
96 gt("Failed to update repositories"),
97 error instanceof Error ? error.message : gt("An error occurred."),
98 );
99 }
100 return;
101 }
102 listQuery.refresh();
103 };
104
105 const remove = (folder: ProjectTrackedFolder) =>
106 Dialog.confirm({
107 title: gt("Remove folder"),
108 message: gt("Agents in this project stop working in {name}. Files on disk are not touched.", {
109 name: folder.name,
110 }),
111 confirmText: gt("Remove", { $context: "verb, removes a folder from the project" }),
112 destructive: true,
113 onConfirm: () => void save({ untrack: folder.path }),
114 });
115
116 const add = (folder: ProjectTrackedFolder) => {
117 const next = appendTrackedFolders(added, [folder]);
118 if (next === null) {
119 Dialog.alert(
120 gt("Too many folders"),
121 gt("A project can cover up to {max} folders.", { max: PROJECT_TRACKED_FOLDERS_MAX }),
122 );
123 return;
124 }
125 void save({ track: folder });
126 };
127
128 if (folders.length === 0 && suggested.length === 0) {
129 return (
130 <SettingsSection
131 title={props.title}
132 footer={gt(
133 "Repositories inside this project's folder are part of it as soon as they are there.",
134 )}
135 >
136 {null}
137 </SettingsSection>
138 );
139 }
140
141 return (
142 <SettingsSection title={props.title}>
143 {folders.map((folder) => (
144 <ListRowLayout
145 key={folder.path}
146 icon="folder"
147 label={folder.name}
148 description={folder.path}
149 meta={
150 folder.state === undefined ? undefined : (
151 <Text
152 textRole="footnote"
153 className={
154 folder.state === "missing" ? "text-warning-foreground" : "text-foreground-muted"
155 }
156 >
157 {m(FOLDER_STATE_LABELS[folder.state])}
158 </Text>
159 )
160 }
161 accessory={
162 // A repository inside the project folder is there for as long as it is on disk.
163 folder.discovered === true ? undefined : (
164 <Pressable
165 accessibilityRole="button"
166 accessibilityLabel={gt("Remove {name} from the project", { name: folder.name })}
167 disabled={!canEdit || isBusy}
168 onPress={() => remove(folder)}
169 className="rounded-full border border-border bg-card px-3 py-1.5 disabled:opacity-40"
170 >
171 <Text className="font-t3-medium text-xs text-danger-foreground">
172 <T context="verb, removes a folder from the project">Remove</T>
173 </Text>
174 </Pressable>
175 )
176 }
177 />
178 ))}
179 {suggested.map((folder) => (
180 <ListActionRow
181 key={folder.path}
182 icon="plus"
183 label={gt("Add {name}", { name: folder.name })}
184 description={gt("Listed in the workspace file · {path}", { path: folder.path })}
185 disabled={!canEdit || isBusy}
186 onPress={() => add(folder)}
187 />
188 ))}
189 </SettingsSection>
190 );
191}