Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.1 KB
Raw Blame
/**
 * Holds the `File` a send staged, so its message can draw the real image while the upload is in flight.
 *
 * Keyed by an id the echo carries, since a name is not unique across messages. An entry only matters until
 * the artifact exists — a reload finds an empty registry and falls back to the artifact — so the oldest are
 * dropped rather than held for the life of the tab.
 */

const MAX_TRACKED_PREVIEWS = 20

const previews = new Map<string, File>()
let sequence = 0

export function rememberAttachmentPreview(file: File): string {
    const id = `preview-${++sequence}`
    previews.set(id, file)
    while (previews.size > MAX_TRACKED_PREVIEWS) {
        const oldest = previews.keys().next()
        if (oldest.done) {
            break
        }
        previews.delete(oldest.value)
    }
    return id
}

export function getAttachmentPreview(previewId: string | undefined): File | undefined {
    return previewId ? previews.get(previewId) : undefined
}

/** Test seam: module state would otherwise leak between cases. */
export function clearAttachmentPreviews(): void {
    previews.clear()
    sequence = 0
}