mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
276 lines
7.7 KiB
TypeScript
276 lines
7.7 KiB
TypeScript
import { AlertBlock } from "@/components/shared/alert-block";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Card,
|
|
CardContent,
|
|
CardDescription,
|
|
CardHeader,
|
|
CardTitle,
|
|
} from "@/components/ui/card";
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
} from "@/components/ui/form";
|
|
import { Input } from "@/components/ui/input";
|
|
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
|
|
import { generateSHA256Hash } from "@/lib/utils";
|
|
import { api } from "@/utils/api";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { Loader2, User } from "lucide-react";
|
|
import { useTranslation } from "next-i18next";
|
|
import { useEffect, useMemo, useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import { toast } from "sonner";
|
|
import { z } from "zod";
|
|
import { Disable2FA } from "./disable-2fa";
|
|
import { Enable2FA } from "./enable-2fa";
|
|
|
|
const profileSchema = z.object({
|
|
email: z.string(),
|
|
password: z.string().nullable(),
|
|
currentPassword: z.string().nullable(),
|
|
image: z.string().optional(),
|
|
});
|
|
|
|
type Profile = z.infer<typeof profileSchema>;
|
|
|
|
const randomImages = [
|
|
"/avatars/avatar-1.png",
|
|
"/avatars/avatar-2.png",
|
|
"/avatars/avatar-3.png",
|
|
"/avatars/avatar-4.png",
|
|
"/avatars/avatar-5.png",
|
|
"/avatars/avatar-6.png",
|
|
"/avatars/avatar-7.png",
|
|
"/avatars/avatar-8.png",
|
|
"/avatars/avatar-9.png",
|
|
"/avatars/avatar-10.png",
|
|
"/avatars/avatar-11.png",
|
|
"/avatars/avatar-12.png",
|
|
];
|
|
|
|
export const ProfileForm = () => {
|
|
const _utils = api.useUtils();
|
|
const { data, refetch, isLoading } = api.user.get.useQuery();
|
|
|
|
console.log(data);
|
|
const {
|
|
mutateAsync,
|
|
isLoading: isUpdating,
|
|
isError,
|
|
error,
|
|
} = api.user.update.useMutation();
|
|
const { t } = useTranslation("settings");
|
|
const [gravatarHash, setGravatarHash] = useState<string | null>(null);
|
|
|
|
const availableAvatars = useMemo(() => {
|
|
if (gravatarHash === null) return randomImages;
|
|
return randomImages.concat([
|
|
`https://www.gravatar.com/avatar/${gravatarHash}`,
|
|
]);
|
|
}, [gravatarHash]);
|
|
|
|
const form = useForm<Profile>({
|
|
defaultValues: {
|
|
email: data?.user?.email || "",
|
|
password: "",
|
|
image: data?.user?.image || "",
|
|
currentPassword: "",
|
|
},
|
|
resolver: zodResolver(profileSchema),
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
form.reset(
|
|
{
|
|
email: data?.user?.email || "",
|
|
password: form.getValues("password") || "",
|
|
image: data?.user?.image || "",
|
|
currentPassword: form.getValues("currentPassword") || "",
|
|
},
|
|
{
|
|
keepValues: true,
|
|
},
|
|
);
|
|
|
|
if (data.user.email) {
|
|
generateSHA256Hash(data.user.email).then((hash) => {
|
|
setGravatarHash(hash);
|
|
});
|
|
}
|
|
}
|
|
}, [form, data]);
|
|
|
|
const onSubmit = async (values: Profile) => {
|
|
await mutateAsync({
|
|
email: values.email.toLowerCase(),
|
|
password: values.password || undefined,
|
|
image: values.image,
|
|
currentPassword: values.currentPassword || undefined,
|
|
})
|
|
.then(async () => {
|
|
await refetch();
|
|
toast.success("Profile Updated");
|
|
form.reset({
|
|
email: values.email,
|
|
password: "",
|
|
image: values.image,
|
|
currentPassword: "",
|
|
});
|
|
})
|
|
.catch(() => {
|
|
toast.error("Error updating the profile");
|
|
});
|
|
};
|
|
|
|
return (
|
|
<div className="w-full">
|
|
<Card className="h-full bg-sidebar p-2.5 rounded-xl max-w-5xl mx-auto">
|
|
<div className="rounded-xl bg-background shadow-md ">
|
|
<CardHeader className="flex flex-row gap-2 flex-wrap justify-between items-center">
|
|
<div>
|
|
<CardTitle className="text-xl flex flex-row gap-2">
|
|
<User className="size-6 text-muted-foreground self-center" />
|
|
{t("settings.profile.title")}
|
|
</CardTitle>
|
|
<CardDescription>
|
|
{t("settings.profile.description")}
|
|
</CardDescription>
|
|
</div>
|
|
{!data?.user.twoFactorEnabled ? <Enable2FA /> : <Disable2FA />}
|
|
</CardHeader>
|
|
|
|
<CardContent className="space-y-2 py-8 border-t">
|
|
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
|
|
{isLoading ? (
|
|
<div className="flex flex-row gap-2 items-center justify-center text-sm text-muted-foreground min-h-[35vh]">
|
|
<span>Loading...</span>
|
|
<Loader2 className="animate-spin size-4" />
|
|
</div>
|
|
) : (
|
|
<>
|
|
<Form {...form}>
|
|
<form
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
className="grid gap-4"
|
|
>
|
|
<div className="space-y-4">
|
|
<FormField
|
|
control={form.control}
|
|
name="email"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("settings.profile.email")}</FormLabel>
|
|
<FormControl>
|
|
<Input
|
|
placeholder={t("settings.profile.email")}
|
|
{...field}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="currentPassword"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Current Password</FormLabel>
|
|
<FormControl>
|
|
<Input
|
|
type="password"
|
|
placeholder={t("settings.profile.password")}
|
|
{...field}
|
|
value={field.value || ""}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="password"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
{t("settings.profile.password")}
|
|
</FormLabel>
|
|
<FormControl>
|
|
<Input
|
|
type="password"
|
|
placeholder={t("settings.profile.password")}
|
|
{...field}
|
|
value={field.value || ""}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="image"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>
|
|
{t("settings.profile.avatar")}
|
|
</FormLabel>
|
|
<FormControl>
|
|
<RadioGroup
|
|
onValueChange={(e) => {
|
|
field.onChange(e);
|
|
}}
|
|
defaultValue={field.value}
|
|
value={field.value}
|
|
className="flex flex-row flex-wrap gap-2 max-xl:justify-center"
|
|
>
|
|
{availableAvatars.map((image) => (
|
|
<FormItem key={image}>
|
|
<FormLabel className="[&:has([data-state=checked])>img]:border-primary [&:has([data-state=checked])>img]:border-1 [&:has([data-state=checked])>img]:p-px cursor-pointer">
|
|
<FormControl>
|
|
<RadioGroupItem
|
|
value={image}
|
|
className="sr-only"
|
|
/>
|
|
</FormControl>
|
|
|
|
<img
|
|
key={image}
|
|
src={image}
|
|
alt="avatar"
|
|
className="h-12 w-12 rounded-full border hover:p-px hover:border-primary transition-transform"
|
|
/>
|
|
</FormLabel>
|
|
</FormItem>
|
|
))}
|
|
</RadioGroup>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</div>
|
|
<div className="flex items-center justify-end gap-2">
|
|
<Button type="submit" isLoading={isUpdating}>
|
|
{t("settings.common.save")}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</Form>
|
|
</>
|
|
)}
|
|
</CardContent>
|
|
</div>
|
|
</Card>
|
|
</div>
|
|
);
|
|
};
|