"use client"; import { useEffect, useState } from "react"; import { Alert, Button, Card, Center, PasswordInput, Select, Stack, Text, TextInput, ThemeIcon, Title, } from "@mantine/core"; import { IconCheck, IconKey, IconLock } from "@tabler/icons-react"; import { useI18n } from "@homarr/translation/client"; interface RequestDetails { widgetName: string; sourceName: string; kinds: Array<"apiKey" | "username" | "password">; expiresAt: number; status: "pending" | "completed"; source: { baseUrl: string; networkScope: "public" | "private" | "loopback"; }; } export function CustomWidgetConfigurationEntry({ token }: { token: string }) { const t = useI18n("customWidget.secretEntry"); const tSecret = useI18n("customWidget.secret"); const [details, setDetails] = useState(null); const [values, setValues] = useState>({}); const [baseUrl, setBaseUrl] = useState(""); const [networkScope, setNetworkScope] = useState<"public" | "private" | "loopback">("public"); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); useEffect(() => { void fetch(`/api/custom-widgets/configuration-request/${encodeURIComponent(token)}`) .then(async (response) => { const body = (await response.json()) as RequestDetails | { error: string }; if (!response.ok || "error" in body) throw new Error("error" in body ? body.error : t("unavailable")); setDetails(body); setBaseUrl(body.source.baseUrl); setNetworkScope(body.source.networkScope); }) .catch((cause: unknown) => setError(cause instanceof Error ? cause.message : t("unavailable"))) .finally(() => setLoading(false)); }, [t, token]); const submit = async () => { setSaving(true); setError(null); try { const response = await fetch(`/api/custom-widgets/configuration-request/${encodeURIComponent(token)}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ baseUrl, networkScope, secrets: values }), }); const body = (await response.json()) as { status?: string; error?: string }; if (!response.ok) throw new Error(body.error ?? t("saveError")); setDetails((current) => (current ? { ...current, status: "completed" } : current)); setValues({}); } catch (cause) { setError(cause instanceof Error ? cause.message : t("saveError")); } finally { setSaving(false); } }; return (
{t("title")} {t("description")} {loading && {t("loading")}} {error && {error}} {details?.status === "completed" && ( }> {t("saved")} )} {details?.status === "pending" && ( {details.widgetName} {t("source", { name: details.sourceName })} setBaseUrl(event.currentTarget.value)} required /> } value={values[kind] ?? ""} onChange={(event) => setValues((current) => ({ ...current, [kind]: event.currentTarget.value }))} autoComplete="off" required /> ); })} {t("expires", { time: new Date(details.expiresAt).toLocaleTimeString() })} )}
); }