-
Notifications
You must be signed in to change notification settings - Fork 4
Profile page header #1863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Profile page header #1863
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
03511a2
wip
ragnep fbae540
Merge branch 'main' into profile-page-header
ragnep d040fcf
wip
ragnep 6230de7
Merge branch 'main' into profile-page-header
ragnep e40a8c1
Merge branch 'main' into profile-page-header
ragnep 13c8ebd
fixed issues
ragnep 8c6ddb7
fixed issues
ragnep 2174322
wip
ragnep d9b0034
fixed issues
ragnep 96274f3
fixed issues
ragnep f688140
fixed issues
ragnep 988582d
wip
ragnep 2bd5e13
image optimization improvements
ragnep d75aba7
fixed ts and lint errors
ragnep dacaa6e
wip
ragnep dda9a36
remove webp label
ragnep a546695
added webp label back
ragnep f562ca6
removed webp
ragnep 599fe6a
adjusted edit pencil icon
ragnep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,97 @@ | ||
| "use client"; | ||
|
|
||
| import Image from "next/image"; | ||
| import { ACCEPTED_FORMATS_DISPLAY } from "./imageValidation"; | ||
| import { useImageUpload } from "./useImageUpload"; | ||
|
|
||
| export default function UserSettingsBannerImageInput({ | ||
| imageToShow, | ||
| setFile, | ||
| }: { | ||
| readonly imageToShow: string | null; | ||
| readonly setFile: (file: File) => void; | ||
| }) { | ||
| const { error, shake, dragging, onFileChange, dragHandlers } = useImageUpload( | ||
| { | ||
| maxSizeBytes: 2097152, | ||
| maxSizeLabel: "2MB", | ||
| setFile, | ||
| } | ||
| ); | ||
|
|
||
| return ( | ||
| <div | ||
| {...dragHandlers} | ||
| className="tw-group tw-flex tw-w-full tw-items-center tw-justify-center" | ||
| > | ||
| <label | ||
| className={` ${ | ||
| dragging | ||
| ? "tw-border-iron-600 tw-bg-iron-800" | ||
| : "tw-border-iron-700 tw-bg-iron-900" | ||
| } tw-relative tw-flex tw-h-64 tw-w-full tw-cursor-pointer tw-flex-col tw-items-center tw-justify-center tw-rounded-lg tw-border-2 tw-border-dashed tw-transition tw-duration-300 tw-ease-out hover:tw-border-iron-600 hover:tw-bg-iron-800 ${ | ||
| shake ? "tw-animate-shake" : "" | ||
| } `} | ||
| > | ||
| <div className="tw-flex tw-flex-col tw-items-center tw-justify-center tw-gap-3 tw-px-4"> | ||
| {imageToShow ? ( | ||
| <div className="tw-relative tw-h-40 tw-w-40"> | ||
| <Image | ||
| src={imageToShow} | ||
| alt="Banner preview" | ||
| fill | ||
| className="tw-rounded-sm tw-object-contain" | ||
| /> | ||
| </div> | ||
| ) : ( | ||
| <> | ||
| <div className="tw-flex tw-h-10 tw-w-10 tw-items-center tw-justify-center tw-rounded-lg tw-border tw-border-solid tw-border-iron-700 tw-bg-iron-900 tw-transition tw-duration-300 tw-ease-out group-hover:tw-bg-iron-800"> | ||
| <div className="tw-flex tw-h-5 tw-w-5 tw-flex-shrink-0 tw-items-center tw-justify-center tw-text-iron-50"> | ||
| <svg | ||
| className="tw-h-6 tw-w-6 tw-flex-shrink-0 tw-text-iron-50" | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| > | ||
| <path | ||
| d="M4 16.2422C2.79401 15.435 2 14.0602 2 12.5C2 10.1564 3.79151 8.23129 6.07974 8.01937C6.54781 5.17213 9.02024 3 12 3C14.9798 3 17.4522 5.17213 17.9203 8.01937C20.2085 8.23129 22 10.1564 22 12.5C22 14.0602 21.206 15.435 20 16.2422M8 16L12 12M12 12L16 16M12 12V21" | ||
| stroke="currentColor" | ||
| strokeWidth="2" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| /> | ||
| </svg> | ||
| </div> | ||
| </div> | ||
| <div className="tw-flex tw-flex-col tw-items-center tw-justify-center"> | ||
| <p className="tw-mb-1 tw-text-sm tw-font-normal tw-text-iron-400"> | ||
| <span className="tw-font-medium tw-text-white"> | ||
| Click to upload | ||
| </span>{" "} | ||
| or drag and drop | ||
| </p> | ||
| <p className="tw-text-xs tw-font-normal tw-text-iron-400"> | ||
| JPEG, JPG, PNG, GIF — max 2MB | ||
| </p> | ||
| </div> | ||
| </> | ||
| )} | ||
| {error && ( | ||
| <p className="tw-text-xs tw-font-medium tw-text-red">{error}</p> | ||
| )} | ||
| </div> | ||
| <input | ||
| id="banner-upload-input" | ||
| type="file" | ||
| className="tw-hidden" | ||
| accept={ACCEPTED_FORMATS_DISPLAY} | ||
| onChange={(e: React.ChangeEvent<HTMLInputElement>) => { | ||
| const f = e.target.files?.[0]; | ||
| if (f) onFileChange(f); | ||
| e.target.value = ""; // allow selecting same file again | ||
| }} | ||
| /> | ||
| </label> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or 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 hidden or 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,27 @@ | ||
| import { getFileExtension } from "@/components/waves/memes/file-upload/utils/formatHelpers"; | ||
|
|
||
| const ACCEPTED_FORMATS = [ | ||
| "image/jpeg", | ||
| "image/jpg", | ||
| "image/png", | ||
| "image/gif", | ||
| "image/webp", | ||
| ]; | ||
|
|
||
| export const ACCEPTED_FORMATS_DISPLAY = ACCEPTED_FORMATS.map( | ||
| (format) => `.${format.replace("image/", "")}` | ||
| ).join(", "); | ||
|
|
||
| const ALLOWED_EXTENSIONS = new Set(["JPG", "JPEG", "PNG", "GIF", "WEBP"]); | ||
|
|
||
| export const isValidImageType = (file: File): boolean => { | ||
| // Primary check: MIME type | ||
| if (ACCEPTED_FORMATS.includes(file.type)) { | ||
| return true; | ||
| } | ||
| // Fallback: extension check when MIME is empty (some OS/browser combos) | ||
| if (file.type === "") { | ||
| return ALLOWED_EXTENSIONS.has(getFileExtension(file)); | ||
| } | ||
| return false; | ||
| }; |
This file contains hidden or 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,95 @@ | ||
| "use client"; | ||
|
|
||
| import { useCallback, useContext, useRef, useState } from "react"; | ||
| import { AuthContext } from "@/components/auth/Auth"; | ||
| import { isValidImageType } from "./imageValidation"; | ||
|
|
||
| interface UseImageUploadOptions { | ||
| maxSizeBytes: number; | ||
| maxSizeLabel: string; | ||
| setFile: (file: File) => void; | ||
| } | ||
|
|
||
| export function useImageUpload({ | ||
| maxSizeBytes, | ||
| maxSizeLabel, | ||
| setFile, | ||
| }: UseImageUploadOptions) { | ||
| const { setToast } = useContext(AuthContext); | ||
| const dragDepth = useRef(0); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [shake, setShake] = useState<boolean>(false); | ||
| const [dragging, setDragging] = useState(false); | ||
|
|
||
| const onFileChange = useCallback( | ||
| (file: File) => { | ||
| setError(null); | ||
| if (!isValidImageType(file)) { | ||
| setToast({ | ||
| type: "error", | ||
| message: "Invalid file type", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (file.size > maxSizeBytes) { | ||
| setError(`File size must be less than ${maxSizeLabel}`); | ||
| setShake(true); | ||
| setTimeout(() => setShake(false), 300); | ||
| return; | ||
| } | ||
|
|
||
| setFile(file); | ||
| }, | ||
| [setFile, setToast, maxSizeBytes, maxSizeLabel] | ||
| ); | ||
|
|
||
| const onDragEnter = useCallback((e: React.DragEvent) => { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| dragDepth.current += 1; | ||
| setDragging(true); | ||
| }, []); | ||
|
|
||
| const onDragLeave = useCallback((e: React.DragEvent) => { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| dragDepth.current -= 1; | ||
| if (dragDepth.current <= 0) { | ||
| dragDepth.current = 0; | ||
| setDragging(false); | ||
| } | ||
| }, []); | ||
|
|
||
| const onDragOver = useCallback((e: React.DragEvent) => { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| }, []); | ||
|
|
||
| const onDrop = useCallback( | ||
| (e: React.DragEvent) => { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| dragDepth.current = 0; | ||
| setDragging(false); | ||
| const file = e.dataTransfer.files[0]; | ||
| if (file) { | ||
| onFileChange(file); | ||
| } | ||
| }, | ||
| [onFileChange] | ||
| ); | ||
|
|
||
| return { | ||
| error, | ||
| shake, | ||
| dragging, | ||
| onFileChange, | ||
| dragHandlers: { | ||
| onDrop, | ||
| onDragEnter, | ||
| onDragLeave, | ||
| onDragOver, | ||
| }, | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.