-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFullAvatar.tsx
185 lines (175 loc) · 5.07 KB
/
FullAvatar.tsx
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
import Avatar from 'boring-avatars'
import clsx from 'clsx'
import { ChangeEvent } from 'react'
import { toast } from 'react-hot-toast'
import { FaCropAlt } from 'react-icons/fa'
import { UploadFileButton } from '@src/components/FileInput'
import { useConfirmModal } from '@src/context/ConfirmContext'
import { useUserData } from '@src/context/UserContext'
import { storage } from '@src/firebase'
import { useDisclosure } from '@src/hooks/useDisclosure'
import { onErrorToast } from '@src/hooks/useErrorToast'
import { CloseIcon } from '@src/icons/CloseIcon'
import {
ImageCropModal,
createImageFromFile,
getCroppedImage,
} from '@src/profile/ImageCropModal'
import {
useBigAvatar,
useUserBigAvatar,
useUserSmallAvatar,
} from '@src/profile/useAvartar'
import { Button } from '@src/ui/Button'
import { IconButton } from '@src/ui/IconButton'
export const FullAvatarUpload = () => {
const cropModal = useDisclosure()
const { user } = useUserData()
const { url, fetchUrl: reloadBigAvatar } = useUserBigAvatar()
const { fetchUrl: reloadSmallAvatar } = useUserSmallAvatar()
const onFileSelect = async (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files?.length === 0) return
const file = e.target.files?.[0]
if (file) {
try {
const image = await createImageFromFile(file)
const croppedImage = await getCroppedImage(image)
if (croppedImage) {
onUpload(croppedImage)
}
} catch (e: any) {
onErrorToast(e)
}
}
}
const onUpload = (file: File) => {
if (user) {
const uploadTask = storage
.ref(`images/${user.id}.jpeg`)
.put(file, { contentType: 'image/jpeg' })
uploadTask.on(
'state_changed',
() => {
// const progress = Math.round(
// (snapshot.bytesTransferred / snapshot.totalBytes) * 100
// )
// setProgress(progress)
},
(error) => onErrorToast(error),
() => {
reloadBigAvatar()
toast.success(
<div>
<b>อัปโหลดรูปสำเร็จ!</b>
<p>กดรีเฟรชหากรูปของคุณยังไม่เปลี่ยน</p>
</div>,
{ duration: 4000 }
)
setTimeout(() => {
reloadSmallAvatar()
}, 1000)
}
)
}
}
const confirm = useConfirmModal()
const onRemove = () => {
if (!user) {
return
}
confirm({
cancleText: 'ยกเลิก',
submitText: 'ยืนยัน',
title: 'ยืนยันลบโปรไฟล์',
subtitle: 'คุณยืนยันที่จะลบรูปโปรไฟล์ของคุณหรือไม่',
onSubmit: async () => {
try {
await Promise.all([
storage.ref(`images/${user.id}.jpeg`).delete(),
storage.ref(`images/${user.id}_32.jpeg`).delete(),
])
reloadBigAvatar()
reloadSmallAvatar()
} catch {}
},
})
}
return (
<div>
<div className="group relative w-80 flex-1">
<AvatarImage
url={url}
name={user!.showName}
className="group-hover:brightness-50"
/>
{url && (
<div className="invisible absolute top-1 right-1 flex gap-2 group-hover:visible">
<IconButton
icon={<CloseIcon />}
size="xs"
aria-label="remove-profile-picture"
onClick={onRemove}
/>
</div>
)}
<div className="invisible absolute top-1/2 right-1/2 flex translate-x-1/2 -translate-y-1/2 gap-2 group-hover:visible">
<UploadFileButton accept=".png,.jpg,.jpeg" onChange={onFileSelect} />
{url && (
<>
<Button
size="sm"
aria-label="edit-profile-image"
leftIcon={<FaCropAlt />}
onClick={cropModal.onOpen}
>
ตัดภาพ
</Button>
<ImageCropModal {...cropModal} />
</>
)}
</div>
</div>
</div>
)
}
export interface FullAvatarProps {
userId: number
name: string
}
export const FullAvatar = ({ userId, name }: FullAvatarProps) => {
const { url } = useBigAvatar(userId)
return <AvatarImage url={url} name={name} />
}
export interface AvatarImageProps {
url: string | undefined | null
name: string
className?: string
}
export const AvatarImage = ({ url, name, className }: AvatarImageProps) => {
return url ? (
<img
className={clsx(
'h-80 w-80 rounded-md bg-gray-300 object-cover',
className
)}
src={url}
/>
) : (
<div className={clsx('h-80 w-80 overflow-hidden rounded-md', className)}>
<Avatar
square
size={320}
name={name}
variant="beam"
colors={[
'#ffd5ae',
'#b1e9fc',
'#b9f6ba',
'#ffb1b2',
'#ffe0ae',
'#CBD5E0',
]}
/>
</div>
)
}