import { Button } from "../../components/ui/Button";
import * as Dialog from "../../components/ui/Dialog";
import { useAtomValue } from "@effect/atom-react";
import {
type ConnectionRoute,
type ConnectionRouteKind,
connectionRouteAddress,
connectionRouteId,
connectionRouteKind,
connectionRouteLabel,
connectionRoutes,
isLearned,
pendingRouteOrder,
} from "@t3tools/client-runtime/connection";
import { type EnvironmentId, sessionGrantsScope } from "@t3tools/contracts";
import {
AUTH_SCOPE_OPTIONS,
sessionPermissionsUnavailableMessage,
} from "@t3tools/shared/authScopeOptions";
import { AsyncResult } from "effect/reactivity";
import * as Option from "effect/Option";
import { useState } from "react";
import { Platform, Pressable, View } from "react-native";
import Reanimated, { ReduceMotion, useAnimatedStyle, withTiming } from "react-native-reanimated";
import { type AppSymbolName, SymbolView } from "../../components/ui/AppSymbol";
import { AppText as Text } from "../../components/ui/AppText";
import { StatusPill } from "../../components/StatusPill";
import { environmentCatalog } from "../../connection/catalog";
import { cn } from "../../lib/cn";
import { environmentSession } from "../../state/session";
import { useAtomCommand } from "../../state/use-atom-command";
import { connectionTone } from "../connection/connectionTone";
import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow";
import { SettingsDragHandle } from "./components/SettingsDragHandle";
import { SettingsSection } from "./components/SettingsSection";
const ICON_SIZE = Platform.OS === "android" ? 24 : 22;
const REMOVE_SIZE = 20;
const ROUTE_ICONS: Record<ConnectionRouteKind, AppSymbolName> = {
relay: "cloud",
loopback: "desktopcomputer",
lan: "wifi",
tailnet: "point.3.connected.trianglepath.dotted",
vpn: { ios: "lock.shield", android: "lock" },
public: "globe",
ssh: "terminal",
};
export function EnvironmentRoutesSection({
environmentId,
connected,
onAddRoute,
}: {
readonly environmentId: EnvironmentId;
readonly connected: boolean;
readonly onAddRoute: () => void;
}) {
const entry = useAtomValue(environmentCatalog.catalogValueAtom).entries.get(environmentId);
const prepared = useAtomValue(environmentSession.preparedConnectionValueAtom(environmentId));
const sessionResult = useAtomValue(environmentSession.sessionStateAtom(environmentId));
const session = AsyncResult.getOrElse(sessionResult, () => null);
const [permissionsOpen, setPermissionsOpen] = useState(false);
const reorder = useAtomCommand(environmentCatalog.reorderRoutes, "route reorder");
const removeRoute = useAtomCommand(environmentCatalog.removeRoute, "route removal");
const [editing, setEditing] = useState(false);
const saved = entry === undefined ? [] : connectionRoutes(entry);
const savedIds = saved.map((route) => connectionRouteId(route.target));
const [pending, setPending] = useState<ReadonlyArray<string> | null>(null);
const shownPending = pendingRouteOrder(pending, savedIds);
// Dropped during render so a stale order never outlives the save it was for.
if (pending !== null && shownPending === null) setPending(null);
const order = shownPending ?? savedIds;
const [drag, setDrag] = useState<{ readonly id: string; readonly translation: number } | null>(
null,
);
const [heights, setHeights] = useState<ReadonlyMap<string, number>>(() => new Map());
// Remount dropped rows so data order and cleared drag offsets change together without flashing.
const [drops, setDrops] = useState(0);
if (entry === undefined) return null;
const byId = new Map(saved.map((route) => [connectionRouteId(route.target), route]));
const routes = order.flatMap((id) => byId.get(id) ?? []);
const activeRouteId =
connected && Option.isSome(prepared) ? connectionRouteId(prepared.value.target) : null;
const commit = (next: ReadonlyArray<string>) => {
setPending(next);
void reorder({ environmentId, routeIds: next }).then((result) => {
if (result._tag === "Failure") setPending(null);
});
};
const move = (from: number, to: number) => {
if (to < 0 || to >= order.length || from === to) return;
const next = [...order];
const [moved] = next.splice(from, 1);
next.splice(to, 0, moved!);
commit(next);
};
const dropIndex = (from: number, translation: number) => {
const step = translation > 0 ? 1 : -1;
let to = from;
let remaining = Math.abs(translation);
while (to + step >= 0 && to + step < order.length) {
const next = heights.get(order[to + step]!) ?? 0;
if (next === 0 || remaining < next / 2) break;
remaining -= next;
to += step;
}
return to;
};
const confirmRemove = (route: ConnectionRoute) =>
Dialog.alert(
`Remove ${connectionRouteLabel(route)}?`,
connectionRouteAddress(route) ?? undefined,
[
{ text: "Cancel", style: "cancel" },
{
text: "Remove",
style: "destructive",
onPress: () =>
void removeRoute({ environmentId, routeId: connectionRouteId(route.target) }),
},
],
);
const dragFrom = drag === null ? -1 : order.indexOf(drag.id);
const dragTo = drag === null ? -1 : dropIndex(dragFrom, drag.translation);
return (
<SettingsSection
title="Routes"
trailing={
routes.length > 1 ? (
<Button
label={editing ? "Done" : "Edit"}
tone="text"
onPress={() => setEditing((value) => !value)}
/>
) : undefined
}
>
<SettingsActionRow
icon="checkmark.circle"
label="Your permissions"
onPress={() => setPermissionsOpen((open) => !open)}
/>
{permissionsOpen ? (
<View className="gap-2 px-4 py-3">
{activeRouteId !== null &&
sessionResult._tag !== "Failure" &&
!sessionResult.waiting &&
session?.authenticated ? (
<>
<Text className="text-sm text-foreground-muted">
Applies to the route marked In use. Other routes may have different permissions and
have not been checked.
</Text>
{AUTH_SCOPE_OPTIONS.map(({ scope, title }) => (
<Text key={scope} className="text-sm text-foreground">
{title}: {sessionGrantsScope(session, scope) ? "Allowed" : "Not granted"}
</Text>
))}
</>
) : (
<Text className="text-sm text-foreground-muted">
{sessionPermissionsUnavailableMessage({
connected: activeRouteId !== null,
failed: sessionResult._tag === "Failure",
rejected:
sessionResult._tag !== "Failure" &&
!sessionResult.waiting &&
session?.authenticated === false,
})}
</Text>
)}
</View>
) : null}
{routes.map((route, index) => {
const id = connectionRouteId(route.target);
const lifted = drag === null ? 0 : (heights.get(drag.id) ?? 0);
const shift =
dragFrom === -1 || index === dragFrom
? 0
: dragFrom < dragTo && index > dragFrom && index <= dragTo
? -lifted
: dragFrom > dragTo && index < dragFrom && index >= dragTo
? lifted
: 0;
return (
<RouteRow
key={`${id}:${drops}`}
route={route}
position={index + 1}
count={routes.length}
inUse={id === activeRouteId}
editing={editing}
offset={index === dragFrom ? (drag?.translation ?? 0) : shift}
lifted={index === dragFrom}
onHeight={(height) =>
setHeights((current) =>
current.get(id) === height ? current : new Map(current).set(id, height),
)
}
onDragStart={() => setDrag({ id, translation: 0 })}
onDragMove={(translation) => setDrag({ id, translation })}
onDragEnd={(translation, cancelled) => {
setDrag(null);
setDrops((count) => count + 1);
if (!cancelled) move(index, dropIndex(index, translation));
}}
onStep={(direction) => move(index, direction === "up" ? index - 1 : index + 1)}
// A learned route would be learned again, so it is only reordered.
onRemove={
routes.length > 1 && !isLearned(route) ? () => confirmRemove(route) : undefined
}
/>
);
})}
<SettingsActionRow icon="plus" label="Add route" onPress={onAddRoute} />
</SettingsSection>
);
}
function RouteRow(props: {
readonly route: ConnectionRoute;
readonly position: number;
readonly count: number;
readonly inUse: boolean;
readonly editing: boolean;
readonly offset: number;
readonly lifted: boolean;
readonly onHeight: (height: number) => void;
readonly onDragStart: () => void;
readonly onDragMove: (translation: number) => void;
readonly onDragEnd: (translation: number, cancelled: boolean) => void;
readonly onStep: (direction: "up" | "down") => void;
readonly onRemove: (() => void) | undefined;
}) {
const { route, lifted, offset } = props;
const label = connectionRouteLabel(route);
const address = connectionRouteAddress(route);
const style = useAnimatedStyle(() => ({
transform: [
{
translateY: lifted
? offset
: withTiming(offset, { duration: 160, reduceMotion: ReduceMotion.System }),
},
],
zIndex: lifted ? 1 : 0,
}));
return (
<Reanimated.View
style={style}
onLayout={(event) => props.onHeight(event.nativeEvent.layout.height)}
className={cn("flex-row items-center gap-4 pl-4", lifted && "bg-grouped-card shadow-md")}
>
{props.editing && props.onRemove ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={`Remove ${label} route`}
hitSlop={8}
onPress={props.onRemove}
className="active:opacity-70"
>
<SymbolView
name="xmark.circle.fill"
size={REMOVE_SIZE}
tintColorClassName="accent-danger-foreground"
type="monochrome"
/>
</Pressable>
) : props.editing ? (
<View style={{ width: REMOVE_SIZE }} />
) : null}
<View
accessible
accessibilityLabel={[
label,
address,
isLearned(route) ? "Found automatically" : null,
props.inUse ? "In use" : null,
`Route ${props.position} of ${props.count}`,
]
.filter((part) => part !== null)
.join(", ")}
className={cn(
"min-w-0 flex-1 flex-row items-center gap-4 py-4 android:py-3",
address !== null ? "android:min-h-18" : "android:min-h-14",
!props.editing && "pr-4",
)}
>
<SymbolView
name={ROUTE_ICONS[connectionRouteKind(route)]}
size={ICON_SIZE}
tintColorClassName="accent-icon"
type="monochrome"
weight="regular"
/>
<View className="min-w-0 flex-1 gap-0.5 android:gap-1">
<View className="flex-row items-center gap-2">
<Text numberOfLines={1} className="shrink text-lg text-foreground android:text-base">
{label}
</Text>
{props.inUse ? (
<StatusPill {...connectionTone("connected")} label="In use" size="compact" />
) : null}
</View>
{address !== null ? (
<Text numberOfLines={1} className="text-sm text-foreground-muted">
{isLearned(route) ? `${address} · found automatically` : address}
</Text>
) : null}
</View>
</View>
{props.editing ? (
<SettingsDragHandle
title={label}
canMoveUp={props.position > 1}
canMoveDown={props.position < props.count}
onStart={props.onDragStart}
onMove={props.onDragMove}
onEnd={props.onDragEnd}
onStep={props.onStep}
/>
) : null}
</Reanimated.View>
);
}