2026-04-09 10:01:23 +08:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
import { useState } from "react"
|
|
|
|
|
import { CopyIcon } from "lucide-react"
|
|
|
|
|
import { toast } from "sonner"
|
|
|
|
|
|
|
|
|
|
import { type AdminUser } from "@/lib/api/admin"
|
2026-05-25 12:06:15 +08:00
|
|
|
import { useI18n } from "@/i18n/provider"
|
2026-04-09 10:01:23 +08:00
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
} from "@/components/ui/dialog"
|
|
|
|
|
|
|
|
|
|
type ResetPasswordDialogsProps = {
|
|
|
|
|
open: boolean
|
|
|
|
|
saving: boolean
|
|
|
|
|
item: AdminUser | null
|
|
|
|
|
password: string
|
|
|
|
|
onOpenChange: (open: boolean) => void
|
|
|
|
|
onConfirm: () => Promise<void>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ResetPasswordDialogs({
|
|
|
|
|
open,
|
|
|
|
|
saving,
|
|
|
|
|
item,
|
|
|
|
|
password,
|
|
|
|
|
onOpenChange,
|
|
|
|
|
onConfirm,
|
|
|
|
|
}: ResetPasswordDialogsProps) {
|
2026-05-25 12:06:15 +08:00
|
|
|
const t = useI18n()
|
2026-04-09 10:01:23 +08:00
|
|
|
const [copying, setCopying] = useState(false)
|
|
|
|
|
const showingResult = password.trim().length > 0
|
|
|
|
|
|
|
|
|
|
async function handleCopy() {
|
|
|
|
|
if (!password || copying) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setCopying(true)
|
|
|
|
|
try {
|
|
|
|
|
await navigator.clipboard.writeText(password)
|
2026-05-25 12:06:15 +08:00
|
|
|
toast.success(t("user.copied"))
|
2026-04-09 10:01:23 +08:00
|
|
|
} catch {
|
2026-05-25 12:06:15 +08:00
|
|
|
toast.error(t("user.copyFailed"))
|
2026-04-09 10:01:23 +08:00
|
|
|
} finally {
|
|
|
|
|
setCopying(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Dialog open={open && !showingResult} onOpenChange={onOpenChange}>
|
|
|
|
|
<DialogContent showCloseButton={!saving}>
|
|
|
|
|
<DialogHeader>
|
2026-05-25 12:06:15 +08:00
|
|
|
<DialogTitle>{t("user.confirmResetTitle")}</DialogTitle>
|
2026-04-09 10:01:23 +08:00
|
|
|
<DialogDescription>
|
2026-05-25 12:06:15 +08:00
|
|
|
{t("user.confirmResetDescription", { username: item?.username || "-" })}
|
2026-04-09 10:01:23 +08:00
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button onClick={() => void onConfirm()} disabled={saving}>
|
2026-05-25 12:06:15 +08:00
|
|
|
{saving ? t("user.resetting") : t("user.confirmReset")}
|
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>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
<Dialog open={open && showingResult} onOpenChange={onOpenChange}>
|
|
|
|
|
<DialogContent>
|
|
|
|
|
<DialogHeader>
|
2026-05-25 12:06:15 +08:00
|
|
|
<DialogTitle>{t("user.resetSuccessTitle")}</DialogTitle>
|
2026-04-09 10:01:23 +08:00
|
|
|
<DialogDescription>
|
2026-05-25 12:06:15 +08:00
|
|
|
{t("user.resetSuccessDescription", { username: item?.username || "-" })}
|
2026-04-09 10:01:23 +08:00
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
2026-06-21 10:09:52 +08:00
|
|
|
<div className="rounded-md border bg-muted/35 p-4">
|
2026-05-25 12:06:15 +08:00
|
|
|
<div className="text-xs text-muted-foreground">{t("user.newPassword")}</div>
|
2026-04-09 10:01:23 +08:00
|
|
|
<div className="mt-2 break-all font-mono text-base">{password}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button type="button" variant="outline" onClick={() => void handleCopy()} disabled={copying}>
|
|
|
|
|
<CopyIcon />
|
2026-05-25 12:06:15 +08:00
|
|
|
{copying ? t("user.copying") : t("user.copyPassword")}
|
2026-04-09 10:01:23 +08:00
|
|
|
</Button>
|
|
|
|
|
<Button type="button" onClick={() => onOpenChange(false)}>
|
2026-05-25 12:06:15 +08:00
|
|
|
{t("user.close")}
|
2026-04-09 10:01:23 +08:00
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|