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

SourceControlAppAccessNotice.tsx

59 lines2.4 KB

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