-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
UserAvatar.vue
224 lines (204 loc) · 5.81 KB
/
UserAvatar.vue
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<script lang="ts" setup>
import { apiClient } from "@/utils/api-client";
import {
IconRiPencilFill,
IconAddCircle,
VButton,
VAvatar,
VDropdown,
VDropdownItem,
VModal,
VSpace,
Toast,
Dialog,
VLoading,
} from "@halo-dev/components";
import { ref, defineAsyncComponent, type Ref, toRefs } from "vue";
import { usePermission } from "@/utils/permission";
import { useQuery, useQueryClient } from "@tanstack/vue-query";
import { useI18n } from "vue-i18n";
import { useFileDialog } from "@vueuse/core";
import { computed } from "vue";
import { rbacAnnotations } from "@/constants/annotations";
const props = withDefaults(
defineProps<{
name?: string;
isCurrentUser?: boolean;
}>(),
{
name: "-",
isCurrentUser: false,
}
);
const { isCurrentUser, name } = toRefs(props);
const queryClient = useQueryClient();
const { currentUserHasPermission } = usePermission();
const { t } = useI18n();
const { data: avatar, isFetching } = useQuery({
queryKey: ["user-avatar", name, isCurrentUser],
queryFn: async () => {
const { data } = props.isCurrentUser
? await apiClient.user.getCurrentUserDetail()
: await apiClient.user.getUserDetail({
name: props.name,
});
const annotations = data?.user.metadata.annotations;
// Check avatar has been updated. if not, we need retry.
if (
annotations?.[rbacAnnotations.AVATAR_ATTACHMENT_NAME] !==
annotations?.[rbacAnnotations.LAST_AVATAR_ATTACHMENT_NAME]
) {
throw new Error("Avatar is not updated");
}
return data.user.spec.avatar || "";
},
retry: 5,
retryDelay: 1000,
});
const UserAvatarCropper = defineAsyncComponent(
() => import("./UserAvatarCropper.vue")
);
interface IUserAvatarCropperType
extends Ref<InstanceType<typeof UserAvatarCropper>> {
getCropperFile(): Promise<File>;
}
const { open, reset, onChange } = useFileDialog({
accept: ".jpg, .jpeg, .png",
multiple: false,
});
const userAvatarCropper = ref<IUserAvatarCropperType>();
const visibleCropperModal = ref(false);
const originalFile = ref<File>() as Ref<File>;
onChange((files) => {
if (!files) {
return;
}
if (files.length > 0) {
originalFile.value = files[0];
visibleCropperModal.value = true;
}
});
const uploadSaving = ref(false);
const handleUploadAvatar = () => {
userAvatarCropper.value?.getCropperFile().then((file) => {
uploadSaving.value = true;
apiClient.user
.uploadUserAvatar({
name: props.isCurrentUser ? "-" : props.name,
file: file,
})
.then(() => {
queryClient.invalidateQueries({ queryKey: ["user-avatar"] });
queryClient.invalidateQueries({ queryKey: ["user-detail"] });
handleCloseCropperModal();
})
.catch(() => {
Toast.error(t("core.components.user_avatar.toast_upload_failed"));
})
.finally(() => {
uploadSaving.value = false;
});
});
};
const handleRemoveCurrentAvatar = () => {
Dialog.warning({
title: t("core.components.user_avatar.remove.title"),
description: t("core.common.dialog.descriptions.cannot_be_recovered"),
confirmType: "danger",
confirmText: t("core.common.buttons.confirm"),
cancelText: t("core.common.buttons.cancel"),
onConfirm: async () => {
apiClient.user
.deleteUserAvatar({
name: props.isCurrentUser ? "-" : props.name,
})
.then(() => {
queryClient.invalidateQueries({ queryKey: ["user-avatar"] });
queryClient.invalidateQueries({ queryKey: ["user-detail"] });
})
.catch(() => {
Toast.error(t("core.components.user_avatar.toast_remove_failed"));
});
},
});
};
const handleCloseCropperModal = () => {
visibleCropperModal.value = false;
reset();
};
const changeUploadAvatar = () => {
reset();
open();
};
const hasAvatar = computed(() => {
return !!avatar.value;
});
</script>
<template>
<VLoading v-if="isFetching" class="h-full w-full" />
<div v-else class="group h-full w-full">
<VAvatar
:src="avatar"
:alt="props.name"
circle
width="100%"
height="100%"
class="ring-4 ring-white drop-shadow-md"
/>
<VDropdown
v-if="currentUserHasPermission(['system:users:manage']) || isCurrentUser"
>
<div
class="absolute left-0 right-0 top-0 hidden h-full w-full cursor-pointer items-center rounded-full border-0 bg-black/60 text-center transition-all duration-300 group-hover:flex"
:class="{
'!flex': !hasAvatar,
}"
>
<IconAddCircle
v-if="!hasAvatar"
class="inline-block w-full self-center text-2xl text-white"
/>
<IconRiPencilFill
v-else
class="inline-block w-full self-center text-2xl text-white"
/>
</div>
<template #popper>
<VDropdownItem @click="open()">
{{ $t("core.common.buttons.upload") }}
</VDropdownItem>
<VDropdownItem v-if="hasAvatar" @click="handleRemoveCurrentAvatar">
{{ $t("core.common.buttons.delete") }}
</VDropdownItem>
</template>
</VDropdown>
</div>
<VModal
:visible="visibleCropperModal"
:width="1200"
:title="$t('core.components.user_avatar.cropper_modal.title')"
mount-to-body
@update:visible="handleCloseCropperModal"
>
<UserAvatarCropper
ref="userAvatarCropper"
:file="originalFile"
@change-file="changeUploadAvatar"
/>
<template #footer>
<VSpace>
<VButton
v-if="visibleCropperModal"
:loading="uploadSaving"
type="secondary"
@click="handleUploadAvatar"
>
{{ $t("core.common.buttons.submit") }}
</VButton>
<VButton @click="handleCloseCropperModal">
{{ $t("core.common.buttons.cancel") }}
</VButton>
</VSpace>
</template>
</VModal>
</template>