|
| 1 | +import * as React from "react"; |
| 2 | +import { toast } from "sonner"; |
| 3 | + |
| 4 | +import { rewriteRelayUrl } from "@/shared/lib/mediaUrl"; |
| 5 | + |
| 6 | +export type AvatarPresentationState = "failed" | "pending" | "ready"; |
| 7 | + |
| 8 | +export type AvatarPresentation = { |
| 9 | + displayUrl: string; |
| 10 | + state: AvatarPresentationState; |
| 11 | +}; |
| 12 | + |
| 13 | +type AvatarPresentationEntry = { |
| 14 | + generation: number; |
| 15 | + localPreviewUrl: string | null; |
| 16 | + remoteUrl: string; |
| 17 | + snapshot: AvatarPresentation; |
| 18 | +}; |
| 19 | + |
| 20 | +const PROBE_DELAYS_MS = [0, 750, 1_500, 3_000] as const; |
| 21 | +const PROBE_TIMEOUT_MS = 3_000; |
| 22 | +const READY_PRESENTATION_TTL_MS = 30_000; |
| 23 | +const presentations = new Map<string, AvatarPresentationEntry>(); |
| 24 | +const listeners = new Set<() => void>(); |
| 25 | +let nextGeneration = 1; |
| 26 | + |
| 27 | +function emitChange(): void { |
| 28 | + for (const listener of listeners) listener(); |
| 29 | +} |
| 30 | + |
| 31 | +function toastId(remoteUrl: string): string { |
| 32 | + return `avatar-presentation:${remoteUrl}`; |
| 33 | +} |
| 34 | + |
| 35 | +function releaseLocalPreview(entry: AvatarPresentationEntry): void { |
| 36 | + if (!entry.localPreviewUrl) return; |
| 37 | + URL.revokeObjectURL(entry.localPreviewUrl); |
| 38 | + entry.localPreviewUrl = null; |
| 39 | +} |
| 40 | + |
| 41 | +function isCurrent(entry: AvatarPresentationEntry): boolean { |
| 42 | + return presentations.get(entry.remoteUrl)?.generation === entry.generation; |
| 43 | +} |
| 44 | + |
| 45 | +function wait(delayMs: number): Promise<void> { |
| 46 | + return new Promise((resolve) => window.setTimeout(resolve, delayMs)); |
| 47 | +} |
| 48 | + |
| 49 | +function buildProbeUrl(remoteUrl: string, attempt: number): string { |
| 50 | + try { |
| 51 | + const url = new URL(remoteUrl); |
| 52 | + url.searchParams.set( |
| 53 | + "buzz_avatar_probe", |
| 54 | + `${Date.now()}-${attempt.toString()}`, |
| 55 | + ); |
| 56 | + return url.toString(); |
| 57 | + } catch { |
| 58 | + return remoteUrl; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function probeImage( |
| 63 | + remoteUrl: string, |
| 64 | + attempt: number, |
| 65 | +): Promise<string | null> { |
| 66 | + return new Promise((resolve) => { |
| 67 | + const image = new Image(); |
| 68 | + let settled = false; |
| 69 | + const verifiedUrl = buildProbeUrl(remoteUrl, attempt); |
| 70 | + const finish = (result: string | null) => { |
| 71 | + if (settled) return; |
| 72 | + settled = true; |
| 73 | + window.clearTimeout(timeoutId); |
| 74 | + image.onload = null; |
| 75 | + image.onerror = null; |
| 76 | + resolve(result); |
| 77 | + }; |
| 78 | + const timeoutId = window.setTimeout(() => finish(null), PROBE_TIMEOUT_MS); |
| 79 | + |
| 80 | + image.onload = () => finish(verifiedUrl); |
| 81 | + image.onerror = () => finish(null); |
| 82 | + image.referrerPolicy = "no-referrer"; |
| 83 | + image.src = rewriteRelayUrl(verifiedUrl); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +async function verifyPresentation( |
| 88 | + entry: AvatarPresentationEntry, |
| 89 | +): Promise<void> { |
| 90 | + for (const [attempt, delayMs] of PROBE_DELAYS_MS.entries()) { |
| 91 | + await wait(delayMs); |
| 92 | + if (!isCurrent(entry) || entry.snapshot.state !== "pending") return; |
| 93 | + |
| 94 | + const verifiedUrl = await probeImage(entry.remoteUrl, attempt); |
| 95 | + if (!isCurrent(entry) || entry.snapshot.state !== "pending") return; |
| 96 | + if (!verifiedUrl) continue; |
| 97 | + |
| 98 | + releaseLocalPreview(entry); |
| 99 | + entry.snapshot = { displayUrl: verifiedUrl, state: "ready" }; |
| 100 | + toast.dismiss(toastId(entry.remoteUrl)); |
| 101 | + emitChange(); |
| 102 | + window.setTimeout(() => { |
| 103 | + if (!isCurrent(entry) || entry.snapshot.state !== "ready") return; |
| 104 | + presentations.delete(entry.remoteUrl); |
| 105 | + emitChange(); |
| 106 | + }, READY_PRESENTATION_TTL_MS); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if (!isCurrent(entry) || entry.snapshot.state !== "pending") return; |
| 111 | + entry.snapshot = { |
| 112 | + displayUrl: entry.remoteUrl, |
| 113 | + state: "failed", |
| 114 | + }; |
| 115 | + emitChange(); |
| 116 | + toast.error("Avatar couldn’t finish uploading", { |
| 117 | + action: { |
| 118 | + label: "Retry", |
| 119 | + onClick: () => retryAvatarPresentation(entry.remoteUrl), |
| 120 | + }, |
| 121 | + description: "Your default avatar is showing instead.", |
| 122 | + id: toastId(entry.remoteUrl), |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +export function beginAvatarPresentation(remoteUrl: string, image: Blob): void { |
| 127 | + const existing = presentations.get(remoteUrl); |
| 128 | + if (existing) releaseLocalPreview(existing); |
| 129 | + |
| 130 | + const localPreviewUrl = URL.createObjectURL(image); |
| 131 | + const entry: AvatarPresentationEntry = { |
| 132 | + generation: nextGeneration++, |
| 133 | + localPreviewUrl, |
| 134 | + remoteUrl, |
| 135 | + snapshot: { displayUrl: localPreviewUrl, state: "pending" }, |
| 136 | + }; |
| 137 | + presentations.set(remoteUrl, entry); |
| 138 | + emitChange(); |
| 139 | + void verifyPresentation(entry); |
| 140 | +} |
| 141 | + |
| 142 | +export function retryAvatarPresentation(remoteUrl: string): void { |
| 143 | + const entry = presentations.get(remoteUrl); |
| 144 | + if (entry?.snapshot.state !== "failed") return; |
| 145 | + entry.generation = nextGeneration++; |
| 146 | + entry.snapshot = { |
| 147 | + displayUrl: entry.localPreviewUrl ?? entry.remoteUrl, |
| 148 | + state: "pending", |
| 149 | + }; |
| 150 | + emitChange(); |
| 151 | + void verifyPresentation(entry); |
| 152 | +} |
| 153 | + |
| 154 | +export function getAvatarPresentation( |
| 155 | + remoteUrl: string | null, |
| 156 | +): AvatarPresentation | null { |
| 157 | + if (!remoteUrl) return null; |
| 158 | + return presentations.get(remoteUrl)?.snapshot ?? null; |
| 159 | +} |
| 160 | + |
| 161 | +export function subscribeAvatarPresentations(listener: () => void): () => void { |
| 162 | + listeners.add(listener); |
| 163 | + return () => listeners.delete(listener); |
| 164 | +} |
| 165 | + |
| 166 | +export function useAvatarPresentation( |
| 167 | + remoteUrl: string | null, |
| 168 | +): AvatarPresentation | null { |
| 169 | + return React.useSyncExternalStore( |
| 170 | + subscribeAvatarPresentations, |
| 171 | + () => getAvatarPresentation(remoteUrl), |
| 172 | + () => null, |
| 173 | + ); |
| 174 | +} |
0 commit comments