-
Notifications
You must be signed in to change notification settings - Fork 1.1k
web: Improve upload user image UI #1224
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fad4ee8
improve upload image ui
ameer2468 c9ab8d9
tweaks
ameer2468 d4313ee
change variant
ameer2468 70f7037
toast if file size greater than 1 mb
ameer2468 6579bb5
some cleanups
ameer2468 3301cd2
change remove button to trash button icon
ameer2468 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
Some comments aren't visible on the classic Files Changed page.
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
140 changes: 140 additions & 0 deletions
140
apps/web/app/(org)/dashboard/settings/account/components/ProfileImage.tsx
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,140 @@ | ||
| "use client"; | ||
|
|
||
| import { Button } from "@cap/ui"; | ||
| import { faImage, faTrash } from "@fortawesome/free-solid-svg-icons"; | ||
| import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
| import clsx from "clsx"; | ||
| import Image from "next/image"; | ||
| import { useEffect, useRef, useState } from "react"; | ||
| import { toast } from "sonner"; | ||
| import { Tooltip } from "@/components/Tooltip"; | ||
|
|
||
| interface ProfileImageProps { | ||
| initialPreviewUrl?: string | null; | ||
| onChange?: (file: File | null) => void; | ||
| onRemove?: () => void; | ||
| disabled?: boolean; | ||
| isUploading?: boolean; | ||
| isRemoving?: boolean; | ||
| } | ||
|
|
||
| export function ProfileImage({ | ||
| initialPreviewUrl, | ||
| onChange, | ||
| onRemove, | ||
| disabled = false, | ||
| isUploading = false, | ||
| isRemoving = false, | ||
| }: ProfileImageProps) { | ||
| const [previewUrl, setPreviewUrl] = useState<string | null>( | ||
| initialPreviewUrl || null, | ||
| ); | ||
| const fileInputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| // Reset isRemoving when the parent confirms the operation completed | ||
| useEffect(() => { | ||
| if (initialPreviewUrl !== undefined) { | ||
| setPreviewUrl(initialPreviewUrl); | ||
| } | ||
| }, [initialPreviewUrl]); | ||
|
|
||
| const handleFileChange = () => { | ||
| const file = fileInputRef.current?.files?.[0]; | ||
| if (!file) return; | ||
| const sizeLimit = 1024 * 1024 * 1; | ||
| if (file.size > sizeLimit) { | ||
| toast.error("File size must be 1MB or less"); | ||
| return; | ||
| } | ||
| if (previewUrl && previewUrl !== initialPreviewUrl) { | ||
| URL.revokeObjectURL(previewUrl); | ||
| } | ||
| const objectUrl = URL.createObjectURL(file); | ||
| setPreviewUrl(objectUrl); | ||
| onChange?.(file); | ||
| }; | ||
|
|
||
| const handleRemove = () => { | ||
| setPreviewUrl(null); | ||
| if (fileInputRef.current) { | ||
| fileInputRef.current.value = ""; | ||
| } | ||
| onRemove?.(); | ||
| }; | ||
|
|
||
| const handleUploadClick = () => { | ||
| if (!disabled && !isUploading && !isRemoving) { | ||
| fileInputRef.current?.click(); | ||
| } | ||
| }; | ||
|
|
||
| const isLoading = isUploading || isRemoving; | ||
|
|
||
| return ( | ||
| <div className="rounded-xl border border-dashed bg-gray-2 h-fit border-gray-4"> | ||
| <div className="flex gap-5 p-5"> | ||
| <div | ||
| className={clsx( | ||
| "flex justify-center items-center rounded-full border size-14 bg-gray-3 border-gray-6", | ||
| previewUrl ? "border-solid" : "border-dashed", | ||
| )} | ||
| > | ||
| {previewUrl ? ( | ||
| <Image | ||
| src={previewUrl} | ||
| alt="Profile Image" | ||
| width={56} | ||
| className="object-cover rounded-full size-14" | ||
| height={56} | ||
| /> | ||
| ) : ( | ||
| <FontAwesomeIcon icon={faImage} className="size-4 text-gray-9" /> | ||
| )} | ||
| </div> | ||
| <input | ||
| type="file" | ||
| className="hidden h-0" | ||
| accept="image/jpeg, image/jpg, image/png, image/svg+xml" | ||
| ref={fileInputRef} | ||
| onChange={handleFileChange} | ||
| disabled={disabled || isLoading} | ||
| /> | ||
| <div className="space-y-3"> | ||
| <div className="flex gap-2"> | ||
| {!isRemoving && ( | ||
| <Button | ||
| type="button" | ||
| variant="gray" | ||
| disabled={disabled || isLoading} | ||
| size="xs" | ||
| onClick={handleUploadClick} | ||
| spinner={isUploading} | ||
| > | ||
| {isUploading ? "Uploading..." : "Upload Image"} | ||
| </Button> | ||
| )} | ||
| {(previewUrl || isRemoving) && ( | ||
| <Tooltip content="Remove image"> | ||
| <Button | ||
| type="button" | ||
| variant="outline" | ||
| className="p-0 size-8" | ||
| disabled={disabled || isLoading} | ||
| size="icon" | ||
| onClick={handleRemove} | ||
| spinner={isRemoving} | ||
| > | ||
| <FontAwesomeIcon | ||
| icon={faTrash} | ||
| className="size-2.5 text-gray-12" | ||
| /> | ||
| </Button> | ||
| </Tooltip> | ||
| )} | ||
| </div> | ||
| <p className="text-xs text-gray-10">Recommended size: 120x120</p> | ||
| </div> | ||
| </div> | ||
| </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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memory leak: Object URL not revoked on remove.
When removing an image, if
previewUrlis a local blob URL, it must be revoked before setting to null, otherwise it leaks memory.Apply this diff:
const handleRemove = () => { + // Revoke local object URL before removing + if (previewUrl && previewUrl.startsWith("blob:")) { + URL.revokeObjectURL(previewUrl); + } setPreviewUrl(null); if (fileInputRef.current) { fileInputRef.current.value = ""; } onRemove?.(); };🤖 Prompt for AI Agents