The single-config Nextcloud Contacts sync tab is gone — the unified Nextcloud tab is the UI path for connecting an account. POST /api/contacts/sync is untouched (Contact rows still feed contact-linking; the handler is stateless, so no persisted config orphans). Legacy ?tab=nextcloud deep links map to the unified tab on first render and clean the URL; CardDAV/help wording follows. Regression asserts the tab is gone and the legacy deep link lands on the Nextcloud form.
261 lines
14 KiB
TypeScript
261 lines
14 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect, useCallback, Fragment } from 'react'
|
|
import { useSearchParams, useRouter } from 'next/navigation'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'
|
|
import { User, Mail, Server, CloudCog, Shield, Bell, Bot, KeyRound, PlugZap, FolderSync, CalendarDays, BookUser, ArrowLeft, Check, X, AlertTriangle } from 'lucide-react'
|
|
import { toast } from 'sonner'
|
|
import { NotificationsSettings } from '@/components/notifications-settings'
|
|
import { ProfileSettings } from '@/components/profile-settings'
|
|
import { AiSettings } from '@/components/ai-settings'
|
|
import { ApiKeysSettings } from '@/components/api-keys-settings'
|
|
import { McpSettings } from '@/components/mcp-settings'
|
|
import { WebdavSettings } from '@/components/webdav-settings'
|
|
import { CalDavSettings } from '@/components/caldav-settings'
|
|
import { CardDavSettings } from '@/components/carddav-settings'
|
|
import { NextcloudSettings } from '@/components/nextcloud-settings'
|
|
import type { OidcStatus } from '@/lib/oidc-config'
|
|
import { cn } from '@/lib/utils'
|
|
import { useMediaQuery } from '@/lib/use-media-query'
|
|
|
|
|
|
// Settings are grouped by purpose, not a flat strip: account, the external
|
|
// services that feed your data, the assistant/automation surface, and
|
|
// machine/developer access. Each item keeps its Radix tab value so deep
|
|
// links and the browser suites' role="tab" locators are unchanged.
|
|
const NAV_GROUPS = [
|
|
{ label: 'Account', items: [
|
|
{ value: 'profile', label: 'Profile', icon: User },
|
|
{ value: 'oidc', label: 'Sign-in', icon: Shield },
|
|
] },
|
|
{ label: 'Connections', items: [
|
|
{ value: 'nextcloud-connect', label: 'Nextcloud', icon: CloudCog },
|
|
{ value: 'webdav', label: 'WebDAV', icon: FolderSync },
|
|
{ value: 'caldav', label: 'CalDAV', icon: CalendarDays },
|
|
{ value: 'carddav', label: 'CardDAV', icon: BookUser },
|
|
{ value: 'imap', label: 'IMAP', icon: Mail },
|
|
] },
|
|
{ label: 'AI & Automation', items: [
|
|
{ value: 'ai', label: 'AI', icon: Bot },
|
|
{ value: 'mcp', label: 'MCP', icon: PlugZap },
|
|
{ value: 'notifications', label: 'Notifications', icon: Bell },
|
|
] },
|
|
{ label: 'Developer', items: [
|
|
{ value: 'api-keys', label: 'API Keys', icon: KeyRound },
|
|
] },
|
|
]
|
|
|
|
export function SettingsClient({ oidc, callbackUrl }: { oidc: OidcStatus; callbackUrl: string }) {
|
|
// ?tab=<value> deep links (email footer "Email preferences" lands on
|
|
// notifications). Unknown values fall back to profile; the Radix tab
|
|
// values stay the canonical section ids.
|
|
const requestedTab = useSearchParams().get('tab') ?? ''
|
|
const router = useRouter()
|
|
// Automations moved out of Settings to its own workspace; legacy deep
|
|
// links (?tab=automations) redirect there instead of falling back to profile.
|
|
// The Contacts sync tab was removed — its legacy ?tab=nextcloud deep link
|
|
// lands on the unified Nextcloud tab (the sync API itself remains).
|
|
useEffect(() => {
|
|
if (requestedTab === 'automations') router.replace('/automations')
|
|
if (requestedTab === 'nextcloud') router.replace('/settings?tab=nextcloud-connect')
|
|
}, [requestedTab, router])
|
|
// The Contacts sync tab was removed — its legacy ?tab=nextcloud deep link
|
|
// lands on the unified Nextcloud tab (the sync API itself remains). Map the
|
|
// legacy value BEFORE deriving the initial section so the tab is selected
|
|
// on first render; the effect above only cleans the URL.
|
|
const mappedTab = requestedTab === 'nextcloud' ? 'nextcloud-connect' : requestedTab
|
|
const initialSection = NAV_GROUPS.some(g => g.items.some(i => i.value === mappedTab))
|
|
? mappedTab
|
|
: 'profile'
|
|
const [imapConfig, setImapConfig] = useState({ host: '', port: 993, tls: true, username: '', password: '', folder: 'INBOX' })
|
|
const [savingImap, setSavingImap] = useState(false)
|
|
const [testingImap, setTestingImap] = useState(false)
|
|
const [section, setSection] = useState(initialSection)
|
|
// Below md the grouped nav becomes a list -> detail flow (same
|
|
// master-detail pattern as collections/tags/contacts); at md+ the
|
|
// nav and the selected panel sit side by side. A deep link opens the
|
|
// detail pane directly on narrow screens.
|
|
const isNarrow = useMediaQuery('(max-width: 767px)')
|
|
const [mobileDetail, setMobileDetail] = useState(initialSection !== 'profile')
|
|
|
|
useEffect(() => {
|
|
fetch('/api/settings/imap').then(r => r.json()).then(data => {
|
|
if (data) setImapConfig(prev => ({ ...prev, host: data.host ?? '', port: data.port ?? 993, tls: data.tls ?? true, username: data.username ?? '', folder: data.folder ?? 'INBOX' }))
|
|
}).catch(() => {})
|
|
}, [])
|
|
|
|
// Every action clears its spinner in finally and tolerates non-JSON or
|
|
// failed network responses, so no state can stick after an outage.
|
|
const saveImap = async () => {
|
|
setSavingImap(true)
|
|
try {
|
|
const res = await fetch('/api/settings/imap', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
// tls is forced on: legacy stored configs with tls=false migrate to
|
|
// verified TLS on the next save (review NET-02).
|
|
body: JSON.stringify({ ...imapConfig, tls: true }),
|
|
})
|
|
if (res.ok) toast.success('IMAP settings saved')
|
|
else toast.error((await res.json().catch(() => null))?.error ?? 'Failed to save')
|
|
} catch { toast.error('Failed to save') }
|
|
finally { setSavingImap(false) }
|
|
}
|
|
|
|
const testImap = async () => {
|
|
setTestingImap(true)
|
|
try {
|
|
const res = await fetch('/api/imap/test', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ ...imapConfig, tls: true }),
|
|
})
|
|
const data = await res.json().catch(() => null)
|
|
if (res.ok && data?.ok) toast.success('Connection successful!')
|
|
else toast.error(data?.error ?? 'Connection failed')
|
|
} catch { toast.error('Connection failed') }
|
|
finally { setTestingImap(false) }
|
|
}
|
|
|
|
return (
|
|
<div className="max-w-5xl space-y-6">
|
|
<div>
|
|
<h1 className="font-display text-2xl font-bold tracking-tight">Settings</h1>
|
|
<p className="text-sm text-muted-foreground mt-1">Configure your integrations and preferences</p>
|
|
</div>
|
|
|
|
<Tabs
|
|
value={section}
|
|
onValueChange={(v) => { setSection(v); if (isNarrow) setMobileDetail(true) }}
|
|
orientation="vertical"
|
|
className="w-full"
|
|
>
|
|
<div className="flex flex-col gap-6 md:flex-row md:gap-8">
|
|
<TabsList
|
|
aria-label="Settings sections"
|
|
className={cn(
|
|
'flex h-auto w-full flex-col items-stretch justify-start gap-0.5 rounded-lg border bg-card/50 p-2 md:w-56 md:shrink-0',
|
|
isNarrow && mobileDetail && 'hidden'
|
|
)}
|
|
>
|
|
{NAV_GROUPS.map((g) => (
|
|
<Fragment key={g.label}>
|
|
<div className="px-3 pb-1 pt-3 text-[11px] font-semibold uppercase tracking-wider text-muted-foreground first:pt-1">{g.label}</div>
|
|
{g.items.map((it) => (
|
|
<TabsTrigger
|
|
key={it.value}
|
|
value={it.value}
|
|
className="w-full justify-start gap-2 rounded-md px-3 py-2 data-[state=active]:bg-accent data-[state=active]:text-accent-foreground data-[state=active]:shadow-none"
|
|
>
|
|
<it.icon className="h-3.5 w-3.5 shrink-0" /> {it.label}
|
|
</TabsTrigger>
|
|
))}
|
|
</Fragment>
|
|
))}
|
|
</TabsList>
|
|
|
|
<div className={cn('min-w-0 flex-1', isNarrow && !mobileDetail && 'hidden')}>
|
|
{isNarrow && mobileDetail && (
|
|
<Button variant="ghost" size="sm" className="mb-3 -ml-2 gap-1 text-muted-foreground" onClick={() => setMobileDetail(false)}>
|
|
<ArrowLeft className="h-4 w-4" /> All settings
|
|
</Button>
|
|
)}
|
|
<TabsContent value="profile" className="mt-0 space-y-4">
|
|
<ProfileSettings />
|
|
</TabsContent>
|
|
<TabsContent value="oidc" className="mt-0 space-y-4">
|
|
<div className="rounded-lg border p-4 space-y-3">
|
|
<h3 className="font-medium flex items-center gap-2"><Shield className="h-4 w-4" /> Sign-in (OIDC)</h3>
|
|
<p className="text-sm text-muted-foreground">Authelia single sign-on, configured via environment variables (read-only)</p>
|
|
<div className={`flex items-start gap-2 rounded-md p-3 text-sm ${oidc.enabled ? 'bg-green-500/10 text-green-700 dark:text-green-400' : 'bg-amber-500/10 text-amber-800 dark:text-amber-300'}`}>
|
|
{oidc.enabled ? <Check className="h-4 w-4 mt-0.5 shrink-0" /> : <AlertTriangle className="h-4 w-4 mt-0.5 shrink-0" />}
|
|
<span>
|
|
{oidc.enabled
|
|
? <>Configured for <span className="font-mono break-all">{oidc.issuer}</span>. Authelia sign-in is enabled.</>
|
|
: <>Not connected. Authelia sign-in is disabled until all three variables below hold real values. Users can sign in with email and password meanwhile. Setup guide: <span className="font-mono">docs/AUTHELIA.md</span></>}
|
|
</span>
|
|
</div>
|
|
<div className="space-y-2">
|
|
{([['OIDC_ISSUER', oidc.issuerSet], ['OIDC_CLIENT_ID', oidc.clientIdSet], ['OIDC_CLIENT_SECRET', oidc.clientSecretSet]] as const).map(([name, set]) => (
|
|
<div key={name} className="flex items-center gap-2">
|
|
<span className="text-xs font-mono bg-muted px-2 py-1 rounded">{name}</span>
|
|
<span className="text-sm text-muted-foreground">{set ? 'Set' : 'Not set'}</span>
|
|
{set ? <Check className="h-4 w-4 text-green-500" /> : <X className="h-4 w-4 text-muted-foreground" />}
|
|
</div>
|
|
))}
|
|
</div>
|
|
<div className="space-y-1 pt-2 border-t">
|
|
<p className="text-xs font-medium text-muted-foreground">Redirect URI to register in Authelia</p>
|
|
<p className="text-xs font-mono bg-muted px-2 py-1.5 rounded break-all" suppressHydrationWarning>{callbackUrl}</p>
|
|
</div>
|
|
</div>
|
|
</TabsContent>
|
|
<TabsContent value="nextcloud-connect" className="mt-0 space-y-4">
|
|
<NextcloudSettings onManage={(tab) => { setSection(tab); if (isNarrow) setMobileDetail(true) }} />
|
|
</TabsContent>
|
|
<TabsContent value="webdav" className="mt-0 space-y-4">
|
|
<WebdavSettings />
|
|
</TabsContent>
|
|
<TabsContent value="caldav" className="mt-0 space-y-4">
|
|
<CalDavSettings />
|
|
</TabsContent>
|
|
<TabsContent value="carddav" className="mt-0 space-y-4">
|
|
<CardDavSettings />
|
|
</TabsContent>
|
|
<TabsContent value="imap" className="mt-0 space-y-4">
|
|
<div className="rounded-lg border p-4 space-y-3">
|
|
<h3 className="font-medium flex items-center gap-2"><Server className="h-4 w-4" /> IMAP Configuration</h3>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div>
|
|
<label className="text-xs font-medium text-muted-foreground">Host</label>
|
|
<Input value={imapConfig.host} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setImapConfig(p => ({ ...p, host: e.target.value }))} placeholder="imap.example.com" />
|
|
</div>
|
|
<div>
|
|
<label className="text-xs font-medium text-muted-foreground">Port</label>
|
|
<Input type="number" value={imapConfig.port} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setImapConfig(p => ({ ...p, port: parseInt(e.target.value) || 993 }))} />
|
|
</div>
|
|
<div>
|
|
<label className="text-xs font-medium text-muted-foreground">Username</label>
|
|
<Input value={imapConfig.username} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setImapConfig(p => ({ ...p, username: e.target.value }))} />
|
|
</div>
|
|
<div>
|
|
<label className="text-xs font-medium text-muted-foreground">Password</label>
|
|
<Input type="password" value={imapConfig.password} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setImapConfig(p => ({ ...p, password: e.target.value }))} />
|
|
</div>
|
|
<div>
|
|
<label className="text-xs font-medium text-muted-foreground">Folder</label>
|
|
<Input value={imapConfig.folder} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setImapConfig(p => ({ ...p, folder: e.target.value }))} />
|
|
</div>
|
|
<div className="flex items-end">
|
|
{/* Review NET-02: mailbox logins always run over verified TLS;
|
|
the backend rejects insecure settings, so there is no toggle. */}
|
|
<p className="text-sm text-muted-foreground">TLS is required (implicit TLS, port 993 by default)</p>
|
|
</div>
|
|
</div>
|
|
<div className="flex gap-2">
|
|
<Button onClick={testImap} variant="outline" loading={testingImap}>Test Connection</Button>
|
|
<Button onClick={saveImap} loading={savingImap}>Save IMAP Settings</Button>
|
|
</div>
|
|
</div>
|
|
</TabsContent>
|
|
<TabsContent value="ai" className="mt-0 space-y-4">
|
|
<AiSettings />
|
|
</TabsContent>
|
|
<TabsContent value="mcp" className="mt-0 space-y-4">
|
|
<McpSettings />
|
|
</TabsContent>
|
|
<TabsContent value="notifications" className="mt-0 space-y-4">
|
|
<NotificationsSettings />
|
|
</TabsContent>
|
|
<TabsContent value="api-keys" className="mt-0 space-y-4">
|
|
<ApiKeysSettings />
|
|
</TabsContent>
|
|
</div>
|
|
</div>
|
|
</Tabs>
|
|
</div>
|
|
)
|
|
}
|