refactor to use react-hook-form and zod (#21195)
Some checks are pending
CI / AMD64 Build (push) Waiting to run
CI / ARM Build (push) Waiting to run
CI / Jetson Jetpack 6 (push) Waiting to run
CI / AMD64 Extra Build (push) Blocked by required conditions
CI / ARM Extra Build (push) Blocked by required conditions
CI / Synaptics Build (push) Blocked by required conditions
CI / Assemble and push default build (push) Blocked by required conditions

This commit is contained in:
Josh Hawkins 2025-12-08 10:19:34 -06:00 committed by GitHub
parent 152e585206
commit dfd837cfb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,6 @@
import { Button } from "../ui/button"; import { Button } from "../ui/button";
import { Input } from "../ui/input"; import { Input } from "../ui/input";
import { useState, useEffect } from "react"; import { useState, useEffect, useMemo } from "react";
import { import {
Dialog, Dialog,
DialogContent, DialogContent,
@ -9,14 +9,23 @@ import {
DialogHeader, DialogHeader,
DialogTitle, DialogTitle,
} from "../ui/dialog"; } from "../ui/dialog";
import {
import { Label } from "../ui/label"; Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "../ui/form";
import { LuCheck, LuX, LuEye, LuEyeOff, LuExternalLink } from "react-icons/lu"; import { LuCheck, LuX, LuEye, LuEyeOff, LuExternalLink } from "react-icons/lu";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useDocDomain } from "@/hooks/use-doc-domain"; import { useDocDomain } from "@/hooks/use-doc-domain";
import useSWR from "swr"; import useSWR from "swr";
import { formatSecondsToDuration } from "@/utils/dateUtil"; import { formatSecondsToDuration } from "@/utils/dateUtil";
import ActivityIndicator from "../indicators/activity-indicator"; import ActivityIndicator from "../indicators/activity-indicator";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
type SetPasswordProps = { type SetPasswordProps = {
show: boolean; show: boolean;
@ -44,11 +53,6 @@ export default function SetPasswordDialog({
const refreshTimeLabel = refreshSeconds const refreshTimeLabel = refreshSeconds
? formatSecondsToDuration(refreshSeconds) ? formatSecondsToDuration(refreshSeconds)
: "30 minutes"; : "30 minutes";
const [oldPassword, setOldPassword] = useState<string>("");
const [password, setPassword] = useState<string>("");
const [confirmPassword, setConfirmPassword] = useState<string>("");
const [passwordStrength, setPasswordStrength] = useState<number>(0);
const [error, setError] = useState<string | null>(null);
// visibility toggles for password fields // visibility toggles for password fields
const [showOldPassword, setShowOldPassword] = useState<boolean>(false); const [showOldPassword, setShowOldPassword] = useState<boolean>(false);
@ -56,92 +60,136 @@ export default function SetPasswordDialog({
useState<boolean>(false); useState<boolean>(false);
const [showConfirmPassword, setShowConfirmPassword] = const [showConfirmPassword, setShowConfirmPassword] =
useState<boolean>(false); useState<boolean>(false);
const [hasInitialized, setHasInitialized] = useState<boolean>(false);
// Password strength requirements // Create form schema with conditional old password requirement
const formSchema = useMemo(() => {
const baseSchema = {
password: z
.string()
.min(8, t("users.dialog.form.password.requirements.length"))
.regex(/[A-Z]/, t("users.dialog.form.password.requirements.uppercase"))
.regex(/\d/, t("users.dialog.form.password.requirements.digit"))
.regex(
/[!@#$%^&*(),.?":{}|<>]/,
t("users.dialog.form.password.requirements.special"),
),
confirmPassword: z.string(),
};
const requirements = { if (username) {
length: password.length >= 8, return z
uppercase: /[A-Z]/.test(password), .object({
digit: /\d/.test(password), oldPassword: z
special: /[!@#$%^&*(),.?":{}|<>]/.test(password), .string()
}; .min(1, t("users.dialog.passwordSetting.currentPasswordRequired")),
...baseSchema,
useEffect(() => { })
if (show) { .refine((data) => data.password === data.confirmPassword, {
if (!hasInitialized) { message: t("users.dialog.passwordSetting.doNotMatch"),
setOldPassword(""); path: ["confirmPassword"],
setPassword(""); });
setConfirmPassword("");
setError(null);
setHasInitialized(true);
}
} else { } else {
setHasInitialized(false); return z
.object(baseSchema)
.refine((data) => data.password === data.confirmPassword, {
message: t("users.dialog.passwordSetting.doNotMatch"),
path: ["confirmPassword"],
});
} }
}, [show, hasInitialized]); }, [username, t]);
useEffect(() => { type FormValues = z.infer<typeof formSchema>;
if (show && initialError) {
setError(initialError); const defaultValues = username
} ? {
}, [show, initialError]); oldPassword: "",
password: "",
confirmPassword: "",
}
: {
password: "",
confirmPassword: "",
};
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
mode: "onChange",
defaultValues: defaultValues as FormValues,
});
const password = form.watch("password");
const confirmPassword = form.watch("confirmPassword");
// Password strength calculation // Password strength calculation
const passwordStrength = useMemo(() => {
useEffect(() => { if (!password) return 0;
if (!password) {
setPasswordStrength(0);
return;
}
let strength = 0; let strength = 0;
if (requirements.length) strength += 1; if (password.length >= 8) strength += 1;
if (requirements.digit) strength += 1; if (/\d/.test(password)) strength += 1;
if (requirements.special) strength += 1; if (/[!@#$%^&*(),.?":{}|<>]/.test(password)) strength += 1;
if (requirements.uppercase) strength += 1; if (/[A-Z]/.test(password)) strength += 1;
setPasswordStrength(strength); return strength;
// we know that these deps are correct
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [password]); }, [password]);
const handleSave = async () => { const requirements = useMemo(
if (!password) { () => ({
setError(t("users.dialog.passwordSetting.cannotBeEmpty")); length: password?.length >= 8,
return; uppercase: /[A-Z]/.test(password || ""),
} digit: /\d/.test(password || ""),
special: /[!@#$%^&*(),.?":{}|<>]/.test(password || ""),
}),
[password],
);
// Validate all requirements // Reset form and visibility toggles when dialog opens/closes
if (!requirements.length) { useEffect(() => {
setError(t("users.dialog.form.password.requirements.length")); if (show) {
return; form.reset();
} setShowOldPassword(false);
if (!requirements.uppercase) { setShowPasswordVisible(false);
setError(t("users.dialog.form.password.requirements.uppercase")); setShowConfirmPassword(false);
return;
}
if (!requirements.digit) {
setError(t("users.dialog.form.password.requirements.digit"));
return;
}
if (!requirements.special) {
setError(t("users.dialog.form.password.requirements.special"));
return;
} }
}, [show, form]);
if (password !== confirmPassword) { // Handle backend errors
setError(t("users.dialog.passwordSetting.doNotMatch")); useEffect(() => {
return; if (show && initialError) {
const errorMsg = String(initialError);
// Check if the error is about incorrect current password
if (
errorMsg.toLowerCase().includes("current password is incorrect") ||
errorMsg.toLowerCase().includes("current password incorrect")
) {
if (username) {
form.setError("oldPassword" as keyof FormValues, {
type: "manual",
message: t("users.dialog.passwordSetting.incorrectCurrentPassword"),
});
}
} else {
// For other errors, show as form-level error
form.setError("root", {
type: "manual",
message: errorMsg,
});
}
} }
}, [show, initialError, form, t, username]);
// Require old password when changing own password (username is provided) const onSubmit = async (values: FormValues) => {
if (username && !oldPassword) { const oldPassword =
setError(t("users.dialog.passwordSetting.currentPasswordRequired")); "oldPassword" in values
return; ? (
} values as {
oldPassword: string;
onSave(password, oldPassword || undefined); password: string;
confirmPassword: string;
}
).oldPassword
: undefined;
onSave(values.password, oldPassword);
}; };
const getStrengthLabel = () => { const getStrengthLabel = () => {
@ -200,293 +248,311 @@ export default function SetPasswordDialog({
</p> </p>
</DialogHeader> </DialogHeader>
<div className="space-y-4 pt-4"> <Form {...form}>
{username && ( <form
<div className="space-y-2"> onSubmit={form.handleSubmit(onSubmit)}
<Label htmlFor="old-password"> className="space-y-4 pt-4"
{t("users.dialog.form.currentPassword.title")} >
</Label> {username && (
<div className="relative"> <FormField
<Input control={form.control}
id="old-password" name={"oldPassword" as keyof FormValues}
className="h-10 pr-10" render={({ field }) => (
type={showOldPassword ? "text" : "password"} <FormItem>
value={oldPassword} <FormLabel>
onChange={(event) => { {t("users.dialog.form.currentPassword.title")}
setOldPassword(event.target.value); </FormLabel>
setError(null); <FormControl>
}} <div className="relative">
placeholder={t( <Input
"users.dialog.form.currentPassword.placeholder", {...field}
)} type={showOldPassword ? "text" : "password"}
/> placeholder={t(
<Button "users.dialog.form.currentPassword.placeholder",
type="button" )}
variant="ghost" className="h-10 pr-10"
size="sm" />
tabIndex={-1} <Button
aria-label={ type="button"
showOldPassword variant="ghost"
? t("users.dialog.form.password.hide", { size="sm"
ns: "views/settings", tabIndex={-1}
}) aria-label={
: t("users.dialog.form.password.show", { showOldPassword
ns: "views/settings", ? t("users.dialog.form.password.hide", {
}) ns: "views/settings",
} })
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent" : t("users.dialog.form.password.show", {
onClick={() => setShowOldPassword(!showOldPassword)} ns: "views/settings",
> })
{showOldPassword ? ( }
<LuEyeOff className="size-4" /> className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
) : ( onClick={() => setShowOldPassword(!showOldPassword)}
<LuEye className="size-4" /> >
)} {showOldPassword ? (
</Button> <LuEyeOff className="size-4" />
</div> ) : (
</div> <LuEye className="size-4" />
)} )}
</Button>
<div className="space-y-2"> </div>
<Label htmlFor="password"> </FormControl>
{t("users.dialog.form.newPassword.title")} <FormMessage />
</Label> </FormItem>
<div className="relative">
<Input
id="password"
className="h-10 pr-10"
type={showPasswordVisible ? "text" : "password"}
value={password}
onChange={(event) => {
setPassword(event.target.value);
setError(null);
}}
placeholder={t("users.dialog.form.newPassword.placeholder")}
autoFocus
/>
<Button
type="button"
variant="ghost"
size="sm"
tabIndex={-1}
aria-label={
showPasswordVisible
? t("users.dialog.form.password.hide", {
ns: "views/settings",
})
: t("users.dialog.form.password.show", {
ns: "views/settings",
})
}
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowPasswordVisible(!showPasswordVisible)}
>
{showPasswordVisible ? (
<LuEyeOff className="size-4" />
) : (
<LuEye className="size-4" />
)}
</Button>
</div>
{password && (
<div className="mt-2 space-y-2">
<div className="flex h-1.5 w-full overflow-hidden rounded-full bg-secondary-foreground">
<div
className={`${getStrengthColor()} transition-all duration-300`}
style={{ width: `${(passwordStrength / 4) * 100}%` }}
/>
</div>
<p className="text-xs text-muted-foreground">
{t("users.dialog.form.password.strength.title")}
<span className="font-medium">{getStrengthLabel()}</span>
</p>
<div className="space-y-1 rounded-md bg-muted/50 p-2">
<p className="text-xs font-medium text-muted-foreground">
{t("users.dialog.form.password.requirements.title")}
</p>
<ul className="space-y-1">
<li className="flex items-center gap-2 text-xs">
{requirements.length ? (
<LuCheck className="size-3.5 text-green-500" />
) : (
<LuX className="size-3.5 text-red-500" />
)}
<span
className={
requirements.length
? "text-green-600"
: "text-red-600"
}
>
{t("users.dialog.form.password.requirements.length")}
</span>
</li>
<li className="flex items-center gap-2 text-xs">
{requirements.uppercase ? (
<LuCheck className="size-3.5 text-green-500" />
) : (
<LuX className="size-3.5 text-red-500" />
)}
<span
className={
requirements.uppercase
? "text-green-600"
: "text-red-600"
}
>
{t("users.dialog.form.password.requirements.uppercase")}
</span>
</li>
<li className="flex items-center gap-2 text-xs">
{requirements.digit ? (
<LuCheck className="size-3.5 text-green-500" />
) : (
<LuX className="size-3.5 text-red-500" />
)}
<span
className={
requirements.digit ? "text-green-600" : "text-red-600"
}
>
{t("users.dialog.form.password.requirements.digit")}
</span>
</li>
<li className="flex items-center gap-2 text-xs">
{requirements.special ? (
<LuCheck className="size-3.5 text-green-500" />
) : (
<LuX className="size-3.5 text-red-500" />
)}
<span
className={
requirements.special
? "text-green-600"
: "text-red-600"
}
>
{t("users.dialog.form.password.requirements.special")}
</span>
</li>
</ul>
</div>
</div>
)}
</div>
<div className="space-y-2">
<Label htmlFor="confirm-password">
{t("users.dialog.form.password.confirm.title")}
</Label>
<div className="relative">
<Input
id="confirm-password"
className="h-10 pr-10"
type={showConfirmPassword ? "text" : "password"}
value={confirmPassword}
onChange={(event) => {
setConfirmPassword(event.target.value);
setError(null);
}}
placeholder={t(
"users.dialog.form.newPassword.confirm.placeholder",
)} )}
/> />
<Button )}
type="button"
variant="ghost"
size="sm"
tabIndex={-1}
aria-label={
showConfirmPassword
? t("users.dialog.form.password.hide", {
ns: "views/settings",
})
: t("users.dialog.form.password.show", {
ns: "views/settings",
})
}
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
>
{showConfirmPassword ? (
<LuEyeOff className="size-4" />
) : (
<LuEye className="size-4" />
)}
</Button>
</div>
{/* Password match indicator */} <FormField
{password && confirmPassword && ( control={form.control}
<div className="mt-1 flex items-center gap-1.5 text-xs"> name="password"
{password === confirmPassword ? ( render={({ field }) => (
<> <FormItem>
<LuCheck className="size-3.5 text-green-500" /> <FormLabel>
<span className="text-green-600"> {t("users.dialog.form.newPassword.title")}
{t("users.dialog.form.password.match")} </FormLabel>
</span> <FormControl>
</> <div className="relative">
) : ( <Input
<> {...field}
<LuX className="size-3.5 text-red-500" /> type={showPasswordVisible ? "text" : "password"}
<span className="text-red-600"> placeholder={t(
{t("users.dialog.form.password.notMatch")} "users.dialog.form.newPassword.placeholder",
</span> )}
</> className="h-10 pr-10"
)} autoFocus
/>
<Button
type="button"
variant="ghost"
size="sm"
tabIndex={-1}
aria-label={
showPasswordVisible
? t("users.dialog.form.password.hide", {
ns: "views/settings",
})
: t("users.dialog.form.password.show", {
ns: "views/settings",
})
}
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() =>
setShowPasswordVisible(!showPasswordVisible)
}
>
{showPasswordVisible ? (
<LuEyeOff className="size-4" />
) : (
<LuEye className="size-4" />
)}
</Button>
</div>
</FormControl>
{password && (
<div className="mt-2 space-y-2">
<div className="flex h-1.5 w-full overflow-hidden rounded-full bg-secondary-foreground">
<div
className={`${getStrengthColor()} transition-all duration-300`}
style={{ width: `${(passwordStrength / 4) * 100}%` }}
/>
</div>
<p className="text-xs text-muted-foreground">
{t("users.dialog.form.password.strength.title")}
<span className="font-medium">
{getStrengthLabel()}
</span>
</p>
<div className="space-y-1 rounded-md bg-muted/50 p-2">
<p className="text-xs font-medium text-muted-foreground">
{t("users.dialog.form.password.requirements.title")}
</p>
<ul className="space-y-1">
<li className="flex items-center gap-2 text-xs">
{requirements.length ? (
<LuCheck className="size-3.5 text-green-500" />
) : (
<LuX className="size-3.5 text-red-500" />
)}
<span
className={
requirements.length
? "text-green-600"
: "text-red-600"
}
>
{t(
"users.dialog.form.password.requirements.length",
)}
</span>
</li>
<li className="flex items-center gap-2 text-xs">
{requirements.uppercase ? (
<LuCheck className="size-3.5 text-green-500" />
) : (
<LuX className="size-3.5 text-red-500" />
)}
<span
className={
requirements.uppercase
? "text-green-600"
: "text-red-600"
}
>
{t(
"users.dialog.form.password.requirements.uppercase",
)}
</span>
</li>
<li className="flex items-center gap-2 text-xs">
{requirements.digit ? (
<LuCheck className="size-3.5 text-green-500" />
) : (
<LuX className="size-3.5 text-red-500" />
)}
<span
className={
requirements.digit
? "text-green-600"
: "text-red-600"
}
>
{t(
"users.dialog.form.password.requirements.digit",
)}
</span>
</li>
<li className="flex items-center gap-2 text-xs">
{requirements.special ? (
<LuCheck className="size-3.5 text-green-500" />
) : (
<LuX className="size-3.5 text-red-500" />
)}
<span
className={
requirements.special
? "text-green-600"
: "text-red-600"
}
>
{t(
"users.dialog.form.password.requirements.special",
)}
</span>
</li>
</ul>
</div>
</div>
)}
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="confirmPassword"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("users.dialog.form.password.confirm.title")}
</FormLabel>
<FormControl>
<div className="relative">
<Input
{...field}
type={showConfirmPassword ? "text" : "password"}
placeholder={t(
"users.dialog.form.newPassword.confirm.placeholder",
)}
className="h-10 pr-10"
/>
<Button
type="button"
variant="ghost"
size="sm"
tabIndex={-1}
aria-label={
showConfirmPassword
? t("users.dialog.form.password.hide", {
ns: "views/settings",
})
: t("users.dialog.form.password.show", {
ns: "views/settings",
})
}
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() =>
setShowConfirmPassword(!showConfirmPassword)
}
>
{showConfirmPassword ? (
<LuEyeOff className="size-4" />
) : (
<LuEye className="size-4" />
)}
</Button>
</div>
</FormControl>
{password &&
confirmPassword &&
password === confirmPassword && (
<div className="mt-1 flex items-center gap-1.5 text-xs">
<LuCheck className="size-3.5 text-green-500" />
<span className="text-green-600">
{t("users.dialog.form.password.match")}
</span>
</div>
)}
<FormMessage />
</FormItem>
)}
/>
{form.formState.errors.root && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
{form.formState.errors.root.message}
</div> </div>
)} )}
</div>
{error && ( <DialogFooter className="flex flex-col-reverse gap-2 sm:flex-row sm:justify-end">
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive"> <div className="flex flex-1 flex-col justify-end">
{error} <div className="flex flex-row gap-2 pt-5">
</div> <Button
)} className="flex flex-1"
</div> aria-label={t("button.cancel", { ns: "common" })}
onClick={onCancel}
<DialogFooter className="flex flex-col-reverse gap-2 sm:flex-row sm:justify-end"> type="button"
<div className="flex flex-1 flex-col justify-end"> disabled={isLoading}
<div className="flex flex-row gap-2 pt-5"> >
<Button {t("button.cancel", { ns: "common" })}
className="flex flex-1" </Button>
aria-label={t("button.cancel", { ns: "common" })} <Button
onClick={onCancel} variant="select"
type="button" aria-label={t("button.save", { ns: "common" })}
disabled={isLoading} className="flex flex-1"
> type="submit"
{t("button.cancel", { ns: "common" })} disabled={isLoading || !form.formState.isValid}
</Button> >
<Button {isLoading ? (
variant="select" <div className="flex flex-row items-center gap-2">
aria-label={t("button.save", { ns: "common" })} <ActivityIndicator />
className="flex flex-1" <span>{t("button.saving", { ns: "common" })}</span>
onClick={handleSave} </div>
disabled={ ) : (
isLoading || t("button.save", { ns: "common" })
!password || )}
password !== confirmPassword || </Button>
(username && !oldPassword) || </div>
!requirements.length || </div>
!requirements.uppercase || </DialogFooter>
!requirements.digit || </form>
!requirements.special </Form>
}
>
{isLoading ? (
<div className="flex flex-row items-center gap-2">
<ActivityIndicator />
<span>{t("button.saving", { ns: "common" })}</span>
</div>
) : (
t("button.save", { ns: "common" })
)}
</Button>
</div>
</div>
</DialogFooter>
</DialogContent> </DialogContent>
</Dialog> </Dialog>
); );