'use client'; import { useState, useEffect, useCallback } from 'react'; import { useParams, useRouter } from 'next/navigation'; import Link from 'next/link'; import { ArrowLeft, Loader2, Building2, Trash2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Separator } from '@/components/ui/separator'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from '@/components/ui/alert-dialog'; interface WorkspaceData { id: string; name: string; description: string | null; slug: string; ownerId: string; } export default function WorkspaceSettingsPage() { const params = useParams(); const router = useRouter(); const workspaceId = params.workspaceId as string; const [workspace, setWorkspace] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const [formData, setFormData] = useState({ name: '', description: '' }); const [deleteConfirmation, setDeleteConfirmation] = useState(''); const fetchWorkspace = useCallback(async () => { try { const res = await fetch(`/api/workspaces/${workspaceId}`); if (!res.ok) { router.push('/workspaces'); return; } const data = await res.json(); const workspace = data.data; setWorkspace(workspace); setFormData({ name: workspace.name, description: workspace.description || '', }); } catch { setError('Failed to load workspace'); } finally { setIsLoading(false); } }, [workspaceId, router]); useEffect(() => { fetchWorkspace(); }, [fetchWorkspace]); const handleSave = async (e: React.FormEvent) => { e.preventDefault(); setIsSaving(true); setError(''); setSuccess(''); try { const res = await fetch(`/api/workspaces/${workspaceId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData), }); if (!res.ok) { const data = await res.json(); setError(data.error || 'Failed to update workspace'); return; } setSuccess('Workspace updated successfully'); } catch { setError('Something went wrong'); } finally { setIsSaving(false); } }; const handleDelete = async () => { if (!workspace) return; if (deleteConfirmation !== workspace.name) return; setIsDeleting(true); try { const res = await fetch(`/api/workspaces/${workspaceId}`, { method: 'DELETE', }); if (!res.ok) { const data = await res.json(); setError(data.error || 'Failed to delete workspace'); return; } router.push('/workspaces'); } catch { setError('Failed to delete workspace'); } finally { setIsDeleting(false); } }; if (isLoading) { return (
); } if (!workspace) return null; return (
Back to Workspace

Workspace Settings

Manage workspace configuration

General
setFormData({ ...formData, name: e.target.value })} required disabled={isSaving} />