2026-04-09 10:01:23 +08:00
|
|
|
"use client"
|
|
|
|
|
|
2026-05-25 12:06:15 +08:00
|
|
|
import { useEffect, useMemo, useState } from "react"
|
2026-04-09 10:01:23 +08:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
2026-06-09 19:50:41 +08:00
|
|
|
import { Controller, Resolver, useForm } from "react-hook-form"
|
2026-04-09 10:01:23 +08:00
|
|
|
import { z } from "zod/v4"
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
type AdminUser,
|
|
|
|
|
type UpdateAdminUserPayload,
|
|
|
|
|
fetchUserDetail,
|
|
|
|
|
} from "@/lib/api/admin"
|
2026-05-25 12:06:15 +08:00
|
|
|
import { useI18n } from "@/i18n/provider"
|
2026-06-09 19:50:41 +08:00
|
|
|
import { ImageInput } from "@/components/image-input"
|
2026-04-09 10:01:23 +08:00
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
|
import {
|
|
|
|
|
Drawer,
|
|
|
|
|
DrawerContent,
|
|
|
|
|
DrawerDescription,
|
|
|
|
|
DrawerFooter,
|
|
|
|
|
DrawerHeader,
|
|
|
|
|
DrawerTitle,
|
|
|
|
|
} from "@/components/ui/drawer"
|
|
|
|
|
import {
|
|
|
|
|
Field,
|
|
|
|
|
FieldContent,
|
|
|
|
|
FieldError,
|
|
|
|
|
FieldLabel,
|
|
|
|
|
} from "@/components/ui/field"
|
|
|
|
|
import { Input } from "@/components/ui/input"
|
|
|
|
|
|
|
|
|
|
type UserEditDrawerProps = {
|
|
|
|
|
open: boolean
|
|
|
|
|
saving: boolean
|
|
|
|
|
itemId: number | null
|
|
|
|
|
onOpenChange: (open: boolean) => void
|
|
|
|
|
onSubmit: (payload: UpdateAdminUserPayload) => Promise<void>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const emptyForm: EditForm = {
|
|
|
|
|
nickname: "",
|
|
|
|
|
avatar: "",
|
|
|
|
|
mobile: "",
|
|
|
|
|
email: "",
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 12:06:15 +08:00
|
|
|
type EditForm = {
|
|
|
|
|
nickname: string
|
|
|
|
|
avatar: string
|
|
|
|
|
mobile: string
|
|
|
|
|
email: string
|
|
|
|
|
}
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
function toNullableString(value: string) {
|
|
|
|
|
const output = value.trim()
|
|
|
|
|
return output ? output : null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildForm(item: AdminUser | null): EditForm {
|
|
|
|
|
if (!item) {
|
|
|
|
|
return emptyForm
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
nickname: item.nickname || "",
|
|
|
|
|
avatar: item.avatar || "",
|
|
|
|
|
mobile: item.mobile || "",
|
|
|
|
|
email: item.email || "",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildPayload(userId: number, form: EditForm): UpdateAdminUserPayload {
|
|
|
|
|
return {
|
|
|
|
|
id: userId,
|
|
|
|
|
nickname: form.nickname.trim(),
|
|
|
|
|
avatar: form.avatar.trim(),
|
|
|
|
|
mobile: toNullableString(form.mobile),
|
|
|
|
|
email: toNullableString(form.email),
|
|
|
|
|
remark: "",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function EditDrawer({
|
|
|
|
|
open,
|
|
|
|
|
saving,
|
|
|
|
|
itemId,
|
|
|
|
|
onOpenChange,
|
|
|
|
|
onSubmit,
|
|
|
|
|
}: UserEditDrawerProps) {
|
|
|
|
|
return (
|
|
|
|
|
<Drawer open={open} onOpenChange={onOpenChange} direction="right">
|
|
|
|
|
{open ? (
|
|
|
|
|
<UserEditDrawerBody
|
|
|
|
|
key={itemId ? `edit-${itemId}` : "edit"}
|
|
|
|
|
itemId={itemId}
|
|
|
|
|
saving={saving}
|
|
|
|
|
onOpenChange={onOpenChange}
|
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</Drawer>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UserEditDrawerBodyProps = {
|
|
|
|
|
saving: boolean
|
|
|
|
|
itemId: number | null
|
|
|
|
|
onOpenChange: (open: boolean) => void
|
|
|
|
|
onSubmit: (payload: UpdateAdminUserPayload) => Promise<void>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function UserEditDrawerBody({
|
|
|
|
|
saving,
|
|
|
|
|
itemId,
|
|
|
|
|
onOpenChange,
|
|
|
|
|
onSubmit,
|
|
|
|
|
}: UserEditDrawerBodyProps) {
|
2026-05-25 12:06:15 +08:00
|
|
|
const t = useI18n()
|
2026-04-09 10:01:23 +08:00
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
const [item, setItem] = useState<AdminUser | null>(null)
|
2026-05-25 12:06:15 +08:00
|
|
|
const editFormSchema = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
z.object({
|
|
|
|
|
nickname: z.string().trim().min(1, t("user.nicknameRequired")),
|
2026-06-09 19:50:41 +08:00
|
|
|
avatar: z.string().trim(),
|
2026-05-25 12:06:15 +08:00
|
|
|
mobile: z
|
|
|
|
|
.string()
|
|
|
|
|
.trim()
|
|
|
|
|
.refine(
|
|
|
|
|
(value) => value.length === 0 || /^[0-9+\-\s]{6,20}$/.test(value),
|
|
|
|
|
t("user.mobileInvalid")
|
|
|
|
|
),
|
|
|
|
|
email: z
|
|
|
|
|
.string()
|
|
|
|
|
.trim()
|
|
|
|
|
.refine(
|
|
|
|
|
(value) =>
|
|
|
|
|
value.length === 0 || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value),
|
|
|
|
|
t("user.emailInvalid")
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
[t]
|
|
|
|
|
)
|
|
|
|
|
const editFormResolver = useMemo(
|
|
|
|
|
() => zodResolver(editFormSchema as never) as Resolver<EditForm>,
|
|
|
|
|
[editFormSchema]
|
|
|
|
|
)
|
|
|
|
|
const form = useForm<EditForm>({
|
2026-04-09 10:01:23 +08:00
|
|
|
resolver: editFormResolver,
|
|
|
|
|
defaultValues: emptyForm,
|
|
|
|
|
})
|
|
|
|
|
const {
|
2026-06-09 19:50:41 +08:00
|
|
|
control,
|
2026-04-09 10:01:23 +08:00
|
|
|
handleSubmit,
|
|
|
|
|
reset,
|
|
|
|
|
register,
|
|
|
|
|
formState: { errors },
|
|
|
|
|
} = form
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
async function loadDetail() {
|
|
|
|
|
if (!itemId) {
|
|
|
|
|
setItem(null)
|
|
|
|
|
reset(emptyForm)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
setLoading(true)
|
|
|
|
|
try {
|
|
|
|
|
const data = await fetchUserDetail(itemId)
|
|
|
|
|
setItem(data)
|
|
|
|
|
reset(buildForm(data))
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to load user:", error)
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void loadDetail()
|
|
|
|
|
}, [itemId, reset])
|
|
|
|
|
|
|
|
|
|
async function onFormSubmit(values: EditForm) {
|
|
|
|
|
if (!itemId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await onSubmit(buildPayload(itemId, values))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-07-28 19:38:05 +08:00
|
|
|
<DrawerContent className="min-w-2xl overflow-hidden">
|
|
|
|
|
<DrawerHeader className="shrink-0">
|
2026-05-25 12:06:15 +08:00
|
|
|
<DrawerTitle>{t("user.editTitle")}</DrawerTitle>
|
|
|
|
|
<DrawerDescription>
|
|
|
|
|
{t("user.currentUser", { username: item?.username || "-" })}
|
|
|
|
|
</DrawerDescription>
|
2026-04-09 10:01:23 +08:00
|
|
|
</DrawerHeader>
|
|
|
|
|
{loading ? (
|
2026-07-28 19:38:05 +08:00
|
|
|
<div className="flex min-h-0 flex-1 items-center justify-center py-12">
|
2026-05-25 12:06:15 +08:00
|
|
|
<div className="text-muted-foreground">{t("user.loading")}</div>
|
2026-04-09 10:01:23 +08:00
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<form
|
2026-07-28 19:38:05 +08:00
|
|
|
className="flex min-h-0 flex-1 flex-col"
|
2026-04-09 10:01:23 +08:00
|
|
|
onSubmit={handleSubmit(onFormSubmit)}
|
|
|
|
|
>
|
2026-07-28 19:38:05 +08:00
|
|
|
<div className="min-h-0 flex-1 space-y-4 overflow-y-auto px-4 pb-4">
|
2026-04-09 10:01:23 +08:00
|
|
|
<Field data-invalid={!!errors.nickname}>
|
2026-05-25 12:06:15 +08:00
|
|
|
<FieldLabel htmlFor="user-nickname">{t("user.nickname")}</FieldLabel>
|
2026-04-09 10:01:23 +08:00
|
|
|
<FieldContent>
|
|
|
|
|
<Input
|
|
|
|
|
id="user-nickname"
|
2026-05-25 12:06:15 +08:00
|
|
|
placeholder={t("user.nicknamePlaceholder")}
|
2026-04-09 10:01:23 +08:00
|
|
|
aria-invalid={!!errors.nickname}
|
|
|
|
|
{...register("nickname")}
|
|
|
|
|
/>
|
|
|
|
|
<FieldError errors={[errors.nickname]} />
|
|
|
|
|
</FieldContent>
|
|
|
|
|
</Field>
|
2026-07-28 23:08:31 +08:00
|
|
|
<Field data-invalid={!!errors.avatar}>
|
2026-06-09 19:50:41 +08:00
|
|
|
<FieldLabel>{t("user.avatar")}</FieldLabel>
|
2026-04-09 10:01:23 +08:00
|
|
|
<FieldContent>
|
2026-06-09 19:50:41 +08:00
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name="avatar"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<ImageInput
|
|
|
|
|
value={field.value}
|
|
|
|
|
onChange={field.onChange}
|
|
|
|
|
disabled={saving || loading}
|
|
|
|
|
prefix="avatar"
|
|
|
|
|
placeholder={t("user.avatarPlaceholder")}
|
|
|
|
|
className="size-16 rounded-full"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-04-09 10:01:23 +08:00
|
|
|
/>
|
|
|
|
|
<FieldError errors={[errors.avatar]} />
|
|
|
|
|
</FieldContent>
|
|
|
|
|
</Field>
|
|
|
|
|
<Field data-invalid={!!errors.mobile}>
|
2026-05-25 12:06:15 +08:00
|
|
|
<FieldLabel htmlFor="user-mobile">{t("user.mobile")}</FieldLabel>
|
2026-04-09 10:01:23 +08:00
|
|
|
<FieldContent>
|
|
|
|
|
<Input
|
|
|
|
|
id="user-mobile"
|
2026-05-25 12:06:15 +08:00
|
|
|
placeholder={t("user.mobilePlaceholder")}
|
2026-04-09 10:01:23 +08:00
|
|
|
aria-invalid={!!errors.mobile}
|
|
|
|
|
{...register("mobile")}
|
|
|
|
|
/>
|
|
|
|
|
<FieldError errors={[errors.mobile]} />
|
|
|
|
|
</FieldContent>
|
|
|
|
|
</Field>
|
|
|
|
|
<Field data-invalid={!!errors.email}>
|
2026-05-25 12:06:15 +08:00
|
|
|
<FieldLabel htmlFor="user-email">{t("user.email")}</FieldLabel>
|
2026-04-09 10:01:23 +08:00
|
|
|
<FieldContent>
|
|
|
|
|
<Input
|
|
|
|
|
id="user-email"
|
2026-05-25 12:06:15 +08:00
|
|
|
placeholder={t("user.emailPlaceholder")}
|
2026-04-09 10:01:23 +08:00
|
|
|
aria-invalid={!!errors.email}
|
|
|
|
|
{...register("email")}
|
|
|
|
|
/>
|
|
|
|
|
<FieldError errors={[errors.email]} />
|
|
|
|
|
</FieldContent>
|
|
|
|
|
</Field>
|
|
|
|
|
</div>
|
2026-07-28 19:38:05 +08:00
|
|
|
<DrawerFooter className="shrink-0 border-t bg-background">
|
2026-04-09 10:01:23 +08:00
|
|
|
<Button type="submit" disabled={saving || loading}>
|
2026-05-25 12:06:15 +08:00
|
|
|
{saving ? t("user.saving") : t("user.saveEdit")}
|
2026-04-09 10:01:23 +08:00
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => onOpenChange(false)}
|
|
|
|
|
disabled={saving}
|
|
|
|
|
>
|
2026-05-25 12:06:15 +08:00
|
|
|
{t("user.cancel")}
|
2026-04-09 10:01:23 +08:00
|
|
|
</Button>
|
|
|
|
|
</DrawerFooter>
|
|
|
|
|
</form>
|
|
|
|
|
)}
|
|
|
|
|
</DrawerContent>
|
|
|
|
|
);
|
|
|
|
|
}
|