Files
Termix/src/ui/ssh/dialogs/SSHAuthDialog.tsx
T

300 lines
10 KiB
TypeScript
Raw Normal View History

+7
2025-11-05 10:36:16 -06:00
import React, { useState } from "react";
2026-05-11 01:23:11 -05:00
import { Button } from "@/components/button.tsx";
import { PasswordInput } from "@/components/password-input.tsx";
import { Label } from "@/components/label.tsx";
2026-01-24 19:49:42 -06:00
import {
Tabs,
TabsContent,
TabsList,
TabsTrigger,
2026-05-11 01:23:11 -05:00
} from "@/components/tabs.tsx";
+7
2025-11-05 10:36:16 -06:00
import { Shield, AlertCircle, Upload } from "lucide-react";
import { useTranslation } from "react-i18next";
import CodeMirror from "@uiw/react-codemirror";
import { oneDark } from "@codemirror/theme-one-dark";
import { EditorView } from "@codemirror/view";
interface SSHAuthDialogProps {
isOpen: boolean;
reason: "no_keyboard" | "auth_failed" | "timeout";
onSubmit: (credentials: {
password?: string;
sshKey?: string;
keyPassword?: string;
}) => void;
onCancel: () => void;
hostInfo: {
ip: string;
port: number;
username: string;
name?: string;
};
backgroundColor?: string;
}
export function SSHAuthDialog({
isOpen,
reason,
onSubmit,
onCancel,
hostInfo,
2026-05-28 22:29:20 -05:00
backgroundColor,
+7
2025-11-05 10:36:16 -06:00
}: SSHAuthDialogProps) {
const { t } = useTranslation();
const [authTab, setAuthTab] = useState<"password" | "key">("password");
const [password, setPassword] = useState("");
const [sshKey, setSshKey] = useState("");
const [keyPassword, setKeyPassword] = useState("");
const [loading, setLoading] = useState(false);
if (!isOpen) return null;
2026-05-28 22:29:20 -05:00
const hostDisplay = hostInfo.name
? `${hostInfo.name} (${hostInfo.username}@${hostInfo.ip}:${hostInfo.port})`
: `${hostInfo.username}@${hostInfo.ip}:${hostInfo.port}`;
+7
2025-11-05 10:36:16 -06:00
const getReasonMessage = () => {
switch (reason) {
case "no_keyboard":
return t("auth.sshNoKeyboardInteractive");
case "auth_failed":
return t("auth.sshAuthenticationFailed");
case "timeout":
return t("auth.sshAuthenticationTimeout");
default:
return t("auth.sshAuthenticationRequired");
}
};
const getReasonDescription = () => {
switch (reason) {
case "no_keyboard":
return t("auth.sshNoKeyboardInteractiveDescription");
case "auth_failed":
return t("auth.sshAuthFailedDescription");
case "timeout":
return t("auth.sshTimeoutDescription");
default:
return t("auth.sshProvideCredentialsDescription");
}
};
2026-05-28 22:29:20 -05:00
const canSubmit = () =>
authTab === "password" ? password !== "" : sshKey.trim() !== "";
+7
2025-11-05 10:36:16 -06:00
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
try {
const credentials: {
password?: string;
sshKey?: string;
keyPassword?: string;
} = {};
if (authTab === "password") {
2026-05-28 22:29:20 -05:00
if (password !== "") credentials.password = password;
+7
2025-11-05 10:36:16 -06:00
} else {
if (sshKey.trim()) {
credentials.sshKey = sshKey;
2026-05-28 22:29:20 -05:00
if (keyPassword.trim()) credentials.keyPassword = keyPassword;
+7
2025-11-05 10:36:16 -06:00
}
}
onSubmit(credentials);
} finally {
setLoading(false);
}
};
const handleKeyFileUpload = async (
e: React.ChangeEvent<HTMLInputElement>,
) => {
const file = e.target.files?.[0];
if (file) {
try {
2026-05-28 22:29:20 -05:00
setSshKey(await file.text());
+7
2025-11-05 10:36:16 -06:00
} catch (error) {
console.error("Failed to read SSH key file:", error);
}
}
};
return (
2026-05-28 22:29:20 -05:00
<div className="absolute inset-0 flex items-center justify-center z-500 animate-in fade-in duration-200">
<div
className="absolute inset-0 bg-canvas rounded-md"
style={{ backgroundColor: backgroundColor || undefined }}
/>
<div className="bg-card border border-border w-full max-w-xl mx-4 relative z-10 animate-in fade-in zoom-in-95 duration-200">
<div className="p-4 border-b border-border">
<div className="flex items-center gap-2">
<Shield className="size-4 text-accent-brand" />
<h3 className="text-xs font-bold uppercase tracking-widest">
{t("auth.sshAuthenticationRequired")}
</h3>
</div>
<p className="text-[10px] font-mono font-bold tracking-tight text-muted-foreground mt-1">
{hostDisplay}
</p>
</div>
<form onSubmit={handleSubmit}>
<div className="p-4 flex flex-col gap-4">
<div
className={`flex items-start gap-3 p-3 border ${
reason === "auth_failed"
? "border-destructive/20 bg-destructive/10"
: "border-border bg-muted/10"
}`}
>
<AlertCircle
className={`size-4 shrink-0 mt-0.5 ${reason === "auth_failed" ? "text-destructive" : "text-accent-brand"}`}
/>
<div>
<p
className={`text-[10px] font-bold uppercase tracking-widest ${reason === "auth_failed" ? "text-destructive" : ""}`}
>
{getReasonMessage()}
</p>
<p
className={`text-xs mt-1 ${reason === "auth_failed" ? "text-destructive/80" : "text-muted-foreground"}`}
>
{getReasonDescription()}
</p>
</div>
</div>
+7
2025-11-05 10:36:16 -06:00
<Tabs
value={authTab}
onValueChange={(v) => setAuthTab(v as "password" | "key")}
>
2026-05-28 22:29:20 -05:00
<TabsList className="w-full rounded-none">
<TabsTrigger
value="password"
className="flex-1 rounded-none text-[10px] font-bold uppercase tracking-widest"
>
+7
2025-11-05 10:36:16 -06:00
{t("credentials.password")}
</TabsTrigger>
2026-05-28 22:29:20 -05:00
<TabsTrigger
value="key"
className="flex-1 rounded-none text-[10px] font-bold uppercase tracking-widest"
>
{t("credentials.sshKey")}
</TabsTrigger>
+7
2025-11-05 10:36:16 -06:00
</TabsList>
2026-05-28 22:29:20 -05:00
<TabsContent
value="password"
className="mt-3 flex flex-col gap-2"
>
<Label className="text-[10px] font-bold uppercase tracking-widest text-muted-foreground">
{t("credentials.password")}
</Label>
<PasswordInput
placeholder={t("placeholders.enterPassword")}
value={password}
onChange={(e) => setPassword(e.target.value)}
autoFocus
className="rounded-none bg-muted/50 border-border text-xs"
/>
<p className="text-[10px] text-muted-foreground">
{t("auth.sshPasswordDescription")}
</p>
+7
2025-11-05 10:36:16 -06:00
</TabsContent>
2026-05-28 22:29:20 -05:00
<TabsContent value="key" className="mt-3 flex flex-col gap-3">
<div className="flex flex-col gap-2">
<Label className="text-[10px] font-bold uppercase tracking-widest text-muted-foreground">
+7
2025-11-05 10:36:16 -06:00
{t("credentials.sshPrivateKey")}
</Label>
2026-05-28 22:29:20 -05:00
<div className="relative">
<input
id="key-upload"
type="file"
accept="*,.pem,.key,.txt,.ppk"
onChange={handleKeyFileUpload}
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
/>
<Button
type="button"
variant="outline"
className="w-full justify-start rounded-none text-[10px] font-bold uppercase tracking-widest border-border"
>
<Upload className="size-3.5 mr-2" />
<span className="truncate">
{t("credentials.uploadPrivateKeyFile")}
</span>
</Button>
+7
2025-11-05 10:36:16 -06:00
</div>
<CodeMirror
value={sshKey}
onChange={(value) => setSshKey(value)}
placeholder={t("placeholders.pastePrivateKey")}
theme={oneDark}
2026-05-28 22:29:20 -05:00
className="border border-border text-xs"
minHeight="160px"
maxHeight="260px"
+7
2025-11-05 10:36:16 -06:00
basicSetup={{
lineNumbers: true,
foldGutter: false,
dropCursor: false,
allowMultipleSelections: false,
highlightSelectionMatches: false,
searchKeymap: false,
scrollPastEnd: false,
}}
extensions={[
EditorView.theme({
".cm-scroller": {
overflow: "auto",
+1
2025-12-31 22:20:12 -06:00
scrollbarWidth: "thin",
scrollbarColor:
"var(--scrollbar-thumb) var(--scrollbar-track)",
+7
2025-11-05 10:36:16 -06:00
},
}),
]}
/>
</div>
2026-05-28 22:29:20 -05:00
<div className="flex flex-col gap-2">
<Label className="text-[10px] font-bold uppercase tracking-widest text-muted-foreground">
+7
2025-11-05 10:36:16 -06:00
{t("credentials.keyPassword")} ({t("common.optional")})
</Label>
<PasswordInput
placeholder={t("placeholders.keyPassword")}
value={keyPassword}
onChange={(e) => setKeyPassword(e.target.value)}
2026-05-28 22:29:20 -05:00
className="rounded-none bg-muted/50 border-border text-xs"
+7
2025-11-05 10:36:16 -06:00
/>
2026-05-28 22:29:20 -05:00
<p className="text-[10px] text-muted-foreground">
+7
2025-11-05 10:36:16 -06:00
{t("auth.sshKeyPasswordDescription")}
</p>
</div>
</TabsContent>
</Tabs>
2026-05-28 22:29:20 -05:00
</div>
+7
2025-11-05 10:36:16 -06:00
2026-05-28 22:29:20 -05:00
<div className="p-4 border-t border-border flex justify-end gap-2">
<Button
type="button"
variant="ghost"
onClick={onCancel}
disabled={loading}
className="rounded-none text-[10px] font-bold uppercase tracking-widest"
>
{t("common.cancel")}
</Button>
<Button
type="submit"
variant="outline"
disabled={!canSubmit() || loading}
className="border-accent-brand/40 text-accent-brand hover:bg-accent-brand/10 rounded-none text-[10px] font-bold uppercase tracking-widest"
>
{loading ? t("common.connecting") : t("common.connect")}
</Button>
</div>
</form>
</div>
+7
2025-11-05 10:36:16 -06:00
</div>
);
}