This commit is contained in:
ryan
2026-07-13 15:17:45 +08:00
parent 08e61ce833
commit e0eb4e5975
334 changed files with 17212 additions and 12321 deletions
@@ -1,91 +1,97 @@
"use client"
'use client';
import {useEffect, useState} from "react"
import {Button} from "@/components/ui/button"
import {Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Switch} from "@/components/ui/switch"
import {useAdminUsers} from "@/contexts/admin-users-context"
import type {CreateUserRequest} from "@/lib/services/admin"
import { useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import { useAdminUsers } from '@/contexts/admin-users-context';
import type { CreateUserRequest } from '@/lib/services/admin';
const emptyForm: CreateUserRequest = {
username: "",
password: "",
nickname: "",
email: "",
username: '',
password: '',
nickname: '',
email: '',
is_active: true,
is_admin: false,
}
};
export function CreateUserModal({
isOpen,
onClose,
}: {
isOpen: boolean
onClose: () => void
isOpen: boolean;
onClose: () => void;
}) {
const { createUser } = useAdminUsers()
const [form, setForm] = useState<CreateUserRequest>(emptyForm)
const [saving, setSaving] = useState(false)
const [errors, setErrors] = useState<Record<string, string>>({})
const { createUser } = useAdminUsers();
const [form, setForm] = useState<CreateUserRequest>(emptyForm);
const [saving, setSaving] = useState(false);
const [errors, setErrors] = useState<Record<string, string>>({});
useEffect(() => {
if (isOpen) {
setForm(emptyForm)
setErrors({})
setForm(emptyForm);
setErrors({});
} else {
setSaving(false)
setSaving(false);
}
}, [isOpen])
}, [isOpen]);
const validate = () => {
const newErrors: Record<string, string> = {}
const newErrors: Record<string, string> = {};
if (!form.username.trim()) {
newErrors.username = "用户名不能为空"
newErrors.username = '用户名不能为空';
} else if (form.username.trim().length < 3) {
newErrors.username = "用户名长度不能少于 3 位"
newErrors.username = '用户名长度不能少于 3 位';
}
if (!form.email.trim()) {
newErrors.email = "邮箱不能为空"
newErrors.email = '邮箱不能为空';
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email.trim())) {
newErrors.email = "邮箱格式不正确"
newErrors.email = '邮箱格式不正确';
}
if (!form.password) {
newErrors.password = "密码不能为空"
newErrors.password = '密码不能为空';
} else if (form.password.length < 8) {
newErrors.password = "密码长度不能少于 8 位"
newErrors.password = '密码长度不能少于 8 位';
}
setErrors(newErrors)
return Object.keys(newErrors).length === 0
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSave = async (e: React.FormEvent) => {
e.preventDefault()
if (!validate()) return
e.preventDefault();
if (!validate()) return;
setSaving(true)
setSaving(true);
try {
await createUser({
...form,
username: form.username.trim(),
nickname: form.nickname?.trim() || undefined,
email: form.email.trim(),
})
onClose()
});
onClose();
} catch {
// Errors are already handled by context toast notifications
} finally {
setSaving(false)
setSaving(false);
}
}
};
return (
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent className="max-w-md">
<DialogContent className='max-w-md'>
<DialogHeader>
<DialogTitle>新增用户</DialogTitle>
<DialogDescription>
@@ -93,90 +99,106 @@ export function CreateUserModal({
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSave} className="space-y-4 pt-2">
<div className="space-y-1.5">
<Label htmlFor="username">用户名</Label>
<form onSubmit={handleSave} className='space-y-4 pt-2'>
<div className='space-y-1.5'>
<Label htmlFor='username'>用户名</Label>
<Input
id="username"
id='username'
value={form.username}
onChange={(e) => setForm((prev) => ({ ...prev, username: e.target.value }))}
placeholder="请输入用户名 (至少 3 位)"
onChange={(e) =>
setForm((prev) => ({ ...prev, username: e.target.value }))
}
placeholder='请输入用户名 (至少 3 位)'
/>
{errors.username && (
<p className="text-xs text-destructive">{errors.username}</p>
<p className='text-xs text-destructive'>{errors.username}</p>
)}
</div>
<div className="space-y-1.5">
<Label htmlFor="nickname">昵称 (选填)</Label>
<div className='space-y-1.5'>
<Label htmlFor='nickname'>昵称 (选填)</Label>
<Input
id="nickname"
id='nickname'
value={form.nickname}
onChange={(e) => setForm((prev) => ({ ...prev, nickname: e.target.value }))}
placeholder="请输入昵称"
onChange={(e) =>
setForm((prev) => ({ ...prev, nickname: e.target.value }))
}
placeholder='请输入昵称'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="email">邮箱</Label>
<div className='space-y-1.5'>
<Label htmlFor='email'>邮箱</Label>
<Input
id="email"
type="email"
id='email'
type='email'
value={form.email}
onChange={(e) => setForm((prev) => ({ ...prev, email: e.target.value }))}
placeholder="请输入邮箱地址"
onChange={(e) =>
setForm((prev) => ({ ...prev, email: e.target.value }))
}
placeholder='请输入邮箱地址'
/>
{errors.email && (
<p className="text-xs text-destructive">{errors.email}</p>
<p className='text-xs text-destructive'>{errors.email}</p>
)}
</div>
<div className="space-y-1.5">
<Label htmlFor="password">密码</Label>
<div className='space-y-1.5'>
<Label htmlFor='password'>密码</Label>
<Input
id="password"
type="password"
id='password'
type='password'
value={form.password}
onChange={(e) => setForm((prev) => ({ ...prev, password: e.target.value }))}
placeholder="请输入登录密码 (至少 8 位)"
onChange={(e) =>
setForm((prev) => ({ ...prev, password: e.target.value }))
}
placeholder='请输入登录密码 (至少 8 位)'
/>
{errors.password && (
<p className="text-xs text-destructive">{errors.password}</p>
<p className='text-xs text-destructive'>{errors.password}</p>
)}
</div>
<div className="flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10">
<div className='flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10'>
<div>
<div className="font-medium text-sm">启用账户</div>
<div className="text-xs text-muted-foreground">禁用后此账号将无法登录系统。</div>
<div className='font-medium text-sm'>启用账户</div>
<div className='text-xs text-muted-foreground'>
禁用后此账号将无法登录系统。
</div>
</div>
<Switch
checked={form.is_active}
onCheckedChange={(checked) => setForm((prev) => ({ ...prev, is_active: checked }))}
onCheckedChange={(checked) =>
setForm((prev) => ({ ...prev, is_active: checked }))
}
/>
</div>
<div className="flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10">
<div className='flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10'>
<div>
<div className="font-medium text-sm">管理员权限</div>
<div className="text-xs text-muted-foreground">开启后此账号将拥有后台管理权限。</div>
<div className='font-medium text-sm'>管理员权限</div>
<div className='text-xs text-muted-foreground'>
开启后此账号将拥有后台管理权限。
</div>
</div>
<Switch
checked={form.is_admin}
onCheckedChange={(checked) => setForm((prev) => ({ ...prev, is_admin: checked }))}
onCheckedChange={(checked) =>
setForm((prev) => ({ ...prev, is_admin: checked }))
}
/>
</div>
<div className="flex justify-end gap-2 pt-2 border-t mt-2">
<Button variant="outline" type="button" onClick={onClose}>
<div className='flex justify-end gap-2 pt-2 border-t mt-2'>
<Button variant='outline' type='button' onClick={onClose}>
取消
</Button>
<Button type="submit" disabled={saving} variant="secondary">
{saving ? "创建中..." : "创建"}
<Button type='submit' disabled={saving} variant='secondary'>
{saving ? '创建中...' : '创建'}
</Button>
</div>
</form>
</DialogContent>
</Dialog>
)
);
}
@@ -1,14 +1,20 @@
"use client"
'use client';
import {useEffect, useState} from "react"
import {Button} from "@/components/ui/button"
import {Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Switch} from "@/components/ui/switch"
import {useAdminUsers} from "@/contexts/admin-users-context"
import {useAuth} from "@/components/providers/auth-provider"
import type {AdminUser} from "@/lib/services/admin"
import { useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import { useAdminUsers } from '@/contexts/admin-users-context';
import { useAuth } from '@/components/providers/auth-provider';
import type { AdminUser } from '@/lib/services/admin';
import {
AlertDialog,
AlertDialogAction,
@@ -17,15 +23,15 @@ import {
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle
} from "@/components/ui/alert-dialog"
import {Loader2} from "lucide-react"
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { Loader2 } from 'lucide-react';
interface EditUserForm {
nickname: string
email: string
is_admin: boolean
password?: string
nickname: string;
email: string;
is_admin: boolean;
password?: string;
}
export function EditUserModal({
@@ -33,115 +39,116 @@ export function EditUserModal({
onClose,
user,
}: {
isOpen: boolean
onClose: () => void
user: AdminUser | null
isOpen: boolean;
onClose: () => void;
user: AdminUser | null;
}) {
const { updateUser, deleteUser, getUserDetail } = useAdminUsers()
const { user: currentUser } = useAuth()
const isSelf = currentUser && user && currentUser.id.toString() === user.id.toString()
const { updateUser, deleteUser, getUserDetail } = useAdminUsers();
const { user: currentUser } = useAuth();
const isSelf =
currentUser && user && currentUser.id.toString() === user.id.toString();
const [form, setForm] = useState<EditUserForm>({
nickname: "",
email: "",
nickname: '',
email: '',
is_admin: false,
password: "",
})
const [saving, setSaving] = useState(false)
const [errors, setErrors] = useState<Record<string, string>>({})
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
password: '',
});
const [saving, setSaving] = useState(false);
const [errors, setErrors] = useState<Record<string, string>>({});
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
useEffect(() => {
let active = true
let active = true;
if (isOpen && user) {
setForm({
nickname: user.nickname || "",
email: user.email || "",
nickname: user.nickname || '',
email: user.email || '',
is_admin: user.is_admin || false,
password: "",
})
setErrors({})
password: '',
});
setErrors({});
getUserDetail(user.id)
.then((detail) => {
if (active && detail) {
setForm({
nickname: detail.nickname || "",
email: detail.email || "",
nickname: detail.nickname || '',
email: detail.email || '',
is_admin: detail.is_admin || false,
password: "",
})
password: '',
});
}
})
.catch(() => {
// ignore fetching error
})
});
} else {
setSaving(false)
setSaving(false);
}
return () => {
active = false
}
}, [isOpen, user, getUserDetail])
active = false;
};
}, [isOpen, user, getUserDetail]);
const validate = () => {
const newErrors: Record<string, string> = {}
const newErrors: Record<string, string> = {};
if (!form.email.trim()) {
newErrors.email = "邮箱不能为空"
newErrors.email = '邮箱不能为空';
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email.trim())) {
newErrors.email = "邮箱格式不正确"
newErrors.email = '邮箱格式不正确';
}
if (form.password && form.password.length < 8) {
newErrors.password = "密码长度不能少于 8 位"
newErrors.password = '密码长度不能少于 8 位';
}
setErrors(newErrors)
return Object.keys(newErrors).length === 0
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSave = async (e: React.FormEvent) => {
e.preventDefault()
if (!user || !validate()) return
e.preventDefault();
if (!user || !validate()) return;
setSaving(true)
setSaving(true);
try {
await updateUser(user.id, {
nickname: form.nickname.trim() || undefined,
email: form.email.trim(),
is_admin: form.is_admin,
password: form.password?.trim() || undefined,
})
onClose()
});
onClose();
} catch {
// Errors are handled by context toast notifications
} finally {
setSaving(false)
setSaving(false);
}
}
};
const handleDelete = async () => {
if (!user) return
setSaving(true)
if (!user) return;
setSaving(true);
try {
await deleteUser(user)
onClose()
await deleteUser(user);
onClose();
} catch {
// Errors are handled by context toast notifications
} finally {
setSaving(false)
setShowDeleteConfirm(false)
setSaving(false);
setShowDeleteConfirm(false);
}
}
};
if (!user) return null
if (!user) return null;
return (
<>
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent className="max-w-md">
<DialogContent className='max-w-md'>
<DialogHeader>
<DialogTitle>编辑用户</DialogTitle>
<DialogDescription>
@@ -149,89 +156,102 @@ export function EditUserModal({
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSave} className="space-y-4 pt-2">
<div className="space-y-1.5">
<Label htmlFor="username" className="text-xs text-muted-foreground">用户名 (不可修改)</Label>
<form onSubmit={handleSave} className='space-y-4 pt-2'>
<div className='space-y-1.5'>
<Label
htmlFor='username'
className='text-xs text-muted-foreground'
>
用户名 (不可修改)
</Label>
<Input
id="username"
id='username'
value={user.username}
disabled
className="bg-muted/50 cursor-not-allowed select-none font-mono"
className='bg-muted/50 cursor-not-allowed select-none font-mono'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="nickname">昵称 (选填)</Label>
<div className='space-y-1.5'>
<Label htmlFor='nickname'>昵称 (选填)</Label>
<Input
id="nickname"
id='nickname'
value={form.nickname}
onChange={(e) => setForm((prev) => ({ ...prev, nickname: e.target.value }))}
placeholder="请输入昵称"
onChange={(e) =>
setForm((prev) => ({ ...prev, nickname: e.target.value }))
}
placeholder='请输入昵称'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="email">邮箱</Label>
<div className='space-y-1.5'>
<Label htmlFor='email'>邮箱</Label>
<Input
id="email"
type="email"
id='email'
type='email'
value={form.email}
onChange={(e) => setForm((prev) => ({ ...prev, email: e.target.value }))}
placeholder="请输入邮箱地址"
onChange={(e) =>
setForm((prev) => ({ ...prev, email: e.target.value }))
}
placeholder='请输入邮箱地址'
/>
{errors.email && (
<p className="text-xs text-destructive">{errors.email}</p>
<p className='text-xs text-destructive'>{errors.email}</p>
)}
</div>
<div className="space-y-1.5">
<Label htmlFor="password">重置密码 (选填)</Label>
<div className='space-y-1.5'>
<Label htmlFor='password'>重置密码 (选填)</Label>
<Input
id="password"
type="password"
id='password'
type='password'
value={form.password}
onChange={(e) => setForm((prev) => ({ ...prev, password: e.target.value }))}
placeholder="留空表示保持当前密码,输入则重置 (至少 8 位)"
onChange={(e) =>
setForm((prev) => ({ ...prev, password: e.target.value }))
}
placeholder='留空表示保持当前密码,输入则重置 (至少 8 位)'
/>
{errors.password && (
<p className="text-xs text-destructive">{errors.password}</p>
<p className='text-xs text-destructive'>{errors.password}</p>
)}
</div>
<div className="flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10">
<div className='flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10'>
<div>
<div className="font-medium text-sm">管理员权限</div>
<div className="text-xs text-muted-foreground">
{isSelf ? "不能撤销当前登录用户的管理员权限。" : "开启后此账号将拥有后台管理权限。"}
<div className='font-medium text-sm'>管理员权限</div>
<div className='text-xs text-muted-foreground'>
{isSelf
? '不能撤销当前登录用户的管理员权限。'
: '开启后此账号将拥有后台管理权限。'}
</div>
</div>
<Switch
checked={form.is_admin}
disabled={!!isSelf || saving}
onCheckedChange={async (checked) => {
setForm((prev) => ({ ...prev, is_admin: checked }))
setSaving(true)
setForm((prev) => ({ ...prev, is_admin: checked }));
setSaving(true);
try {
await updateUser(user.id, {
nickname: form.nickname.trim() || undefined,
email: form.email.trim(),
is_admin: checked,
})
});
} catch {
setForm((prev) => ({ ...prev, is_admin: !checked }))
setForm((prev) => ({ ...prev, is_admin: !checked }));
} finally {
setSaving(false)
setSaving(false);
}
}}
/>
</div>
<div className="flex justify-between items-center pt-2 border-t mt-2">
<div className='flex justify-between items-center pt-2 border-t mt-2'>
<div>
{!user.is_admin && (
<Button
type="button"
variant="destructive"
type='button'
variant='destructive'
onClick={() => setShowDeleteConfirm(true)}
disabled={saving}
>
@@ -239,12 +259,17 @@ export function EditUserModal({
</Button>
)}
</div>
<div className="flex gap-2">
<Button variant="outline" type="button" onClick={onClose} disabled={saving}>
<div className='flex gap-2'>
<Button
variant='outline'
type='button'
onClick={onClose}
disabled={saving}
>
取消
</Button>
<Button type="submit" disabled={saving} variant="secondary">
{saving ? "保存中..." : "保存"}
<Button type='submit' disabled={saving} variant='secondary'>
{saving ? '保存中...' : '保存'}
</Button>
</div>
</div>
@@ -257,24 +282,25 @@ export function EditUserModal({
<AlertDialogHeader>
<AlertDialogTitle>确认删除用户</AlertDialogTitle>
<AlertDialogDescription>
确定要删除用户 {user.nickname || user.username} 吗?该操作会移除用户账号,删除后无法撤销。
确定要删除用户 {user.nickname || user.username}{' '}
吗?该操作会移除用户账号,删除后无法撤销。
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={saving}>取消</AlertDialogCancel>
<AlertDialogAction
onClick={(e) => {
e.preventDefault()
handleDelete()
e.preventDefault();
handleDelete();
}}
disabled={saving}
>
{saving && <Loader2 className="size-3 animate-spin mr-1" />}
{saving && <Loader2 className='size-3 animate-spin mr-1' />}
确认删除
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
)
);
}
@@ -1,21 +1,29 @@
"use client"
'use client';
import * as React from "react"
import {Globe, Loader2, Mail, MapPin, ShieldCheck, Smartphone, UserCheck,} from "lucide-react"
import * as React from 'react';
import {
Globe,
Loader2,
Mail,
MapPin,
ShieldCheck,
Smartphone,
UserCheck,
} from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Badge} from "@/components/ui/badge"
import {Avatar, AvatarFallback, AvatarImage} from "@/components/ui/avatar"
import {Sheet, SheetContent, SheetTitle} from "@/components/ui/sheet"
import type {AdminUser} from "@/lib/services/admin"
import {formatDateTime} from "@/lib/utils"
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Sheet, SheetContent, SheetTitle } from '@/components/ui/sheet';
import type { AdminUser } from '@/lib/services/admin';
import { formatDateTime } from '@/lib/utils';
interface UserDetailSheetProps {
selectedUser: AdminUser | null
isOpen: boolean
onOpenChange: (open: boolean) => void
detailLoading: boolean
onStatusToggle: (user: AdminUser) => Promise<void>
selectedUser: AdminUser | null;
isOpen: boolean;
onOpenChange: (open: boolean) => void;
detailLoading: boolean;
onStatusToggle: (user: AdminUser) => Promise<void>;
}
export function UserDetailSheet({
@@ -25,36 +33,43 @@ export function UserDetailSheet({
detailLoading,
onStatusToggle,
}: UserDetailSheetProps) {
const displayValue = (value?: string) => value && value.trim() ? value : "-"
const displayValue = (value?: string) =>
value && value.trim() ? value : '-';
return (
<Sheet open={isOpen} onOpenChange={onOpenChange}>
<SheetContent className="sm:max-w-[400px] w-full p-0 flex flex-col gap-0">
<SheetTitle className="px-5 py-3">用户档案</SheetTitle>
<SheetContent className='sm:max-w-[400px] w-full p-0 flex flex-col gap-0'>
<SheetTitle className='px-5 py-3'>用户档案</SheetTitle>
{selectedUser && (
<>
<div className="flex-1 overflow-y-auto scrollbar-thin scrollbar-thumb-border scrollbar-track-transparent">
<div className="flex flex-col pb-6">
<div className="px-5 py-6 border-b border-border/50">
<div className="flex flex-col items-center text-center gap-3">
<Avatar className="h-20 w-20 rounded-full border-4 border-background ring-1 ring-border/20">
<div className='flex-1 overflow-y-auto scrollbar-thin scrollbar-thumb-border scrollbar-track-transparent'>
<div className='flex flex-col pb-6'>
<div className='px-5 py-6 border-b border-border/50'>
<div className='flex flex-col items-center text-center gap-3'>
<Avatar className='h-20 w-20 rounded-full border-4 border-background ring-1 ring-border/20'>
<AvatarImage src={selectedUser.avatar_url} />
<AvatarFallback className="rounded-full text-xl font-medium bg-secondary text-secondary-foreground">
<AvatarFallback className='rounded-full text-xl font-medium bg-secondary text-secondary-foreground'>
{selectedUser.username.substring(0, 2).toUpperCase()}
</AvatarFallback>
</Avatar>
<div className="space-y-1.5">
<h3 className="text-lg font-bold tracking-tight">{selectedUser.nickname}</h3>
<div className="flex items-center justify-center gap-2">
<code className="px-1.5 py-0.5 rounded-md bg-muted text-[10px] font-mono text-muted-foreground">@{selectedUser.username}</code>
<Badge variant="secondary" className="h-4.5 px-1.5 text-[9px] uppercase font-medium">
<div className='space-y-1.5'>
<h3 className='text-lg font-bold tracking-tight'>
{selectedUser.nickname}
</h3>
<div className='flex items-center justify-center gap-2'>
<code className='px-1.5 py-0.5 rounded-md bg-muted text-[10px] font-mono text-muted-foreground'>
@{selectedUser.username}
</code>
<Badge
variant='secondary'
className='h-4.5 px-1.5 text-[9px] uppercase font-medium'
>
UID: {selectedUser.id}
</Badge>
{selectedUser.is_admin && (
<Badge className="h-4.5 px-1.5 text-[9px] uppercase font-medium">
<Badge className='h-4.5 px-1.5 text-[9px] uppercase font-medium'>
Admin
</Badge>
)}
@@ -62,91 +77,132 @@ export function UserDetailSheet({
</div>
{detailLoading && (
<div className="flex items-center gap-1 text-[10px] text-muted-foreground">
<Loader2 className="size-3 animate-spin" />
<div className='flex items-center gap-1 text-[10px] text-muted-foreground'>
<Loader2 className='size-3 animate-spin' />
正在刷新详情
</div>
)}
<div className="gap-4 w-full max-w-[240px] mt-1 pt-4 border-t border-border/50">
<div className="flex flex-col gap-0.5">
<span className="text-[9px] uppercase tracking-widest text-muted-foreground font-medium">注册时间</span>
<span className="font-mono text-xs font-semibold">{formatDateTime(selectedUser.created_at).split(' ')[0]}</span>
<div className='gap-4 w-full max-w-[240px] mt-1 pt-4 border-t border-border/50'>
<div className='flex flex-col gap-0.5'>
<span className='text-[9px] uppercase tracking-widest text-muted-foreground font-medium'>
注册时间
</span>
<span className='font-mono text-xs font-semibold'>
{
formatDateTime(selectedUser.created_at).split(
' ',
)[0]
}
</span>
</div>
</div>
</div>
</div>
<div className="p-6 space-y-6">
<div className="space-y-4">
<h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider px-1">个人资料</h4>
<div className="rounded-lg border divide-y bg-background/50">
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<Mail className="size-3" />
<div className='p-6 space-y-6'>
<div className='space-y-4'>
<h4 className='text-xs font-semibold text-muted-foreground uppercase tracking-wider px-1'>
个人资料
</h4>
<div className='rounded-lg border divide-y bg-background/50'>
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<Mail className='size-3' />
邮箱
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.email)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.email)}
</span>
</div>
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<Smartphone className="size-3" />
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<Smartphone className='size-3' />
手机
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.phone)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.phone)}
</span>
</div>
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<span className="size-3 flex items-center justify-center font-bold text-[9px]">⚧</span>
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<span className='size-3 flex items-center justify-center font-bold text-[9px]'>
⚧
</span>
性别
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.gender)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.gender)}
</span>
</div>
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<MapPin className="size-3" />
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<MapPin className='size-3' />
所在地
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.location)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.location)}
</span>
</div>
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<Globe className="size-3" />
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<Globe className='size-3' />
网站
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.website)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.website)}
</span>
</div>
<div className="flex flex-col gap-2 p-3.5 text-sm">
<span className="text-[10px] text-muted-foreground">简介</span>
<span className="break-words text-[10px] leading-5">{displayValue(selectedUser.bio)}</span>
<div className='flex flex-col gap-2 p-3.5 text-sm'>
<span className='text-[10px] text-muted-foreground'>
简介
</span>
<span className='break-words text-[10px] leading-5'>
{displayValue(selectedUser.bio)}
</span>
</div>
</div>
</div>
<div className="space-y-4">
<h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider px-1">系统记录</h4>
<div className="rounded-lg border divide-y bg-background/50">
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">账户状态</span>
<Badge variant={selectedUser.is_active ? "secondary" : "outline"} className="text-[10px]">
{selectedUser.is_active ? "正常" : "禁用"}
<div className='space-y-4'>
<h4 className='text-xs font-semibold text-muted-foreground uppercase tracking-wider px-1'>
系统记录
</h4>
<div className='rounded-lg border divide-y bg-background/50'>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>账户状态</span>
<Badge
variant={
selectedUser.is_active ? 'secondary' : 'outline'
}
className='text-[10px]'
>
{selectedUser.is_active ? '正常' : '禁用'}
</Badge>
</div>
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">管理员</span>
<span className="font-mono text-[10px]">{selectedUser.is_admin ? "是" : "否"}</span>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>管理员</span>
<span className='font-mono text-[10px]'>
{selectedUser.is_admin ? '是' : '否'}
</span>
</div>
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">最后登录</span>
<span className="font-mono text-[10px]">{formatDateTime(selectedUser.last_login_at)}</span>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>最后登录</span>
<span className='font-mono text-[10px]'>
{formatDateTime(selectedUser.last_login_at)}
</span>
</div>
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">注册时间</span>
<span className="font-mono text-[10px]">{formatDateTime(selectedUser.created_at)}</span>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>注册时间</span>
<span className='font-mono text-[10px]'>
{formatDateTime(selectedUser.created_at)}
</span>
</div>
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">最后更新</span>
<span className="font-mono text-[10px]">{formatDateTime(selectedUser.updated_at)}</span>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>最后更新</span>
<span className='font-mono text-[10px]'>
{formatDateTime(selectedUser.updated_at)}
</span>
</div>
</div>
</div>
@@ -154,20 +210,20 @@ export function UserDetailSheet({
</div>
{!selectedUser.is_admin && (
<div className="p-4 border-t bg-background/80 backdrop-blur-md shrink-0">
<div className='p-4 border-t bg-background/80 backdrop-blur-md shrink-0'>
<Button
variant={selectedUser.is_active ? "destructive" : "default"}
className="w-full h-9 text-xs font-medium transition-all active:scale-[0.98]"
variant={selectedUser.is_active ? 'destructive' : 'default'}
className='w-full h-9 text-xs font-medium transition-all active:scale-[0.98]'
onClick={() => onStatusToggle(selectedUser)}
>
{selectedUser.is_active ? (
<>
<ShieldCheck className="size-3 mr-1" />
<ShieldCheck className='size-3 mr-1' />
封禁账户
</>
) : (
<>
<UserCheck className="size-3 mr-1" />
<UserCheck className='size-3 mr-1' />
解除封禁
</>
)}
@@ -179,5 +235,5 @@ export function UserDetailSheet({
)}
</SheetContent>
</Sheet>
)
);
}
@@ -1,14 +1,27 @@
"use client"
'use client';
import * as React from "react"
import {ChevronDown, ChevronLeft, ChevronRight, Filter, Loader2, Search, X} from "lucide-react"
import * as React from 'react';
import {
ChevronDown,
ChevronLeft,
ChevronRight,
Filter,
Loader2,
Search,
X,
} from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Badge} from "@/components/ui/badge"
import {Separator} from "@/components/ui/separator"
import {DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger} from "@/components/ui/dropdown-menu"
import {useAdminUsers} from "@/contexts/admin-users-context"
import {cn} from "@/lib/utils"
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Separator } from '@/components/ui/separator';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { useAdminUsers } from '@/contexts/admin-users-context';
import { cn } from '@/lib/utils';
export function UserFilterBar() {
const {
@@ -26,33 +39,35 @@ export function UserFilterBar() {
setSearchUsername,
setSearchEmail,
setStatusFilter,
fetchUsers
} = useAdminUsers()
fetchUsers,
} = useAdminUsers();
const totalPages = Math.ceil(total / pageSize)
const hasSearchFilter = Boolean(searchUserId || searchUsername || searchEmail)
const totalPages = Math.ceil(total / pageSize);
const hasSearchFilter = Boolean(
searchUserId || searchUsername || searchEmail,
);
return (
<div className="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-3">
<div className="flex items-center gap-2 flex-wrap">
<div className='flex flex-col lg:flex-row lg:items-center lg:justify-between gap-3'>
<div className='flex items-center gap-2 flex-wrap'>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
variant='outline'
size='sm'
className={cn(
"h-5 border-dashed text-[10px] font-medium shadow-none focus-visible:ring-0",
hasSearchFilter && "bg-primary/5 border-primary/20"
'h-5 border-dashed text-[10px] font-medium shadow-none focus-visible:ring-0',
hasSearchFilter && 'bg-primary/5 border-primary/20',
)}
>
<Search className="size-3 mr-1" />
<Search className='size-3 mr-1' />
搜索
{hasSearchFilter && (
<>
<Separator orientation="vertical" className="mx-1" />
<Separator orientation='vertical' className='mx-1' />
<Badge
variant="default"
className="text-[10px] h-3 px-1 rounded-full"
variant='default'
className='text-[10px] h-3 px-1 rounded-full'
>
!
</Badge>
@@ -60,35 +75,35 @@ export function UserFilterBar() {
)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-56 p-3" align="start">
<div className="space-y-2.5">
<DropdownMenuContent className='w-56 p-3' align='start'>
<div className='space-y-2.5'>
<input
className="w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background"
placeholder="输入用户 ID..."
className='w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background'
placeholder='输入用户 ID...'
value={searchUserId}
onChange={(e) => setSearchUserId(e.target.value)}
/>
<input
className="w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background"
placeholder="输入 username..."
className='w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background'
placeholder='输入 username...'
value={searchUsername}
onChange={(e) => setSearchUsername(e.target.value)}
/>
<input
className="w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background"
placeholder="输入邮箱..."
className='w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background'
placeholder='输入邮箱...'
value={searchEmail}
onChange={(e) => setSearchEmail(e.target.value)}
/>
{hasSearchFilter && (
<Button
variant="ghost"
size="sm"
className="w-full h-6 text-xs"
variant='ghost'
size='sm'
className='w-full h-6 text-xs'
onClick={() => {
setSearchUserId("")
setSearchUsername("")
setSearchEmail("")
setSearchUserId('');
setSearchUsername('');
setSearchEmail('');
}}
>
清除
@@ -101,21 +116,21 @@ export function UserFilterBar() {
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
variant='outline'
size='sm'
className={cn(
"h-5 border-dashed text-[10px] font-medium shadow-none focus-visible:ring-0",
statusFilter !== 'all' && "bg-primary/5 border-primary/20"
'h-5 border-dashed text-[10px] font-medium shadow-none focus-visible:ring-0',
statusFilter !== 'all' && 'bg-primary/5 border-primary/20',
)}
>
<Filter className="size-3" />
<Filter className='size-3' />
状态
{statusFilter !== 'all' && (
<>
<Separator orientation="vertical" className="mx-1" />
<Separator orientation='vertical' className='mx-1' />
<Badge
variant="default"
className="text-[10px] h-3 px-1 rounded-full"
variant='default'
className='text-[10px] h-3 px-1 rounded-full'
>
1
</Badge>
@@ -123,107 +138,130 @@ export function UserFilterBar() {
)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-[120px]" align="start">
<DropdownMenuContent className='w-[120px]' align='start'>
<DropdownMenuItem
onSelect={(e) => { e.preventDefault(); setStatusFilter('all') }}
onSelect={(e) => {
e.preventDefault();
setStatusFilter('all');
}}
>
<div className={cn(
"mr-2 flex size-3 items-center justify-center rounded-sm border border-primary",
statusFilter === 'all'
? "bg-primary text-primary-foreground"
: "opacity-50"
)} />
<span className="text-xs">全部状态</span>
<div
className={cn(
'mr-2 flex size-3 items-center justify-center rounded-sm border border-primary',
statusFilter === 'all'
? 'bg-primary text-primary-foreground'
: 'opacity-50',
)}
/>
<span className='text-xs'>全部状态</span>
</DropdownMenuItem>
<DropdownMenuItem
onSelect={(e) => { e.preventDefault(); setStatusFilter('active') }}
onSelect={(e) => {
e.preventDefault();
setStatusFilter('active');
}}
>
<div className={cn(
"mr-2 flex size-3 items-center justify-center rounded-sm border border-primary",
statusFilter === 'active'
? "bg-primary text-primary-foreground"
: "opacity-50"
)} />
<span className="text-xs">正常</span>
<div
className={cn(
'mr-2 flex size-3 items-center justify-center rounded-sm border border-primary',
statusFilter === 'active'
? 'bg-primary text-primary-foreground'
: 'opacity-50',
)}
/>
<span className='text-xs'>正常</span>
</DropdownMenuItem>
<DropdownMenuItem
onSelect={(e) => { e.preventDefault(); setStatusFilter('inactive') }}
onSelect={(e) => {
e.preventDefault();
setStatusFilter('inactive');
}}
>
<div className={cn(
"mr-2 flex size-3 items-center justify-center rounded-sm border border-primary",
statusFilter === 'inactive'
? "bg-primary text-primary-foreground"
: "opacity-50"
)} />
<span className="text-xs">禁用</span>
<div
className={cn(
'mr-2 flex size-3 items-center justify-center rounded-sm border border-primary',
statusFilter === 'inactive'
? 'bg-primary text-primary-foreground'
: 'opacity-50',
)}
/>
<span className='text-xs'>禁用</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
{(hasSearchFilter || statusFilter !== 'all') && (
<>
<Separator orientation="vertical" className="h-6 hidden sm:block" />
<Separator orientation='vertical' className='h-6 hidden sm:block' />
<Button
variant="ghost"
size="sm"
variant='ghost'
size='sm'
onClick={() => {
setSearchUserId("")
setSearchUsername("")
setSearchEmail("")
setStatusFilter('all')
setSearchUserId('');
setSearchUsername('');
setSearchEmail('');
setStatusFilter('all');
}}
className="h-5 px-2 lg:px-3 text-[11px] font-medium text-muted-foreground hover:text-foreground"
className='h-5 px-2 lg:px-3 text-[11px] font-medium text-muted-foreground hover:text-foreground'
>
<X className="size-3" />
<X className='size-3' />
清空筛选
</Button>
</>
)}
</div>
<Separator className="lg:hidden" />
<Separator className='lg:hidden' />
<div className="flex items-center gap-1.5 self-end lg:self-auto">
<span className="text-[10px] text-muted-foreground whitespace-nowrap">
<div className='flex items-center gap-1.5 self-end lg:self-auto'>
<span className='text-[10px] text-muted-foreground whitespace-nowrap'>
{total} 条记录
</span>
<div className="flex items-center border border-dashed rounded-md shadow-none">
<div className='flex items-center border border-dashed rounded-md shadow-none'>
<Button
variant="ghost"
size="icon"
className="h-5.5 w-6 rounded-none rounded-l-md disabled:opacity-30"
variant='ghost'
size='icon'
className='h-5.5 w-6 rounded-none rounded-l-md disabled:opacity-30'
onClick={() => setPage(Math.max(1, page - 1))}
disabled={page <= 1 || loading}
>
<ChevronLeft className="size-3" />
<ChevronLeft className='size-3' />
</Button>
<span className="text-[10px] font-mono text-muted-foreground px-2 border-x border-dashed">
<span className='text-[10px] font-mono text-muted-foreground px-2 border-x border-dashed'>
{page}/{totalPages}
</span>
<Button
variant="ghost"
size="icon"
className="h-5.5 w-6 rounded-none rounded-r-md disabled:opacity-30"
variant='ghost'
size='icon'
className='h-5.5 w-6 rounded-none rounded-r-md disabled:opacity-30'
onClick={() => setPage(Math.min(totalPages, page + 1))}
disabled={page >= totalPages || loading}
>
<ChevronRight className="size-3" />
<ChevronRight className='size-3' />
</Button>
</div>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="sm" className="h-6 border-dashed text-[10px] px-2 font-mono shadow-none" disabled={loading}>
<Button
variant='outline'
size='sm'
className='h-6 border-dashed text-[10px] px-2 font-mono shadow-none'
disabled={loading}
>
{pageSize}条/页
<ChevronDown className="size-3 opacity-50" />
<ChevronDown className='size-3 opacity-50' />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{[20, 50, 100].map(size => (
<DropdownMenuContent align='end'>
{[20, 50, 100].map((size) => (
<DropdownMenuItem
key={size}
onClick={() => setPageSize(size)}
className={cn("font-mono text-xs", pageSize === size && "bg-accent")}
className={cn(
'font-mono text-xs',
pageSize === size && 'bg-accent',
)}
>
{size}条/页
</DropdownMenuItem>
@@ -232,16 +270,16 @@ export function UserFilterBar() {
</DropdownMenu>
<Button
variant="outline"
size="icon"
className="h-6 w-6 border-dashed shadow-none"
variant='outline'
size='icon'
className='h-6 w-6 border-dashed shadow-none'
onClick={() => fetchUsers(true)}
disabled={loading}
title="刷新数据"
title='刷新数据'
>
<Loader2 className={cn("size-3", loading && "animate-spin")} />
<Loader2 className={cn('size-3', loading && 'animate-spin')} />
</Button>
</div>
</div>
)
);
}