Open History to see the last commit
1import { useGT, plural, part } from "gt-react-native";
2import { RegistryContext } from "@effect/atom-react";
3import { offersLinkedEnvironments } from "@t3tools/client-runtime/link-request";
4import { peerLinkHealth } from "@t3tools/client-runtime/state/peerLinks";
5import { useFocusEffect } from "@react-navigation/native";
6import { AuthAccessReadScope, type EnvironmentId } from "@t3tools/contracts";
7import * as DateTime from "effect/DateTime";
8import { useCallback, useContext, useState } from "react";
9import { AppState, View } from "react-native";
10
11import { AppText as Text } from "../../components/ui/AppText";
12import { useEnvironmentServerConfig } from "../../state/entities";
13import { peerLinkEnvironment } from "../../state/peerLinks";
14import { useEnvironmentQuery } from "../../state/query";
15import { useEnvironmentScope } from "../../state/session";
16import { SettingsSection } from "../settings/components/SettingsSection";
17
18export function LinkedEnvironmentsSection(props: {
19 readonly environmentId: EnvironmentId;
20 readonly environmentLabel: string;
21}) {
22 const gt = useGT();
23 const config = useEnvironmentServerConfig(props.environmentId);
24 const canRead = useEnvironmentScope(props.environmentId, AuthAccessReadScope);
25 const supported = canRead && offersLinkedEnvironments(config);
26 const listAtom = supported
27 ? peerLinkEnvironment.list({ environmentId: props.environmentId, input: {} })
28 : null;
29 const links = useEnvironmentQuery(listAtom);
30 const registry = useContext(RegistryContext);
31 // Links change outside this client, and the shared list only revalidates when built, so
32 // showing the screen and bringing the app back ask again.
33 useFocusEffect(
34 useCallback(() => {
35 if (listAtom === null) return;
36 const askAgain = () => {
37 if (!registry.get(listAtom).waiting) registry.refresh(listAtom);
38 };
39 askAgain();
40 const subscription = AppState.addEventListener("change", (state) => {
41 if (state === "active") askAgain();
42 });
43 return () => subscription.remove();
44 }, [listAtom, registry]),
45 );
46 const [nowMs, setNowMs] = useState(Date.now);
47 const hasLinks = supported && (links.data?.links.length ?? 0) > 0;
48 useFocusEffect(
49 useCallback(() => {
50 if (!hasLinks) return;
51 const updateNow = () => setNowMs(Date.now());
52 updateNow();
53 const timer = setInterval(updateNow, 60_000);
54 const subscription = AppState.addEventListener("change", (state) => {
55 if (state === "active") updateNow();
56 });
57 return () => {
58 clearInterval(timer);
59 subscription.remove();
60 };
61 }, [hasLinks]),
62 );
63 if (!supported || links.error || !links.data || links.data.links.length === 0) return null;
64 const now = DateTime.makeUnsafe(nowMs);
65 return (
66 <View className="mt-5 gap-3">
67 <SettingsSection title={gt`Linked from ${{ environment: props.environmentLabel }}`}>
68 {links.data.links.map((link) => {
69 const health = peerLinkHealth(link, now);
70 const status =
71 health.kind === "expired"
72 ? gt`Expired. Link it again with a new pairing code.`
73 : health.kind === "unreachable"
74 ? (health.detail ?? gt`Not answering right now.`)
75 : health.kind === "failing"
76 ? health.detail
77 : health.expiresInDays !== null
78 ? gt`${plural(
79 { count: health.expiresInDays },
80 {
81 one: part`Expires in ${{ count: health.expiresInDays }} day`,
82 other: part`Expires in ${{ count: health.expiresInDays }} days`,
83 },
84 )}`
85 : gt`Reachable`;
86 return (
87 <View key={link.environmentId} className="gap-0.5 p-4">
88 <Text className="text-base font-t3-bold text-foreground">{link.label}</Text>
89 <Text
90 className={
91 health.kind === "reachable" && health.expiresInDays === null
92 ? "text-xs text-foreground-muted"
93 : "text-xs text-danger-foreground"
94 }
95 numberOfLines={2}
96 >
97 {status}
98 </Text>
99 </View>
100 );
101 })}
102 </SettingsSection>
103 </View>
104 );
105}