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

ProjectRepositoriesSection.tsx

187 lines6.9 KB

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, type Msg } 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 | Msg> = {
33 git: msg("Git repository"),
34 plain: msg({ context: "a plain folder that is not a Git repository" })`Folder`,
35 missing: msg({ context: "folder state: not found on disk" })`Missing`,
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: folder.name }}. Files on disk are not touched.`,
109 confirmText: gt({ context: "verb, removes a folder from the project" })`Remove`,
110 destructive: true,
111 onConfirm: () => void save({ untrack: folder.path }),
112 });
113​
114 const add = (folder: ProjectTrackedFolder) => {
115 const next = appendTrackedFolders(added, [folder]);
116 if (next === null) {
117 Dialog.alert(
118 gt`Too many folders`,
119 gt`A project can cover up to ${{ max: PROJECT_TRACKED_FOLDERS_MAX }} folders.`,
120 );
121 return;
122 }
123 void save({ track: folder });
124 };
125​
126 if (folders.length === 0 && suggested.length === 0) {
127 return (
128 <SettingsSection
129 title={props.title}
130 footer={gt`Repositories inside this project's folder are part of it as soon as they are there.`}
131 >
132 {null}
133 </SettingsSection>
134 );
135 }
136​
137 return (
138 <SettingsSection title={props.title}>
139 {folders.map((folder) => (
140 <ListRowLayout
141 key={folder.path}
142 icon="folder"
143 label={folder.name}
144 description={folder.path}
145 meta={
146 folder.state === undefined ? undefined : (
147 <Text
148 textRole="footnote"
149 className={
150 folder.state === "missing" ? "text-warning-foreground" : "text-foreground-muted"
151 }
152 >
153 {m(FOLDER_STATE_LABELS[folder.state])}
154 </Text>
155 )
156 }
157 accessory={
158 // A repository inside the project folder is there for as long as it is on disk.
159 folder.discovered === true ? undefined : (
160 <Pressable
161 accessibilityRole="button"
162 accessibilityLabel={gt`Remove ${{ name: folder.name }} from the project`}
163 disabled={!canEdit || isBusy}
164 onPress={() => remove(folder)}
165 className="rounded-full border border-border bg-card px-3 py-1.5 disabled:opacity-40"
166 >
167 <Text className="font-t3-medium text-xs text-danger-foreground">
168 <T context="verb, removes a folder from the project">Remove</T>
169 </Text>
170 </Pressable>
171 )
172 }
173 />
174 ))}
175 {suggested.map((folder) => (
176 <ListActionRow
177 key={folder.path}
178 icon="plus"
179 label={gt`Add ${{ name: folder.name }}`}
180 description={gt`Listed in the workspace file · ${{ path: folder.path }}`}
181 disabled={!canEdit || isBusy}
182 onPress={() => add(folder)}
183 />
184 ))}
185 </SettingsSection>
186 );