feat(remote): rename servers without interrupting host sessions
This commit is contained in:
1 parent
47c587217d
commit
719a5c3fcf
19 files changed
+294
-8
No files matched your search
@@ -39,6 +39,9 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro
|
||||
const [quickOpen, setQuickOpen] = useState(!!returnedPair);
|
||||
const [pairRoute, setPairRoute] = useState("");
|
||||
const [details, setDetails] = useState("");
|
||||
const [renaming, setRenaming] = useState<RemoteProfile | null>(null);
|
||||
const [name, setName] = useState("");
|
||||
const [renameError, setRenameError] = useState("");
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [savedId, setSavedId] = useState("");
|
||||
const [busy, setBusy] = useState("");
|
||||
@@ -60,6 +63,8 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro
|
||||
const operation = useRef(0);
|
||||
const optionsId = useId();
|
||||
const statusId = useId();
|
||||
const nameId = useId();
|
||||
const nameInput = useRef<HTMLInputElement>(null);
|
||||
const refresh = connections?.refresh;
|
||||
const page = useRef<HTMLDivElement>(null);
|
||||
useEffect(() => { if (connections?.managing) page.current?.focus({ preventScroll: true }); }, [connections?.managing]);
|
||||
@@ -228,6 +233,16 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro
|
||||
} catch (reason) { if (mounted.current) showError(reason); }
|
||||
finally { if (mounted.current) setBusy(""); }
|
||||
};
|
||||
const rename = async () => {
|
||||
if (!renaming || busy || !name.trim() || name.trim() === renaming.name) return;
|
||||
setBusy("rename"); setRenameError("");
|
||||
try {
|
||||
await connections.rename(renaming.id, name.trim());
|
||||
if (mounted.current) setRenaming(null);
|
||||
} catch (reason) {
|
||||
if (mounted.current) setRenameError(t(`remote.errors.${reason instanceof Error ? reason.message : "unknown"}`, { defaultValue: t("remote.errors.unknown") }));
|
||||
} finally { if (mounted.current) setBusy(""); }
|
||||
};
|
||||
const disconnect = async (id: string) => {
|
||||
setBusy(id); setError("");
|
||||
try { await disconnectHost(id); if (mounted.current) setDisconnecting(null); }
|
||||
@@ -308,6 +323,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro
|
||||
onKeyDown={(event) => { editorTrigger.current = event.currentTarget; }}
|
||||
aria-label={t("remote.manage", { name: profile.name })}><MoreHorizontal className="h-4 w-4" /></Button></DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onSelect={() => { setRenaming(profile); setName(profile.name); setRenameError(""); }}>{t("remote.rename.action")}</DropdownMenuItem>
|
||||
{entries.length > 1 ? <DropdownMenuItem onSelect={() => setDetails(groupId)}>{t("remote.sameInstance.details")}</DropdownMenuItem> : connectionActions(profile)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
@@ -333,6 +349,31 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Dialog open={!!renaming} onOpenChange={(open) => { if (!open && !busy) setRenaming(null); }}>
|
||||
<DialogContent className="max-h-[85dvh] max-w-sm overflow-y-auto" onOpenAutoFocus={(event) => {
|
||||
event.preventDefault(); nameInput.current?.focus(); nameInput.current?.select();
|
||||
}} onCloseAutoFocus={(event) => { event.preventDefault(); editorTrigger.current?.focus({ preventScroll: true }); }}>
|
||||
<DialogHeader className="pr-5 text-left">
|
||||
<DialogTitle>{t("remote.rename.title")}</DialogTitle>
|
||||
<DialogDescription>{t("remote.rename.hint")}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<form className="space-y-4" onSubmit={(event) => { event.preventDefault(); void rename(); }}>
|
||||
<div className="space-y-2">
|
||||
<label htmlFor={nameId} className="text-[13px] font-medium">{t("remote.rename.name")}</label>
|
||||
<Input ref={nameInput} id={nameId} value={name} maxLength={64} required disabled={!!busy}
|
||||
aria-invalid={!!renameError} aria-describedby={renameError ? `${nameId}-error` : undefined}
|
||||
onChange={(event) => { setName(event.target.value); setRenameError(""); }} />
|
||||
</div>
|
||||
{renameError && <p id={`${nameId}-error`} role="alert" className="text-[13px] leading-5 text-foreground">{renameError}</p>}
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button type="button" variant="ghost" disabled={!!busy} onClick={() => setRenaming(null)}>{t("common.cancel")}</Button>
|
||||
<Button type="submit" disabled={!!busy || !name.trim() || name.trim() === renaming?.name} aria-busy={!!busy}>
|
||||
{busy && <Loader2 aria-hidden className="mr-2 h-4 w-4 animate-spin motion-reduce:animate-none" />}{t(busy ? "remote.saving" : "remote.save")}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
<Dialog open={!!detailGroup} onOpenChange={(open) => { if (!open) setDetails(""); }}>
|
||||
<DialogContent className="flex max-h-[85dvh] max-w-md flex-col overflow-hidden">
|
||||
<DialogHeader className="pr-5 text-left"><DialogTitle>{t("remote.sameInstance.details")}</DialogTitle><DialogDescription>{t("remote.sameInstance.detailsHint")}</DialogDescription></DialogHeader>
|
||||
|
||||
@@ -21,6 +21,7 @@ const RemoteContext = createContext<{
|
||||
directory: RemoteDirectory | null;
|
||||
directoryError: boolean;
|
||||
refresh: () => Promise<RemoteDirectory>;
|
||||
rename: (id: string, name: string) => Promise<void>;
|
||||
connect: (id: string, stillWanted?: () => boolean) => Promise<void>;
|
||||
disconnect: (id: string) => Promise<void>;
|
||||
cancel: () => void;
|
||||
@@ -138,7 +139,7 @@ export function RemoteInstances({ children }: { children: ReactNode }) {
|
||||
return <RemoteContext.Provider value={{ available, localActive: !selected, directory,
|
||||
activeHostId: selected?.id || null, hostStates, managing, closeManagement: () => changeManagement(false), selectLocal,
|
||||
openHostIds: frames.map((frame) => frame.connection.id),
|
||||
directoryError: hosts.directoryError, refresh: hosts.refresh, connect, disconnect: hosts.disconnect, cancel: hosts.cancel }}>
|
||||
directoryError: hosts.directoryError, refresh: hosts.refresh, rename: hosts.rename, connect, disconnect: hosts.disconnect, cancel: hosts.cancel }}>
|
||||
<HostNavigationContext.Provider value={bridge.embedded || (available || selected ? picker : null)}>
|
||||
<div className="flex h-full min-h-0 flex-col bg-background">
|
||||
<div className="relative min-h-0 flex-1 overflow-hidden">
|
||||
|
||||
@@ -58,17 +58,44 @@ export function useHostSessions() {
|
||||
publish();
|
||||
}, [publish]);
|
||||
const local = useCallback(() => { cancel(); select(null); }, [cancel, select]);
|
||||
const applyDirectory = useCallback((next: RemoteDirectory) => {
|
||||
setDirectory(next); setDirectoryError(false);
|
||||
// Names are metadata, not a new host session. Keep iframe URLs, drafts,
|
||||
// selection and connection state intact when this or another tab renames.
|
||||
let changed = false;
|
||||
for (const profile of next.profiles) {
|
||||
const frame = cache.current.get(profile.id);
|
||||
if (frame && frame.connection.name !== profile.name) {
|
||||
frame.connection = { ...frame.connection, name: profile.name };
|
||||
changed = true;
|
||||
}
|
||||
if (selectedRef.current?.id === profile.id && selectedRef.current.name !== profile.name) {
|
||||
const value = { ...selectedRef.current, name: profile.name };
|
||||
selectedRef.current = value; setSelected(value); rememberSelectedRemote(value);
|
||||
}
|
||||
if (pendingRef.current?.id === profile.id && pendingRef.current.name !== profile.name) {
|
||||
setPendingSwitch({ id: profile.id, name: profile.name });
|
||||
}
|
||||
}
|
||||
if (changed) publish();
|
||||
}, [publish, setPendingSwitch]);
|
||||
const rename = useCallback(async (id: string, name: string) => {
|
||||
const next = await remoteAction<RemoteDirectory>(client, "rename", { id, name });
|
||||
// Discard health reads begun before the save completed.
|
||||
directoryGeneration.current += 1;
|
||||
if (mounted.current) applyDirectory(next);
|
||||
}, [applyDirectory, client]);
|
||||
const refresh = useCallback(async () => {
|
||||
const generation = ++directoryGeneration.current;
|
||||
try {
|
||||
const next = window.top === window ? await readRemoteInstances(getToken()) : { available: false, profiles: [] };
|
||||
if (mounted.current && generation === directoryGeneration.current) { setDirectory(next); setDirectoryError(false); }
|
||||
if (mounted.current && generation === directoryGeneration.current) applyDirectory(next);
|
||||
return next;
|
||||
} catch (reason) {
|
||||
if (mounted.current && generation === directoryGeneration.current) setDirectoryError(true);
|
||||
throw reason;
|
||||
}
|
||||
}, [getToken]);
|
||||
}, [applyDirectory, getToken]);
|
||||
|
||||
const connect = useCallback(async (id: string, stillWanted: () => boolean = () => true) => {
|
||||
cancel();
|
||||
@@ -120,7 +147,7 @@ export function useHostSessions() {
|
||||
});
|
||||
if (!ready) return;
|
||||
}
|
||||
if (wanted()) select(connection);
|
||||
if (wanted()) select(frame.connection);
|
||||
} catch (reason) {
|
||||
if (wanted()) {
|
||||
setFailure({ id, code: reason instanceof Error ? reason.message : "unknown" });
|
||||
@@ -193,7 +220,7 @@ export function useHostSessions() {
|
||||
try {
|
||||
const next = await readRemoteInstances(getToken());
|
||||
if (cancelled || generation !== directoryGeneration.current) return;
|
||||
setDirectory(next); setDirectoryError(false);
|
||||
applyDirectory(next);
|
||||
for (const [id, frame] of cache.current) {
|
||||
if (id === pendingRef.current?.id) continue;
|
||||
const profile = next.profiles.find((profile) => profile.id === id);
|
||||
@@ -230,7 +257,7 @@ export function useHostSessions() {
|
||||
window.removeEventListener("pageshow", wake);
|
||||
document.removeEventListener("visibilitychange", wake);
|
||||
};
|
||||
}, [client, getToken, publish]);
|
||||
return { directory, directoryError, refresh, selected, recentIds, frames, pending,
|
||||
}, [applyDirectory, client, getToken, publish]);
|
||||
return { directory, directoryError, refresh, rename, selected, recentIds, frames, pending,
|
||||
error: failure?.code || "", errorId: failure?.id, clearError, connect, cancel, local, disconnect, loaded };
|
||||
}
|
||||
@@ -69,9 +69,10 @@ export function useSidebarHostBridge(frames: HostFrame[], selectedId: string | u
|
||||
return () => window.removeEventListener("message", receive);
|
||||
}, [initialize, nodes]);
|
||||
useEffect(() => { setAnchor(null); }, [selectedId]);
|
||||
const selectedName = frames.find((frame) => frame.connection.id === selectedId)?.connection.name;
|
||||
useEffect(() => {
|
||||
if (selectedId && peers.current.has(selectedId)) initialize(selectedId);
|
||||
}, [initialize, selectedId, activity.pendingName, activity.error]);
|
||||
}, [initialize, selectedId, selectedName, activity.pendingName, activity.error]);
|
||||
useEffect(() => {
|
||||
for (const id of peers.current.keys()) if (!frames.some((frame) => frame.connection.id === id)) peers.current.delete(id);
|
||||
}, [frames]);
|
||||
|
||||
Reference in new issue
Block a user