-
Notifications
You must be signed in to change notification settings - Fork 990
Keep avatar preview visible during upload #2237
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
3b7f458
Show uploaded avatar in onboarding modal
3d8eed4
Preview avatar while upload is pending
ca3ed4e
Keep avatar preview until relay image loads
f84fe64
Centralize pending avatar presentation
loganj 1eccae1
Refine pending avatar states
loganj 43e528b
Restore empty avatar after upload failure
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
174 changes: 174 additions & 0 deletions
174
desktop/src/features/profile/avatarPresentationStore.ts
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,174 @@ | ||
| import * as React from "react"; | ||
| import { toast } from "sonner"; | ||
|
|
||
| import { rewriteRelayUrl } from "@/shared/lib/mediaUrl"; | ||
|
|
||
| export type AvatarPresentationState = "failed" | "pending" | "ready"; | ||
|
|
||
| export type AvatarPresentation = { | ||
| displayUrl: string; | ||
| state: AvatarPresentationState; | ||
| }; | ||
|
|
||
| type AvatarPresentationEntry = { | ||
| generation: number; | ||
| localPreviewUrl: string | null; | ||
| remoteUrl: string; | ||
| snapshot: AvatarPresentation; | ||
| }; | ||
|
|
||
| const PROBE_DELAYS_MS = [0, 750, 1_500, 3_000] as const; | ||
| const PROBE_TIMEOUT_MS = 3_000; | ||
| const READY_PRESENTATION_TTL_MS = 30_000; | ||
| const presentations = new Map<string, AvatarPresentationEntry>(); | ||
| const listeners = new Set<() => void>(); | ||
| let nextGeneration = 1; | ||
|
|
||
| function emitChange(): void { | ||
| for (const listener of listeners) listener(); | ||
| } | ||
|
|
||
| function toastId(remoteUrl: string): string { | ||
| return `avatar-presentation:${remoteUrl}`; | ||
| } | ||
|
|
||
| function releaseLocalPreview(entry: AvatarPresentationEntry): void { | ||
| if (!entry.localPreviewUrl) return; | ||
| URL.revokeObjectURL(entry.localPreviewUrl); | ||
| entry.localPreviewUrl = null; | ||
| } | ||
|
|
||
| function isCurrent(entry: AvatarPresentationEntry): boolean { | ||
| return presentations.get(entry.remoteUrl)?.generation === entry.generation; | ||
| } | ||
|
|
||
| function wait(delayMs: number): Promise<void> { | ||
| return new Promise((resolve) => window.setTimeout(resolve, delayMs)); | ||
| } | ||
|
|
||
| function buildProbeUrl(remoteUrl: string, attempt: number): string { | ||
| try { | ||
| const url = new URL(remoteUrl); | ||
| url.searchParams.set( | ||
| "buzz_avatar_probe", | ||
| `${Date.now()}-${attempt.toString()}`, | ||
| ); | ||
| return url.toString(); | ||
| } catch { | ||
| return remoteUrl; | ||
| } | ||
| } | ||
|
|
||
| function probeImage( | ||
| remoteUrl: string, | ||
| attempt: number, | ||
| ): Promise<string | null> { | ||
| return new Promise((resolve) => { | ||
| const image = new Image(); | ||
| let settled = false; | ||
| const verifiedUrl = buildProbeUrl(remoteUrl, attempt); | ||
| const finish = (result: string | null) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| window.clearTimeout(timeoutId); | ||
| image.onload = null; | ||
| image.onerror = null; | ||
| resolve(result); | ||
| }; | ||
| const timeoutId = window.setTimeout(() => finish(null), PROBE_TIMEOUT_MS); | ||
|
|
||
| image.onload = () => finish(verifiedUrl); | ||
| image.onerror = () => finish(null); | ||
| image.referrerPolicy = "no-referrer"; | ||
| image.src = rewriteRelayUrl(verifiedUrl); | ||
| }); | ||
| } | ||
|
|
||
| async function verifyPresentation( | ||
| entry: AvatarPresentationEntry, | ||
| ): Promise<void> { | ||
| for (const [attempt, delayMs] of PROBE_DELAYS_MS.entries()) { | ||
| await wait(delayMs); | ||
| if (!isCurrent(entry) || entry.snapshot.state !== "pending") return; | ||
|
|
||
| const verifiedUrl = await probeImage(entry.remoteUrl, attempt); | ||
| if (!isCurrent(entry) || entry.snapshot.state !== "pending") return; | ||
| if (!verifiedUrl) continue; | ||
|
|
||
| releaseLocalPreview(entry); | ||
| entry.snapshot = { displayUrl: verifiedUrl, state: "ready" }; | ||
| toast.dismiss(toastId(entry.remoteUrl)); | ||
| emitChange(); | ||
| window.setTimeout(() => { | ||
| if (!isCurrent(entry) || entry.snapshot.state !== "ready") return; | ||
| presentations.delete(entry.remoteUrl); | ||
| emitChange(); | ||
| }, READY_PRESENTATION_TTL_MS); | ||
| return; | ||
| } | ||
|
|
||
| if (!isCurrent(entry) || entry.snapshot.state !== "pending") return; | ||
| entry.snapshot = { | ||
| displayUrl: entry.remoteUrl, | ||
| state: "failed", | ||
| }; | ||
| emitChange(); | ||
| toast.error("Avatar couldn’t finish uploading", { | ||
| action: { | ||
| label: "Retry", | ||
| onClick: () => retryAvatarPresentation(entry.remoteUrl), | ||
| }, | ||
| description: "Your default avatar is showing instead.", | ||
| id: toastId(entry.remoteUrl), | ||
| }); | ||
| } | ||
|
|
||
| export function beginAvatarPresentation(remoteUrl: string, image: Blob): void { | ||
| const existing = presentations.get(remoteUrl); | ||
| if (existing) releaseLocalPreview(existing); | ||
|
|
||
| const localPreviewUrl = URL.createObjectURL(image); | ||
| const entry: AvatarPresentationEntry = { | ||
| generation: nextGeneration++, | ||
| localPreviewUrl, | ||
| remoteUrl, | ||
| snapshot: { displayUrl: localPreviewUrl, state: "pending" }, | ||
| }; | ||
| presentations.set(remoteUrl, entry); | ||
| emitChange(); | ||
| void verifyPresentation(entry); | ||
| } | ||
|
|
||
| export function retryAvatarPresentation(remoteUrl: string): void { | ||
| const entry = presentations.get(remoteUrl); | ||
| if (entry?.snapshot.state !== "failed") return; | ||
| entry.generation = nextGeneration++; | ||
| entry.snapshot = { | ||
| displayUrl: entry.localPreviewUrl ?? entry.remoteUrl, | ||
| state: "pending", | ||
| }; | ||
| emitChange(); | ||
| void verifyPresentation(entry); | ||
| } | ||
|
|
||
| export function getAvatarPresentation( | ||
| remoteUrl: string | null, | ||
| ): AvatarPresentation | null { | ||
| if (!remoteUrl) return null; | ||
| return presentations.get(remoteUrl)?.snapshot ?? null; | ||
| } | ||
|
|
||
| export function subscribeAvatarPresentations(listener: () => void): () => void { | ||
| listeners.add(listener); | ||
| return () => listeners.delete(listener); | ||
| } | ||
|
|
||
| export function useAvatarPresentation( | ||
| remoteUrl: string | null, | ||
| ): AvatarPresentation | null { | ||
| return React.useSyncExternalStore( | ||
| subscribeAvatarPresentations, | ||
| () => getAvatarPresentation(remoteUrl), | ||
| () => null, | ||
| ); | ||
| } | ||
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
79 changes: 79 additions & 0 deletions
79
desktop/src/features/profile/ui/ProfileAvatarEditor.helpers.ts
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,79 @@ | ||
| import * as React from "react"; | ||
|
|
||
| import { beginAvatarPresentation } from "@/features/profile/avatarPresentationStore"; | ||
|
|
||
| export const DONE_BUTTON_CONTENT_TRANSITION = { | ||
| duration: 0.14, | ||
| ease: [0.23, 1, 0.32, 1], | ||
| } as const; | ||
|
|
||
| export const DONE_BUTTON_SHELL_TRANSITION = { | ||
| duration: 0.18, | ||
| ease: [0.23, 1, 0.32, 1], | ||
| } as const; | ||
|
|
||
| export function waitForPendingButtonPaint() { | ||
| return new Promise<void>((resolve) => { | ||
| if ( | ||
| typeof window === "undefined" || | ||
| typeof window.requestAnimationFrame !== "function" | ||
| ) { | ||
| setTimeout(resolve, 0); | ||
| return; | ||
| } | ||
|
|
||
| window.requestAnimationFrame(() => { | ||
| window.requestAnimationFrame(() => setTimeout(resolve, 0)); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| export function useUploadPreviewLifecycle({ | ||
| clearFallback, | ||
| onSuccess, | ||
| showFallback, | ||
| }: { | ||
| clearFallback: () => void; | ||
| onSuccess: (uploadedUrl: string) => void; | ||
| showFallback: (file: File) => void; | ||
| }) { | ||
| const pendingFileRef = React.useRef<File | null>(null); | ||
|
|
||
| return { | ||
| onUploadSettled: () => { | ||
| pendingFileRef.current = null; | ||
| clearFallback(); | ||
| }, | ||
| onUploadStart: (file: File) => { | ||
| pendingFileRef.current = file; | ||
| showFallback(file); | ||
| }, | ||
| onUploadSuccess: (uploadedUrl: string) => { | ||
| const pendingFile = pendingFileRef.current; | ||
| if (pendingFile) beginAvatarPresentation(uploadedUrl, pendingFile); | ||
| onSuccess(uploadedUrl); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export function useLocalAvatarPreview() { | ||
| const [previewUrl, setPreviewUrl] = React.useState<string | null>(null); | ||
| const previewUrlRef = React.useRef<string | null>(null); | ||
|
|
||
| const clearPreview = React.useCallback(() => { | ||
| if (previewUrlRef.current) URL.revokeObjectURL(previewUrlRef.current); | ||
| previewUrlRef.current = null; | ||
| setPreviewUrl(null); | ||
| }, []); | ||
|
|
||
| const showFilePreview = React.useCallback((file: File) => { | ||
| if (previewUrlRef.current) URL.revokeObjectURL(previewUrlRef.current); | ||
| const nextUrl = URL.createObjectURL(file); | ||
| previewUrlRef.current = nextUrl; | ||
| setPreviewUrl(nextUrl); | ||
| }, []); | ||
|
|
||
| React.useEffect(() => clearPreview, [clearPreview]); | ||
|
|
||
| return { clearPreview, previewUrl, showFilePreview }; | ||
| } |
Oops, something went wrong.
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.