"use client"; import type { ChangeEvent } from "react"; import { useRef } from "react"; import { ActionButton } from "@/components/ui/action-button"; import ActionConfirmingDialog from "@/components/ui/action-confirming-dialog"; import { UserAvatar as UserAvatarImage } from "@/components/ui/user-avatar"; import useUpload from "@/lib/hooks/upload-file"; import { useTranslation } from "@/lib/i18n/client"; import { Image as ImageIcon, Upload, User, X } from "lucide-react"; import { useUpdateUserAvatar, useWhoAmI, } from "@karakeep/shared-react/hooks/users"; import { Button } from "../ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; import { toast } from "../ui/use-toast"; export default function UserAvatar() { const { t } = useTranslation(); const fileInputRef = useRef(null); const whoami = useWhoAmI(); const image = whoami.data?.image ?? null; const updateAvatar = useUpdateUserAvatar({ onError: () => { toast({ description: t("common.something_went_wrong"), variant: "destructive", }); }, }); const upload = useUpload({ onSuccess: async (resp) => { try { await updateAvatar.mutateAsync({ assetId: resp.assetId }); toast({ description: t("settings.info.avatar.updated"), }); } catch { // handled in onError } }, onError: (err) => { toast({ description: err.error, variant: "destructive", }); }, }); const isBusy = upload.isPending || updateAvatar.isPending; const handleSelectFile = () => { fileInputRef.current?.click(); }; const handleFileChange = (event: ChangeEvent) => { const file = event.target.files?.[0]; if (!file) { return; } upload.mutate(file); event.target.value = ""; }; return ( {t("settings.info.avatar.title")}

{t("settings.info.avatar.description")}

} className="h-full w-full" />
{image ? t("settings.info.avatar.change") : t("settings.info.avatar.upload")}
{t("settings.info.avatar.remove_confirm_description")}

} actionButton={(setDialogOpen) => ( updateAvatar.mutate( { assetId: null }, { onSuccess: () => { toast({ description: t("settings.info.avatar.removed"), }); setDialogOpen(false); }, }, ) } > {t("settings.info.avatar.remove")} )} >
); }