Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { StaticScreenProps } from "@react-navigation/native";
3import {
4 hasIssueTrackerTeams,
5 issueTrackerSummary,
6} from "@t3tools/client-runtime/state/issue-trackers";
7import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
8import {
9 AuthSettingsWriteScope,
10 type EnvironmentId,
11 type SourceControlProviderKind,
12} from "@t3tools/contracts";
13import * as Option from "effect/Option";
14import { T, useGT, useMessages } from "gt-react-native";
15import type { ReactNode } from "react";
16import { useSafeAreaInsets } from "react-native-safe-area-context";
17
18import { AppText as Text } from "../../components/ui/AppText";
19import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
20import { useEnvironments } from "../../state/environments";
21import { issueTrackerEnvironment } from "../../state/issueTrackers";
22import { useEnvironmentQuery } from "../../state/query";
23import { serverEnvironment } from "../../state/server";
24import { useEnvironmentsWithScope } from "../../state/session";
25import { sourceControlEnvironment } from "../../state/sourceControl";
26import {
27 IssueTrackerCredentialForm,
28 IssueTrackerProjectRows,
29} from "./components/IssueTrackerSection";
30import { SettingsScreen } from "./components/SettingsScreen";
31import { SettingsSection } from "./components/SettingsSection";
32import {
33 SourceControlHostFieldsForm,
34 SourceControlTokenForm,
35 sourceControlProviderSummary,
36} from "./components/SourceControlProvidersSection";
37import { AppSignInRow } from "./components/SourceControlSignInSection";
38import type { SettingsTarget } from "./settings-environment-filter";
39
40/** The environment a detail page edits, once it is connected, and whether this session may. */
41function useDetailEnvironment(environmentId: EnvironmentId) {
42 const { environments } = useEnvironments();
43 const environment = environments.find(
44 (entry): entry is SettingsTarget =>
45 entry.environmentId === environmentId &&
46 entry.connection.phase === "connected" &&
47 entry.serverConfig !== null,
48 );
49 const writable = useEnvironmentsWithScope(
50 environment ? [environment] : [],
51 AuthSettingsWriteScope,
52 );
53 return { environment, canWrite: writable.has(environmentId) };
54}
55
56function DetailPage(props: { readonly title: string; readonly children: ReactNode }) {
57 const insets = useSafeAreaInsets();
58 return (
59 <SettingsScreen title={props.title}>
60 <ScrollView
61 contentInsetAdjustmentBehavior="automatic"
62 showsVerticalScrollIndicator={false}
63 keyboardShouldPersistTaps="handled"
64 className="flex-1"
65 contentContainerClassName="gap-6 px-5 pt-4"
66 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
67 >
68 {props.children}
69 </ScrollView>
70 </SettingsScreen>
71 );
72}
73
74const Unavailable = () => (
75 <Text className="px-2 text-base text-foreground-muted">
76 <T>Connect this environment to change its settings.</T>
77 </Text>
78);
79
80export function SettingsSourceControlProviderRouteScreen({
81 route,
82}: StaticScreenProps<{
83 readonly environmentId: EnvironmentId;
84 readonly kind: SourceControlProviderKind;
85}>) {
86 const gt = useGT();
87 const messages = useMessages();
88 const { environmentId, kind } = route.params;
89 const { environment, canWrite } = useDetailEnvironment(environmentId);
90 const definition = sourceControlClients.find(kind);
91 const discovery = useEnvironmentQuery(
92 sourceControlEnvironment.discovery({ environmentId, input: {} }),
93 );
94 const canUpdate = useAtomValue(serverEnvironment.updateSettings.permissionAtom(environmentId));
95 const item = discovery.data?.sourceControlProviders.find((entry) => entry.kind === kind);
96 const credentials = definition?.credentials;
97 const appSignIn = item?.appSignIn === true ? credentials?.appSignIn : undefined;
98 const disabled = !canWrite || !canUpdate;
99 return (
100 <DetailPage
101 title={
102 definition?.label ??
103 gt("Provider", { $context: "settings page of one source control provider" })
104 }
105 >
106 {!environment || !definition ? (
107 <Unavailable />
108 ) : (
109 <>
110 {item ? (
111 <Text selectable className="px-2 text-sm text-foreground-muted">
112 {[Option.getOrNull(item.version), sourceControlProviderSummary(item, messages)]
113 .filter(Boolean)
114 .join(" · ")}
115 </Text>
116 ) : null}
117 {appSignIn ? (
118 <SettingsSection
119 title={gt("Sign in", { $context: "settings section title" })}
120 footer={gt("A far higher API rate limit than a personal token or CLI login.")}
121 >
122 <AppSignInRow
123 environment={environment}
124 label={appSignIn.appName}
125 definition={definition}
126 appSignIn={appSignIn}
127 disabled={disabled}
128 />
129 </SettingsSection>
130 ) : null}
131 {!credentials && definition.settings ? (
132 <SettingsSection title={gt("Credentials")}>
133 <SourceControlHostFieldsForm
134 environment={environment}
135 definition={definition}
136 settings={definition.settings}
137 disabled={disabled}
138 onSaved={discovery.refresh}
139 />
140 </SettingsSection>
141 ) : null}
142 {credentials ? (
143 <SettingsSection
144 title={
145 appSignIn
146 ? gt("Or use your own credentials")
147 : gt("Token", { $context: "access token field" })
148 }
149 >
150 <SourceControlTokenForm
151 environment={environment}
152 definition={definition}
153 credentials={credentials}
154 disabled={disabled}
155 onSaved={discovery.refresh}
156 />
157 </SettingsSection>
158 ) : null}
159 </>
160 )}
161 </DetailPage>
162 );
163}
164
165export function SettingsIssueTrackerRouteScreen({
166 route,
167}: StaticScreenProps<{
168 readonly environmentId: EnvironmentId;
169 readonly tracker: string;
170}>) {
171 const gt = useGT();
172 const messages = useMessages();
173 const { environmentId } = route.params;
174 const { environment, canWrite } = useDetailEnvironment(environmentId);
175 const query = useEnvironmentQuery(
176 issueTrackerEnvironment.connections({ environmentId, input: {} }),
177 );
178 const canEdit = useAtomValue(issueTrackerEnvironment.connect.permissionAtom(environmentId));
179 const tracker = query.data?.trackers.find((entry) => entry.tracker === route.params.tracker);
180 const disabled = !canWrite || !canEdit;
181 const usable = tracker?.connected === true && tracker.account !== null;
182 return (
183 <DetailPage title={tracker?.displayName ?? gt("Issue tracker")}>
184 {!environment || !tracker ? (
185 <Unavailable />
186 ) : (
187 <>
188 <Text selectable className="px-2 text-sm text-foreground-muted">
189 {issueTrackerSummary(tracker, query.data?.bindings ?? {}, messages)}
190 </Text>
191 <SettingsSection title={tracker.credential.label}>
192 <IssueTrackerCredentialForm
193 environmentId={environmentId}
194 tracker={tracker}
195 disabled={disabled}
196 />
197 </SettingsSection>
198 {usable && hasIssueTrackerTeams(tracker, query.data?.bindings ?? {}) ? (
199 <SettingsSection
200 title={gt("Projects")}
201 footer={gt(
202 "Pick one or more {tracker} {scope}s for each project. Their issues list beside the repository's own, and agents file new issues in the first one unless asked for another.",
203 { tracker: tracker.displayName, scope: tracker.scopeNoun },
204 )}
205 >
206 <IssueTrackerProjectRows
207 environmentId={environmentId}
208 tracker={tracker}
209 bindings={query.data?.bindings ?? {}}
210 refreshing={query.isPending}
211 refreshFailed={query.error !== null}
212 disabled={disabled}
213 />
214 </SettingsSection>
215 ) : null}
216 </>
217 )}
218 </DetailPage>
219 );
220}