Add authentication

This commit is contained in:
2025-07-18 19:56:07 -04:00
parent 3b9c0cc31b
commit 1121e5c6ff
25 changed files with 3047 additions and 109 deletions

View File

@@ -0,0 +1,359 @@
"use client";
import { useState } from "react";
import { Button } from "~/components/ui/button";
import { Badge } from "~/components/ui/badge";
import { Input } from "~/components/ui/input";
import { Label } from "~/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "~/components/ui/select";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "~/components/ui/dialog";
import { api } from "~/trpc/react";
import { formatRole, getAvailableRoles } from "~/lib/auth-client";
import type { SystemRole } from "~/lib/auth-client";
interface UserWithRoles {
id: string;
name: string | null;
email: string;
image: string | null;
createdAt: Date;
roles: SystemRole[];
}
export function AdminUserTable() {
const [search, setSearch] = useState("");
const [selectedRole, setSelectedRole] = useState<SystemRole | "">("");
const [page, setPage] = useState(1);
const [selectedUser, setSelectedUser] = useState<UserWithRoles | null>(null);
const [roleToAssign, setRoleToAssign] = useState<SystemRole | "">("");
const {
data: usersData,
isLoading,
refetch,
} = api.users.list.useQuery({
page,
limit: 10,
search: search || undefined,
role: selectedRole || undefined,
});
const assignRole = api.users.assignRole.useMutation({
onSuccess: () => {
void refetch();
setSelectedUser(null);
setRoleToAssign("");
},
});
const removeRole = api.users.removeRole.useMutation({
onSuccess: () => {
void refetch();
},
});
const handleAssignRole = () => {
if (!selectedUser || !roleToAssign) return;
assignRole.mutate({
userId: selectedUser.id,
role: roleToAssign,
});
};
const handleRemoveRole = (userId: string, role: SystemRole) => {
removeRole.mutate({
userId,
role,
});
};
const availableRoles = getAvailableRoles();
if (isLoading) {
return (
<div className="flex items-center justify-center py-8">
<div className="h-8 w-8 animate-spin rounded-full border-b-2 border-blue-600"></div>
</div>
);
}
return (
<div className="space-y-4">
{/* Filters */}
<div className="flex flex-col gap-4 sm:flex-row sm:items-end">
<div className="flex-1">
<Label htmlFor="search">Search Users</Label>
<Input
id="search"
placeholder="Search by name or email..."
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
</div>
<div className="w-full sm:w-48">
<Label htmlFor="role-filter">Filter by Role</Label>
<Select
value={selectedRole}
onValueChange={(value) => setSelectedRole(value as SystemRole | "")}
>
<SelectTrigger>
<SelectValue placeholder="All roles" />
</SelectTrigger>
<SelectContent>
<SelectItem value="">All roles</SelectItem>
{availableRoles.map((role) => (
<SelectItem key={role.value} value={role.value}>
{role.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
{/* Users Table */}
<div className="rounded-md border">
<div className="overflow-x-auto">
<table className="w-full">
<thead>
<tr className="border-b bg-slate-50">
<th className="px-4 py-3 text-left text-sm font-medium text-slate-700">
User
</th>
<th className="px-4 py-3 text-left text-sm font-medium text-slate-700">
Email
</th>
<th className="px-4 py-3 text-left text-sm font-medium text-slate-700">
Roles
</th>
<th className="px-4 py-3 text-left text-sm font-medium text-slate-700">
Created
</th>
<th className="px-4 py-3 text-left text-sm font-medium text-slate-700">
Actions
</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-200">
{usersData?.users.map((user) => (
<tr key={user.id} className="hover:bg-slate-50">
<td className="px-4 py-3">
<div className="flex items-center gap-3">
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-blue-100">
<span className="text-sm font-semibold text-blue-600">
{(user.name ?? user.email).charAt(0).toUpperCase()}
</span>
</div>
<div>
<p className="font-medium text-slate-900">
{user.name ?? "Unnamed User"}
</p>
<p className="text-sm text-slate-500">
ID: {user.id.slice(0, 8)}...
</p>
</div>
</div>
</td>
<td className="px-4 py-3">
<p className="text-sm text-slate-900">{user.email}</p>
</td>
<td className="px-4 py-3">
<div className="flex flex-wrap gap-1">
{user.roles.length > 0 ? (
user.roles.map((role) => (
<div key={role} className="flex items-center gap-1">
<Badge variant="secondary" className="text-xs">
{formatRole(role)}
</Badge>
<Button
variant="ghost"
size="sm"
className="h-4 w-4 p-0 text-red-500 hover:text-red-700"
onClick={() => handleRemoveRole(user.id, role)}
disabled={removeRole.isPending}
>
×
</Button>
</div>
))
) : (
<span className="text-xs text-slate-500">No roles</span>
)}
</div>
</td>
<td className="px-4 py-3">
<p className="text-sm text-slate-600">
{new Date(user.createdAt).toLocaleDateString()}
</p>
</td>
<td className="px-4 py-3">
<Dialog>
<DialogTrigger asChild>
<Button
variant="outline"
size="sm"
onClick={() => setSelectedUser(user)}
>
Manage
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Manage User Roles</DialogTitle>
<DialogDescription>
Assign or remove roles for {user.name ?? user.email}
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div>
<Label htmlFor="role-select">Assign Role</Label>
<div className="flex gap-2">
<Select
value={roleToAssign}
onValueChange={(value) =>
setRoleToAssign(value as SystemRole)
}
>
<SelectTrigger>
<SelectValue placeholder="Select a role" />
</SelectTrigger>
<SelectContent>
{availableRoles
.filter(
(role) =>
!user.roles.includes(role.value),
)
.map((role) => (
<SelectItem
key={role.value}
value={role.value}
>
{role.label}
</SelectItem>
))}
</SelectContent>
</Select>
<Button
onClick={handleAssignRole}
disabled={!roleToAssign || assignRole.isPending}
>
{assignRole.isPending
? "Assigning..."
: "Assign"}
</Button>
</div>
</div>
<div>
<Label>Current Roles</Label>
<div className="mt-2 space-y-2">
{user.roles.length > 0 ? (
user.roles.map((role) => (
<div
key={role}
className="flex items-center justify-between rounded-md border p-2"
>
<div>
<Badge variant="secondary">
{formatRole(role)}
</Badge>
<p className="mt-1 text-xs text-slate-600">
{
availableRoles.find(
(r) => r.value === role,
)?.description
}
</p>
</div>
<Button
variant="outline"
size="sm"
onClick={() =>
handleRemoveRole(user.id, role)
}
disabled={removeRole.isPending}
>
Remove
</Button>
</div>
))
) : (
<p className="text-sm text-slate-500">
No roles assigned
</p>
)}
</div>
</div>
</div>
</DialogContent>
</Dialog>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
{/* Pagination */}
{usersData && usersData.pagination.pages > 1 && (
<div className="flex items-center justify-between">
<p className="text-sm text-slate-600">
Showing {usersData.users.length} of {usersData.pagination.total}{" "}
users
</p>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
onClick={() => setPage(page - 1)}
disabled={page === 1}
>
Previous
</Button>
<span className="flex items-center px-3 text-sm">
{page} of {usersData.pagination.pages}
</span>
<Button
variant="outline"
size="sm"
onClick={() => setPage(page + 1)}
disabled={page === usersData.pagination.pages}
>
Next
</Button>
</div>
</div>
)}
{/* Error Messages */}
{assignRole.error && (
<div className="rounded-md bg-red-50 p-3 text-sm text-red-700">
<p className="font-medium">Error assigning role</p>
<p>{assignRole.error.message}</p>
</div>
)}
{removeRole.error && (
<div className="rounded-md bg-red-50 p-3 text-sm text-red-700">
<p className="font-medium">Error removing role</p>
<p>{removeRole.error.message}</p>
</div>
)}
</div>
);
}

View File

@@ -0,0 +1,119 @@
"use client";
import { Badge } from "~/components/ui/badge";
import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card";
import { Separator } from "~/components/ui/separator";
import {
getAvailableRoles,
getRolePermissions,
getRoleColor,
} from "~/lib/auth-client";
export function RoleManagement() {
const availableRoles = getAvailableRoles();
// Mock data for role statistics - in a real implementation, this would come from an API
const roleStats = {
administrator: 2,
researcher: 15,
wizard: 8,
observer: 12,
};
return (
<div className="space-y-4">
<div>
<h3 className="mb-2 text-sm font-medium text-slate-900">
System Roles Overview
</h3>
<p className="text-xs text-slate-600">
Roles define user permissions and access levels within HRIStudio
</p>
</div>
<div className="space-y-3">
{availableRoles.map((role) => (
<Card key={role.value} className="border-l-4 border-l-transparent">
<CardHeader className="pb-2">
<div className="flex items-center justify-between">
<CardTitle className="text-sm">
<Badge
variant="outline"
className={`${getRoleColor(role.value)} text-xs`}
>
{role.label}
</Badge>
</CardTitle>
<span className="text-xs text-slate-500">
{roleStats[role.value as keyof typeof roleStats] || 0} users
</span>
</div>
</CardHeader>
<CardContent className="space-y-3">
<p className="text-xs text-slate-600">{role.description}</p>
<Separator />
<div>
<h4 className="mb-2 text-xs font-medium text-slate-700">
Key Permissions:
</h4>
<div className="space-y-1">
{getRolePermissions(role.value)
?.slice(0, 3)
.map((permission, index) => (
<div key={index} className="flex items-center gap-2">
<div className="h-1.5 w-1.5 rounded-full bg-slate-400"></div>
<span className="text-xs text-slate-600">
{permission}
</span>
</div>
))}
{getRolePermissions(role.value)?.length > 3 && (
<div className="flex items-center gap-2">
<div className="h-1.5 w-1.5 rounded-full bg-slate-300"></div>
<span className="text-xs text-slate-500">
+{getRolePermissions(role.value).length - 3} more
</span>
</div>
)}
</div>
</div>
</CardContent>
</Card>
))}
</div>
<Separator />
<div className="rounded-md bg-blue-50 p-3">
<div className="flex items-start gap-2">
<div className="flex h-5 w-5 items-center justify-center rounded-full bg-blue-100">
<svg
className="h-3 w-3 text-blue-600"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
</div>
<div>
<h4 className="text-xs font-medium text-blue-900">
Role Hierarchy
</h4>
<p className="mt-1 text-xs text-blue-800">
Administrator has access to all features. Users can have multiple
roles for different access levels.
</p>
</div>
</div>
</div>
</div>
);
}

View File

@@ -0,0 +1,182 @@
"use client";
import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card";
import { Badge } from "~/components/ui/badge";
export function SystemStats() {
// TODO: Implement admin.getSystemStats API endpoint
// const { data: stats, isLoading } = api.admin.getSystemStats.useQuery({});
const isLoading = false;
if (isLoading) {
return (
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-4">
{Array.from({ length: 4 }).map((_, i) => (
<Card key={i} className="animate-pulse">
<CardHeader className="pb-2">
<div className="h-4 w-20 rounded bg-slate-200"></div>
</CardHeader>
<CardContent>
<div className="mb-2 h-8 w-12 rounded bg-slate-200"></div>
<div className="h-3 w-24 rounded bg-slate-200"></div>
</CardContent>
</Card>
))}
</div>
);
}
// Mock data for now since we don't have the actual admin router implemented
const mockStats = {
totalUsers: 42,
totalStudies: 15,
totalExperiments: 38,
totalTrials: 127,
activeTrials: 3,
systemHealth: "healthy",
uptime: "7 days, 14 hours",
storageUsed: "2.3 GB",
};
const displayStats = mockStats;
return (
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-4">
{/* Total Users */}
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
Total Users
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{displayStats.totalUsers}</div>
<div className="mt-1 flex items-center gap-2">
<Badge variant="secondary" className="text-xs">
All roles
</Badge>
</div>
</CardContent>
</Card>
{/* Total Studies */}
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
Studies
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{displayStats.totalStudies}</div>
<div className="mt-1 flex items-center gap-2">
<Badge variant="secondary" className="text-xs">
Active
</Badge>
</div>
</CardContent>
</Card>
{/* Total Experiments */}
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
Experiments
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">
{displayStats.totalExperiments}
</div>
<div className="mt-1 flex items-center gap-2">
<Badge variant="secondary" className="text-xs">
Published
</Badge>
</div>
</CardContent>
</Card>
{/* Total Trials */}
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
Trials
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{displayStats.totalTrials}</div>
<div className="mt-1 flex items-center gap-2">
<Badge variant="outline" className="text-xs">
{displayStats.activeTrials} running
</Badge>
</div>
</CardContent>
</Card>
{/* System Health */}
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
System Health
</CardTitle>
</CardHeader>
<CardContent>
<div className="flex items-center gap-2">
<div className="flex h-3 w-3 items-center justify-center">
<div className="h-2 w-2 rounded-full bg-green-500"></div>
</div>
<span className="text-sm font-medium text-green-700">
{displayStats.systemHealth === "healthy" ? "Healthy" : "Issues"}
</span>
</div>
<div className="mt-1 text-xs text-slate-500">
All services operational
</div>
</CardContent>
</Card>
{/* Uptime */}
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
Uptime
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-sm font-medium">{displayStats.uptime}</div>
<div className="mt-1 text-xs text-slate-500">Since last restart</div>
</CardContent>
</Card>
{/* Storage Usage */}
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
Storage Used
</CardTitle>
</CardHeader>
<CardContent>
<div className="text-sm font-medium">{displayStats.storageUsed}</div>
<div className="mt-1 text-xs text-slate-500">Media & database</div>
</CardContent>
</Card>
{/* Recent Activity */}
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-slate-600">
Recent Activity
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-1">
<div className="text-xs text-slate-600">2 trials started today</div>
<div className="text-xs text-slate-600">1 new user registered</div>
<div className="text-xs text-slate-600">
3 experiments published
</div>
</div>
</CardContent>
</Card>
</div>
);
}

View File

@@ -0,0 +1,161 @@
"use client";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
import { Label } from "~/components/ui/label";
import { api } from "~/trpc/react";
const passwordSchema = z
.object({
currentPassword: z.string().min(1, "Current password is required"),
newPassword: z
.string()
.min(6, "Password must be at least 6 characters")
.max(100, "Password is too long"),
confirmPassword: z.string().min(1, "Please confirm your new password"),
})
.refine((data) => data.newPassword === data.confirmPassword, {
message: "Passwords don't match",
path: ["confirmPassword"],
});
type PasswordFormData = z.infer<typeof passwordSchema>;
export function PasswordChangeForm() {
const [showForm, setShowForm] = useState(false);
const form = useForm<PasswordFormData>({
resolver: zodResolver(passwordSchema),
defaultValues: {
currentPassword: "",
newPassword: "",
confirmPassword: "",
},
});
const changePassword = api.users.changePassword.useMutation({
onSuccess: () => {
form.reset();
setShowForm(false);
},
onError: (error) => {
console.error("Error changing password:", error);
},
});
const onSubmit = (data: PasswordFormData) => {
changePassword.mutate({
currentPassword: data.currentPassword,
newPassword: data.newPassword,
});
};
const handleCancel = () => {
form.reset();
setShowForm(false);
};
if (!showForm) {
return (
<div className="space-y-4">
<div>
<p className="text-sm text-slate-600">
Change your account password for enhanced security
</p>
</div>
<div className="flex justify-end">
<Button onClick={() => setShowForm(true)} variant="outline">
Change Password
</Button>
</div>
</div>
);
}
return (
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
{changePassword.error && (
<div className="rounded-md bg-red-50 p-3 text-sm text-red-700">
<p className="font-medium">Error changing password</p>
<p>{changePassword.error.message}</p>
</div>
)}
{changePassword.isSuccess && (
<div className="rounded-md bg-green-50 p-3 text-sm text-green-700">
<p className="font-medium">Password changed successfully</p>
<p>Your password has been updated.</p>
</div>
)}
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="currentPassword">Current Password</Label>
<Input
id="currentPassword"
type="password"
{...form.register("currentPassword")}
placeholder="Enter your current password"
disabled={changePassword.isPending}
/>
{form.formState.errors.currentPassword && (
<p className="text-sm text-red-600">
{form.formState.errors.currentPassword.message}
</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="newPassword">New Password</Label>
<Input
id="newPassword"
type="password"
{...form.register("newPassword")}
placeholder="Enter your new password"
disabled={changePassword.isPending}
/>
{form.formState.errors.newPassword && (
<p className="text-sm text-red-600">
{form.formState.errors.newPassword.message}
</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="confirmPassword">Confirm New Password</Label>
<Input
id="confirmPassword"
type="password"
{...form.register("confirmPassword")}
placeholder="Confirm your new password"
disabled={changePassword.isPending}
/>
{form.formState.errors.confirmPassword && (
<p className="text-sm text-red-600">
{form.formState.errors.confirmPassword.message}
</p>
)}
</div>
</div>
<div className="flex justify-end gap-3">
<Button
type="button"
variant="outline"
onClick={handleCancel}
disabled={changePassword.isPending}
>
Cancel
</Button>
<Button type="submit" disabled={changePassword.isPending}>
{changePassword.isPending ? "Changing..." : "Change Password"}
</Button>
</div>
</form>
);
}

View File

@@ -0,0 +1,148 @@
"use client";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
import { Label } from "~/components/ui/label";
import { api } from "~/trpc/react";
import { useRouter } from "next/navigation";
const profileSchema = z.object({
name: z.string().min(1, "Name is required").max(100, "Name is too long"),
email: z.string().email("Invalid email address"),
});
type ProfileFormData = z.infer<typeof profileSchema>;
interface ProfileEditFormProps {
user: {
id: string;
name: string | null;
email: string | null;
image: string | null;
};
}
export function ProfileEditForm({ user }: ProfileEditFormProps) {
const [isEditing, setIsEditing] = useState(false);
const router = useRouter();
const form = useForm<ProfileFormData>({
resolver: zodResolver(profileSchema),
defaultValues: {
name: user.name ?? "",
email: user.email ?? "",
},
});
const updateProfile = api.users.update.useMutation({
onSuccess: () => {
setIsEditing(false);
router.refresh();
},
onError: (error) => {
console.error("Error updating profile:", error);
},
});
const onSubmit = (data: ProfileFormData) => {
updateProfile.mutate({
id: user.id,
name: data.name,
email: data.email,
});
};
const handleCancel = () => {
form.reset();
setIsEditing(false);
};
if (!isEditing) {
return (
<div className="space-y-4">
<div className="grid grid-cols-2 gap-4">
<div>
<Label className="text-sm font-medium text-slate-700">Name</Label>
<p className="mt-1 text-sm text-slate-900">
{user.name ?? "Not set"}
</p>
</div>
<div>
<Label className="text-sm font-medium text-slate-700">Email</Label>
<p className="mt-1 text-sm text-slate-900">
{user.email ?? "Not set"}
</p>
</div>
</div>
<div className="flex justify-end">
<Button onClick={() => setIsEditing(true)} variant="outline">
Edit Profile
</Button>
</div>
</div>
);
}
return (
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
{updateProfile.error && (
<div className="rounded-md bg-red-50 p-3 text-sm text-red-700">
<p className="font-medium">Error updating profile</p>
<p>{updateProfile.error.message}</p>
</div>
)}
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="name">Full Name</Label>
<Input
id="name"
{...form.register("name")}
placeholder="Enter your full name"
disabled={updateProfile.isPending}
/>
{form.formState.errors.name && (
<p className="text-sm text-red-600">
{form.formState.errors.name.message}
</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="email">Email Address</Label>
<Input
id="email"
type="email"
{...form.register("email")}
placeholder="Enter your email address"
disabled={updateProfile.isPending}
/>
{form.formState.errors.email && (
<p className="text-sm text-red-600">
{form.formState.errors.email.message}
</p>
)}
</div>
</div>
<div className="flex justify-end gap-3">
<Button
type="button"
variant="outline"
onClick={handleCancel}
disabled={updateProfile.isPending}
>
Cancel
</Button>
<Button type="submit" disabled={updateProfile.isPending}>
{updateProfile.isPending ? "Saving..." : "Save Changes"}
</Button>
</div>
</form>
);
}

View File

@@ -0,0 +1,36 @@
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "~/lib/utils"
const badgeVariants = cva(
"inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
{
variants: {
variant: {
default:
"border-transparent bg-primary text-primary-foreground hover:bg-primary/80",
secondary:
"border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80",
destructive:
"border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80",
outline: "text-foreground",
},
},
defaultVariants: {
variant: "default",
},
}
)
export interface BadgeProps
extends React.HTMLAttributes<HTMLDivElement>,
VariantProps<typeof badgeVariants> {}
function Badge({ className, variant, ...props }: BadgeProps) {
return (
<div className={cn(badgeVariants({ variant }), className)} {...props} />
)
}
export { Badge, badgeVariants }

View File

@@ -0,0 +1,120 @@
import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { X } from "lucide-react"
import { cn } from "~/lib/utils"
const Dialog = DialogPrimitive.Root
const DialogTrigger = DialogPrimitive.Trigger
const DialogPortal = DialogPrimitive.Portal
const DialogClose = DialogPrimitive.Close
const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
/>
))
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
))
DialogContent.displayName = DialogPrimitive.Content.displayName
const DialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-1.5 text-center sm:text-left",
className
)}
{...props}
/>
)
DialogHeader.displayName = "DialogHeader"
const DialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
DialogFooter.displayName = "DialogFooter"
const DialogTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
"text-lg font-semibold leading-none tracking-tight",
className
)}
{...props}
/>
))
DialogTitle.displayName = DialogPrimitive.Title.displayName
const DialogDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
DialogDescription.displayName = DialogPrimitive.Description.displayName
export {
Dialog,
DialogPortal,
DialogOverlay,
DialogClose,
DialogTrigger,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
}

View File

@@ -0,0 +1,158 @@
import * as React from "react"
import * as SelectPrimitive from "@radix-ui/react-select"
import { Check, ChevronDown, ChevronUp } from "lucide-react"
import { cn } from "~/lib/utils"
const Select = SelectPrimitive.Root
const SelectGroup = SelectPrimitive.Group
const SelectValue = SelectPrimitive.Value
const SelectTrigger = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
>(({ className, children, ...props }, ref) => (
<SelectPrimitive.Trigger
ref={ref}
className={cn(
"flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
className
)}
{...props}
>
{children}
<SelectPrimitive.Icon asChild>
<ChevronDown className="h-4 w-4 opacity-50" />
</SelectPrimitive.Icon>
</SelectPrimitive.Trigger>
))
SelectTrigger.displayName = SelectPrimitive.Trigger.displayName
const SelectScrollUpButton = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
>(({ className, ...props }, ref) => (
<SelectPrimitive.ScrollUpButton
ref={ref}
className={cn(
"flex cursor-default items-center justify-center py-1",
className
)}
{...props}
>
<ChevronUp className="h-4 w-4" />
</SelectPrimitive.ScrollUpButton>
))
SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName
const SelectScrollDownButton = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
>(({ className, ...props }, ref) => (
<SelectPrimitive.ScrollDownButton
ref={ref}
className={cn(
"flex cursor-default items-center justify-center py-1",
className
)}
{...props}
>
<ChevronDown className="h-4 w-4" />
</SelectPrimitive.ScrollDownButton>
))
SelectScrollDownButton.displayName =
SelectPrimitive.ScrollDownButton.displayName
const SelectContent = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
>(({ className, children, position = "popper", ...props }, ref) => (
<SelectPrimitive.Portal>
<SelectPrimitive.Content
ref={ref}
className={cn(
"relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
position === "popper" &&
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
className
)}
position={position}
{...props}
>
<SelectScrollUpButton />
<SelectPrimitive.Viewport
className={cn(
"p-1",
position === "popper" &&
"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
)}
>
{children}
</SelectPrimitive.Viewport>
<SelectScrollDownButton />
</SelectPrimitive.Content>
</SelectPrimitive.Portal>
))
SelectContent.displayName = SelectPrimitive.Content.displayName
const SelectLabel = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
>(({ className, ...props }, ref) => (
<SelectPrimitive.Label
ref={ref}
className={cn("py-1.5 pl-8 pr-2 text-sm font-semibold", className)}
{...props}
/>
))
SelectLabel.displayName = SelectPrimitive.Label.displayName
const SelectItem = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
>(({ className, children, ...props }, ref) => (
<SelectPrimitive.Item
ref={ref}
className={cn(
"relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
className
)}
{...props}
>
<span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<SelectPrimitive.ItemIndicator>
<Check className="h-4 w-4" />
</SelectPrimitive.ItemIndicator>
</span>
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
))
SelectItem.displayName = SelectPrimitive.Item.displayName
const SelectSeparator = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
>(({ className, ...props }, ref) => (
<SelectPrimitive.Separator
ref={ref}
className={cn("-mx-1 my-1 h-px bg-muted", className)}
{...props}
/>
))
SelectSeparator.displayName = SelectPrimitive.Separator.displayName
export {
Select,
SelectGroup,
SelectValue,
SelectTrigger,
SelectContent,
SelectLabel,
SelectItem,
SelectSeparator,
SelectScrollUpButton,
SelectScrollDownButton,
}

View File

@@ -0,0 +1,28 @@
"use client"
import * as React from "react"
import * as SeparatorPrimitive from "@radix-ui/react-separator"
import { cn } from "~/lib/utils"
function Separator({
className,
orientation = "horizontal",
decorative = true,
...props
}: React.ComponentProps<typeof SeparatorPrimitive.Root>) {
return (
<SeparatorPrimitive.Root
data-slot="separator"
decorative={decorative}
orientation={orientation}
className={cn(
"bg-border shrink-0 data-[orientation=horizontal]:h-px data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-px",
className
)}
{...props}
/>
)
}
export { Separator }