okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221apps/host: the owner's UI and the Node App Host0b3299a01

main
1.9 KiB21 linesraw
1import { tr } from '../lib/i18n';
2import { useState } from 'react';
3import { Btn } from './ui';
4import { safePreviewUrl, type PreviewArtifact } from '../lib/artifacts';
5import type { ChatMessage } from '../lib/types';
6export function ChatMedia({ items, onPreview }: {
7    items: NonNullable<ChatMessage['media']>;
8    onPreview: (a: PreviewArtifact) => void;
9}) {
10    return <div className="chat-media">{items.map(m => <Media key={m.src} item={m} onPreview={onPreview}/>)}</div>;
11}
12function Media({ item: m, onPreview }: {
13    item: NonNullable<ChatMessage['media']>[number];
14    onPreview: (a: PreviewArtifact) => void;
15}) {
16    const [playing, setPlaying] = useState(() => !matchMedia('(prefers-reduced-motion: reduce)').matches), [error, setError] = useState(false);
17    const src = safePreviewUrl(m.src), poster = m.poster ? safePreviewUrl(m.poster) : null;
18    if (!src)
19        return null;
20    return <figure className="chat-media__item"><div className="chat-media__head"><span>{tr(m.kind === 'gif' ? 'Animated GIF' : m.kind === 'video' ? 'Video' : 'Image')}</span><span className="muted">{m.title}</span></div>{error ? <p className="preview-placeholder" role="alert">{tr("This media could not load.")}</p> : m.kind === 'video' ? <video src={src} poster={poster ?? undefined} controls playsInline preload="metadata" aria-label={m.title} onError={() => setError(true)}/> : <button className="chat-media__image" aria-label={tr("Open {0} in sidebar", [m.title])} onClick={() => onPreview({ id: m.src, kind: 'image', url: src, title: m.title, demo: true })}><img src={m.kind === 'gif' && !playing && poster ? poster : src} alt={m.caption ?? m.title} loading="lazy" onError={() => setError(true)}/></button>}<figcaption><span>{m.caption}</span>{tr(m.kind === 'gif' && poster ? <Btn size="sm" variant="ghost" onClick={() => setPlaying(v => !v)}>{tr(playing ? 'Pause animation' : 'Play animation')}</Btn> : null)}</figcaption></figure>;
21}