import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Icon } from "@/components/icons"; import { Button } from "@/components/ui/button"; import { Modal } from "@/components/ui/dialog"; import { Field, Input } from "@/components/ui/input"; import { ApiError, errorI18nKey } from "@/lib/api"; export interface NewWorkspaceInput { cwd: string; name?: string; } export interface NewWorkspaceDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onCreate: (input: NewWorkspaceInput) => Promise; } /** Path is required, display name optional; validation is inline, not a toast. */ export function NewWorkspaceDialog({ open, onOpenChange, onCreate }: NewWorkspaceDialogProps) { const { t } = useTranslation(); const [cwd, setCwd] = useState(""); const [name, setName] = useState(""); const [pathError, setPathError] = useState(null); const [formError, setFormError] = useState(null); const [busy, setBusy] = useState(false); useEffect(() => { if (!open) return; setCwd(""); setName(""); setPathError(null); setFormError(null); setBusy(false); }, [open]); async function submit(event: React.FormEvent) { event.preventDefault(); if (busy) return; const trimmed = cwd.trim(); if (!trimmed) { setPathError(t("workspaces.pathRequired")); setFormError(null); return; } setPathError(null); setFormError(null); setBusy(true); try { await onCreate({ cwd: trimmed, ...(name.trim() ? { name: name.trim() } : {}) }); onOpenChange(false); } catch (error) { const key = error instanceof ApiError ? errorI18nKey(error.code) : "workspaces.addFailed"; setFormError(t(key)); } finally { setBusy(false); } } return ( } >
void submit(event)}> { setCwd(event.target.value); if (pathError) setPathError(null); }} /> setName(event.target.value)} /> {formError ? (
{formError}
) : null}
); }