import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { ChevronDown } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Disclosure } from "@/components/ui/disclosure";
import { fetchSettings, type UpdateGuide as Guide } from "@/lib/api";
import { copyTextToClipboard } from "@/lib/clipboard";
function Command({ command, label, collapsed = false }: { command: string; label: string; collapsed?: boolean }) {
const { t } = useTranslation();
const [copy, setCopy] = useState<"idle" | "pending" | "copied" | "failed">("idle");
const content =
{label}
;
return collapsed ? {label}>}>
{content}
: content;
}
/** Display and copy only. The package manager/deployment remains the owner. */
export function UpdateGuide({ guide, token, targetVersion }: { guide: Guide; token: string; targetVersion: string }) {
const { t } = useTranslation();
const active = useRef(true);
const [checking, setChecking] = useState(false);
const [result, setResult] = useState("");
useEffect(() => { active.current = true; return () => { active.current = false; }; }, []);
const check = async () => {
if (checking) return;
setChecking(true);
setResult("");
const feedback = new Promise(resolve => setTimeout(resolve, 600));
try {
const settings = await fetchSettings(token).finally(() => feedback);
const version = settings.version?.current;
if (!version) throw new Error("No running version");
if (active.current) setResult(t(version === targetVersion ? "updates.guide.verified" : "updates.guide.observed", { version, target: targetVersion }));
} catch {
if (active.current) setResult(t("updates.guide.checkFailed"));
} finally { if (active.current) setChecking(false); }
};
return
{t("updates.guide.host", { host: guide.host })}
{t(guide.method === "container" ? "updates.guide.containerHost" : "updates.guide.where")}
{guide.environment}
{t("updates.guide.beforeTitle")}
{t("updates.guide.before")}
{t("updates.guide.installTitle", { shell: guide.shell === "powershell" ? "PowerShell" : "sh / bash / zsh" })}
{t(`updates.guide.reasons.${guide.reason}`)}
{guide.inspect_command && }
{guide.update_command && }
{t("updates.guide.verifyTitle")}
{t("updates.guide.verify", { version: targetVersion })}
{guide.verify_command && }
{result}
;
}