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