import type { mastodon } from "masto"; import { type Component, Index, Match, Switch, createMemo, createSignal, untrack, } from "solid-js"; import { createElementSize, useWindowSize, } from "@solid-primitives/resize-observer"; import { useStore } from "@nanostores/solid"; import { $settings } from "../../settings/stores"; import { averageColorHex } from "~platform/blurhash"; import "./MediaAttachmentGrid.css"; import "~material/cards.css"; import { Preview } from "@suid/icons-material"; import { IconButton } from "@suid/material"; import Masonry from "~platform/Masonry"; import { createMediaQuickview } from "~platform/MediaQuickview"; type ElementSize = { width: number; height: number }; function constraintedSize( { width: owidth, height: oheight }: Readonly, // originalSize { width: mwidth, height: mheight }: Readonly>, // modifier { width: maxWidth, height: maxHeight }: Readonly, // maxSize ) { const ySize = owidth + (mwidth ?? 0); const yScale = ySize > maxWidth ? ySize / maxWidth : 1; const xSize = oheight + (mheight ?? 0); const xScale = xSize > maxHeight ? xSize / maxHeight : 1; const maxScale = Math.max(yScale, xScale); const scaledWidth = owidth / maxScale; const scaledHeight = oheight / maxScale; return { width: scaledWidth, height: scaledHeight, }; } function isolateCallback(event: Event) { if (event.target !== event.currentTarget) { event.stopPropagation(); } } const MediaAttachmentGrid: Component<{ attachments: mastodon.v1.MediaAttachment[]; sensitive?: boolean; }> = (props) => { const [rootRef, setRootRef] = createSignal(); const settings = useStore($settings); const windowSize = useWindowSize(); const [reveal, setReveal] = createSignal([] as number[]); const openMediaQuickview = createMediaQuickview(); const openViewerFor = (index: number) => { openMediaQuickview( props.attachments.map((item) => { return { cat: item.type, src: item.url as string, alt: item.description || undefined, }; }), index, ); }; const columnCount = () => { if (props.attachments.length === 1) { return 1; } else if (props.attachments.length % 2 === 0) { return 2; } else { return 3; } }; const rawElementSize = createElementSize(rootRef); const elementWidth = () => rawElementSize.width; const itemMaxSize = createMemo(() => { const ewidth = elementWidth(); const width = ewidth ? (ewidth - (columnCount() - 1) * 4) / columnCount() : 1; return { height: windowSize.height * 0.35, width, }; }); const itemStyle = (item: mastodon.v1.MediaAttachment) => { const { width, height } = constraintedSize( item.meta?.small || { width: 1, height: 1 }, { width: 2, height: 2 }, itemMaxSize(), ); const accentColor = item.meta?.colors?.accent ?? (item.blurhash ? averageColorHex(item.blurhash) : undefined); return Object.assign( { width: `${width}px`, height: `${height}px`, "contain-intrinsic-size": `${width}px ${height}px`, }, accentColor ? { "--media-color-accent": accentColor } : {}, ); }; const isReveal = (idx: number) => { return reveal().includes(idx); }; const addReveal = (idx: number) => { if (!untrack(() => isReveal(idx))) { setReveal((x) => [...x, idx]); } }; return ( {(item, index) => { const itemType = () => item().type; const style = createMemo(() => itemStyle(item())); return (
{item().description
); }}
); }; export default MediaAttachmentGrid;