The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 277 lines 10 KB
Raw
1 import { issueTrackerSummary } from "@t3tools/client-runtime/state/issue-trackers";
2 import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
3 import type {
4 EnvironmentId,
5 IssueTrackerConnection,
6 IssueTrackerConnectionList,
7 ProjectId,
8 } from "@t3tools/contracts";
9 import { AsyncResult } from "effect/reactivity";
10 import { useNavigation } from "@react-navigation/native";
11 import { useGT, useMessages } from "gt-react-native";
12 import { useState } from "react";
13 import { Platform, View } from "react-native";
14 import { tryOpenExternalUrl } from "../../../lib/openExternalUrl";
15 import Svg, { Path } from "react-native-svg";
16 import { withUniwind } from "uniwind";
17
18 import { SymbolView } from "../../../components/ui/AppSymbol";
19 import { AppText as Text } from "../../../components/ui/AppText";
20 import { Button } from "../../../components/ui/Button";
21 import { Menu } from "../../../components/ui/Menu";
22 import { SecretTextInput } from "../../../components/ui/SecretTextInput";
23 import { useProjects } from "../../../state/entities";
24 import { issueTrackerEnvironment } from "../../../state/issueTrackers";
25 import { useEnvironmentQuery } from "../../../state/query";
26 import { useAtomCommand } from "../../../state/use-atom-command";
27 import type { SettingsTarget } from "../settings-environment-filter";
28 import { SettingsProviderRow } from "./SettingsProviderRow";
29 import { SettingsRow } from "./SettingsRow";
30 import { SettingsSection } from "./SettingsSection";
31
32 const UNBOUND = "";
33 const ThemedSvg = withUniwind(Svg);
34 const ICON_SIZE = Platform.OS === "android" ? 20 : 18;
35
36 /** Art per tracker kind; the server supplies everything else about a tracker. */
37 const TRACKER_MARKS: Partial<Record<string, string>> = {
38 linear:
39 "M1.225 61.523c-.222-.949.908-1.546 1.597-.857l36.512 36.512c.69.69.092 1.82-.857 1.597C20.15 94.612 5.388 79.85 1.225 61.523ZM.002 46.889a.99.99 0 0 0 .29.76l52.062 52.061a.99.99 0 0 0 .76.29c2.765-.169 5.47-.559 8.1-1.152.75-.169 1.014-1.084.47-1.63L2.784 38.32c-.545-.545-1.46-.28-1.63.47A50.229 50.229 0 0 0 .002 46.89ZM4.472 29.34a.99.99 0 0 0 .208 1.104l64.878 64.877a.99.99 0 0 0 1.104.208 50.293 50.293 0 0 0 5.807-3.132.99.99 0 0 0 .154-1.53L9.135 23.38a.99.99 0 0 0-1.53.154 50.29 50.29 0 0 0-3.132 5.807ZM12.293 18.229c-.309-.309-.334-.803-.045-1.13C21.376 6.626 34.637 0 49.45 0 76.869 0 99.1 22.231 99.1 49.65c0 14.813-6.626 28.074-17.099 37.202-.327.29-.821.264-1.13-.045L12.293 18.23Z",
40 };
41
42 /**
43 * Standalone issue trackers, such as Linear, for each selected environment: connect one with a
44 * credential, then pick which of its scopes each project lists beside its repository's issues.
45 */
46 export function IssueTrackerSection(props: { readonly environments: readonly SettingsTarget[] }) {
47 const gt = useGT();
48 return (
49 <>
50 {props.environments.map((environment) =>
51 environment.serverConfig.environment.capabilities.issues === true ? (
52 <EnvironmentTrackers
53 key={environment.environmentId}
54 environment={environment}
55 title={
56 props.environments.length > 1
57 ? gt("Issue trackers · {environment}", { environment: environment.label })
58 : gt("Issue trackers")
59 }
60 />
61 ) : null,
62 )}
63 </>
64 );
65 }
66
67 function EnvironmentTrackers(props: {
68 readonly environment: SettingsTarget;
69 readonly title: string;
70 }) {
71 const gt = useGT();
72 const messages = useMessages();
73 const navigation = useNavigation();
74 const { environmentId } = props.environment;
75 const query = useEnvironmentQuery(
76 issueTrackerEnvironment.connections({ environmentId, input: {} }),
77 );
78 const trackers = query.data?.trackers ?? [];
79 if (trackers.length === 0) {
80 return query.error ? (
81 <SettingsSection title={props.title} footer={query.error}>
82 <View className="p-4">
83 <Button label={gt("Try again")} tone="secondary" onPress={query.refresh} />
84 </View>
85 </SettingsSection>
86 ) : null;
87 }
88 return (
89 <SettingsSection title={props.title}>
90 {trackers.map((tracker, index) => (
91 <SettingsProviderRow
92 key={tracker.tracker}
93 icon={<IssueTrackerMark tracker={tracker.tracker} />}
94 label={tracker.displayName}
95 description={issueTrackerSummary(tracker, query.data?.bindings ?? {}, messages)}
96 ready={tracker.connected && tracker.account !== null}
97 separated={index > 0}
98 onPress={() =>
99 navigation.navigate("SettingsIssueTracker", {
100 environmentId,
101 tracker: tracker.tracker,
102 })
103 }
104 />
105 ))}
106 </SettingsSection>
107 );
108 }
109
110 export function IssueTrackerMark(props: { readonly tracker: string }) {
111 const mark = TRACKER_MARKS[props.tracker];
112 return mark ? (
113 <ThemedSvg
114 width={ICON_SIZE}
115 height={ICON_SIZE}
116 viewBox="0 0 100 100"
117 colorClassName="accent-icon"
118 >
119 <Path fill="currentColor" d={mark} />
120 </ThemedSvg>
121 ) : (
122 <SymbolView name="circle.dashed" size={ICON_SIZE} tintColorClassName="accent-icon" />
123 );
124 }
125
126 /** The credential field for one tracker, with what replaces, removes or creates it. */
127 export function IssueTrackerCredentialForm(props: {
128 readonly environmentId: EnvironmentId;
129 readonly tracker: IssueTrackerConnection;
130 readonly disabled: boolean;
131 }) {
132 const gt = useGT();
133 const { environmentId, tracker } = props;
134 const connect = useAtomCommand(issueTrackerEnvironment.connect, { reportFailure: false });
135 const [draft, setDraft] = useState("");
136 const [busy, setBusy] = useState(false);
137 const [failure, setFailure] = useState<string | null>(null);
138 const submit = async (token: string | null) => {
139 setBusy(true);
140 setFailure(null);
141 const result = await connect({ environmentId, input: { tracker: tracker.tracker, token } });
142 setBusy(false);
143 if (!AsyncResult.isFailure(result)) {
144 setDraft("");
145 return;
146 }
147 const error = squashAtomCommandFailure(result);
148 setFailure(error instanceof Error ? error.message : gt("The request failed."));
149 };
150 const helpUrl = tracker.credential.helpUrl;
151 const locked = props.disabled || busy;
152 return (
153 <View className="gap-3 p-4">
154 <SecretTextInput
155 accessibilityLabel={tracker.credential.label}
156 placeholder={
157 tracker.connected
158 ? gt("Saved. Enter a new value to replace it")
159 : tracker.credential.label
160 }
161 value={draft}
162 onChangeText={setDraft}
163 onSubmitEditing={(value) => {
164 if (value.trim()) void submit(value.trim());
165 }}
166 editable={!locked}
167 />
168 {failure ? (
169 <Text selectable className="text-xs text-danger-foreground">
170 {failure}
171 </Text>
172 ) : null}
173 <View className="flex-row flex-wrap justify-end gap-2">
174 {helpUrl ? (
175 <Button
176 label={gt("Create one")}
177 tone="text"
178 size="compact"
179 onPress={() => void tryOpenExternalUrl(helpUrl)}
180 />
181 ) : null}
182 {tracker.connected ? (
183 <Button
184 label={gt("Disconnect")}
185 tone="secondary"
186 size="compact"
187 disabled={locked}
188 onPress={() => void submit(null)}
189 />
190 ) : null}
191 <Button
192 label={
193 tracker.connected
194 ? gt("Replace", { $context: "verb, replaces a saved credential" })
195 : gt("Connect", { $context: "verb, connects an issue tracker" })
196 }
197 tone="primary"
198 size="compact"
199 loading={busy}
200 disabled={locked || !draft.trim()}
201 onPress={() => void submit(draft.trim())}
202 />
203 </View>
204 </View>
205 );
206 }
207
208 /** One row per project of the environment: which of the tracker's scopes it reads, if any. */
209 export function IssueTrackerProjectRows(props: {
210 readonly environmentId: EnvironmentId;
211 readonly tracker: IssueTrackerConnection;
212 readonly bindings: IssueTrackerConnectionList["bindings"];
213 readonly disabled: boolean;
214 }) {
215 const gt = useGT();
216 const { environmentId, tracker } = props;
217 const bind = useAtomCommand(issueTrackerEnvironment.bindProject, {
218 label: `bind project to ${tracker.displayName}`,
219 });
220 const projects = useProjects().filter((project) => project.environmentId === environmentId);
221 const [binding, setBinding] = useState<ProjectId | null>(null);
222 const change = async (projectId: ProjectId, scope: string) => {
223 if (props.disabled || binding !== null) return;
224 setBinding(projectId);
225 await bind({
226 environmentId,
227 input: {
228 projectId,
229 binding: scope === UNBOUND ? null : { tracker: tracker.tracker, scope },
230 },
231 });
232 setBinding(null);
233 };
234 return projects.map((project) => {
235 const bound = props.bindings[project.id];
236 // A project bound to another tracker keeps that binding; this row shows it unbound.
237 const value = bound?.tracker === tracker.tracker ? bound.scope : UNBOUND;
238 const locked = props.disabled || binding !== null;
239 return (
240 <Menu
241 key={project.id}
242 title={gt("{tracker} {scope} for {project}", {
243 tracker: tracker.displayName,
244 scope: tracker.scopeNoun,
245 project: project.title,
246 })}
247 actions={[
248 {
249 id: UNBOUND,
250 title: gt("Not bound"),
251 state: value === UNBOUND ? "on" : "off",
252 attributes: { disabled: locked },
253 },
254 ...tracker.scopes.map((scope) => ({
255 id: scope.key,
256 title: `${scope.name} (${scope.key})`,
257 state: scope.key === value ? ("on" as const) : ("off" as const),
258 attributes: { disabled: locked },
259 })),
260 ]}
261 onPressAction={({ nativeEvent }) => void change(project.id, nativeEvent.event)}
262 >
263 <SettingsRow
264 icon="folder"
265 label={project.title}
266 valuePosition="trailing"
267 value={
268 value === UNBOUND
269 ? gt("Not bound")
270 : (tracker.scopes.find((scope) => scope.key === value)?.name ?? value)
271 }
272 disabled={locked}
273 />
274 </Menu>
275 );
276 });
277 }