refactor(dashboard): centralize access guards and split interactive pages into client components

This commit is contained in:
Yusuf İpek
2026-02-25 16:42:47 +03:00
parent a2b07b3e19
commit 6eea327083
29 changed files with 4859 additions and 4539 deletions
@@ -1,11 +1,17 @@
'use client';
import { useParams } from 'next/navigation';
import { MembersManagementPage } from '@/components/members-management-page';
import { requireWorkspaceAccessOrRedirect } from '@/lib/route-access';
export default function WorkspaceMembersPage() {
const params = useParams();
const workspaceId = params.workspaceId as string;
interface WorkspaceMembersPageProps {
params: Promise<{ workspaceId: string }>;
}
export default async function WorkspaceMembersPage({ params }: WorkspaceMembersPageProps) {
const { workspaceId } = await params;
await requireWorkspaceAccessOrRedirect({
workspaceId,
intent: 'manage',
});
return (
<MembersManagementPage
@@ -15,7 +21,6 @@ export default function WorkspaceMembersPage() {
title="Members"
subtitle="Manage who has access to this workspace and all its projects"
membersDescription="Admins can manage projects and members. Commentators can view and comment only."
forbiddenRedirect="/workspaces"
/>
);
}
@@ -95,7 +95,7 @@ export default async function WorkspacePage({ params, searchParams }: WorkspaceP
const isAdmin = isOwner || membership?.role === 'ADMIN';
if (!isOwner && !isMember) {
redirect('/workspaces');
redirect('/dashboard');
}
const totalPages = Math.ceil(workspace._count.projects / pageSize);
@@ -0,0 +1,181 @@
'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { ArrowLeft, Loader2, Globe, Lock, UserPlus, FolderPlus } 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';
type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC';
const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [
{
value: 'PRIVATE',
label: 'Private',
description: 'Only workspace members and project members can access',
icon: <Lock className="h-5 w-5" />,
},
{
value: 'INVITE',
label: 'Invite Only',
description: 'Share with specific people via email',
icon: <UserPlus className="h-5 w-5" />,
},
{
value: 'PUBLIC',
label: 'Public',
description: 'Anyone with the link can view',
icon: <Globe className="h-5 w-5" />,
},
];
export default function NewWorkspaceProjectPageClient({ workspaceId }: { workspaceId: string }) {
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const [formData, setFormData] = useState({
name: '',
description: '',
visibility: 'PRIVATE' as Visibility,
});
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
setError('');
try {
const response = await fetch('/api/projects', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ...formData, workspaceId }),
});
const data = await response.json();
if (!response.ok) {
setError(data.error || 'Failed to create project');
return;
}
router.push(`/projects/${data.data.id}`);
} catch {
setError('Something went wrong. Please try again.');
} finally {
setIsLoading(false);
}
};
return (
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
<div className="w-full max-w-xl">
<div className="mb-8">
<Link
href={`/workspaces/${workspaceId}`}
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<ArrowLeft className="h-4 w-4 mr-1" />
Back to Workspace
</Link>
</div>
<Card className="border-border/50 shadow-lg">
<CardHeader className="text-center pb-2">
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
<FolderPlus className="h-7 w-7 text-primary" />
</div>
<CardTitle className="text-2xl">Create Project in Workspace</CardTitle>
<CardDescription className="text-base">
This project will be accessible to all workspace members
</CardDescription>
</CardHeader>
<CardContent className="pt-6">
<form onSubmit={handleSubmit} className="space-y-6">
<div className="space-y-2">
<Label htmlFor="name" className="text-sm font-medium">
Project Name
</Label>
<Input
id="name"
placeholder="e.g., Product Launch Video"
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
required
disabled={isLoading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="description" className="text-sm font-medium">
Description{' '}
<span className="text-muted-foreground font-normal">(optional)</span>
</Label>
<Textarea
id="description"
placeholder="What is this project about?"
value={formData.description}
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
rows={3}
disabled={isLoading}
/>
</div>
<div className="space-y-3">
<Label className="text-sm font-medium">Visibility</Label>
<div className="grid gap-2">
{visibilityOptions.map((option) => (
<label
key={option.value}
className={`flex items-start gap-3 rounded-lg border p-3 cursor-pointer transition-colors ${
formData.visibility === option.value
? 'border-primary bg-primary/5'
: 'hover:bg-accent/50'
}`}
>
<input
type="radio"
name="visibility"
value={option.value}
checked={formData.visibility === option.value}
onChange={(e) =>
setFormData({ ...formData, visibility: e.target.value as Visibility })
}
className="sr-only"
/>
<div className="mt-0.5 text-muted-foreground">{option.icon}</div>
<div>
<p className="text-sm font-medium">{option.label}</p>
<p className="text-xs text-muted-foreground">{option.description}</p>
</div>
</label>
))}
</div>
</div>
{error && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
{error}
</div>
)}
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Creating...
</>
) : (
'Create Project'
)}
</Button>
</form>
</CardContent>
</Card>
</div>
</div>
);
}
@@ -1,182 +1,17 @@
'use client';
import { requireWorkspaceAccessOrRedirect } from '@/lib/route-access';
import NewWorkspaceProjectPageClient from './new-workspace-project-page-client';
import { useState, use } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { ArrowLeft, Loader2, Globe, Lock, UserPlus, FolderPlus } 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';
interface NewWorkspaceProjectPageProps {
params: Promise<{ workspaceId: string }>;
}
type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC';
export default async function NewWorkspaceProjectPage({ params }: NewWorkspaceProjectPageProps) {
const { workspaceId } = await params;
const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [
{
value: 'PRIVATE',
label: 'Private',
description: 'Only workspace members and project members can access',
icon: <Lock className="h-5 w-5" />,
},
{
value: 'INVITE',
label: 'Invite Only',
description: 'Share with specific people via email',
icon: <UserPlus className="h-5 w-5" />,
},
{
value: 'PUBLIC',
label: 'Public',
description: 'Anyone with the link can view',
icon: <Globe className="h-5 w-5" />,
},
];
export default function NewWorkspaceProjectPage({ params }: { params: Promise<{ workspaceId: string }> }) {
const { workspaceId } = use(params);
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const [formData, setFormData] = useState({
name: '',
description: '',
visibility: 'PRIVATE' as Visibility,
await requireWorkspaceAccessOrRedirect({
workspaceId,
intent: 'manage',
});
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
setError('');
try {
const response = await fetch('/api/projects', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ...formData, workspaceId }),
});
const data = await response.json();
if (!response.ok) {
setError(data.error || 'Failed to create project');
return;
}
router.push(`/projects/${data.data.id}`);
} catch {
setError('Something went wrong. Please try again.');
} finally {
setIsLoading(false);
}
};
return (
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
<div className="w-full max-w-xl">
<div className="mb-8">
<Link
href={`/workspaces/${workspaceId}`}
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<ArrowLeft className="h-4 w-4 mr-1" />
Back to Workspace
</Link>
</div>
<Card className="border-border/50 shadow-lg">
<CardHeader className="text-center pb-2">
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
<FolderPlus className="h-7 w-7 text-primary" />
</div>
<CardTitle className="text-2xl">Create Project in Workspace</CardTitle>
<CardDescription className="text-base">
This project will be accessible to all workspace members
</CardDescription>
</CardHeader>
<CardContent className="pt-6">
<form onSubmit={handleSubmit} className="space-y-6">
<div className="space-y-2">
<Label htmlFor="name" className="text-sm font-medium">
Project Name
</Label>
<Input
id="name"
placeholder="e.g., Product Launch Video"
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
required
disabled={isLoading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="description" className="text-sm font-medium">
Description{' '}
<span className="text-muted-foreground font-normal">(optional)</span>
</Label>
<Textarea
id="description"
placeholder="What is this project about?"
value={formData.description}
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
rows={3}
disabled={isLoading}
/>
</div>
<div className="space-y-3">
<Label className="text-sm font-medium">Visibility</Label>
<div className="grid gap-2">
{visibilityOptions.map((option) => (
<label
key={option.value}
className={`flex items-start gap-3 rounded-lg border p-3 cursor-pointer transition-colors ${
formData.visibility === option.value
? 'border-primary bg-primary/5'
: 'hover:bg-accent/50'
}`}
>
<input
type="radio"
name="visibility"
value={option.value}
checked={formData.visibility === option.value}
onChange={(e) =>
setFormData({ ...formData, visibility: e.target.value as Visibility })
}
className="sr-only"
/>
<div className="mt-0.5 text-muted-foreground">{option.icon}</div>
<div>
<p className="text-sm font-medium">{option.label}</p>
<p className="text-xs text-muted-foreground">{option.description}</p>
</div>
</label>
))}
</div>
</div>
{error && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
{error}
</div>
)}
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Creating...
</>
) : (
'Create Project'
)}
</Button>
</form>
</CardContent>
</Card>
</div>
</div>
);
return <NewWorkspaceProjectPageClient workspaceId={workspaceId} />;
}
@@ -1,268 +1,17 @@
'use client';
import { requireWorkspaceAccessOrRedirect } from '@/lib/route-access';
import WorkspaceSettingsPageClient from './workspace-settings-page-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;
interface WorkspaceSettingsPageProps {
params: Promise<{ workspaceId: string }>;
}
export default function WorkspaceSettingsPage() {
const params = useParams();
const router = useRouter();
const workspaceId = params.workspaceId as string;
export default async function WorkspaceSettingsPage({ params }: WorkspaceSettingsPageProps) {
const { workspaceId } = await params;
const [workspace, setWorkspace] = useState<WorkspaceData | null>(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('');
await requireWorkspaceAccessOrRedirect({
workspaceId,
intent: 'manage',
});
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 (
<div className="flex items-center justify-center min-h-[50vh]">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
);
}
if (!workspace) return null;
return (
<div className="px-6 lg:px-8 py-8 w-full max-w-2xl mx-auto">
<div className="mb-6">
<Link
href={`/workspaces/${workspaceId}`}
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<ArrowLeft className="h-4 w-4 mr-1" />
Back to Workspace
</Link>
</div>
<div className="mb-8">
<h1 className="text-3xl font-bold tracking-tight">Workspace Settings</h1>
<p className="text-muted-foreground mt-1">
Manage workspace configuration
</p>
</div>
<Card className="mb-8">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Building2 className="h-5 w-5" />
General
</CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={handleSave} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="name">Workspace Name</Label>
<Input
id="name"
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
required
disabled={isSaving}
/>
</div>
<div className="space-y-2">
<Label htmlFor="description">Description</Label>
<Textarea
id="description"
value={formData.description}
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
rows={3}
disabled={isSaving}
/>
</div>
{error && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
{error}
</div>
)}
{success && (
<div className="rounded-md bg-green-500/10 p-3 text-sm text-green-700 dark:text-green-400">
{success}
</div>
)}
<Button type="submit" disabled={isSaving}>
{isSaving ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Saving...
</>
) : (
'Save Changes'
)}
</Button>
</form>
</CardContent>
</Card>
<Separator className="my-8" />
{/* Danger Zone */}
<Card className="border-destructive/50">
<CardHeader>
<CardTitle className="text-destructive">Danger Zone</CardTitle>
<CardDescription>
Irreversible actions. Proceed with caution.
</CardDescription>
</CardHeader>
<CardContent>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="destructive">
<Trash2 className="h-4 w-4 mr-2" />
Delete Workspace
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete &quot;{workspace.name}&quot;?</AlertDialogTitle>
<AlertDialogDescription asChild>
<div className="space-y-4">
<p>
This will permanently delete this workspace and everything inside it
(projects, videos, comments, images, and voice notes). This action cannot be undone.
</p>
<div className="space-y-2">
<Label htmlFor="delete-workspace-confirm">
Type <strong className="text-foreground">{workspace.name}</strong> to confirm
</Label>
<Input
id="delete-workspace-confirm"
value={deleteConfirmation}
onChange={(e) => setDeleteConfirmation(e.target.value)}
placeholder="Workspace name"
className="h-11"
/>
</div>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={() => setDeleteConfirmation('')}>
Cancel
</AlertDialogCancel>
<AlertDialogAction
onClick={handleDelete}
disabled={deleteConfirmation !== workspace.name || isDeleting}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{isDeleting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
Delete Workspace
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</CardContent>
</Card>
</div>
);
return <WorkspaceSettingsPageClient workspaceId={workspaceId} />;
}
@@ -0,0 +1,266 @@
'use client';
import { useState, useEffect, useCallback } from 'react';
import { 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 WorkspaceSettingsPageClient({ workspaceId }: { workspaceId: string }) {
const router = useRouter();
const [workspace, setWorkspace] = useState<WorkspaceData | null>(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('/dashboard');
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 (
<div className="flex items-center justify-center min-h-[50vh]">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
);
}
if (!workspace) return null;
return (
<div className="px-6 lg:px-8 py-8 w-full max-w-2xl mx-auto">
<div className="mb-6">
<Link
href={`/workspaces/${workspaceId}`}
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<ArrowLeft className="h-4 w-4 mr-1" />
Back to Workspace
</Link>
</div>
<div className="mb-8">
<h1 className="text-3xl font-bold tracking-tight">Workspace Settings</h1>
<p className="text-muted-foreground mt-1">
Manage workspace configuration
</p>
</div>
<Card className="mb-8">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Building2 className="h-5 w-5" />
General
</CardTitle>
</CardHeader>
<CardContent>
<form onSubmit={handleSave} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="name">Workspace Name</Label>
<Input
id="name"
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
required
disabled={isSaving}
/>
</div>
<div className="space-y-2">
<Label htmlFor="description">Description</Label>
<Textarea
id="description"
value={formData.description}
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
rows={3}
disabled={isSaving}
/>
</div>
{error && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
{error}
</div>
)}
{success && (
<div className="rounded-md bg-green-500/10 p-3 text-sm text-green-700 dark:text-green-400">
{success}
</div>
)}
<Button type="submit" disabled={isSaving}>
{isSaving ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Saving...
</>
) : (
'Save Changes'
)}
</Button>
</form>
</CardContent>
</Card>
<Separator className="my-8" />
{/* Danger Zone */}
<Card className="border-destructive/50">
<CardHeader>
<CardTitle className="text-destructive">Danger Zone</CardTitle>
<CardDescription>
Irreversible actions. Proceed with caution.
</CardDescription>
</CardHeader>
<CardContent>
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="destructive">
<Trash2 className="h-4 w-4 mr-2" />
Delete Workspace
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete &quot;{workspace.name}&quot;?</AlertDialogTitle>
<AlertDialogDescription asChild>
<div className="space-y-4">
<p>
This will permanently delete this workspace and everything inside it
(projects, videos, comments, images, and voice notes). This action cannot be undone.
</p>
<div className="space-y-2">
<Label htmlFor="delete-workspace-confirm">
Type <strong className="text-foreground">{workspace.name}</strong> to confirm
</Label>
<Input
id="delete-workspace-confirm"
value={deleteConfirmation}
onChange={(e) => setDeleteConfirmation(e.target.value)}
placeholder="Workspace name"
className="h-11"
/>
</div>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={() => setDeleteConfirmation('')}>
Cancel
</AlertDialogCancel>
<AlertDialogAction
onClick={handleDelete}
disabled={deleteConfirmation !== workspace.name || isDeleting}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{isDeleting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
Delete Workspace
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</CardContent>
</Card>
</div>
);
}
@@ -0,0 +1,129 @@
'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { ArrowLeft, Loader2, Building2 } 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';
export default function NewWorkspacePage() {
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const [formData, setFormData] = useState({
name: '',
description: '',
});
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
setError('');
try {
const response = await fetch('/api/workspaces', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData),
});
const data = await response.json();
if (!response.ok) {
setError(data.error || 'Failed to create workspace');
return;
}
router.push(`/workspaces/${data.data.id}`);
} catch {
setError('Something went wrong. Please try again.');
} finally {
setIsLoading(false);
}
};
return (
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
<div className="w-full max-w-xl">
<div className="mb-8">
<Link
href="/workspaces"
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<ArrowLeft className="h-4 w-4 mr-1" />
Back to Workspaces
</Link>
</div>
<Card className="border-border/50 shadow-lg">
<CardHeader className="text-center pb-2">
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
<Building2 className="h-7 w-7 text-primary" />
</div>
<CardTitle className="text-2xl">Create New Workspace</CardTitle>
<CardDescription className="text-base">
Set up a workspace to organize projects and invite your team
</CardDescription>
</CardHeader>
<CardContent className="pt-6">
<form onSubmit={handleSubmit} className="space-y-6">
<div className="space-y-2">
<Label htmlFor="name" className="text-sm font-medium">
Workspace Name
</Label>
<Input
id="name"
placeholder="e.g., My Studio"
value={formData.name}
onChange={(e) =>
setFormData({ ...formData, name: e.target.value })
}
required
disabled={isLoading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="description" className="text-sm font-medium">
Description{' '}
<span className="text-muted-foreground font-normal">(optional)</span>
</Label>
<Textarea
id="description"
placeholder="What is this workspace for?"
value={formData.description}
onChange={(e) =>
setFormData({ ...formData, description: e.target.value })
}
rows={3}
disabled={isLoading}
/>
</div>
{error && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
{error}
</div>
)}
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Creating...
</>
) : (
'Create Workspace'
)}
</Button>
</form>
</CardContent>
</Card>
</div>
</div>
);
}
+5 -127
View File
@@ -1,129 +1,7 @@
'use client';
import { requireAuthOrRedirect } from '@/lib/route-access';
import NewWorkspacePageClient from './new-workspace-page-client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { ArrowLeft, Loader2, Building2 } 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';
export default function NewWorkspacePage() {
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const [formData, setFormData] = useState({
name: '',
description: '',
});
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
setError('');
try {
const response = await fetch('/api/workspaces', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData),
});
const data = await response.json();
if (!response.ok) {
setError(data.error || 'Failed to create workspace');
return;
}
router.push(`/workspaces/${data.data.id}`);
} catch {
setError('Something went wrong. Please try again.');
} finally {
setIsLoading(false);
}
};
return (
<div className="min-h-[calc(100vh-4rem)] flex items-start justify-center py-12 px-4">
<div className="w-full max-w-xl">
<div className="mb-8">
<Link
href="/workspaces"
className="inline-flex items-center text-sm text-muted-foreground hover:text-foreground transition-colors"
>
<ArrowLeft className="h-4 w-4 mr-1" />
Back to Workspaces
</Link>
</div>
<Card className="border-border/50 shadow-lg">
<CardHeader className="text-center pb-2">
<div className="mx-auto w-14 h-14 rounded-full bg-primary/10 flex items-center justify-center mb-4">
<Building2 className="h-7 w-7 text-primary" />
</div>
<CardTitle className="text-2xl">Create New Workspace</CardTitle>
<CardDescription className="text-base">
Set up a workspace to organize projects and invite your team
</CardDescription>
</CardHeader>
<CardContent className="pt-6">
<form onSubmit={handleSubmit} className="space-y-6">
<div className="space-y-2">
<Label htmlFor="name" className="text-sm font-medium">
Workspace Name
</Label>
<Input
id="name"
placeholder="e.g., My Studio"
value={formData.name}
onChange={(e) =>
setFormData({ ...formData, name: e.target.value })
}
required
disabled={isLoading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="description" className="text-sm font-medium">
Description{' '}
<span className="text-muted-foreground font-normal">(optional)</span>
</Label>
<Textarea
id="description"
placeholder="What is this workspace for?"
value={formData.description}
onChange={(e) =>
setFormData({ ...formData, description: e.target.value })
}
rows={3}
disabled={isLoading}
/>
</div>
{error && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
{error}
</div>
)}
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Creating...
</>
) : (
'Create Workspace'
)}
</Button>
</form>
</CardContent>
</Card>
</div>
</div>
);
export default async function NewWorkspacePage() {
await requireAuthOrRedirect();
return <NewWorkspacePageClient />;
}