import { useEffect, useRef } from "react"; import { useMutation, useQuery } from "@tanstack/react-query"; import { tailscaleApi } from "@/api/tailscale"; import { describeError } from "@/api/errors"; import { useTailscaleConnections } from "@/hooks/useTailscaleConnections"; import { Link } from "@/lib/router"; import { Button } from "./ui/button"; import { Field } from "./agent-config-primitives"; import { cn } from "@/lib/utils"; import { ANALYTICS_MASK_CLASS } from "@/lib/analytics/privacy"; export function TailscaleSshMachine(props: { companyId: string; connectionId: string; deviceId: string; port: number; onPrivateKey: (secretId: string) => void; onChange: (selection: { connectionId: string; deviceId: string; host: string }) => void; }) { const mounted = useRef(false); useEffect(() => { mounted.current = true; return () => { mounted.current = false; }; }, []); const connections = useTailscaleConnections(props.companyId); const connected = (connections.data ?? []).filter(connection => connection.status === "connected"); const onlyConnectionId = connected.length === 1 ? connected[0]!.id : null; const showConnectionSelector = connected.length > 1 || Boolean(props.connectionId && onlyConnectionId && props.connectionId !== onlyConnectionId); useEffect(() => { if (!props.connectionId && onlyConnectionId) props.onChange({ connectionId: onlyConnectionId, deviceId: "", host: "" }); }, [props.connectionId, onlyConnectionId, props.onChange]); const devices = useQuery({ queryKey: ["tailscale-devices", props.companyId, props.connectionId], queryFn: () => tailscaleApi.devices(props.companyId, props.connectionId), enabled: Boolean(props.connectionId) && connections.canManage }); const pairing = useMutation({ mutationFn: (selection: { companyId: string; connectionId: string; deviceId: string; port: number }) => tailscaleApi.pairing(selection.companyId, selection.connectionId, selection.deviceId, selection.port), onSuccess: (result, selection) => { if (mounted.current && selection.companyId === props.companyId && selection.connectionId === props.connectionId && selection.deviceId === props.deviceId && selection.port === props.port) props.onPrivateKey(result.privateKeySecretId); } }); const error = pairing.error ?? connections.error ?? devices.error; if (!connections.canManage) return
An instance administrator must configure this Tailscale machine.
; returnManage Tailscale connector
{showConnectionSelector ?Run this on the selected computer as the user who will run tasks. Enter the printed username, workspace, and known hosts below. Paperclip keeps the private key in its vault.
> : null}{describeError(error, { action: "connect Tailscale" }).body}
: null}