Files
buzz/desktop/src/features/messages/lib/timelineImagePreload.ts
Tyler b0ad2b66dd Retain a stable message window in channel timelines (#1698)
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
Signed-off-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
2026-07-12 21:41:38 -04:00

58 lines
1.8 KiB
TypeScript

import { rewriteRelayUrl } from "@/shared/lib/mediaUrl";
import type { TimelineMessage } from "../types";
import { parseImetaTags } from "./parseImeta";
/**
* Return non-message-media image URLs worth warming before a virtualized row
* mounts. Inline image attachments deliberately stay out of this projection:
* their native-lazy thumbnail must load before the full-resolution request.
*/
export function timelineImageUrls(message: TimelineMessage): string[] {
const urls = new Set<string>();
const add = (url: string | null | undefined) => {
if (url) urls.add(rewriteRelayUrl(url));
};
add(message.avatarUrl);
if (message.tags) {
for (const entry of parseImetaTags(message.tags).values()) {
// Video poster frames are not part of the progressive image path.
if (entry.m?.startsWith("video/")) {
add(entry.image);
add(entry.thumb);
}
}
for (const tag of message.tags) {
if (tag[0] === "emoji") add(tag[2]);
}
}
for (const reaction of message.reactions ?? []) add(reaction.emojiUrl);
return [...urls];
}
export type TimelineImagePreloadState = {
activeImages: Set<HTMLImageElement>;
requestedUrls: Set<string>;
};
/** Start all image requests now, independently of Virtua row mounting. */
export function preloadTimelineImages(
messages: readonly TimelineMessage[],
state: TimelineImagePreloadState,
): void {
for (const message of messages) {
for (const url of timelineImageUrls(message)) {
if (state.requestedUrls.has(url)) continue;
state.requestedUrls.add(url);
const image = new Image();
state.activeImages.add(image);
const release = () => state.activeImages.delete(image);
image.addEventListener("load", release, { once: true });
image.addEventListener("error", release, { once: true });
image.src = url;
}
}
}