-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate AWS S3 and CloudFront for handling user avatar updates
- Loading branch information
Showing
23 changed files
with
4,186 additions
and
1,752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,5 @@ yarn-error.log* | |
next-env.d.ts | ||
|
||
.idea | ||
|
||
/private |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"use client" | ||
|
||
import {FC, Fragment, ReactElement, RefObject, useRef} from "react"; | ||
import {Input} from "@nextui-org/input"; | ||
import MediaType from "@/app/api/utils/MediaType"; | ||
import toast from "react-hot-toast"; | ||
import UploadS3File from "@/app/(site)/hooks/s3/UploadS3File"; | ||
|
||
export type FileUploadProps = { | ||
uploadKey?: string, | ||
/** | ||
* Used to update an image to prevent the cluttering | ||
* of the S3 bucket and CloudFront CDN. | ||
*/ | ||
oldKey?: string, | ||
/** | ||
* Must end with a forward slash. | ||
*/ | ||
uploadPath?: string, | ||
isPublicObject?: boolean, | ||
onUploadStart?: (file: File) => void, | ||
onUploadSuccess?: (key: string) => void, | ||
onUploadError?: (error: string) => void, | ||
onFileRemove?: () => void, | ||
disabled?: boolean, | ||
fileTypes: MediaType[] | ||
children?: (inputRef: RefObject<HTMLInputElement>) => ReactElement | ReactElement[], | ||
showToast?: boolean, | ||
toastOptions?: { | ||
uploadingMsg?: string, | ||
successMsg?: string, | ||
errorHandler?: (error: string) => any | ||
} | ||
} | ||
|
||
export const FileUpload: FC<FileUploadProps> = ({ | ||
uploadKey, | ||
oldKey, | ||
uploadPath, | ||
onUploadStart, | ||
onUploadSuccess, | ||
onUploadError, | ||
onFileRemove, | ||
fileTypes, | ||
disabled, | ||
children, | ||
showToast, | ||
toastOptions, | ||
isPublicObject | ||
}) => { | ||
const {trigger: triggerUpload} = UploadS3File() | ||
const inputRef = useRef<HTMLInputElement>(null) | ||
|
||
return ( | ||
<Fragment> | ||
<Input | ||
ref={inputRef} | ||
type="file" | ||
className="hidden" | ||
accept={fileTypes.join(",")} | ||
isDisabled={disabled} | ||
onChange={async (e) => { | ||
e.preventDefault() | ||
|
||
const upload = async () => { | ||
const allFiles = e.target.files; | ||
if (!allFiles || !allFiles.length) { | ||
if (onFileRemove) | ||
onFileRemove(); | ||
return Promise.resolve(); | ||
} | ||
const file = allFiles[0]; | ||
if (onUploadStart) | ||
onUploadStart(file); | ||
|
||
return triggerUpload({ | ||
body: { | ||
file, | ||
key: uploadKey, | ||
oldKey, | ||
path: uploadPath, | ||
isPublic: isPublicObject | ||
} | ||
}).then((res) => { | ||
if (onUploadSuccess) | ||
onUploadSuccess(res.data.key.replace("avatars/", "")); | ||
} | ||
).catch(e => { | ||
if (onUploadError) | ||
onUploadError(e.message); | ||
}); | ||
}; | ||
|
||
if (showToast) { | ||
const defaultErrorHandler = (msg: string) => `There was an error uploading a new avatar: ${msg}`; | ||
await toast.promise( | ||
upload(), | ||
{ | ||
loading: toastOptions?.uploadingMsg ?? "Uploading new avatar...", | ||
success: toastOptions?.successMsg ?? "Successfully updated new avatar!", | ||
error: toastOptions?.errorHandler ?? defaultErrorHandler | ||
} | ||
) | ||
} else await upload() | ||
|
||
e.target.files = null | ||
}} | ||
/> | ||
{children && children(inputRef)} | ||
</Fragment> | ||
) | ||
} | ||
|
||
export default FileUpload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/app/(site)/components/inputs/editable/EditableAvatar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {FC} from "react"; | ||
import FileUpload, {FileUploadProps} from "@/app/(site)/components/FileUpload"; | ||
import MediaType from "@/app/api/utils/MediaType"; | ||
import {Member} from "@prisma/client"; | ||
import useCloudFrontUrl from "@/app/(site)/hooks/s3/useCloudFrontUrl"; | ||
import {Avatar, AvatarProps, Skeleton} from "@nextui-org/react"; | ||
import clsx from "clsx"; | ||
import {AnimatePresence} from "framer-motion"; | ||
|
||
type Props = { | ||
editEnabled?: boolean, | ||
member?: Member, | ||
avatarUrl?: string, | ||
srcOverride?: string, | ||
} | ||
& Omit<AvatarProps, "src" | "onClick"> | ||
& Pick<FileUploadProps, "onFileRemove" | "onUploadSuccess" | "onUploadError" | "onUploadStart"> | ||
|
||
const EditableMemberAvatar: FC<Props> = ({ | ||
editEnabled, | ||
member, | ||
avatarUrl, | ||
onFileRemove, | ||
onUploadSuccess, | ||
onUploadStart, | ||
onUploadError, | ||
srcOverride, | ||
...avatarProps | ||
}) => { | ||
const { | ||
data: fetchedAvatarUrl, | ||
isLoading: avatarIsLoading | ||
} = useCloudFrontUrl(avatarUrl === undefined && member?.image ? `avatars/${member.image}` : null) | ||
|
||
|
||
return ( | ||
<FileUpload | ||
oldKey={member?.image ?? undefined} | ||
isPublicObject | ||
uploadPath="avatars/" | ||
disabled={!editEnabled} | ||
fileTypes={[MediaType.JPEG, MediaType.PNG, MediaType.WEBP]} | ||
onUploadStart={onUploadStart} | ||
onUploadError={onUploadError} | ||
onUploadSuccess={onUploadSuccess} | ||
onFileRemove={onFileRemove} | ||
> | ||
{(ref) => ( | ||
<AnimatePresence> | ||
{avatarIsLoading ? ( | ||
<Skeleton | ||
isLoaded={!avatarIsLoading} | ||
className={clsx("rounded-full", avatarProps.className)}> | ||
</Skeleton> | ||
) : ( | ||
<Avatar | ||
src={srcOverride ?? (avatarUrl ?? fetchedAvatarUrl?.url)} | ||
onClick={() => ref.current?.click()} | ||
classNames={{ | ||
base: clsx(editEnabled && "cursor-pointer"), | ||
}} | ||
{...avatarProps} | ||
/> | ||
)} | ||
|
||
</AnimatePresence> | ||
)} | ||
</FileUpload> | ||
) | ||
} | ||
|
||
export default EditableMemberAvatar |
Oops, something went wrong.