import "./share.css" import { batch, createEffect, createMemo, createSignal, For, on, onCleanup, onMount, Show, type JSX } from "solid-js" import { barTiming, drawShareCard, mainLength, leaderColor, loadShareFonts, shareDuration, shareSizes, type ShareCard, type ShareOptions, type ShareTiming, } from "./card" import { embedSnippet, embedUrl, packCard } from "./embed" import { useI18n } from "../context/i18n" import { useLanguage } from "../context/language" import { translate } from "../i18n" import { dataFlag } from "../lib/flag" import { route, type Locale } from "../lib/language" import { cardLogos, loadShareLogos } from "./logos" import { cycleShareOption, designMode, designOnly, setShareOption, shareControls, shareOptions } from "./design" import { shareTracks, type ShareTrack } from "./music" import type { ShareView } from "./embed" import { loadSpectrum, type Spectrum } from "./spectrum" import { ShareCalendar, shareDayLabel, shareRangeLabel } from "./calendar" // `view` is the range the section showed, so an embed of it can show the same. `variant` makes the card again for a // focus, for the editor's Date and Lead rows (share/sections.ts). export type ShareDraft = Omit & { view?: ShareView variant?: (focus: ShareFocus) => ShareDraft | undefined } /** One column of a bar chart (`day`) instead of the whole range, and whether to lead with the top series or the total. */ export type ShareFocus = { day?: number; lead?: "top" | "total" } const [request, setRequest] = createSignal() let opener: HTMLElement | undefined // Every share button on the page, so the editor's arrows can step from one section to the next. const sections = new Set<{ element: HTMLElement; card: () => ShareDraft | undefined }>() // The next section along in page order, wrapping round at either end, skipping any with nothing to share yet. function step(direction: 1 | -1) { const order = [...sections] .filter((item) => item.element.isConnected) .toSorted((a, b) => (a.element.compareDocumentPosition(b.element) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1)) const current = order.findIndex((item) => item.element === opener) const next = order .map((_, offset) => order[(current + direction * (offset + 1) + order.length * 2) % order.length]!) .find((item) => item.card()) if (!next) return opener = next.element setRequest(next.card()) } /** * The share icon in a section's heading. `card` reads the section's current view when it's pressed, in the page's * language. */ export function ShareButton(props: { card: (locale: Locale) => ShareDraft | undefined; label: string }) { if (!dataFlag("share")) return null const language = useLanguage() const i18n = useI18n() let element: HTMLButtonElement | undefined onMount(() => { if (!element) return const entry = { element, card: () => props.card(language.locale()) } sections.add(entry) onCleanup(() => sections.delete(entry)) }) return ( ) } const sizes = [ { value: "4:5", label: "share.size.feed" }, { value: "1:1", label: "share.size.square" }, { value: "9:16", label: "share.size.story" }, { value: "16:9", label: "share.size.wide" }, ] as const const layouts = [ { value: "headline", label: "share.layout.headline" }, { value: "number", label: "share.layout.number" }, { value: "chart", label: "share.layout.title" }, ] as const /** * The share editor, mounted once on the page: the card in the middle, its options beside it (under it on phones, one * group at a time), and Copy link and Export in the top bar. Images and videos come from the same drawing code as * the preview (card.ts). */ export function ShareDialog(props: { updatedAt: string | null; url: string }) { const i18n = useI18n() const language = useLanguage() let dialog: HTMLDialogElement | undefined let preview: HTMLCanvasElement | undefined // The card's width on screen, for the embed code under it to match. const [cardBox, setCardBox] = createSignal({ left: 0, top: 0, width: 0, height: 0 }) const cardWidth = () => cardBox().width // The fill of the thin bar under the card that follows the video preview as it plays. let playhead: HTMLElement | undefined let audio: HTMLAudioElement | undefined const [format, setFormat] = createSignal<"image" | "video" | "embed">("video") // An embed shows the latest data by default; a fixed one keeps today's numbers (share/embed.ts). const [fixed, setFixed] = createSignal(false) const [snippet, setSnippet] = createSignal("") const [copied, setCopied] = createSignal(false) const [notice, setNotice] = createSignal() const [rendering, setRendering] = createSignal() // Cancel shows on hover only once the pointer has left the button since Export was pressed, so the press that // started the render doesn't flash Cancel under it. const [cancelReady, setCancelReady] = createSignal(false) const [ready, setReady] = createSignal(false) const [playing, setPlaying] = createSignal(true) // The chosen track, and whether the video is muted; muting keeps the choice for unmuting. const [track, setTrack] = createSignal(shareTracks[0]?.id ?? "") const [muted, setMuted] = createSignal(false) // The track under the pointer in the open music list, playing in place of the chosen one until the list closes. const [hovered, setHovered] = createSignal() // The open dropdown or the date picker, by id; one at a time. const [menu, setMenu] = createSignal() // The editor opens on the card alone, the modal hugging it, with Close, Mute, Shuffle, Customize and Export; Customize // reveals every setting, and the modal grows to its full size around them. const [customizing, setCustomizing] = createSignal(false) const [viewport, setViewport] = createSignal<{ width: number; height: number }>() onMount(() => { const measure = () => setViewport({ width: innerWidth, height: innerHeight }) measure() addEventListener("resize", measure) onCleanup(() => removeEventListener("resize", measure)) }) // The card as large as the full modal would show it, with the stage's padding across, and down the bar over it, the // stage's padding and the section arrows under it (share.css). Never narrower than the bar's own buttons need. const hug = () => { const view = viewport() if (!view) return undefined const full = { width: Math.min(1200, view.width - 160), height: Math.min(800, view.height - 128) } const across = 64 const down = 52 + 64 + 12 + 64 const [width, height] = size() const scale = Math.min(1, (full.width - across) / width, (full.height - down) / height) return { width: Math.max(400, Math.round(width * scale + across)), height: Math.round(height * scale + down) } } const customize = () => { setCustomizing(true) // Its button goes, so the focus moves on to the first setting. requestAnimationFrame(() => bar?.querySelector("[data-slot=dropdown-trigger]")?.focus()) } // The bar compacts only when it has to: its full layout first, then step by step (share.css data-compact) until it // fits on one row. On phones the settings are in the sheet and this stays off. let bar: HTMLDivElement | undefined const fitBar = () => { if (!bar) return const wide = matchMedia("(min-width: 761px)").matches const rows = () => { const items = [...bar!.children].filter( (item): item is HTMLElement => item instanceof HTMLElement && item.offsetParent !== null, ) return new Set(items.map((item) => item.offsetTop)).size } for (const level of wide ? [0, 1, 2, 3] : [0]) { bar.dataset.compact = String(level) if (rows() <= 1) return } } createEffect(() => { format() leads() day() track() starts() if (!card()) return requestAnimationFrame(fitBar) }) // Counts Shuffle presses: each one spins the changed settings' reels, like an odometer, to their new values. const [rolls, setRolls] = createSignal(0) const roll = () => setRolls((count) => count + 1) // On phones, the settings sheet: closed until Settings opens it. const [sheet, setSheet] = createSignal(false) // The chart column the card is about, or the whole range; each section starts on the whole range. const [day, setDay] = createSignal() // Lead with the top model or the total: the sections that can say a total (Top Models). const [lead, setLead] = createSignal<"top" | "total">("top") const section = createMemo(() => request()?.id) createEffect(() => { section() setDay(undefined) setLead("top") }) // A bar chart over its range drawn as bars, or as a race of the leaders overtaking each other (card.ts drawRace). // Over a range: bars or a race; for one day: bars, a ranked list or a dot matrix. A style that doesn't // suit the chart shows as its bars. const [chartStyle, setChartStyle] = createSignal<"bars" | "race" | "list" | "dots">("bars") const raceable = () => starts().length > 1 const chartStyles = () => (day() === undefined ? (["bars", "race"] as const) : (["bars", "list", "dots"] as const)) const style = () => ((chartStyles() as readonly string[]).includes(chartStyle()) ? chartStyle() : "bars") const leads = createMemo(() => request()?.id === "top-models" && !!request()?.variant) // The first days of the columns the date picker can choose from: a bar chart's, when its section can make a card // for one. const starts = createMemo(() => { const value = request() if (!value?.variant || value.chart.kind !== "stacked") return [] const dates = value.chart.columns.map((column) => column.start ?? "") return dates.every(Boolean) ? dates : [] }) const weekly = () => request()?.view?.period === "weekly" const dateLabel = () => { const index = day() return index === undefined ? shareRangeLabel(language.locale(), starts(), weekly()) : shareDayLabel(language.locale(), starts()[index] ?? "", weekly()) } const card = createMemo(() => { const value = request() if (!value) return undefined const focus = { day: day(), lead: lead() } const chosen = focus.day === undefined && focus.lead === "top" ? value : (value.variant?.(focus) ?? value) const chart = style() !== "bars" && chosen.chart.kind === "stacked" ? { ...chosen.chart, style: style() as "race" | "list" | "dots" } : chosen.chart return { ...chosen, chart, variant: undefined, updatedAt: props.updatedAt, url: props.url } }) // The card's provider logos (the leaderboard and comparison cards); the preview redraws once they've loaded. const [logos, setLogos] = createSignal(0) createEffect(() => { const value = card() if (!value) return const ids = cardLogos(value.chart) if (ids.length > 0) void loadShareLogos(ids).then(() => setLogos((count) => count + 1)) }) // A copy of the options, so reading them tracks every field. const options = createMemo(() => ({ ...shareOptions })) const size = createMemo(() => shareSizes[options().size]) const chosen = createMemo(() => shareTracks.find((item) => item.id === track())) const music = createMemo(() => { const id = hovered() if (id) return shareTracks.find((item) => item.id === id) return muted() ? undefined : chosen() }) const silent = () => !chosen() // The chosen track's spectrum, for the dot equaliser; measured once per track (spectrum.ts). A plain signal, not a // resource: a resource would suspend the page's Suspense boundary, which takes the dialog out of the DOM and puts it // back no longer modal, under the page. const [spectrum, setSpectrum] = createSignal() createEffect(() => { const url = music()?.url setSpectrum(undefined) if (!url) return let current = true onCleanup(() => (current = false)) void loadSpectrum(url).then((value) => current && setSpectrum(value)) }) // With a soundtrack, the animation follows its bars and the video ends with it. const timing = createMemo(() => { const sound = music() return sound ? barTiming(sound.bar, sound.verse, sound.length, sound.beat, spectrum()) : undefined }) // The code to paste for an embed, rebuilt as its options change; a fixed one packs the card into its fragment. createEffect(() => { const value = card() const opts = options() if (format() !== "embed" || !value) return // In the card's language, which the embed page takes from its path. const url = embedUrl( new URL(route(value.locale, import.meta.env.BASE_URL), location.origin).toString(), value, opts, ) const done = (address: URL) => setSnippet(embedSnippet(address.toString(), value.title, opts.size)) if (!fixed()) return done(url) let current = true onCleanup(() => (current = false)) void packCard(value).then((packed) => { if (!current) return url.hash = `card=${packed}` done(url) }) }) let video: { key: string; file: Promise } | undefined // The video being rendered, to stop when Cancel is pressed or the editor closes. let renderAbort: AbortController | undefined let closing: ReturnType | undefined // Load the card's font with the page, so the preview is ready the moment the editor opens. onMount(() => void loadShareFonts()) // Opening sets the settings itself (from last time, or at random); only changes made after are remembered. let opening = false createEffect(() => { const value = request() if (!value) return clearTimeout(closing) setNotice(undefined) // Stepping to another section keeps the editor open, its music and its options. if (dialog?.open) return opening = true setCustomizing(false) // Each opening plays from the start, even on the same card as last time. clock.key = "" setMenu(undefined) setSheet(false) // The settings from last time, once they were changed; otherwise a card made at random that opens silent, with no // music until one is chosen (or Shuffle picks one). if (!restoreSettings()) { setFormat("video") shuffle() setTrack("none") } // Never playing on opening: a remembered track waits, muted, until it's unmuted or chosen again. setMuted(true) // The card it opens on is where undo stops; changes from here on are remembered. queueMicrotask(() => { opening = false history.past = [] history.future = [] }) dialog?.showModal() void loadShareFonts().then(() => setReady(true)) }) // Notices clear themselves after a moment. createEffect(() => { if (!notice()) return const timer = setTimeout(() => setNotice(undefined), 2400) onCleanup(() => clearTimeout(timer)) }) // The preview's clock. Changing the layout, size or style redraws on the same clock, so the animation and its // music carry on; only a new card, track, type or play state starts it again. let clock = { key: "", start: 0, loop: -1 } // The running preview's loop, for seeking from the timeline under the card. let loop: { length: number; sound: ShareTrack | undefined } = { length: 0, sound: undefined } // The preview: the still for an image; for a video, the animation on a loop with its soundtrack. createEffect(() => { const value = card() const opts = options() const sound = music() logos() if (!value || !preview || !ready()) return const ctx = preview.getContext("2d") if (!ctx) return preview.width = size()[0] preview.height = size()[1] const still = format() === "image" || matchMedia("(prefers-reduced-motion: reduce)").matches if (still || !playing()) { audio?.pause() // So playing again starts the animation and its track over; the clock would otherwise look unchanged. clock.key = "" return drawShareCard(ctx, value, opts, Infinity, timing()) } // An embed plays its card once, silently, and rests on it, as it will on the host page. if (format() === "embed") { audio?.pause() const start = performance.now() const length = mainLength(value, opts) const tick = () => { const time = (performance.now() - start) / 1000 drawShareCard(ctx, value, opts, time >= length ? Infinity : time) if (time < length) handle = requestAnimationFrame(tick) } let handle = requestAnimationFrame(tick) onCleanup(() => cancelAnimationFrame(handle)) return } const length = shareDuration(value, opts, timing()) + 0.6 loop = { length, sound } // Any change to the card, a Shuffle (even one that lands on the same card) or a new track starts the video over, // from its blank card. const key = [ value.id, sound?.id ?? "none", format(), playing(), opts.layout, opts.size, opts.theme, lead(), style(), day() ?? "", rolls(), ].join(" ") if (clock.key !== key) clock = { key, start: performance.now(), loop: -1 } const frame = () => { const elapsed = (performance.now() - clock.start) / 1000 const turn = Math.floor(elapsed / length) if (turn !== clock.loop) { clock.loop = turn restartAudio(sound) } // The pause between loops stays on the blank card the next one opens on, not the finished card. const at = elapsed - turn * length drawShareCard(ctx, value, opts, at > length - 0.6 ? 0 : at, timing()) if (playhead) playhead.style.scale = `${Math.min(1, (elapsed - turn * length) / length)} 1` handle = requestAnimationFrame(frame) } let handle = requestAnimationFrame(frame) onCleanup(() => cancelAnimationFrame(handle)) }) // Moves the video preview, and its music, to `fraction` of the way through; a paused preview plays from there. const seek = (fraction: number) => { if (format() !== "video") return if (!playing()) setPlaying(true) if (!loop.length) return const at = Math.max(0, Math.min(1, fraction)) * loop.length const turn = Math.max(0, clock.loop) clock.loop = turn clock.start = performance.now() - (turn * loop.length + at) * 1000 if (!loop.sound) return restartAudio(loop.sound) if (audio) audio.currentTime = at } const seekFrom = (event: PointerEvent, element: HTMLElement) => { const box = element.getBoundingClientRect() seek((event.clientX - box.left) / box.width) } const progress = () => { if (!loop.length) return 0 const elapsed = (performance.now() - clock.start) / 1000 return (elapsed % loop.length) / loop.length } const restartAudio = (sound: ShareTrack | undefined) => { if (!sound) return audio?.pause() if (!audio || !audio.src.endsWith(sound.url)) { audio?.pause() audio = new Audio(sound.url) } audio.currentTime = 0 // Browsers only play after a click; choosing a track or pressing play is one. void audio.play().catch(() => undefined) } const close = () => dialog?.close() // A card made at random: every setting but the type, which is for choosing on purpose, and the date, which belongs // to its section. Any layout and style, a size for a feed or a story (not Wide), and a clean track. Muted stays // muted: the track still changes, ready for unmuting. Playing from the start again, as each opening does. const shuffle = () => batch(() => { setFixed(pick([false, true] as const)) setShareOption("layout", pick(layouts).value) setShareOption("size", pick(sizes.filter((item) => item.value !== "16:9")).value) setShareOption("theme", pick(["dark", "light", "leader"] as const)) // Never an explicit track: those are for choosing on purpose. setTrack(pick(shareTracks.filter((item) => !item.explicit)).id) setLead(pick(["top", "total"] as const)) setChartStyle(pick(chartStyles())) setPlaying(true) }) // Undo and redo (⌘Z, ⇧⌘Z) for the settings. Each change, Shuffle as one, keeps the settings before it; the history // starts again with each section, and each opening, so undo never reaches back past the card in front of you. const settings = createMemo( () => ({ section: request()?.id, format: format(), day: day(), lead: lead(), chart: chartStyle(), layout: options().layout, size: options().size, theme: options().theme, track: track(), muted: muted(), fixed: fixed(), }), undefined, { equals: (a, b) => JSON.stringify(a) === JSON.stringify(b) }, ) const history = { past: [] as ReturnType[], future: [] as ReturnType[] } let last: ReturnType | undefined let restoring = false createEffect(() => { const now = settings() if (last?.section !== now.section) { history.past = [] history.future = [] } else if (last && !restoring) { history.past.push(last) history.future = [] } // Remembered for next time: every change made in the open editor, past its opening. if (!opening && dialog?.open && last?.section === now.section) saveSettings(now) restoring = false last = now }) // The remembered settings, in this browser: all but the date, which belongs to its section. const settingsKey = "opencode-data-share-settings" const saveSettings = (value: ReturnType) => { const { section, day, ...kept } = value try { localStorage.setItem(settingsKey, JSON.stringify(kept)) } catch { // Storage full or blocked: the settings just aren't remembered. } } const restoreSettings = () => { const saved = parseSettings(localStorage.getItem(settingsKey)) if (!saved) return false batch(() => { setFormat(saved.format) setLead(saved.lead) setChartStyle(saved.chart) setShareOption("layout", saved.layout) setShareOption("size", saved.size) setShareOption("theme", saved.theme) setTrack(saved.track) setMuted(saved.muted) setFixed(saved.fixed) }) return true } const travel = (redo: boolean) => { const target = (redo ? history.future : history.past).pop() const now = last if (!target || !now) return ;(redo ? history.past : history.future).push(now) restoring = true batch(() => { setFormat(target.format) setDay(target.day) setLead(target.lead) setChartStyle(target.chart) setShareOption("layout", target.layout) setShareOption("size", target.size) setShareOption("theme", target.theme) setTrack(target.track) setMuted(target.muted) setFixed(target.fixed) }) } const file = async (kind: "image" | "video") => { const value = card() if (!value) throw new Error(i18n.t("share.error.empty")) await loadShareLogos(cardLogos(value.chart)) if (kind === "image") return renderImage(value, options()) const key = JSON.stringify([value, options(), music()?.id]) if (video?.key !== key) { setCancelReady(false) setRendering(0) video = { key, file: renderVideo( value, options(), music(), timing(), setRendering, (renderAbort = new AbortController()).signal, ) .catch((error: unknown) => { // A cancelled or failed render isn't kept, so the next Export starts again. video = undefined throw error }) .finally(() => setRendering(undefined)), } } return video.file } const run = async (action: () => Promise) => { try { const message = await action() if (message) setNotice(message) } catch (error) { if (error instanceof DOMException && error.name === "AbortError") return setNotice(error instanceof Error ? error.message : i18n.t("share.error.generic")) } } // Export saves the PNG or the video. On phones, where saving a file goes nowhere useful, it opens the share sheet. const exportFile = () => { // While a video renders, the button is Cancel. if (rendering() !== undefined) return renderAbort?.abort() return run(async () => { const kind = format() // The button says Copied for a moment instead of a notice. if (kind === "embed") { await copyText(snippet(), dialog, i18n.t("share.error.copy")) setCopied(true) setTimeout(() => setCopied(false), 1600) return undefined } const value = card() const shared = await file(kind) if (!value) return const data = { files: [shared], title: value.title, text: `${value.headline} ${link(value)}` } if (matchMedia("(pointer: coarse)").matches && navigator.canShare?.(data)) { await navigator.share(data) return undefined } download(shared) return i18n.t(kind === "image" ? "share.saved.image" : "share.saved.video") }) } // Pressing the chosen track again plays or pauses it, with the preview. const chooseTrack = (id: string) => { if (id === track() && id !== "none" && !muted()) return setPlaying(!playing()) setTrack(id) setMuted(false) setPlaying(true) } return ( { if (!(event.metaKey || event.ctrlKey) || event.key.toLowerCase() !== "z") return event.preventDefault() travel(event.shiftKey) }} // Escape closes it natively; a click on the backdrop lands on the dialog itself, since the editor fills its box. onClick={(event) => { if (event.target === event.currentTarget) close() }} // Escape closes an open dropdown first, then the editor. onCancel={(event) => { if (!menu() && !sheet()) return event.preventDefault() if (menu()) return setMenu(undefined) setSheet(false) }} onClose={() => { renderAbort?.abort() setPlaying(false) audio?.pause() opener?.focus() // Keep the editor drawn while it fades out. closing = setTimeout(() => { setRequest(undefined) setReady(false) setFormat("video") }, 160) }} > {(value) => (
{/* The bar across the top, like an editor's: Close and Shuffle, every option as a dropdown, the music with arrows either side, and Export on the right. */}
{ bar = element const observer = new ResizeObserver(() => fitBar()) observer.observe(element) onCleanup(() => observer.disconnect()) }} data-slot="editor-bar" role="toolbar" aria-label={i18n.t("share.openSection", { title: value().title })} > {/* Phones only: the settings live in a sheet, opened from here. */}
designOnly.includes(control.key))}> {(control) => { const index = () => control.options.findIndex((option) => option[0] === options()[control.key]) return (
{control.label} {control.options[index()]?.[1]} {index() + 1}/{control.options.length}
) }}
{ preview = element // Where the card sits in its area, for the playback bar and the embed code under it. const observer = new ResizeObserver(() => setCardBox({ left: element.offsetLeft, top: element.offsetTop, width: element.offsetWidth, height: element.offsetHeight, }), ) observer.observe(element) if (element.parentElement) observer.observe(element.parentElement) onCleanup(() => observer.disconnect()) }} data-slot="share-preview" role="img" aria-label={value().headline} style={{ "aspect-ratio": `${size()[0]} / ${size()[1]}` }} /> {/* A video's playback: a thin bar just under the card, filling as the preview plays. */} {/* A timeline: press or drag along it to move the video and its music; the arrow keys step a second. */}
{ event.currentTarget.setPointerCapture(event.pointerId) seekFrom(event, event.currentTarget) }} onPointerMove={(event) => { if (event.currentTarget.hasPointerCapture(event.pointerId)) seekFrom(event, event.currentTarget) }} onKeyDown={(event) => { if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return event.preventDefault() seek(progress() + (event.key === "ArrowLeft" ? -1 : 1) / Math.max(1, loop.length)) }} >
{/* An embed's code under the card, as wide as it, highlighted as it will be copied; pressing it copies it too. It opens by growing, so the card eases smaller to make room rather than jumping. */}
{/* Copying shows here as well as on the button, where the press was. */}
{/* Under the card, the section's name between arrows that step to the previous and next section; a saved or copied notice takes the name's place for a moment. */}
{notice() ?? value().title}
)}
) } // Copies `text`: the Clipboard API, or where a browser refuses it (no focus, no permission) a selected hidden field // inside the editor and the copy command, which the top-layer dialog lets through. async function copyText(text: string, host: HTMLElement | undefined, failure: string) { const written = await navigator.clipboard?.writeText(text).then( () => true, () => false, ) if (written) return const field = document.createElement("textarea") field.value = text field.setAttribute("readonly", "") field.style.position = "fixed" field.style.opacity = "0" ;(host ?? document.body).append(field) field.select() const copied = document.execCommand("copy") field.remove() if (!copied) throw new Error(failure) } // The embed code split for highlighting: the tag's name, each attribute's name and quoted value, and the brackets. function tokens(code: string) { return [...code.matchAll(/(<\/?)([\w-]+)|([\w-]+)(=)("[^"]*")|(>)|(\s+)/g)].flatMap((match) => { if (match[2]) return [ { kind: "punctuation", text: match[1]! }, { kind: "tag", text: match[2] }, ] if (match[3]) return [ { kind: "attribute", text: match[3] }, { kind: "punctuation", text: match[4]! }, { kind: "string", text: match[5]! }, ] return [{ kind: match[6] ? "punctuation" : "space", text: match[0] }] }) } // Settings saved by an earlier visit, if they're still all valid choices. function parseSettings(raw: string | null) { const value: unknown = (() => { try { return raw ? JSON.parse(raw) : undefined } catch { return undefined } })() if (!value || typeof value !== "object") return undefined const item = value as Record const one = (key: string, choices: readonly T[]) => choices.find((choice) => choice === item[key]) const format = one("format", ["video", "image", "embed"] as const) const lead = one("lead", ["top", "total"] as const) const chart = one("chart", ["bars", "race", "list", "dots"] as const) const layout = one( "layout", layouts.map((entry) => entry.value), ) const size = one( "size", sizes.map((entry) => entry.value), ) const theme = one("theme", ["dark", "light", "leader"] as const) const track = one("track", ["none", ...shareTracks.map((entry) => entry.id)]) if (!format || !lead || !chart || !layout || !size || !theme || !track) return undefined return { format, lead, chart, layout, size, theme, track, muted: item.muted === true, fixed: item.fixed === true } } function pick(items: readonly T[]) { return items[Math.floor(Math.random() * items.length)]! } // A dropdown in the bar, a fixed width: by `show`, its icon alone, its value alone, or both (the music: the cover, from // `trigger`, and the name); then a chevron. The choices open in a list under it, each with its icon and name. Arrow keys move through the list, Enter or a click chooses, Escape or a press elsewhere closes it. function Dropdown(props: { id: string label: string open: string | undefined onOpen: (id: string | undefined) => void value: T options: { value: T; label: string; detail?: string; explicit?: boolean; icon?: JSX.Element }[] show?: "icon" | "value" | "both" // Shuffle's count: each change spins a reel of random choices that lands on the value, starting rollDelay ms in. roll?: number rollDelay?: number // Called with the choice under the pointer or keyboard focus while the list is open, and with undefined once it closes. onPreview?: (value: T | undefined) => void trigger?: JSX.Element onSelect: (value: T) => void }) { const i18n = useI18n() const show = () => props.show ?? "icon" // The reel: random choices, then the value, scrolled up through the button like an odometer's wheel. const [reel, setReel] = createSignal() createEffect( on( () => props.roll, () => { if (matchMedia("(prefers-reduced-motion: reduce)").matches) return if (props.options.length < 2) return // Built back from the value, each row a different choice from the one after it, so the reel visibly turns // even with only two choices, flipping between them. const turns = Array.from({ length: 9 }).reduce( (list) => { const others = props.options.filter((item) => item.value !== list[0]) return [others[Math.floor(Math.random() * others.length)]!.value, ...list] }, [props.value], ) setReel(turns) }, { defer: true }, ), ) const option = (value: T) => props.options.find((item) => item.value === value) let element: HTMLDivElement | undefined const open = () => props.open === props.id const current = () => props.options.find((item) => item.value === props.value) // The Explicit badge in the button: the whole word when the name still fits beside it, E when it would be cut off. const [short, setShort] = createSignal(false) createEffect(() => { if (!current()?.explicit) return setShort(false) const frame = requestAnimationFrame(() => { const value = element?.querySelector("[data-slot=dropdown-trigger] [data-slot=dropdown-value]") setShort(!!value && value.scrollWidth > value.clientWidth) }) onCleanup(() => cancelAnimationFrame(frame)) }) const close = (focus: boolean) => { props.onOpen(undefined) if (focus) element?.querySelector("[data-slot=dropdown-trigger]")?.focus() } // Opening focuses the chosen item, so the arrow keys start from it. createEffect(() => { if (!open()) return props.onPreview?.(undefined) requestAnimationFrame(() => element?.querySelector("[role=option][aria-selected=true]")?.focus()) const away = (event: PointerEvent) => { if (element && !element.contains(event.target as Node)) close(false) } document.addEventListener("pointerdown", away, true) onCleanup(() => document.removeEventListener("pointerdown", away, true)) }) const keys = (event: KeyboardEvent) => { const items = [...(element?.querySelectorAll("[role=option]") ?? [])] const index = items.indexOf(document.activeElement as HTMLElement) if (event.key === "ArrowDown" || event.key === "ArrowUp") { event.preventDefault() items[(index + (event.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus() } if (event.key === "Tab") close(false) } return (
{props.label}
{(item) => ( )}
) } function Chevron() { return ( ) } // A track's cover, with `state` the play state over it (the bar's chosen track); for No music, a plain speaker icon // drawn like the bar's other icons. function MusicCover(props: { cover?: string; state?: "playing" | "paused" }) { return ( ) } // The embed's data: a broadcast for live, a padlock for fixed. function DataGlyph(props: { fixed: boolean }) { return ( ) } // A size's shape: its ratio as a rectangle, the longer side 12 units. function AspectGlyph(props: { size: ShareOptions["size"] }) { const box = () => { const [width, height] = shareSizes[props.size] const scale = 12 / Math.max(width, height) return { width: width * scale, height: height * scale } } return ( ) } // The chart's style: columns rising for bars, rows of different lengths for a race. function ChartGlyph(props: { style: "bars" | "race" | "list" | "dots" }) { return ( // Snapped to whole pixels: these lines fall between them at 20px, and blur thinner than the other icons' lines. ) } // Lead with: a trophy for the top model, the sum sign for the total. function LeadGlyph(props: { lead: "top" | "total" }) { return ( ) } // The type icons: a video camera, a picture, and the angle brackets of code. function TypeGlyph(props: { format: "video" | "image" | "embed" }) { return ( ) } // The layout icons, each a card: three lines of headline; a big number, an OpenMono 9; a short title over a chart. function LayoutGlyph(props: { layout: ShareOptions["layout"] }) { return ( ) } function link(card: ShareCard) { return `https://${card.url}/#${card.id}` } function fileName(card: ShareCard, extension: string) { return `opencode-data-${card.id}-${new Date().toISOString().slice(0, 10)}.${extension}` } function download(file: File) { const url = URL.createObjectURL(file) const anchor = document.createElement("a") anchor.href = url anchor.download = file.name anchor.click() setTimeout(() => URL.revokeObjectURL(url), 1000) } async function renderImage(card: ShareCard, options: ShareOptions) { await loadShareFonts() const canvas = document.createElement("canvas") const [width, height] = shareSizes[options.size] canvas.width = width canvas.height = height const ctx = canvas.getContext("2d") if (!ctx) throw new Error(translate(card.locale, "share.error.canvas")) drawShareCard(ctx, card, options, Infinity) const blob = await new Promise((resolve) => canvas.toBlob(resolve, "image/png")) if (!blob) throw new Error(translate(card.locale, "share.error.image")) return new File([blob], fileName(card, "png"), { type: "image/png" }) } // Records the animation in real time from a canvas stream, with the soundtrack mixed in through Web Audio and faded // out at the end. MP4 where the browser can record it (Safari, Chrome), WebM otherwise. async function renderVideo( card: ShareCard, options: ShareOptions, track: ShareTrack | undefined, timing: ShareTiming | undefined, progress: (value: number) => void, signal: AbortSignal, ) { // Created before the first await, while the click still counts, so the context starts running. const context = track ? new AudioContext() : undefined await loadShareFonts() const types = track ? ["video/mp4;codecs=avc1.640028,mp4a.40.2", "video/mp4", "video/webm;codecs=vp9,opus", "video/webm"] : ["video/mp4;codecs=avc1.640028", "video/mp4", "video/webm;codecs=vp9", "video/webm"] const type = types.find((item) => MediaRecorder.isTypeSupported(item)) if (!type) throw new Error(translate(card.locale, "share.error.video")) const canvas = document.createElement("canvas") const [width, height] = shareSizes[options.size] canvas.width = width canvas.height = height const ctx = canvas.getContext("2d") if (!ctx) throw new Error(translate(card.locale, "share.error.canvas")) drawShareCard(ctx, card, options, 0, timing) const length = shareDuration(card, options, timing) const sound = context && track ? await soundtrack(context, track.url, length) : undefined const stream = new MediaStream([ ...canvas.captureStream(30).getVideoTracks(), ...(sound?.stream.getAudioTracks() ?? []), ]) const recorder = new MediaRecorder(stream, { mimeType: type, videoBitsPerSecond: 8_000_000, audioBitsPerSecond: 192_000, }) const chunks: Blob[] = [] recorder.ondataavailable = (event) => chunks.push(event.data) const done = new Promise((resolve) => (recorder.onstop = () => resolve())) recorder.start() sound?.start() const start = performance.now() await new Promise((resolve) => { const frame = () => { const time = (performance.now() - start) / 1000 drawShareCard(ctx, card, options, time, timing) progress(Math.min(1, time / length)) if (time >= length || signal.aborted) return resolve() requestAnimationFrame(frame) } requestAnimationFrame(frame) }) recorder.stop() await done void context?.close() if (signal.aborted) throw new DOMException("Export cancelled", "AbortError") const base = type.split(";")[0] ?? "video/webm" return new File([new Blob(chunks, { type: base })], fileName(card, base === "video/mp4" ? "mp4" : "webm"), { type: base, }) } async function soundtrack(context: AudioContext, url: string, length: number) { const buffer = await context.decodeAudioData(await (await fetch(url)).arrayBuffer()) const destination = context.createMediaStreamDestination() const gain = context.createGain() gain.connect(destination) return { stream: destination.stream, start: () => { const source = context.createBufferSource() source.buffer = buffer source.connect(gain) const now = context.currentTime gain.gain.setValueAtTime(1, now) // Tracks stop dead on their last word; the short ramp only keeps the cut from clicking. gain.gain.setValueAtTime(1, now + Math.max(0, length - 0.15)) gain.gain.linearRampToValueAtTime(0, now + length) source.start(now) source.stop(now + length + 0.1) }, } }