"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { IconInfoCircle } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Input } from "@kandev/ui/input"; import { Label } from "@kandev/ui/label"; import { Separator } from "@kandev/ui/separator"; import { Alert, AlertDescription } from "@kandev/ui/alert"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { useToast } from "@/components/toast-provider"; import { useSettingsSaveContributor } from "@/components/settings/settings-save-provider"; import { createSentryInstance, SENTRY_ERROR_CODES, sentryErrorCode, testSentryConnection, testSentryInstance, updateSentryInstance, } from "@/lib/api/domains/sentry-api"; import { SENTRY_AUTH_METHOD, SENTRY_DEFAULT_URL, type SentryConfig, type TestSentryConnectionResult, } from "@/lib/types/sentry"; import { useTranslation } from "react-i18next"; const FIELD = "space-y-1.5"; const HELP = "text-xs text-muted-foreground"; // None of the four below is copy, so none reaches the catalog — the // pseudo-locale would transliterate them into values the user cannot act on. // SECRET_MASK is a glyph run standing in for a stored token; AUTH_TOKEN_HINT is // the literal prefix every Sentry auth token carries; SELF_HOSTED_EXAMPLE_URL is // a hostname; and each TOKEN_SCOPES `scope` is an API scope identifier the user // must tick verbatim in Sentry. Only the scope descriptions are translatable, so // they travel as `descriptionKey` and resolve at render. const SECRET_MASK = "••••••••"; const AUTH_TOKEN_HINT = "sntrys_..."; const SELF_HOSTED_EXAMPLE_URL = "https://sentry.your-company.com"; const TOKEN_SCOPES: { scope: string; descriptionKey: string }[] = [ { scope: "org:read", descriptionKey: "sentry:scopeOrgRead" }, { scope: "project:read", descriptionKey: "sentry:scopeProjectRead" }, { scope: "event:read", descriptionKey: "sentry:scopeEventRead" }, ]; type FormState = { name: string; url: string; secret: string }; function instanceToForm(instance: SentryConfig | null): FormState { return { name: instance?.name ?? "", url: instance?.url || SENTRY_DEFAULT_URL, secret: "" }; } type FieldProps = { form: FormState; baseline: FormState; idPrefix: string; loading: boolean; update: (key: K, value: FormState[K]) => void; }; function NameField({ form, baseline, idPrefix, loading, update }: FieldProps) { const { t } = useTranslation(); return (
update("name", e.target.value)} disabled={loading} />

{t("sentry:nameHelp")}

); } function UrlField({ form, baseline, idPrefix, loading, update }: FieldProps) { const { t } = useTranslation(); return (
update("url", e.target.value)} disabled={loading} /> {/* Both URLs are interpolated rather than written into the message: the pseudo-locale must not transliterate a hostname the user has to type. */}

{t("sentry:instanceUrlHelp", { defaultUrl: SENTRY_DEFAULT_URL, exampleUrl: SELF_HOSTED_EXAMPLE_URL, })}

); } function SecretField({ form, baseline, idPrefix, loading, update, hasSavedSecret, }: FieldProps & { hasSavedSecret: boolean }) { const { t } = useTranslation(); return (

{t("sentry:grantReadAccess")}

    {TOKEN_SCOPES.map(({ scope, descriptionKey }) => (
  • {scope}{" "} {t(descriptionKey)}
  • ))}
update("secret", e.target.value)} disabled={loading} />
); } function TestResultAlert({ result }: { result: TestSentryConnectionResult | null }) { const { t } = useTranslation(); if (!result) return null; return ( {/* The identity and the error text are both server data, interpolated so neither is ever routed through the catalog. */} {result.ok ? t("sentry:connectedAs", { name: result.displayName || result.email || result.userId, }) : t("sentry:testFailed", { error: result.error })} ); } type UseInstanceFormArgs = { workspaceId: string; instance: SentryConfig | null; form: FormState; }; function useInstanceForm({ workspaceId, instance, form }: UseInstanceFormArgs) { const { t } = useTranslation(); const { toast } = useToast(); const [saving, setSaving] = useState(false); const [testing, setTesting] = useState(false); const [testResult, setTestResult] = useState(null); // A saved instance can re-test its stored token only while its URL is // unchanged. Any typed token or URL change is a candidate configuration. const candidateTest = !instance || !!form.secret || form.url !== instance.url; const handleTest = useCallback(async () => { setTesting(true); setTestResult(null); try { const res = candidateTest ? await testSentryConnection(workspaceId, { secret: form.secret || undefined, url: form.url || undefined, authMethod: SENTRY_AUTH_METHOD, }) : await testSentryInstance(workspaceId, instance!.id); setTestResult(res); } catch (err) { setTestResult({ ok: false, error: String(err) }); } finally { setTesting(false); } }, [workspaceId, instance, candidateTest, form.secret, form.url]); const handleSave = useCallback(async () => { setSaving(true); try { const saved = instance ? await updateSentryInstance(workspaceId, instance.id, { name: form.name.trim(), authMethod: SENTRY_AUTH_METHOD, url: form.url, secret: form.secret, }) : await createSentryInstance(workspaceId, { workspaceId, name: form.name.trim(), authMethod: SENTRY_AUTH_METHOD, url: form.url, secret: form.secret, }); toast({ description: t("sentry:instanceSaved"), variant: "success" }); return saved; } catch (err) { // The name is the user's own; the fallback carries the raw server error. // Both are interpolated so neither is translated. const message = sentryErrorCode(err) === SENTRY_ERROR_CODES.nameTaken ? t("sentry:instanceNameTaken", { name: form.name.trim() }) : t("sentry:saveFailed", { error: String(err) }); toast({ description: message, variant: "error" }); throw err; } finally { setSaving(false); } }, [workspaceId, instance, form, toast, t]); return { saving, testing, testResult, candidateTest, handleTest, handleSave }; } type SentryInstanceFormProps = { workspaceId: string; // instance === null creates a new instance; otherwise the form edits it. instance: SentryConfig | null; // idPrefix scopes element ids + testids so the mutually-exclusive add/edit // forms never collide (e.g. "sentry-add" vs "sentry-edit"). idPrefix: string; onSaved: (cfg: SentryConfig) => void; onCancel: () => void; onDirtyChange?: (isDirty: boolean) => void; }; type CoordinatedSaveOptions = { instance: SentryConfig | null; form: FormState; setForm: (form: FormState) => void; handleSave: () => Promise; onSaved: (cfg: SentryConfig) => void; canSave: boolean; }; function useCoordinatedInstanceSave({ instance, form, setForm, handleSave, onSaved, canSave, }: CoordinatedSaveOptions) { const { t } = useTranslation(); const [baseline, setBaseline] = useState(() => instanceToForm(instance)); const revision = JSON.stringify(form); const latestRevision = useRef(revision); latestRevision.current = revision; const isDirty = instance === null || revision !== JSON.stringify(baseline); useSettingsSaveContributor({ id: `sentry-instance:${instance?.id ?? "new"}`, revision, isDirty, canSave, invalidReason: canSave ? undefined : t("sentry:enterNameAndTokenBeforeSaving"), save: async (submittedRevision) => { const submitted = form; const saved = await handleSave(); setBaseline(submitted); if (instance === null || latestRevision.current === submittedRevision) onSaved(saved); }, discard: () => setForm(baseline), }); return { baseline, isDirty }; } type FormActionsProps = { idPrefix: string; testing: boolean; disableTest: boolean; requiresTestSecret: boolean; onTest: () => void; onCancel: () => void; }; function FormActions({ idPrefix, testing, disableTest, requiresTestSecret, onTest, onCancel, }: FormActionsProps) { const { t } = useTranslation(); return (
); } export function SentryInstanceForm({ workspaceId, instance, idPrefix, onSaved, onCancel, onDirtyChange, }: SentryInstanceFormProps) { const { t } = useTranslation(); const [form, setForm] = useState(() => instanceToForm(instance)); const update = useCallback( (key: K, value: FormState[K]) => setForm((prev) => ({ ...prev, [key]: value })), [], ); const { saving, testing, testResult, candidateTest, handleTest, handleSave } = useInstanceForm({ workspaceId, instance, form, }); const hasSavedSecret = !!instance?.hasSecret; const missingSecret = !hasSavedSecret && !form.secret; const requiresTestSecret = !form.secret && (candidateTest || missingSecret); const disableTest = testing || requiresTestSecret; const canSave = Boolean(form.name.trim()) && !missingSecret; const coordinated = useCoordinatedInstanceSave({ instance, form, setForm, handleSave, onSaved, canSave, }); useEffect(() => onDirtyChange?.(coordinated.isDirty), [coordinated.isDirty, onDirtyChange]); useEffect(() => () => onDirtyChange?.(false), [onDirtyChange]); return (
{instance === null && (

{t("sentry:newInstance")}

)}
); }