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

SourceControlAppAccessNotice.tsx

66 lines2.6 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import {
3 readHostCredentialSettings,
4 sourceControlClients,
5} from "@t3tools/client-runtime/source-control-clients";
6import { T, Var } from "gt-react-native";
7import { Linking, Pressable, View } from "react-native";
8​
9import { AppText as Text } from "../../components/ui/AppText";
10import { useEnvironmentPresentation } from "../../state/presentation";
11import { useEnvironmentQuery } from "../../state/query";
12import { sourceControlEnvironment } from "../../state/sourceControl";
13​
14/**
15 * Says when the server's forge app sign-in cannot reach `repository`, so requests there fall back
16 * to the user's own login. Renders nothing for servers that do not use the app.
17 */
18export function SourceControlAppAccessNotice(props: {
19 readonly environmentId: string;
20 readonly provider: string | undefined;
21 readonly host: string | undefined;
22 /** `owner/name`. */
23 readonly repository: string;
24}) {
25 const environmentId = EnvironmentId.make(props.environmentId);
26 const { presentation } = useEnvironmentPresentation(environmentId);
27 const definition = props.provider ? sourceControlClients.find(props.provider) : undefined;
28 const appSignIn = definition?.credentials?.appSignIn;
29 const host = (props.host ?? definition?.publicHost ?? "").toLowerCase();
30 const settings = presentation?.serverConfig?.settings;
31 const signedIn =
32 definition !== undefined &&
33 settings !== undefined &&
34 (readHostCredentialSettings(settings, definition.kind).signIns[host] ?? "").length > 0;
35 const access = useEnvironmentQuery(
36 definition !== undefined && appSignIn?.hosts.includes(host) && signedIn
37 ? sourceControlEnvironment.appAccess({
38 environmentId,
39 input: { provider: definition.kind, host, repository: props.repository },
40 })
41 : null,
42 );
43 if (appSignIn === undefined || access.data?.status !== "not-covered") return null;
44 const owner = props.repository.split("/")[0] ?? props.repository;
45​
46 return (
47 <View className="mb-4 gap-1 rounded-2xl bg-grouped-card px-4 py-3">
48 <Text className="text-sm text-foreground-muted">
49 <T>
50 <Var>{appSignIn.appName}</Var> isn't installed on <Var>{owner}</Var>, so requests here use
51 your own GitHub login and its lower rate limit.
52 </T>
53 </Text>
54 <Pressable
55 accessibilityRole="link"
56 onPress={() => void Linking.openURL(appSignIn.installUrl).catch(() => undefined)}
57 >
58 <Text className="text-sm font-t3-bold text-foreground">
59 <T>
60 Install on <Var>{owner}</Var>
61 </T>
62 </Text>
63 </Pressable>
64 </View>
65 );