import { useAtomValue } from "@effect/atom-react";
import {
AVAILABLE_CONNECTION_STATE,
type ConnectionDiagnosis,
connectionDiagnosisText,
connectionRoutes,
environmentTailscaleNotice,
everyTailnetRouteFailed,
hasRelayRoute,
presentConnectionDiagnosis,
} from "@t3tools/client-runtime/connection";
import * as Option from "effect/Option";
import { AsyncResult } from "effect/reactivity";
import { ChevronRightIcon } from "lucide-react";
import { APP_VERSION } from "~/branding";
import { environmentCatalog } from "~/connection/catalog";
import { desktopTailscaleStatusAtom, noDesktopTailscaleStatusAtom } from "~/state/desktopTailscale";
import { type EnvironmentPresentation, useRelayEnvironmentDiscovery } from "~/state/environments";
import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
import { useDeadlinePassed } from "../../hooks/useDeadline";
import { useNowMinute } from "../../hooks/useNowMinute";
import { useNowSecond } from "../../hooks/useNowSecond";
import { cn } from "../../lib/utils";
import { Button } from "../ui/button";
import { stackedThreadToast, toastManager } from "../ui/toast";
/**
* What failed on the way to an environment, or null while it is connected, off,
* or has not failed. Reads the supervisor state itself so callers that only need
* the headline do not re-render on every attempt.
*/
export function useConnectionDiagnosis(environment: EnvironmentPresentation) {
const environmentId = environment.environmentId;
const state = AsyncResult.getOrElse(
useAtomValue(environmentCatalog.stateAtom(environmentId)),
() => AVAILABLE_CONNECTION_STATE,
);
const relayStatus = useRelayEnvironmentDiscovery().environments.get(environmentId)?.status;
const routes = connectionRoutes(environment.entry);
const relayOnly = routes.length === 1 && hasRelayRoute(environment.entry);
const phase = environment.connection.phase;
const failing = environment.entry.enabled && phase !== "connected" && phase !== "unsupported";
// Spawning Tailscale can prompt on macOS, so read it only when every Tailscale route failed.
const localTailscale = AsyncResult.getOrElse(
useAtomValue(
failing && everyTailnetRouteFailed(state)
? desktopTailscaleStatusAtom(state.attempt)
: noDesktopTailscaleStatusAtom,
),
() => null,
);
const diagnosis = failing
? presentConnectionDiagnosis({
hostLabel: environment.label,
state,
via: relayOnly ? "relay" : "direct",
relayStatus: hasRelayRoute(environment.entry)
? Option.getOrNull(relayStatus ?? Option.none())
: null,
clientVersion: APP_VERSION,
serverVersion: environment.serverConfig?.environment.serverVersion,
localTailscale,
hostTailscale: environment.serverConfig?.tailscale,
})
: null;
return { diagnosis, state, relayOnly };
}
/** The host's last reported Tailscale problem, for its row while no connection has failed. */
export function HostTailscaleNotice({
environment,
}: {
readonly environment: EnvironmentPresentation;
}) {
return environment.serverConfig?.tailscale?.issues.length ? (
{notice.headline}
{notice.fix}
{diagnosis.headline}
{retryAt !== null ?
What to try