1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
"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 { toast } from "@/components/ui/sonner";
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";
export default function UserAvatar() {
const { t } = useTranslation();
const fileInputRef = useRef<HTMLInputElement>(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<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) {
return;
}
upload.mutate(file);
event.target.value = "";
};
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2 text-xl">
<ImageIcon className="h-5 w-5" />
{t("settings.info.avatar.title")}
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<p className="text-sm text-muted-foreground">
{t("settings.info.avatar.description")}
</p>
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div className="flex items-center gap-4">
<div className="flex size-16 items-center justify-center overflow-hidden rounded-full border bg-muted">
<UserAvatarImage
image={image}
name={t("settings.info.avatar.title")}
fallback={<User className="h-7 w-7 text-muted-foreground" />}
className="h-full w-full"
/>
</div>
<input
ref={fileInputRef}
type="file"
accept="image/*"
className="hidden"
onChange={handleFileChange}
/>
<ActionButton
type="button"
variant="secondary"
onClick={handleSelectFile}
loading={upload.isPending}
disabled={isBusy}
>
<Upload className="mr-2 h-4 w-4" />
{image
? t("settings.info.avatar.change")
: t("settings.info.avatar.upload")}
</ActionButton>
</div>
<ActionConfirmingDialog
title={t("settings.info.avatar.remove_confirm_title")}
description={
<p>{t("settings.info.avatar.remove_confirm_description")}</p>
}
actionButton={(setDialogOpen) => (
<ActionButton
type="button"
variant="destructive"
loading={updateAvatar.isPending}
onClick={() =>
updateAvatar.mutate(
{ assetId: null },
{
onSuccess: () => {
toast({
description: t("settings.info.avatar.removed"),
});
setDialogOpen(false);
},
},
)
}
>
{t("settings.info.avatar.remove")}
</ActionButton>
)}
>
<Button type="button" variant="outline" disabled={!image || isBusy}>
<X className="mr-2 h-4 w-4" />
{t("settings.info.avatar.remove")}
</Button>
</ActionConfirmingDialog>
</div>
</CardContent>
</Card>
);
}
|