diff --git a/desktop-ui/src/components/app/remote/remote-access-dialog.tsx b/desktop-ui/src/components/app/remote/remote-access-dialog.tsx new file mode 100644 index 00000000..ffea7308 --- /dev/null +++ b/desktop-ui/src/components/app/remote/remote-access-dialog.tsx @@ -0,0 +1,91 @@ +"use client"; + +import { useRef, useState } from "react"; + +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { accessRequestTitle } from "@/lib/util.remote-controls"; +import { useRemote } from "@/providers/remote-provider"; + +/** + * Another machine asks this Mac for remote controls (docs/remote-machines.md, "Asking for + * access", step 4). The switches are machine-wide, so the question says so. Only `Allow` or + * `Don't allow` answers it: Escape and a click outside leave it to expire. + */ +export function RemoteAccessDialog() { + const { access } = useRemote(); + if (!access) return null; + // A fresh dialog, and a cleared checkbox, for each request. + return ; +} + +function AccessRequestDialog() { + const { access, answerAccess } = useRemote(); + const [suppress, setSuppress] = useState(false); + // Focus lands on the dialog, not a button, so typing in a terminal can't answer it. + const popup = useRef(null); + if (!access) return null; + + return ( + undefined}> + + + {accessRequestTitle(access.name, access.fingerprint)} + }> +

+ Allow every paired machine to start, stop and restart services on this Mac, until you + turn it off in Settings? +

+ {access.wants === "backgroundStarts" ? ( +

…also when Localify is closed here, in the background.

+ ) : null} +
+ + {access.error ? ( +

+ Could not answer the request: {access.error} +

+ ) : null} +
+ + + + +
+
+ ); +} diff --git a/desktop-ui/src/components/app/remote/remote-command.tsx b/desktop-ui/src/components/app/remote/remote-command.tsx new file mode 100644 index 00000000..1a8df758 --- /dev/null +++ b/desktop-ui/src/components/app/remote/remote-command.tsx @@ -0,0 +1,25 @@ +"use client"; + +import { CheckIcon, CopyIcon } from "@/components/icons"; +import { Button } from "@/components/ui/button"; +import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard"; + +/** A command to run on the target, with a copy button. */ +export function RemoteCommand({ command }: { command: string }) { + const { copy, copied } = useCopyToClipboard(); + return ( +
+ + {command} + + +
+ ); +} diff --git a/desktop-ui/src/components/app/remote/remote-controls.tsx b/desktop-ui/src/components/app/remote/remote-controls.tsx new file mode 100644 index 00000000..69903356 --- /dev/null +++ b/desktop-ui/src/components/app/remote/remote-controls.tsx @@ -0,0 +1,467 @@ +"use client"; + +import { + createContext, + type PropsWithChildren, + useCallback, + useContext, + useEffect, + useMemo, + useRef, + useState, +} from "react"; +import { toast } from "sonner"; + +import type { + RemoteAccessEventDto, + RemoteAccessWants, + RemoteControlAction, + RemoteControlEventDto, + RemoteInventoryItemDto, +} from "@/types/localify"; + +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { openRemoteStream } from "@/lib/remote-stream"; +import { remoteErrorMessage, serviceKey } from "@/lib/util.remote"; +import { + accessCommand, + type AccessView, + accessEventView, + type ControlPhase, + controlLostText, + controlProgressText, + controlRequest, + controlResultText, + quickAction, + type RelaunchConfirmation, + relaunchConfirmation, + relaunchStillConfirmed, +} from "@/lib/util.remote-controls"; +import { createStreamRegistry } from "@/lib/util.stream-registry"; + +import { RemoteCommand } from "./remote-command"; + +/** After access is granted, how long a relaunch waits for the snapshot that allows it. */ +const GRANTED_SNAPSHOT_MS = 15_000; + +/** A control on its way to the target, one per service at a time, as the chip says it. */ +interface ControlInFlight { + text: string; +} + +/** The access request this Mac has open with the machine on screen, if any. */ +interface AccessRequestState { + wants: RemoteAccessWants; + view: AccessView; +} + +interface RemoteControlsValue { + machineName: string; + /** The session's protocol; null while none is open. */ + proto: number | null; + /** Why controls are off: the session is down, and nothing is queued. */ + down: string | null; + /** Whether the target's app runs: a start without it runs in the background there. */ + appRunning: boolean; + inFlight: (item: RemoteInventoryItemDto) => ControlInFlight | null; + run: (item: RemoteInventoryItemDto, action: RemoteControlAction) => void; + access: AccessRequestState | null; + ask: (wants: RemoteAccessWants) => void; + cancelAsk: () => void; + /** Forgets how the last request ended, once it was shown. */ + dismissAccess: () => void; + /** Opens the relaunch confirmation for a legacy run. */ + relaunch: (item: RemoteInventoryItemDto) => void; +} + +const RemoteControlsContext = createContext(null); + +interface RemoteControlsProviderProps { + machineId: string; + machineName: string; + proto: number | null; + down: string | null; + appRunning: boolean; + items: RemoteInventoryItemDto[]; +} + +/** A relaunch the person confirmed, waiting on access first. */ +interface PendingRelaunch { + key: string; + confirmed: RelaunchConfirmation; + wants: RemoteAccessWants; + /** Access was granted: go ahead once the snapshot allows the restart. */ + granted: boolean; +} + +/** + * Start, Stop and Restart of the machine on screen, its access request and the relaunch of + * legacy runs (docs/remote-machines.md, "Quick actions", "State machine", the viewer side). + */ +export function RemoteControlsProvider({ + machineId, + machineName, + proto, + down, + appRunning, + items, + children, +}: PropsWithChildren) { + const [controls, setControls] = useState>({}); + // Set at the click, before React renders: a second click finds it. + const busy = useRef(new Set()); + // The control streams still open, closed when the machine leaves the screen. + const controlStreams = useRef(createStreamRegistry()); + const [access, setAccess] = useState(null); + const closeAccess = useRef<(() => void) | null>(null); + // Counts asks: events of an ask replaced or cancelled since are dropped. + const askSeq = useRef(0); + const [confirming, setConfirming] = useState(null); + const [pending, setPending] = useState(null); + const live = useRef({ machineName, appRunning }); + live.current = { machineName, appRunning }; + + // A machine left on screen keeps nothing open with it, toasts included: the streams' + // events are dropped from here on, so nothing else would close them. + useEffect( + () => () => { + for (const key of controlStreams.current.closeAll()) { + toast.dismiss(controlToastId(machineId, key)); + } + askSeq.current += 1; + closeAccess.current?.(); + closeAccess.current = null; + toast.dismiss(`remote-access:${machineId}`); + }, + [machineId], + ); + + const run = useCallback( + ( + item: RemoteInventoryItemDto, + action: RemoteControlAction, + relaunch: RelaunchConfirmation | null = null, + ) => { + const key = serviceKey(item); + if (busy.current.has(key)) return; + busy.current.add(key); + const service = item.service.name; + const toastId = controlToastId(machineId, key); + const stream = controlStreams.current.add(key); + const progress = (phase: ControlPhase) => { + const { machineName: name, appRunning: app } = live.current; + const text = controlProgressText(action, phase, name, app); + setControls((current) => ({ ...current, [key]: { text } })); + toast.loading(text, { id: toastId }); + }; + const settle = (ok: boolean, text: string) => { + stream.ended(); + busy.current.delete(key); + setControls(({ [key]: _, ...rest }) => rest); + if (ok) toast.success(text, { id: toastId }); + else toast.error(text, { id: toastId }); + }; + progress("sent"); + const onEvent = (event: RemoteControlEventDto) => { + if (!stream.live()) return; + const name = live.current.machineName; + switch (event.event) { + case "sent": + return; + case "progress": + progress(event.phase); + return; + case "unknown": + progress("unknown"); + return; + case "result": { + const view = controlResultText(event, action, service, name); + settle(view.ok, view.text); + return; + } + case "lost": + settle(false, controlLostText(event.code, name)); + return; + } + }; + void openRemoteStream( + { kind: "control", machineId, ...controlRequest(item, action, relaunch) }, + { + onData: onEvent, + onEnd: (error) => { + if (stream.live()) { + settle(false, error ?? controlLostText("closed", live.current.machineName)); + } + }, + }, + ) + .then(stream.opened) + .catch((error: unknown) => { + if (stream.live()) settle(false, error instanceof Error ? error.message : String(error)); + }); + }, + [machineId], + ); + + /** + * Asks the target for access. A card shows the request as it goes; a relaunch confirmed in + * its dialog has no card, so a toast shows it, with Cancel. When it ends without access, the + * toast stays, with the command to run on the target instead. + */ + const ask = useCallback( + (wants: RemoteAccessWants, inToast = false) => { + closeAccess.current?.(); + closeAccess.current = null; + const seq = ++askSeq.current; + const isLatest = () => seq === askSeq.current; + const toastId = `remote-access:${machineId}`; + // Updating a toast keeps the options it had: each step sets them all. + const options = { + id: toastId, + action: undefined, + description: undefined, + duration: undefined, + }; + const show = (view: AccessView) => { + setAccess({ wants, view }); + if (view.step === "waiting") { + if (inToast) { + toast.loading(view.text, { + ...options, + action: { label: "Cancel", onClick: () => cancelRef.current() }, + }); + } + } else if (view.ok) toast.success(view.text, options); + else if (inToast) { + toast.error(view.text, { + ...options, + duration: Number.POSITIVE_INFINITY, + description: ( +
+ Or run this on {live.current.machineName}: + +
+ ), + }); + } + }; + show(accessEventView({ event: "waiting" }, wants, live.current.machineName)); + let ended = false; + void openRemoteStream( + { kind: "access", machineId, wants }, + { + onData: (event) => { + if (!isLatest()) return; + const view = accessEventView(event, wants, live.current.machineName); + if (view.step === "ended") { + ended = true; + closeAccess.current = null; + setPending((current) => + current && event.event === "result" && event.outcome === "granted" + ? { ...current, granted: true } + : null, + ); + } + show(view); + }, + onEnd: (error) => { + if (!isLatest()) return; + closeAccess.current = null; + if (ended) return; + setPending(null); + show( + error + ? { step: "ended", ok: false, text: error } + : accessEventView( + { event: "lost", code: "closed", message: "" }, + wants, + live.current.machineName, + ), + ); + }, + }, + ) + .then((close) => { + if (ended || !isLatest()) close(); + else closeAccess.current = close; + }) + .catch((error: unknown) => { + if (!isLatest()) return; + setPending(null); + show({ + step: "ended", + ok: false, + text: error instanceof Error ? error.message : String(error), + }); + }); + }, + [machineId], + ); + + // Closing the stream sends `access.cancel`. + const cancelAsk = useCallback(() => { + askSeq.current += 1; + closeAccess.current?.(); + closeAccess.current = null; + setAccess(null); + setPending(null); + toast.dismiss(`remote-access:${machineId}`); + }, [machineId]); + const cancelRef = useRef(cancelAsk); + cancelRef.current = cancelAsk; + + const dismissAccess = useCallback( + () => setAccess((current) => (current?.view.step === "ended" ? null : current)), + [], + ); + + const confirmRelaunch = () => { + const item = confirming; + setConfirming(null); + const confirmed = item ? relaunchConfirmation(item) : null; + if (!item || !confirmed) return; + if (down) { + toast.error(down); + return; + } + const next = quickAction(item, "restart", proto, machineName, appRunning); + if (next.kind !== "relaunch") { + toast.error(next.kind === "message" ? next.text : `${machineName} changed in the meantime.`); + return; + } + if (next.access) { + setPending({ key: serviceKey(item), confirmed, wants: next.access, granted: false }); + ask(next.access, true); + return; + } + run(item, "restart", confirmed); + }; + + // Once access is granted, the next snapshot carries the controls it allows. + useEffect(() => { + // Nothing is sent while the session is down; the timeout below gives up. + if (!pending?.granted || down) return; + const item = items.find((candidate) => serviceKey(candidate) === pending.key); + if (!item) { + setPending(null); + toast.error(`This service no longer exists on ${machineName}.`); + return; + } + const next = quickAction(item, "restart", proto, machineName, appRunning); + if (next.kind === "relaunch" && next.access === null) { + setPending(null); + if (relaunchStillConfirmed(pending.confirmed, item)) run(item, "restart", pending.confirmed); + // It changed since the person confirmed: ask again. + else if (relaunchConfirmation(item)) setConfirming(item); + else toast.error(`${machineName} changed in the meantime.`); + return; + } + // A snapshot from before the grant: wait for the next one. + if (next.kind === "relaunch" && next.access === pending.wants) return; + setPending(null); + toast.error( + next.kind === "message" + ? next.text + : remoteErrorMessage( + item.controls.restart.code ?? "", + machineName, + `${machineName} changed in the meantime.`, + ), + ); + }, [items, pending, proto, machineName, appRunning, run, down]); + + useEffect(() => { + if (!pending?.granted) return; + const timer = window.setTimeout(() => { + setPending(null); + toast.error(`${machineName} allowed it, but its new state didn't arrive. Try again.`); + }, GRANTED_SNAPSHOT_MS); + return () => window.clearTimeout(timer); + }, [pending?.granted, machineName]); + + const value = useMemo( + () => ({ + machineName, + proto, + down, + appRunning, + inFlight: (item) => controls[serviceKey(item)] ?? null, + run: (item, action) => run(item, action), + access, + // Asking from a card is not a relaunch's. + ask: (wants) => { + setPending(null); + ask(wants); + }, + cancelAsk, + dismissAccess, + relaunch: setConfirming, + }), + [machineName, proto, down, appRunning, controls, run, access, ask, cancelAsk, dismissAccess], + ); + + return ( + + {children} + setConfirming(null)} + onConfirm={confirmRelaunch} + /> + + ); +} + +function controlToastId(machineId: string, key: string): string { + return `remote-control:${machineId}:${key}`; +} + +export function useRemoteControls(): RemoteControlsValue { + const context = useContext(RemoteControlsContext); + if (!context) throw new Error("useRemoteControls must be used inside RemoteControlsProvider"); + return context; +} + +/** The contract's confirmation before a legacy run is relaunched. */ +function RelaunchDialog({ + item, + machineName, + onCancel, + onConfirm, +}: { + item: RemoteInventoryItemDto | null; + machineName: string; + onCancel: () => void; + onConfirm: () => void; +}) { + return ( + (open ? undefined : onCancel())}> + + + Relaunch on {machineName} + + {item?.service.name} started before this version of Localify on {machineName}. Relaunch + it so its output and controls work here? It restarts with its configured command. + + + + + + + + + ); +} diff --git a/desktop-ui/src/components/app/remote/remote-projects-view.tsx b/desktop-ui/src/components/app/remote/remote-projects-view.tsx index 6018dd15..35a4103f 100644 --- a/desktop-ui/src/components/app/remote/remote-projects-view.tsx +++ b/desktop-ui/src/components/app/remote/remote-projects-view.tsx @@ -16,10 +16,12 @@ import { serviceKey, serviceTone, } from "@/lib/util.remote"; +import { sessionDownReason } from "@/lib/util.remote-controls"; import { cn } from "@/lib/utils"; import { useRemote } from "@/providers/remote-provider"; import { useTerminal } from "@/providers/terminal-provider"; +import { RemoteControlsProvider } from "./remote-controls"; import { RemoteOutputPanel } from "./remote-output-panel"; import { RemoteServiceChip } from "./remote-service-chip"; import { useRemoteInventory } from "./use-remote-inventory"; @@ -47,6 +49,13 @@ export function RemoteProjectsView({ machine, query }: RemoteProjectsViewProps) const groups = useMemo(() => groupInventory(inventory?.items ?? [], query), [inventory, query]); const outputItem = inventory?.items.find((item) => serviceKey(item) === outputKey) ?? null; const outputShown = outputItem !== null; + // A control is sent only on an open session, never queued. + const down = sessionDownReason({ + machine: name, + machineLabel: describeMachine(machine).label, + phase: state.phase, + proto: machine.proto, + }); useEffect(() => { setOutputOpen(outputShown); return () => setOutputOpen(false); @@ -59,57 +68,67 @@ export function RemoteProjectsView({ machine, query }: RemoteProjectsViewProps) }; return ( -
- - You're viewing {name}. Its .localify URLs open only on {name}. - - - {state.phase === "lost" ? ( - - {remoteErrorMessage(state.code, name, state.message)} - {isFinalCode(state.code) ? null : ` Trying again in ${state.retryIn} s.`} + +
+ + You're viewing {name}. Its .localify URLs open only on {name}. - ) : null} - {inventory?.truncated ? ( - {name} has more services than fit here; some are left out. - ) : null} - - {!inventory && state.phase !== "lost" ? ( -
- - Connecting to {name}… -
- ) : null} + + {state.phase === "lost" ? ( + + {remoteErrorMessage(state.code, name, state.message)} + {isFinalCode(state.code) ? null : ` Trying again in ${state.retryIn} s.`} + + ) : null} + {inventory?.truncated ? ( + {name} has more services than fit here; some are left out. + ) : null} - {inventory && groups.length === 0 ? ( -

- {query ? "No service matches." : `${name} has no projects yet.`} -

- ) : null} + {!inventory && state.phase !== "lost" ? ( +
+ + Connecting to {name}… +
+ ) : null} - {groups.map((project) => ( - - ))} + {inventory && groups.length === 0 ? ( +

+ {query ? "No service matches." : `${name} has no projects yet.`} +

+ ) : null} - {outputItem ? ( - <> - {/* Keeps the last cards scrollable above the panel. */} -
- ( + setOutputKey(null)} + onOutput={showOutput} /> - - ) : null} -
+ ))} + + {outputItem ? ( + <> + {/* Keeps the last cards scrollable above the panel. */} +
+ setOutputKey(null)} + /> + + ) : null} +
+ ); } diff --git a/desktop-ui/src/components/app/remote/remote-quick-action-card.tsx b/desktop-ui/src/components/app/remote/remote-quick-action-card.tsx new file mode 100644 index 00000000..86beb70a --- /dev/null +++ b/desktop-ui/src/components/app/remote/remote-quick-action-card.tsx @@ -0,0 +1,126 @@ +"use client"; + +import { useEffect } from "react"; + +import type { RemoteControlAction, RemoteInventoryItemDto } from "@/types/localify"; + +import { SpinnerIcon } from "@/components/icons"; +import { Button } from "@/components/ui/button"; +import { remoteErrorMessage } from "@/lib/util.remote"; +import { accessCommand, type QuickAction } from "@/lib/util.remote-controls"; +import { cn } from "@/lib/utils"; + +import { RemoteCommand } from "./remote-command"; +import { useRemoteControls } from "./remote-controls"; + +/** + * Why a control is not allowed, and what fixes it (docs/remote-machines.md, "Quick actions"): + * ask the target, run a command there, or relaunch a legacy run. + */ +export function RemoteQuickActionCard({ + item, + action, + quick, + onDone, +}: { + item: RemoteInventoryItemDto; + action: RemoteControlAction; + quick: Exclude; + onDone: () => void; +}) { + const { machineName: name, access, ask, relaunch, dismissAccess } = useRemoteControls(); + // An answer shown here is not shown again in the next card. + useEffect(() => dismissAccess, [dismissAccess]); + const code = item.controls[quick.kind === "relaunch" ? "restart" : action].code ?? ""; + + switch (quick.kind) { + case "message": + return

{quick.text}

; + case "command": + return ( +
+

{remoteErrorMessage(code, name, "Not available.")}

+

Update Localify on {name} to ask from here.

+ +
+ ); + case "ask": + return ( +
+

{remoteErrorMessage(code, name, "Not available.")}

+ + {access?.view.step === "waiting" ? null : ( + + )} +

Or run this on {name}:

+ +
+ ); + case "relaunch": + return ( +
+

+ {item.service.name} started before this version of Localify on {name}. +

+ {quick.access ? ( +

+ {remoteErrorMessage(code, name, "")} Relaunching asks {name} for access first. +

+ ) : null} + + {access?.view.step === "waiting" ? null : ( + + )} + {quick.access ? ( + <> +

Or run this on {name}:

+ + + ) : null} +
+ ); + } +} + +/** The access request open with this machine: waiting with Cancel, or how it ended. */ +function AccessStatus() { + const { access, cancelAsk } = useRemoteControls(); + if (!access) return null; + const { view } = access; + if (view.step === "waiting") { + return ( +
+ + {view.text} + +
+ ); + } + return ( +

+ {view.text} +

+ ); +} diff --git a/desktop-ui/src/components/app/remote/remote-service-chip.tsx b/desktop-ui/src/components/app/remote/remote-service-chip.tsx index cc14fbd7..b29f80d5 100644 --- a/desktop-ui/src/components/app/remote/remote-service-chip.tsx +++ b/desktop-ui/src/components/app/remote/remote-service-chip.tsx @@ -1,7 +1,7 @@ "use client"; import { useQuery } from "@tanstack/react-query"; -import { useState } from "react"; +import { useRef, useState } from "react"; import { toast } from "sonner"; import type { RemoteControlAction, RemoteInventoryItemDto } from "@/types/localify"; @@ -15,6 +15,7 @@ import { ExternalLinkIcon, PlayIcon, RefreshIcon, + SpinnerIcon, TerminalIcon, XIcon, } from "@/components/icons"; @@ -26,20 +27,18 @@ import { DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; +import { Popover, PopoverContent } from "@/components/ui/popover"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard"; import { bootstrapQueries } from "@/lib/queries"; import { tauriClient } from "@/lib/tauri-client"; -import { - ownerLabel, - remoteErrorMessage, - remoteHost, - serviceChips, - serviceTone, -} from "@/lib/util.remote"; +import { ownerLabel, remoteHost, serviceChips, serviceTone } from "@/lib/util.remote"; +import { type QuickAction, quickAction } from "@/lib/util.remote-controls"; import { cn } from "@/lib/utils"; import { remoteToneDot } from "./machine-switcher"; +import { useRemoteControls } from "./remote-controls"; +import { RemoteQuickActionCard } from "./remote-quick-action-card"; interface RemoteServiceChipProps { item: RemoteInventoryItemDto; @@ -55,8 +54,24 @@ export function RemoteServiceChip({ item, machineName, onOutput }: RemoteService const { data: bootstrap } = useQuery(bootstrapQueries.read()); const { copy, copied } = useCopyToClipboard(); const [menuOpen, setMenuOpen] = useState(false); + const controls = useRemoteControls(); + const chip = useRef(null); + // The card of a control that is not allowed, under the chip. + const [card, setCard] = useState(null); + const toggle: RemoteControlAction = item.process === "running" ? "stop" : "start"; + const controlOff = controls.down ?? controls.inFlight(item)?.text ?? null; + const quickFor = (action: RemoteControlAction) => + quickAction(item, action, controls.proto, machineName, controls.appRunning); + const restart = quickFor("restart"); + const cardQuick = card ? quickFor(card) : null; + const press = (action: RemoteControlAction) => { + const quick = quickFor(action); + if (quick.kind === "run") controls.run(item, action); + // Restart of a legacy run goes straight to its confirmation. + else if (quick.kind === "relaunch" && action === "restart") controls.relaunch(item); + else setCard((current) => (current === action ? null : action)); + }; const tone = serviceTone(item); - const running = item.process === "running"; const publicUrl = item.publicUrl?.serving ? item.publicUrl.url : null; const host = remoteHost(item.localUrl); const outputReason = outputUnavailableMessage(item, machineName); @@ -76,6 +91,7 @@ export function RemoteServiceChip({ item, machineName, onOutput }: RemoteService return ( press(toggle)} /> + { + if (open) return; + // The chip's own buttons open and toggle the card, and its menu hands focus back + // to it: neither closes the card here. + const event = details.event; + const target = + details.reason === "focus-out" && event instanceof FocusEvent + ? event.relatedTarget + : event?.target; + const inChip = target instanceof Node && chip.current?.contains(target); + if (inChip && (details.reason === "outside-press" || details.reason === "focus-out")) { + return; + } + setCard(null); + }} + > + + {card && cardQuick && cardQuick.kind !== "run" ? ( + setCard(null)} + /> + ) : null} + + + - - - - Show output - {outputReason ? ( - {outputReason} - ) : null} - - + {outputRelaunch(item, restart) ? ( + press("restart")}> + + + Relaunch to share + {controlOff ?? outputReason} + + + ) : ( + + + + Show output + {outputReason ? ( + {outputReason} + ) : null} + + + )} {publicUrl ? ( void openPublicUrl(publicUrl)}> @@ -192,13 +255,13 @@ export function RemoteServiceChip({ item, machineName, onOutput }: RemoteService {copied ? "URL copied" : "Copy URL"} - + press("restart")}> - Restart - - {controlReason(item, "restart", machineName)} - + {restart.kind === "relaunch" ? `Relaunch on ${machineName}` : "Restart"} + {controlOff ? ( + {controlOff} + ) : null} @@ -213,54 +276,65 @@ const CONTROL_LABEL: Record = { restart: "Restart", }; -/** Why a control is unavailable. Remote controls come in a later version. */ -function controlReason( - item: RemoteInventoryItemDto, - action: RemoteControlAction, - machineName: string, -): string { - const code = item.controls[action].code; - return code - ? remoteErrorMessage(code, machineName, "Not available.") - : `${CONTROL_LABEL[action]} from another machine comes in a later version.`; -} - -/** Start or Stop, where this Mac's chips put Launch or Stop; disabled, saying why. */ +/** + * Start or Stop, where this Mac's chips put Launch or Stop. Disabled only while the session + * is down or a control of this service is on its way; a control that is not allowed opens a + * card that says why. + */ function ControlButton({ item, action, machineName, - className, + off, + pending, + onPress, }: { item: RemoteInventoryItemDto; action: RemoteControlAction; machineName: string; - className?: string; + /** Why it can't be pressed now. */ + off: string | null; + pending: boolean; + onPress: () => void; }) { + const label = `${CONTROL_LABEL[action]} ${item.service.name} on ${machineName}`; return ( + } /> - - {CONTROL_LABEL[action]}: {controlReason(item, action, machineName)} - + {off ?? label} ); } +/** Output's "Relaunch to share": a legacy run this viewer can relaunch from here. */ +function outputRelaunch(item: RemoteInventoryItemDto, restart: QuickAction): boolean { + return ( + !item.output.available && item.output.reason === "legacy_run" && restart.kind === "relaunch" + ); +} + /** The dot's tooltip in words, for the keyboard and screen readers. */ function statusSummary(item: RemoteInventoryItemDto): string { const owner = ownerLabel(item.owner); diff --git a/desktop-ui/src/components/app/settings/settings-remote-access.tsx b/desktop-ui/src/components/app/settings/settings-remote-access.tsx index d45aea52..9161ab57 100644 --- a/desktop-ui/src/components/app/settings/settings-remote-access.tsx +++ b/desktop-ui/src/components/app/settings/settings-remote-access.tsx @@ -33,8 +33,6 @@ import { useRemote } from "@/providers/remote-provider"; import { SettingsCard } from "./settings-card"; -const CONTROLS_LATER = "Comes with remote controls, in a later version."; - /** * Settings → Remote access (docs/remote-machines.md, "Desktop app"): what paired machines may * see and do here, all off by default, this Mac's fingerprint, and the paired machines. @@ -81,6 +79,7 @@ export const SettingsRemoteAccess = () => {
) : null} + ) : null} @@ -95,13 +94,11 @@ function UpgradeAction() { function Switches({ settings }: { settings: RemoteSettingsDto }) { const queryClient = useQueryClient(); const change = useMutation(remoteMutations.changeSettings(queryClient)); - const setShare = (share: boolean) => - change.mutate( - { share }, - { - onError: (error) => toast.error("Could not change sharing", { description: error.message }), - }, - ); + const set = (request: Partial) => + change.mutate(request, { + onError: (error) => + toast.error("Could not change remote access", { description: error.message }), + }); return (
@@ -109,20 +106,22 @@ function Switches({ settings }: { settings: RemoteSettingsDto }) { label="Share projects and output with paired machines" checked={settings.share} disabled={change.isPending} - onChange={setShare} + onChange={(share) => set({ share })} note="Output can contain secrets your services print, and what you type into service tabs." /> set({ control })} + note={settings.share ? undefined : "Needs sharing."} /> set({ backgroundStarts })} + note={settings.control ? undefined : "Needs remote controls."} />
); @@ -138,11 +137,11 @@ function SwitchRow({ label: string; checked: boolean; disabled?: boolean; - onChange?: (checked: boolean) => void; + onChange: (checked: boolean) => void; note?: ReactNode; }) { return ( -