"use client"; import { useCallback, useEffect, useState } from "react"; import { IconBrandSentry, IconInfoCircle } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Card, CardContent } from "@kandev/ui/card"; 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 { Switch } from "@kandev/ui/switch"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { useToast } from "@/components/toast-provider"; import { SettingsSection } from "@/components/settings/settings-section"; import { useSentryEnabled } from "@/hooks/domains/sentry/use-sentry-enabled"; import { IntegrationAuthStatusBanner, type IntegrationAuthHealth, } from "@/components/integrations/auth-status-banner"; import { INTEGRATION_STATUS_REFRESH_MS } from "@/hooks/domains/integrations/use-integration-availability"; import { fetchSentryConfig, saveSentryConfig, deleteSentryConfig, testSentryConnection, } from "@/lib/api/domains/sentry-api"; import { SENTRY_AUTH_METHOD, type SentryConfig, type TestSentryConnectionResult, } from "@/lib/types/sentry"; import { SentryIssueWatchersSection } from "./sentry-issue-watchers-section"; type FormState = { secret: string; }; const emptyForm: FormState = { secret: "" }; function configToForm(_cfg: SentryConfig | null): FormState { // Only the (write-only) secret is editable here; org/project are chosen // per-watcher and per-browse, not stored install-wide. return { secret: "" }; } function saveLabel(saving: boolean, hasConfig: boolean): string { if (saving) return "Saving..."; return hasConfig ? "Update" : "Save"; } function configToHealth(config: SentryConfig | null): IntegrationAuthHealth | null { if (!config?.hasSecret) return null; if (!config.lastCheckedAt) return { ok: false, error: "", checkedAt: null }; return { ok: !!config.lastOk, error: config.lastError ?? "", checkedAt: new Date(config.lastCheckedAt), }; } type UpdateFn = (key: K, value: FormState[K]) => void; type SecretFieldProps = { form: FormState; loading: boolean; update: UpdateFn; hasSavedSecret: boolean; }; function SecretField({ form, loading, update, hasSavedSecret }: SecretFieldProps) { return (

Grant Read access to these scopes:

  • org:read{" "} Organization — resolve the org and list issues
  • project:read{" "} Project — list projects and scope searches
  • event:read{" "} Issue & Event — browse issues and run watchers
update("secret", e.target.value)} disabled={loading} />

Create a new personal token at{" "} sentry.io → Settings → Auth Tokens

); } function TestResultAlert({ result }: { result: TestSentryConnectionResult | null }) { if (!result) return null; return ( {result.ok ? `Connected as ${result.displayName || result.email || result.userId}` : `Failed: ${result.error}`} ); } type ActionBarProps = { saving: boolean; testing: boolean; loading: boolean; hasConfig: boolean; disableSave: boolean; disableTest: boolean; onTest: () => void; onSave: () => void; onDelete: () => void; }; function ActionBar({ saving, testing, loading, hasConfig, disableSave, disableTest, onTest, onSave, onDelete, }: ActionBarProps) { return (
{hasConfig && ( )}
); } type SettingsActionsArgs = { form: FormState; setConfig: (cfg: SentryConfig | null) => void; setForm: (form: FormState) => void; setTestResult: (r: TestSentryConnectionResult | null) => void; }; function useSettingsActions({ form, setConfig, setForm, setTestResult }: SettingsActionsArgs) { const { toast } = useToast(); const [saving, setSaving] = useState(false); const [testing, setTesting] = useState(false); const handleTest = useCallback(async () => { setTesting(true); setTestResult(null); try { const res = await testSentryConnection(form.secret || undefined); setTestResult(res); } catch (err) { setTestResult({ ok: false, error: String(err) }); } finally { setTesting(false); } }, [form, setTestResult]); const handleSave = useCallback(async () => { setSaving(true); try { const saved = await saveSentryConfig({ authMethod: SENTRY_AUTH_METHOD, secret: form.secret, }); setConfig(saved); setForm(configToForm(saved)); setTestResult(null); toast({ description: "Sentry configuration saved", variant: "success" }); } catch (err) { toast({ description: `Save failed: ${String(err)}`, variant: "error" }); } finally { setSaving(false); } }, [form, toast, setConfig, setForm, setTestResult]); const handleDelete = useCallback(async () => { if (!confirm("Remove Sentry configuration?")) return; try { await deleteSentryConfig(); setConfig(null); setForm(emptyForm); setTestResult(null); toast({ description: "Sentry configuration removed", variant: "success" }); } catch (err) { toast({ description: `Delete failed: ${String(err)}`, variant: "error" }); } }, [toast, setConfig, setForm, setTestResult]); return { saving, testing, handleTest, handleSave, handleDelete }; } function useSentrySettings() { const { toast } = useToast(); const [config, setConfig] = useState(null); const [form, setForm] = useState(emptyForm); const [loading, setLoading] = useState(true); const [testResult, setTestResult] = useState(null); const health = configToHealth(config); const load = useCallback(async () => { setLoading(true); try { const cfg = (await fetchSentryConfig()) ?? null; setConfig(cfg); setForm(configToForm(cfg)); } catch (err) { toast({ description: `Failed to load Sentry config: ${String(err)}`, variant: "error" }); } finally { setLoading(false); } }, [toast]); useEffect(() => { void load(); }, [load]); useEffect(() => { const id = setInterval(() => { fetchSentryConfig() .then((cfg) => setConfig(cfg ?? null)) .catch(() => { /* transient failures are fine — next tick retries */ }); }, INTEGRATION_STATUS_REFRESH_MS); return () => clearInterval(id); }, []); const update = useCallback( (key: K, value: FormState[K]) => setForm((prev) => ({ ...prev, [key]: value })), [], ); const { saving, testing, handleTest, handleSave, handleDelete } = useSettingsActions({ form, setConfig, setForm, setTestResult, }); return { config, form, loading, saving, testing, testResult, health, update, handleTest, handleSave, handleDelete, }; } function EnabledPill() { const { enabled, setEnabled } = useSentryEnabled(); return (
); } export function SentryConnectionSection() { const s = useSentrySettings(); const missingSecret = !s.config?.hasSecret && !s.form.secret; const disableSave = s.saving || missingSecret; const disableTest = missingSecret; return ( } title="Sentry integration" description="Connect Kandev to Sentry with a user auth token. Credentials are stored encrypted server-side and shared across all workspaces." action={} > ); } export function SentryIntegrationPage() { return (
); }