Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ff98af8e0 | ||
|
|
7ead5c71eb | ||
|
|
e06925cf87 | ||
|
|
a811a7cd0e | ||
|
|
f37bfbdc56 | ||
|
|
5bf11eac55 |
@@ -1,5 +1,13 @@
|
||||
import type { DesktopSshPasswordPromptRequest } from "@t3tools/contracts";
|
||||
import { useEffect, useId, useRef, useState } from "react";
|
||||
import {
|
||||
useEffect,
|
||||
useId,
|
||||
useReducer,
|
||||
useRef,
|
||||
useState,
|
||||
type Dispatch,
|
||||
type RefObject,
|
||||
} from "react";
|
||||
|
||||
import { Button } from "../ui/button";
|
||||
import {
|
||||
@@ -28,16 +36,41 @@ function getPromptErrorMessage(error: unknown): string {
|
||||
: message;
|
||||
}
|
||||
|
||||
export function SshPasswordPromptDialog() {
|
||||
const EXPIRED_PROMPT_MESSAGE = "This SSH password prompt expired. Try connecting again.";
|
||||
|
||||
type PromptDraftState = {
|
||||
password: string;
|
||||
responseError: string | null;
|
||||
now: number;
|
||||
};
|
||||
|
||||
type PromptDraftAction =
|
||||
| { type: "prompt.reset"; now: number }
|
||||
| { type: "clock.tick"; now: number }
|
||||
| { type: "password.set"; password: string }
|
||||
| { type: "error.set"; error: string | null };
|
||||
|
||||
function promptDraftReducer(state: PromptDraftState, action: PromptDraftAction): PromptDraftState {
|
||||
switch (action.type) {
|
||||
case "prompt.reset":
|
||||
return {
|
||||
password: "",
|
||||
responseError: null,
|
||||
now: action.now,
|
||||
};
|
||||
case "clock.tick":
|
||||
return state.now === action.now ? state : { ...state, now: action.now };
|
||||
case "password.set":
|
||||
return state.password === action.password ? state : { ...state, password: action.password };
|
||||
case "error.set":
|
||||
return state.responseError === action.error
|
||||
? state
|
||||
: { ...state, responseError: action.error };
|
||||
}
|
||||
}
|
||||
|
||||
function useSshPasswordPromptQueue() {
|
||||
const [queue, setQueue] = useState<readonly DesktopSshPasswordPromptRequest[]>([]);
|
||||
const [password, setPassword] = useState("");
|
||||
const [isResponding, setIsResponding] = useState(false);
|
||||
const [now, setNow] = useState(() => Date.now());
|
||||
const [responseError, setResponseError] = useState<string | null>(null);
|
||||
const currentRequest = queue[0] ?? null;
|
||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||
const isRespondingRef = useRef(false);
|
||||
const formId = useId();
|
||||
|
||||
useEffect(() => {
|
||||
const bridge = window.desktopBridge;
|
||||
@@ -50,14 +83,20 @@ export function SshPasswordPromptDialog() {
|
||||
});
|
||||
}, []);
|
||||
|
||||
return [queue, setQueue] as const;
|
||||
}
|
||||
|
||||
function useCurrentPromptLifecycle(
|
||||
currentRequest: DesktopSshPasswordPromptRequest | null,
|
||||
inputRef: RefObject<HTMLInputElement | null>,
|
||||
dispatch: Dispatch<PromptDraftAction>,
|
||||
) {
|
||||
useEffect(() => {
|
||||
setPassword("");
|
||||
setResponseError(null);
|
||||
dispatch({ type: "prompt.reset", now: Date.now() });
|
||||
if (!currentRequest) {
|
||||
return;
|
||||
}
|
||||
|
||||
setNow(Date.now());
|
||||
const frame = window.requestAnimationFrame(() => {
|
||||
inputRef.current?.focus();
|
||||
inputRef.current?.select();
|
||||
@@ -65,7 +104,7 @@ export function SshPasswordPromptDialog() {
|
||||
return () => {
|
||||
window.cancelAnimationFrame(frame);
|
||||
};
|
||||
}, [currentRequest]);
|
||||
}, [currentRequest, dispatch, inputRef]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!currentRequest) {
|
||||
@@ -73,32 +112,41 @@ export function SshPasswordPromptDialog() {
|
||||
}
|
||||
|
||||
const interval = window.setInterval(() => {
|
||||
setNow(Date.now());
|
||||
dispatch({ type: "clock.tick", now: Date.now() });
|
||||
}, 1_000);
|
||||
return () => {
|
||||
window.clearInterval(interval);
|
||||
};
|
||||
}, [currentRequest]);
|
||||
}, [currentRequest, dispatch]);
|
||||
}
|
||||
|
||||
export function SshPasswordPromptDialog() {
|
||||
const [queue, setQueue] = useSshPasswordPromptQueue();
|
||||
const [isResponding, setIsResponding] = useState(false);
|
||||
const [draft, dispatchDraft] = useReducer(promptDraftReducer, undefined, () => ({
|
||||
password: "",
|
||||
responseError: null,
|
||||
now: Date.now(),
|
||||
}));
|
||||
const currentRequest = queue[0] ?? null;
|
||||
const inputRef = useRef<HTMLInputElement | null>(null);
|
||||
const isRespondingRef = useRef(false);
|
||||
const formId = useId();
|
||||
useCurrentPromptLifecycle(currentRequest, inputRef, dispatchDraft);
|
||||
|
||||
const expiresAtMs = currentRequest ? Date.parse(currentRequest.expiresAt) : Number.NaN;
|
||||
const remainingMs = Number.isFinite(expiresAtMs) ? Math.max(0, expiresAtMs - now) : null;
|
||||
const remainingMs = Number.isFinite(expiresAtMs) ? Math.max(0, expiresAtMs - draft.now) : null;
|
||||
const isExpired = remainingMs !== null && remainingMs <= 0;
|
||||
const remainingSeconds = remainingMs === null ? null : Math.ceil(remainingMs / 1_000);
|
||||
const remainingLabel =
|
||||
remainingSeconds === null ? null : formatRemainingSeconds(remainingSeconds);
|
||||
|
||||
useEffect(() => {
|
||||
if (isExpired) {
|
||||
setResponseError("This SSH password prompt expired. Try connecting again.");
|
||||
}
|
||||
}, [isExpired]);
|
||||
const visibleResponseError = isExpired ? EXPIRED_PROMPT_MESSAGE : draft.responseError;
|
||||
|
||||
const removeCurrentPrompt = (requestId: string) => {
|
||||
setQueue((currentQueue) =>
|
||||
currentQueue[0]?.requestId === requestId ? currentQueue.slice(1) : currentQueue,
|
||||
);
|
||||
setPassword("");
|
||||
setResponseError(null);
|
||||
dispatchDraft({ type: "prompt.reset", now: Date.now() });
|
||||
};
|
||||
|
||||
const respond = async (nextPassword: string | null) => {
|
||||
@@ -108,13 +156,13 @@ export function SshPasswordPromptDialog() {
|
||||
|
||||
const requestId = currentRequest.requestId;
|
||||
if (nextPassword !== null && isExpired) {
|
||||
setResponseError("This SSH password prompt expired. Try connecting again.");
|
||||
dispatchDraft({ type: "error.set", error: EXPIRED_PROMPT_MESSAGE });
|
||||
return;
|
||||
}
|
||||
|
||||
isRespondingRef.current = true;
|
||||
setIsResponding(true);
|
||||
setResponseError(null);
|
||||
dispatchDraft({ type: "error.set", error: null });
|
||||
try {
|
||||
await window.desktopBridge?.resolveSshPasswordPrompt(requestId, nextPassword);
|
||||
removeCurrentPrompt(requestId);
|
||||
@@ -122,7 +170,7 @@ export function SshPasswordPromptDialog() {
|
||||
if (nextPassword === null) {
|
||||
removeCurrentPrompt(requestId);
|
||||
} else {
|
||||
setResponseError(getPromptErrorMessage(error));
|
||||
dispatchDraft({ type: "error.set", error: getPromptErrorMessage(error) });
|
||||
}
|
||||
} finally {
|
||||
isRespondingRef.current = false;
|
||||
@@ -170,7 +218,7 @@ export function SshPasswordPromptDialog() {
|
||||
id={formId}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
void respond(password);
|
||||
void respond(draft.password);
|
||||
}}
|
||||
>
|
||||
<div className="space-y-2">
|
||||
@@ -194,12 +242,14 @@ export function SshPasswordPromptDialog() {
|
||||
disabled={isResponding || isExpired}
|
||||
name="ssh-password"
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(event) => setPassword(event.target.value)}
|
||||
value={draft.password}
|
||||
onChange={(event) =>
|
||||
dispatchDraft({ type: "password.set", password: event.target.value })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{responseError ? (
|
||||
<p className="text-sm text-destructive">{responseError}</p>
|
||||
{visibleResponseError ? (
|
||||
<p className="text-sm text-destructive">{visibleResponseError}</p>
|
||||
) : (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Use SSH keys to avoid repeated password prompts on new SSH sessions.
|
||||
|
||||
Reference in new issue
Block a user