'use client'; import { useEffect, useState } from 'react'; import { Plus, Trash2, Pencil } from 'lucide-react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; interface Domain { id: string; name: string; color: string; icon: string; sort_order: number; } export function SettingsDomains() { const [domains, setDomains] = useState([]); const [newDomainName, setNewDomainName] = useState(''); const [newDomainColor, setNewDomainColor] = useState('#3b82f6'); const [loading, setLoading] = useState(true); const [creating, setCreating] = useState(false); const [deletingId, setDeletingId] = useState(null); const [domainToDelete, setDomainToDelete] = useState(null); const [error, setError] = useState(null); const [status, setStatus] = useState(null); const [editing, setEditing] = useState(null); const [editName, setEditName] = useState(''); const [editColor, setEditColor] = useState('#3b82f6'); const [saving, setSaving] = useState(false); useEffect(() => { fetchDomains(); }, []); useEffect(() => { if (editing) { setEditName(editing.name); setEditColor(editing.color || '#3b82f6'); } }, [editing]); async function fetchDomains() { setLoading(true); setError(null); try { const response = await fetch('/api/domains?sort=sort_order'); if (!response.ok) throw new Error('Unable to load domains.'); const data = await response.json(); setDomains(data.items || []); } catch (error) { console.error('Failed to fetch domains:', error); setError('Unable to load domains. Please try again.'); } finally { setLoading(false); } } async function addDomain() { if (!newDomainName.trim()) return; setCreating(true); setError(null); setStatus(null); try { const response = await fetch('/api/domains', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: newDomainName, color: newDomainColor, icon: '📁', sort_order: domains.length, }), }); if (!response.ok) throw new Error('Unable to add domain.'); setNewDomainName(''); setStatus('Domain added successfully.'); await fetchDomains(); } catch (error) { console.error('Failed to add domain:', error); setError('Unable to add domain. Please try again.'); } finally { setCreating(false); } } async function deleteDomain() { if (!domainToDelete) return; setDeletingId(domainToDelete.id); setError(null); setStatus(null); try { const response = await fetch('/api/domains/' + domainToDelete.id, { method: 'DELETE' }); if (!response.ok) throw new Error('Unable to delete domain.'); setDomainToDelete(null); setStatus('Domain deleted successfully.'); await fetchDomains(); } catch (error) { console.error('Failed to delete domain:', error); setError('Unable to delete domain. Please try again.'); } finally { setDeletingId(null); } } async function handleEditSave() { if (!editing || !editName.trim()) return; setSaving(true); setError(null); setStatus(null); try { const response = await fetch('/api/domains/' + editing.id, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: editName.trim(), color: editColor, }), }); if (!response.ok) throw new Error('Unable to update domain.'); setEditing(null); setStatus('Domain updated successfully.'); await fetchDomains(); } catch (error) { console.error('Failed to update domain:', error); setError('Unable to update domain. Please try again.'); } finally { setSaving(false); } } return ( Domains Manage your workspace domains (e.g., Personal, Work, OTS). {error && (
{error}
)} {status &&

{status}

} {/* Existing domains */}
{loading ? (

Loading domains...

) : ( domains.map((domain) => (
{domain.name}
)) )}
{/* Add new domain */}
setNewDomainName(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && addDomain()} disabled={creating} className="flex-1" /> setNewDomainColor(e.target.value)} className="h-10 w-10 cursor-pointer rounded border" title="Domain color" disabled={creating} />
{/* Edit Domain Dialog */} !o && setEditing(null)}> Edit domain Update the domain name and color.
setEditName(e.target.value)} autoFocus required />
setEditColor(e.target.value)} className="h-10 w-10 cursor-pointer rounded border" />
!open && setDomainToDelete(null)}> Delete {domainToDelete?.name}? This cannot be undone. Cancel {deletingId ? 'Deleting...' : 'Delete domain'} ); }