|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { |
| 3 | + connectionRouteAddress, |
| 4 | + connectionRouteId, |
| 5 | + connectionRouteLabel, |
| 6 | + connectionRoutes, |
| 7 | +} from "@t3tools/client-runtime/connection"; |
| 8 | +import type { EnvironmentId } from "@t3tools/contracts"; |
| 9 | +import * as Option from "effect/Option"; |
| 10 | +import { Alert, Pressable, View } from "react-native"; |
| 11 | + |
| 12 | +import { SymbolView } from "../../components/AppSymbol"; |
| 13 | +import { AppText as Text } from "../../components/AppText"; |
| 14 | +import { environmentCatalog } from "../../connection/catalog"; |
| 15 | +import { environmentSession } from "../../state/session"; |
| 16 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 17 | +import { SettingsSection } from "./components/SettingsSection"; |
| 18 | + |
| 19 | +/** |
| 20 | + * The ways this device can reach an environment, preferred first. The first |
| 21 | + * route that answers is used, and the connection moves back up the list when |
| 22 | + * a better route becomes reachable again. Hidden for a single route. |
| 23 | + */ |
| 24 | +export function EnvironmentRoutesSection({ |
| 25 | + environmentId, |
| 26 | + connected, |
| 27 | +}: { |
| 28 | + readonly environmentId: EnvironmentId; |
| 29 | + readonly connected: boolean; |
| 30 | +}) { |
| 31 | + const entry = useAtomValue(environmentCatalog.catalogValueAtom).entries.get(environmentId); |
| 32 | + const prepared = useAtomValue(environmentSession.preparedConnectionValueAtom(environmentId)); |
| 33 | + const reorder = useAtomCommand(environmentCatalog.reorderRoutes, "route reorder"); |
| 34 | + const removeRoute = useAtomCommand(environmentCatalog.removeRoute, "route removal"); |
| 35 | + if (entry === undefined) return null; |
| 36 | + const routes = connectionRoutes(entry); |
| 37 | + if (routes.length < 2) return null; |
| 38 | + const activeRouteId = |
| 39 | + connected && Option.isSome(prepared) ? connectionRouteId(prepared.value.target) : null; |
| 40 | + const ids = routes.map((route) => connectionRouteId(route.target)); |
| 41 | + |
| 42 | + const openActions = (index: number) => { |
| 43 | + const route = routes[index]!; |
| 44 | + const label = connectionRouteLabel(route); |
| 45 | + Alert.alert(label, connectionRouteAddress(route) ?? undefined, [ |
| 46 | + ...(index > 0 |
| 47 | + ? [ |
| 48 | + { |
| 49 | + text: "Prefer this route", |
| 50 | + onPress: () => { |
| 51 | + const next = [...ids]; |
| 52 | + next.splice(index, 1); |
| 53 | + next.unshift(ids[index]!); |
| 54 | + void reorder({ environmentId, routeIds: next }); |
| 55 | + }, |
| 56 | + }, |
| 57 | + ] |
| 58 | + : []), |
| 59 | + { |
| 60 | + text: "Remove route", |
| 61 | + style: "destructive", |
| 62 | + onPress: () => void removeRoute({ environmentId, routeId: ids[index]! }), |
| 63 | + }, |
| 64 | + { text: "Cancel", style: "cancel" }, |
| 65 | + ]); |
| 66 | + }; |
| 67 | + |
| 68 | + return ( |
| 69 | + <SettingsSection title="Routes"> |
| 70 | + {routes.map((route, index) => { |
| 71 | + const id = ids[index]!; |
| 72 | + const address = connectionRouteAddress(route); |
| 73 | + return ( |
| 74 | + <Pressable |
| 75 | + key={id} |
| 76 | + accessibilityRole="button" |
| 77 | + accessibilityHint="Shows options for this route" |
| 78 | + onPress={() => openActions(index)} |
| 79 | + className="flex-row items-center gap-3 p-4 active:opacity-70" |
| 80 | + > |
| 81 | + <View className="w-5 items-center"> |
| 82 | + {id === activeRouteId ? ( |
| 83 | + <SymbolView |
| 84 | + name="checkmark" |
| 85 | + size={16} |
| 86 | + tintColorClassName="accent-icon" |
| 87 | + type="monochrome" |
| 88 | + /> |
| 89 | + ) : null} |
| 90 | + </View> |
| 91 | + <View className="min-w-0 flex-1 gap-0.5"> |
| 92 | + <Text className="text-base text-foreground">{connectionRouteLabel(route)}</Text> |
| 93 | + {address !== null ? ( |
| 94 | + <Text numberOfLines={1} className="text-sm text-foreground-muted"> |
| 95 | + {address} |
| 96 | + </Text> |
| 97 | + ) : null} |
| 98 | + </View> |
| 99 | + <Text className="text-sm tabular-nums text-foreground-muted">{index + 1}</Text> |
| 100 | + </Pressable> |
| 101 | + ); |
| 102 | + })} |
| 103 | + <Text className="px-4 pb-4 text-sm text-foreground-muted"> |
| 104 | + The first route that answers is used. Pair this machine again over another address, or add |
| 105 | + it from T3 Connect, to add a route. |
| 106 | + </Text> |
| 107 | + </SettingsSection> |
| 108 | + ); |
| 109 | +} |
0 commit comments