Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : apps/social-web/src/ui/Preview.tsx9 * Module : ui10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Faithful preview: the exact text (line breaks kept), the link, the images and the alternative text.43// A neutral card on purpose: it does not imitate any platform's interface.44import { useMemo } from 'react';45import { get } from '../../../shared/api.ts';46import { usePrefs } from '../prefs.tsx';47import type { Media } from '../types.ts';48import { ExtLink, Hash, Tag, useLoad, type Loaded } from './core.tsx';4950export type PreviewContent = { lang: string; body: string; linkUrl: string | null; altText: string | null; mediaSha256: string[] };51export type MediaIndex = Loaded<Map<string, Media>>;5253/** Media of one brand, indexed by sha256 (signed URLs are short-lived: reload to refresh them). */54export function useMediaIndex(brandId: string | null | undefined): MediaIndex {55 const l = useLoad(async () => (brandId ? (await get<{ media: Media[] }>(`/v1/media?brandId=${encodeURIComponent(brandId)}`)).media : []), [brandId]);56 const map = useMemo(() => (l.data ? new Map(l.data.map((m) => [m.sha256, m])) : undefined), [l.data]);57 return { data: map, error: l.error, loading: l.loading, reload: l.reload };58}5960export function Preview({ content, media, pageName, hash, version }: { content: PreviewContent; media: MediaIndex; pageName?: string | null; hash?: string; version?: number }) {61 const { t, tl } = usePrefs();62 return (63 <article className="preview" lang={content.lang} aria-label={t('pv.title')}>64 <header className="preview-head">65 <span>{t('pv.title')}</span>66 <span>{pageName ? t('pv.page', { page: pageName }) : t('pv.noPage')} · {tl('c.lang', content.lang)}</span>67 </header>68 {content.body ? <p className="preview-body">{content.body}</p> : <p className="empty">{t('pv.emptyBody')}</p>}69 {content.linkUrl ? <p className="preview-link"><span className="preview-k">{t('c.link')}</span> <ExtLink href={content.linkUrl} /></p> : null}70 {content.mediaSha256.length ? (71 <ul className="preview-media">72 {content.mediaSha256.map((sha) => {73 const m = media.data?.get(sha);74 return (75 <li key={sha}>76 {m?.url ? <img src={m.url} alt={content.altText ?? m.alt_text ?? ''} width={m.width} height={m.height} loading="lazy" /> : <div className="preview-missing">{media.loading ? t('c.loading') : t('pv.mediaMissing')}</div>}77 <p className="small"><Hash value={sha} /> {m ? <Tag prefix="mediaStatus" value={m.status} /> : media.data ? <Tag prefix="mediaStatus" value="missing" /> : null}</p>78 </li>79 );80 })}81 </ul>82 ) : null}83 <p className="preview-alt"><span className="preview-k">{t('pv.alt')}</span> {content.altText ? content.altText : <em>{t('pv.noAlt')}</em>}</p>84 {hash ? <footer className="preview-foot">{version !== undefined ? <span>{t('c.version')} {version}</span> : null}<span>{t('c.hash')} <Hash value={hash} full /></span></footer> : null}85 </article>86 );87}88