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

LinkedEnvironmentsSection.tsx

97 lines3.9 KB

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