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