|
| 1 | +import { peerLinkHealth } from "@t3tools/client-runtime/state/peerLinks"; |
| 2 | +import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | +import * as DateTime from "effect/DateTime"; |
| 4 | +import { View } from "react-native"; |
| 5 | + |
| 6 | +import { AppText as Text } from "../../components/AppText"; |
| 7 | +import { useEnvironmentServerConfig } from "../../state/entities"; |
| 8 | +import { peerLinkEnvironment } from "../../state/peerLinks"; |
| 9 | +import { useEnvironmentQuery } from "../../state/query"; |
| 10 | +import { SettingsSection } from "../settings/components/SettingsSection"; |
| 11 | + |
| 12 | +/** |
| 13 | + * The environments one connected environment links to. Listing them needs |
| 14 | + * the administrative access a phone usually lacks, so this shows only when |
| 15 | + * the environment answers; links are made from a desktop or `t3 environment |
| 16 | + * link`, and revoked from the linked environment's Connections. |
| 17 | + */ |
| 18 | +export function LinkedEnvironmentsSection(props: { |
| 19 | + readonly environmentId: EnvironmentId; |
| 20 | + readonly environmentLabel: string; |
| 21 | +}) { |
| 22 | + const config = useEnvironmentServerConfig(props.environmentId); |
| 23 | + const supported = config?.environment.capabilities.peerLinks === true; |
| 24 | + const links = useEnvironmentQuery( |
| 25 | + supported ? peerLinkEnvironment.list({ environmentId: props.environmentId, input: {} }) : null, |
| 26 | + ); |
| 27 | + if (!supported || links.error || !links.data || links.data.links.length === 0) return null; |
| 28 | + const now = DateTime.nowUnsafe(); |
| 29 | + return ( |
| 30 | + <View className="mt-5 gap-3"> |
| 31 | + <SettingsSection title={`Linked from ${props.environmentLabel}`}> |
| 32 | + {links.data.links.map((link) => { |
| 33 | + const health = peerLinkHealth(link, now); |
| 34 | + const status = |
| 35 | + health.kind === "expired" |
| 36 | + ? "Expired. Link it again with a new pairing code." |
| 37 | + : health.kind === "unreachable" |
| 38 | + ? (health.detail ?? "Not answering right now.") |
| 39 | + : health.expiresInDays !== null |
| 40 | + ? `Expires in ${health.expiresInDays} ${health.expiresInDays === 1 ? "day" : "days"}` |
| 41 | + : "Reachable"; |
| 42 | + return ( |
| 43 | + <View key={link.environmentId} className="gap-0.5 p-4"> |
| 44 | + <Text className="text-base font-t3-bold text-foreground">{link.label}</Text> |
| 45 | + <Text |
| 46 | + className={ |
| 47 | + health.kind === "reachable" && health.expiresInDays === null |
| 48 | + ? "text-xs text-foreground-muted" |
| 49 | + : "text-xs text-danger-foreground" |
| 50 | + } |
| 51 | + numberOfLines={2} |
| 52 | + > |
| 53 | + {status} |
| 54 | + </Text> |
| 55 | + </View> |
| 56 | + ); |
| 57 | + })} |
| 58 | + </SettingsSection> |
| 59 | + </View> |
| 60 | + ); |
| 61 | +} |
0 commit comments