"use client"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Skeleton, SkeletonGroup } from "@/components/ui/skeleton"; import { useInstance, useUpdateInstance } from "@/hooks/use-instances"; import type { ConnectInstancePayload, ImportInstancePayload, InstanceActionResult, UpdateInstancePayload, } from "@/types/instance"; import { useI18n } from "@/i18n/i18n-context"; import { ConnectInstanceForm } from "./connect-instance-form"; export interface EditInstanceDialogProps { name: string | null; open: boolean; onOpenChange: (open: boolean) => void; onSuccess?: (result: InstanceActionResult) => void; } export function EditInstanceDialog({ name, open, onOpenChange, onSuccess, }: EditInstanceDialogProps) { const { t } = useI18n(); const detailQuery = useInstance(open ? name : null); const update = useUpdateInstance(name ?? ""); async function handleSubmit( values: ConnectInstancePayload | ImportInstancePayload, ) { if (!name) return; // Edit mode never exposes the token tab, so `values` is always manual. const manual = values as ConnectInstancePayload; // The form's edit schema enforces "AK and SK are filled together or // both blank". Both blank means "keep the existing credential", so // we strip the pair from the patch entirely instead of sending empty // strings (which the server would interpret as "clear the credential" // and brick the instance). const ak = manual.accessKeyId?.trim() ?? ""; const sk = manual.accessKeySecret?.trim() ?? ""; const credentialPatch = ak && sk ? { accessKeyId: ak, accessKeySecret: sk } : {}; // API Key follows the same "blank = keep existing" rule: only forward it // when the user typed a new value, so we never overwrite the stored key // with an empty string. const apiKey = manual.apiKey?.trim() ?? ""; const apiKeyPatch = apiKey ? { apiKey } : {}; const patch: UpdateInstancePayload = { ...credentialPatch, ...apiKeyPatch, otsEndpoint: manual.otsEndpoint, otsInstanceName: manual.otsInstanceName, // The form normalizes empty inputs to `undefined` (optional field). // In edit mode we need to forward an explicit empty string so the // server-side PATCH treats it as "clear this field" instead of // "leave unchanged". ossEndpoint: manual.ossEndpoint ?? "", ossBucketName: manual.ossBucketName ?? "", region: manual.region, }; const result = (await update.mutateAsync( patch, )) as unknown as InstanceActionResult; onSuccess?.(result); onOpenChange(false); } const detail = detailQuery.data; return ( {name ? t("instance.edit.titleWithName", { name }) : t("instance.edit.title")} {t("instance.edit.description")} {detailQuery.isLoading ? ( ) : detailQuery.isError ? (

{t("instance.edit.loadFailed")} {detailQuery.error instanceof Error ? detailQuery.error.message : t("instance.common.unknownError")}

) : detail ? ( ) : null}
); }