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