-
Notifications
You must be signed in to change notification settings - Fork 0
/
Avatar.tsx
170 lines (156 loc) · 4.81 KB
/
Avatar.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
import Image from 'next/image'
import { useState } from 'react'
import {
generatePublicURLAvatar,
uploadPresignedPost,
} from '../services/cloud-service'
import { trpc } from '../util/trpc'
import { ImageUpload } from './ImageUpload'
/*
* For the love of god, this file is very similar to "PostSegmentImage".
* This is because I didn't want to build an abstraction YET.
* If you change something here, change it there as well.
*/
const SIZES = {
tiny: 20,
small: 40,
medium: 100,
large: 180,
} as const
type AvatarSize = keyof typeof SIZES
type ColorVariant = 'brown' | 'orange'
function AvatarPlaceholder({
sizePixels,
username,
variant = 'brown',
}: {
sizePixels: number
username: string
variant?: ColorVariant
}): JSX.Element {
return (
<div
className={`inline-flex items-center justify-center rounded-full text-center text-dlight ${
variant === 'brown' ? 'bg-dtertiary' : 'bg-dsecondary'
}`}
style={{ width: sizePixels, height: sizePixels }}
>
<p style={{ fontSize: sizePixels * 0.6 }} className="uppercase">
{username.charAt(0)}
</p>
</div>
)
}
type Props = {
userId: string
username: string
imageId: string | null
imageBlurDataURL: string | null
imageFileExtension: string | null
size: AvatarSize
isEditable?: boolean
placeholderColorVariant?: ColorVariant
}
export function Avatar({
userId,
username,
imageId,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
imageBlurDataURL,
imageFileExtension,
size = 'medium',
isEditable = false,
placeholderColorVariant = 'brown',
}: Props): JSX.Element {
const [sizePixels] = useState(() => SIZES[size])
const imageURL =
!imageId || !imageFileExtension
? null
: // This implementation assumes that getting the public URL does not need a remote fetch, as this runs on every rerender.
generatePublicURLAvatar({
userId,
imageId,
imageFileExtension,
})
const utils = trpc.useContext()
const avatarUploadMutation =
trpc.imageUpload.getPresignedUrlAvatar.useMutation()
const [isLoadingImageForUpload, setIsLoadingImageForUpload] = useState(
() => false,
)
return (
<div
className="relative h-full w-full"
style={{
width: sizePixels,
height: sizePixels,
}}
>
{/* TODO use EditOverlay instead */}
{isEditable && (
<div className="group absolute z-30 h-full w-full rounded-full hover:cursor-pointer hover:bg-dtertiary hover:bg-opacity-50">
<span className="grid h-full w-full place-items-center">
<ImageUpload
isRounded={true}
inputId={userId}
onUpload={async (fileToUpload) => {
avatarUploadMutation.mutate(
{
fileType: fileToUpload.type,
},
{
onSuccess: async (presignedPost) => {
setIsLoadingImageForUpload(true)
await uploadPresignedPost({
file: fileToUpload,
presignedPost,
onSuccess: () => {
// USER DATA
utils.user.byUserId.invalidate({ userId })
// POSTS DATA
utils.posts.invalidate()
},
})
},
},
)
}}
isLoadingUpload={
avatarUploadMutation.isLoading || isLoadingImageForUpload
}
/>
</span>
</div>
)}
{imageURL ? (
/*
* This <div> is only there to cut out a circle around the image, because Next.js' <Image> component
* does not respect its styling (like: "rounded-full") for the blur placeholder.
* Next.js v12.1.4
* See: https://github.com/vercel/next.js/issues/30033
*/
<div className="flex overflow-hidden rounded-full">
<Image
src={imageURL}
alt="Avatar"
height={sizePixels}
width={sizePixels}
// the following can be used to show a fade-in animation via placeholder, but it will delay when the image is visible
// placeholder="blur"
// blurDataURL={imageBlurDataURL ?? imageBlurDataURLFallback}
// className={`rounded-full duration-75 ease-in-out ${
// isLoadingImage ? 'blur-2xl grayscale' : 'blur-0 grayscale-0'
// }`}
onLoad={() => setIsLoadingImageForUpload(false)}
/>
</div>
) : (
<AvatarPlaceholder
sizePixels={sizePixels}
username={username}
variant={placeholderColorVariant}
/>
)}
</div>
)
}