import React, { useState, useEffect } from "react"; import { Loader2, Save, X } from "lucide-react"; import { Button } from "@/components/ui"; import { YamlEditor } from "@/components/Kubernetes/YamlEditor"; import { createResourceCmd, editResourceCmd } from "@/lib/tauriCommands"; import { BottomPanelTabType } from "@/stores/bottomPanelStore"; export interface YamlEditorTabData { /** Type drives the submit behaviour */ mode: | BottomPanelTabType.EDIT_RESOURCE | BottomPanelTabType.CREATE_RESOURCE | BottomPanelTabType.INSTALL_CHART | BottomPanelTabType.UPGRADE_CHART; clusterId: string; namespace: string; resourceType?: string; resourceName?: string; initialYaml?: string; /** For helm flows: the chart name being installed/upgraded */ chartName?: string; } interface YamlEditorTabProps { tabId: string; data: YamlEditorTabData; onClose?: (tabId: string) => void; } function actionLabel(mode: YamlEditorTabData["mode"]): string { switch (mode) { case BottomPanelTabType.EDIT_RESOURCE: return "Save"; case BottomPanelTabType.CREATE_RESOURCE: return "Create"; case BottomPanelTabType.INSTALL_CHART: return "Install"; case BottomPanelTabType.UPGRADE_CHART: return "Upgrade"; } } export function YamlEditorTab({ tabId, data, onClose }: YamlEditorTabProps) { const [yaml, setYaml] = useState(data.initialYaml ?? ""); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); useEffect(() => { setYaml(data.initialYaml ?? ""); }, [data.initialYaml]); const handleSubmit = async () => { setIsSubmitting(true); setError(null); setSuccess(null); try { switch (data.mode) { case BottomPanelTabType.CREATE_RESOURCE: await createResourceCmd( data.clusterId, data.namespace, data.resourceType ?? "", yaml ); setSuccess("Resource created"); break; case BottomPanelTabType.EDIT_RESOURCE: await editResourceCmd( data.clusterId, data.namespace, data.resourceType ?? "", data.resourceName ?? "", yaml ); setSuccess("Resource updated"); break; case BottomPanelTabType.INSTALL_CHART: case BottomPanelTabType.UPGRADE_CHART: // Helm flows are wired up to the existing helm modals; the YAML view // here just lets the user prepare values.yaml. Submit is no-op until // the corresponding tauri commands are added. setSuccess( data.mode === BottomPanelTabType.INSTALL_CHART ? "Helm install requires the install dialog to complete the flow." : "Helm upgrade requires the upgrade dialog to complete the flow." ); break; } } catch (err) { setError(err instanceof Error ? err.message : String(err)); } finally { setIsSubmitting(false); } }; const label = actionLabel(data.mode); return (
{data.resourceType && {data.resourceType}} {data.resourceName && ( <> {" / "} {data.resourceName} )} {data.chartName && ( {data.chartName} )} {data.namespace && ( ns: {data.namespace} )}
{error && (
{error}
)} {success && (
{success}
)}
); }