Initial commit
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { updateUser } from "@/actions/users";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
import { Eye, EyeOff, ShieldAlert } from "lucide-react";
|
||||
|
||||
export interface EditableUser {
|
||||
id: string;
|
||||
name: string | null;
|
||||
email: string;
|
||||
role: string;
|
||||
isActive: boolean;
|
||||
}
|
||||
|
||||
interface EditUserDialogProps {
|
||||
user: EditableUser;
|
||||
isSelf: boolean;
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
onSuccess?: () => void;
|
||||
}
|
||||
|
||||
export function EditUserDialog({ user, isSelf, open, onClose, onSuccess }: EditUserDialogProps) {
|
||||
const { toast } = useToast();
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [form, setForm] = useState({
|
||||
name: user.name ?? "",
|
||||
role: user.role,
|
||||
isActive: user.isActive,
|
||||
newPassword: "",
|
||||
});
|
||||
const [passwordError, setPasswordError] = useState("");
|
||||
|
||||
const set = <K extends keyof typeof form>(key: K, value: typeof form[K]) => {
|
||||
setForm((f) => ({ ...f, [key]: value }));
|
||||
if (key === "newPassword") setPasswordError("");
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (form.newPassword && form.newPassword.length < 8) {
|
||||
setPasswordError("At least 8 characters");
|
||||
return;
|
||||
}
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
await updateUser({
|
||||
userId: user.id,
|
||||
name: form.name || undefined,
|
||||
role: form.role as "ADMIN" | "PRODUCER" | "SUPERVISOR" | "ARTIST" | "CLIENT",
|
||||
isActive: form.isActive,
|
||||
newPassword: form.newPassword || undefined,
|
||||
});
|
||||
toast({ title: "User updated" });
|
||||
onSuccess?.();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
toast({ title: "Failed to update user", description: err instanceof Error ? err.message : undefined, variant: "destructive" });
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Edit User</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4 py-2">
|
||||
{/* Read-only email */}
|
||||
<div className="space-y-1.5">
|
||||
<Label className="text-zinc-400">Email</Label>
|
||||
<p className="text-sm text-zinc-300 bg-zinc-900 border border-zinc-800 rounded-md px-3 py-2">{user.email}</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Name <span className="text-zinc-500 font-normal">(optional)</span></Label>
|
||||
<Input value={form.name} onChange={(e) => set("name", e.target.value)} placeholder="Jane Smith" />
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label>Role</Label>
|
||||
<Select value={form.role} onValueChange={(v) => set("role", v)} disabled={isSelf}>
|
||||
<SelectTrigger><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="ADMIN">Admin</SelectItem>
|
||||
<SelectItem value="PRODUCER">Producer</SelectItem>
|
||||
<SelectItem value="SUPERVISOR">Supervisor</SelectItem>
|
||||
<SelectItem value="ARTIST">Artist</SelectItem>
|
||||
<SelectItem value="CLIENT">Client</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Status</Label>
|
||||
<Select
|
||||
value={form.isActive ? "active" : "inactive"}
|
||||
onValueChange={(v) => set("isActive", v === "active")}
|
||||
disabled={isSelf}
|
||||
>
|
||||
<SelectTrigger><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="active">Active</SelectItem>
|
||||
<SelectItem value="inactive">Inactive</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{isSelf && (
|
||||
<p className="flex items-center gap-1.5 text-xs text-amber-400 bg-amber-500/10 border border-amber-500/20 rounded-md px-3 py-2">
|
||||
<ShieldAlert className="h-3.5 w-3.5 shrink-0" />
|
||||
You cannot change your own role or deactivate yourself.
|
||||
</p>
|
||||
)}
|
||||
|
||||
<Separator />
|
||||
|
||||
{/* Password reset */}
|
||||
<div className="space-y-1.5">
|
||||
<Label>New Password <span className="text-zinc-500 font-normal">(leave blank to keep current)</span></Label>
|
||||
<div className="relative">
|
||||
<Input
|
||||
type={showPassword ? "text" : "password"}
|
||||
value={form.newPassword}
|
||||
onChange={(e) => set("newPassword", e.target.value)}
|
||||
placeholder="Min. 8 characters"
|
||||
autoComplete="new-password"
|
||||
className="pr-10"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowPassword((s) => !s)}
|
||||
className="absolute right-3 top-1/2 -translate-y-1/2 text-zinc-400 hover:text-zinc-200"
|
||||
tabIndex={-1}
|
||||
>
|
||||
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
||||
</button>
|
||||
</div>
|
||||
{passwordError && <p className="text-xs text-destructive">{passwordError}</p>}
|
||||
</div>
|
||||
|
||||
<DialogFooter className="pt-2">
|
||||
<Button type="button" variant="outline" onClick={onClose}>Cancel</Button>
|
||||
<Button type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting ? "Saving…" : "Save Changes"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { createUser } from "@/actions/users";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
import { Eye, EyeOff } from "lucide-react";
|
||||
|
||||
interface NewUserDialogProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
onSuccess?: () => void;
|
||||
}
|
||||
|
||||
export function NewUserDialog({ open, onClose, onSuccess }: NewUserDialogProps) {
|
||||
const { toast } = useToast();
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [form, setForm] = useState({
|
||||
name: "",
|
||||
email: "",
|
||||
password: "",
|
||||
role: "ARTIST",
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const validate = () => {
|
||||
const e: Record<string, string> = {};
|
||||
if (!form.email.trim()) e.email = "Email is required";
|
||||
else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email)) e.email = "Invalid email";
|
||||
if (!form.password) e.password = "Password is required";
|
||||
else if (form.password.length < 8) e.password = "At least 8 characters";
|
||||
return e;
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
const errs = validate();
|
||||
if (Object.keys(errs).length) { setErrors(errs); return; }
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
await createUser({
|
||||
name: form.name || undefined,
|
||||
email: form.email.trim().toLowerCase(),
|
||||
password: form.password,
|
||||
role: form.role as "ADMIN" | "PRODUCER" | "SUPERVISOR" | "ARTIST" | "CLIENT",
|
||||
});
|
||||
toast({ title: "User created" });
|
||||
setForm({ name: "", email: "", password: "", role: "ARTIST" });
|
||||
setErrors({});
|
||||
onSuccess?.();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
toast({ title: "Failed to create user", description: err instanceof Error ? err.message : undefined, variant: "destructive" });
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const set = (key: string, value: string) => {
|
||||
setForm((f) => ({ ...f, [key]: value }));
|
||||
setErrors((e) => { const n = { ...e }; delete n[key]; return n; });
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>New User</DialogTitle>
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit} className="space-y-4 py-2">
|
||||
<div className="space-y-1.5">
|
||||
<Label>Name <span className="text-zinc-500 font-normal">(optional)</span></Label>
|
||||
<Input value={form.name} onChange={(e) => set("name", e.target.value)} placeholder="Jane Smith" />
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Email <span className="text-red-400">*</span></Label>
|
||||
<Input
|
||||
type="email"
|
||||
value={form.email}
|
||||
onChange={(e) => set("email", e.target.value)}
|
||||
placeholder="jane@studio.com"
|
||||
autoComplete="off"
|
||||
/>
|
||||
{errors.email && <p className="text-xs text-destructive">{errors.email}</p>}
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Password <span className="text-red-400">*</span></Label>
|
||||
<div className="relative">
|
||||
<Input
|
||||
type={showPassword ? "text" : "password"}
|
||||
value={form.password}
|
||||
onChange={(e) => set("password", e.target.value)}
|
||||
placeholder="Min. 8 characters"
|
||||
autoComplete="new-password"
|
||||
className="pr-10"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowPassword((s) => !s)}
|
||||
className="absolute right-3 top-1/2 -translate-y-1/2 text-zinc-400 hover:text-zinc-200"
|
||||
tabIndex={-1}
|
||||
>
|
||||
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
||||
</button>
|
||||
</div>
|
||||
{errors.password && <p className="text-xs text-destructive">{errors.password}</p>}
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Role</Label>
|
||||
<Select value={form.role} onValueChange={(v) => set("role", v)}>
|
||||
<SelectTrigger><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="ADMIN">Admin</SelectItem>
|
||||
<SelectItem value="PRODUCER">Producer</SelectItem>
|
||||
<SelectItem value="SUPERVISOR">Supervisor</SelectItem>
|
||||
<SelectItem value="ARTIST">Artist</SelectItem>
|
||||
<SelectItem value="CLIENT">Client</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<DialogFooter className="pt-2">
|
||||
<Button type="button" variant="outline" onClick={onClose}>Cancel</Button>
|
||||
<Button type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting ? "Creating…" : "Create User"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { NewUserDialog } from "./NewUserDialog";
|
||||
import { EditUserDialog, type EditableUser } from "./EditUserDialog";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { PlusCircle, Pencil, ShieldCheck, Shield, Eye, Palette, User2, Trash2 } from "lucide-react";
|
||||
import { deleteUser } from "@/actions/users";
|
||||
|
||||
const ROLE_CONFIG: Record<string, { label: string; className: string; Icon: React.ElementType }> = {
|
||||
ADMIN: { label: "Admin", className: "bg-red-500/10 text-red-400 border border-red-500/20", Icon: ShieldCheck },
|
||||
PRODUCER: { label: "Producer", className: "bg-blue-500/10 text-blue-400 border border-blue-500/20", Icon: Shield },
|
||||
SUPERVISOR: { label: "Supervisor", className: "bg-violet-500/10 text-violet-400 border border-violet-500/20", Icon: Eye },
|
||||
ARTIST: { label: "Artist", className: "bg-emerald-500/10 text-emerald-400 border border-emerald-500/20", Icon: Palette },
|
||||
CLIENT: { label: "Client", className: "bg-zinc-500/10 text-zinc-400 border border-zinc-500/20", Icon: User2 },
|
||||
};
|
||||
|
||||
function getInitials(name: string | null, email: string) {
|
||||
const src = name ?? email;
|
||||
return src.slice(0, 2).toUpperCase();
|
||||
}
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
name: string | null;
|
||||
email: string;
|
||||
role: string;
|
||||
isActive: boolean;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
interface UsersClientProps {
|
||||
users: User[];
|
||||
currentUserId: string;
|
||||
}
|
||||
|
||||
export function UsersClient({ users, currentUserId }: UsersClientProps) {
|
||||
const router = useRouter();
|
||||
const [showNew, setShowNew] = useState(false);
|
||||
const [editTarget, setEditTarget] = useState<EditableUser | null>(null);
|
||||
const [confirmDeleteId, setConfirmDeleteId] = useState<string | null>(null);
|
||||
const [deleting, setDeleting] = useState(false);
|
||||
|
||||
const handleSuccess = () => {
|
||||
router.refresh();
|
||||
};
|
||||
|
||||
async function handleDelete(userId: string) {
|
||||
setDeleting(true);
|
||||
try {
|
||||
await deleteUser(userId);
|
||||
router.refresh();
|
||||
} finally {
|
||||
setDeleting(false);
|
||||
setConfirmDeleteId(null);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<p className="text-sm text-zinc-400">
|
||||
{users.length} user{users.length !== 1 ? "s" : ""}
|
||||
</p>
|
||||
<Button className="gap-2" onClick={() => setShowNew(true)}>
|
||||
<PlusCircle className="h-4 w-4" />
|
||||
New User
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Table */}
|
||||
<div className="rounded-xl border border-zinc-800 overflow-hidden">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-zinc-900 border-b border-zinc-800">
|
||||
<tr>
|
||||
<th className="px-4 py-3 text-left font-medium text-zinc-400">User</th>
|
||||
<th className="px-4 py-3 text-left font-medium text-zinc-400">Role</th>
|
||||
<th className="px-4 py-3 text-left font-medium text-zinc-400">Status</th>
|
||||
<th className="px-4 py-3 text-right font-medium text-zinc-400"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-zinc-800">
|
||||
{users.map((user) => {
|
||||
const role = ROLE_CONFIG[user.role] ?? ROLE_CONFIG.ARTIST;
|
||||
const RoleIcon = role.Icon;
|
||||
const isSelf = user.id === currentUserId;
|
||||
return (
|
||||
<tr key={user.id} className={cn("bg-zinc-950 hover:bg-zinc-900/60 transition-colors", !user.isActive && "opacity-50")}>
|
||||
<td className="px-4 py-3">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="h-8 w-8 rounded-full bg-zinc-800 border border-zinc-700 flex items-center justify-center shrink-0 text-xs font-semibold text-zinc-300">
|
||||
{getInitials(user.name, user.email)}
|
||||
</div>
|
||||
<div>
|
||||
<p className="font-medium text-white">
|
||||
{user.name ?? <span className="text-zinc-500 italic">No name</span>}
|
||||
{isSelf && <span className="ml-2 text-xs text-amber-400 font-normal">(you)</span>}
|
||||
</p>
|
||||
<p className="text-xs text-zinc-500">{user.email}</p>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<span className={cn("inline-flex items-center gap-1.5 px-2 py-0.5 rounded-full text-xs font-medium", role.className)}>
|
||||
<RoleIcon className="h-3 w-3" />
|
||||
{role.label}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<span className={cn(
|
||||
"inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium border",
|
||||
user.isActive
|
||||
? "bg-emerald-500/10 text-emerald-400 border-emerald-500/20"
|
||||
: "bg-zinc-700/30 text-zinc-500 border-zinc-700/50"
|
||||
)}>
|
||||
<span className={cn("h-1.5 w-1.5 rounded-full", user.isActive ? "bg-emerald-400" : "bg-zinc-500")} />
|
||||
{user.isActive ? "Active" : "Inactive"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-right">
|
||||
<div className="flex items-center justify-end gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="gap-1.5 h-7 text-zinc-400 hover:text-zinc-100"
|
||||
onClick={() => setEditTarget({ id: user.id, name: user.name, email: user.email, role: user.role, isActive: user.isActive })}
|
||||
>
|
||||
<Pencil className="h-3.5 w-3.5" />
|
||||
Edit
|
||||
</Button>
|
||||
{!isSelf && (
|
||||
confirmDeleteId === user.id ? (
|
||||
<>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-7 text-zinc-400 hover:text-zinc-100"
|
||||
onClick={() => setConfirmDeleteId(null)}
|
||||
disabled={deleting}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-7 text-red-400 hover:text-red-300 hover:bg-red-500/10"
|
||||
onClick={() => handleDelete(user.id)}
|
||||
disabled={deleting}
|
||||
>
|
||||
{deleting ? "Deleting…" : "Confirm delete"}
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-7 text-zinc-600 hover:text-red-400 hover:bg-red-500/10"
|
||||
onClick={() => setConfirmDeleteId(user.id)}
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<NewUserDialog open={showNew} onClose={() => setShowNew(false)} onSuccess={handleSuccess} />
|
||||
|
||||
{editTarget && (
|
||||
<EditUserDialog
|
||||
user={editTarget}
|
||||
isSelf={editTarget.id === currentUserId}
|
||||
open={!!editTarget}
|
||||
onClose={() => setEditTarget(null)}
|
||||
onSuccess={() => { setEditTarget(null); handleSuccess(); }}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user