1
Fork 0
mirror of https://github.com/thegeneralist01/archivr synced 2026-12-04 18:56:44 +01:00
archivr/frontend/src/components/PreviewPanel.jsx

180 lines
5.6 KiB
JavaScript

import VideoPreview from './VideoPreview';
import IframePreview from './IframePreview';
import ImagePreview from './ImagePreview';
import TweetPreview from './TweetPreview';
import TextPreview from './TextPreview';
const VIDEO_EXTS = new Set(['mp4', 'webm', 'mov', 'mkv', 'avi', 'm4v', 'ogv']);
const AUDIO_EXTS = new Set(['mp3', 'ogg', 'm4a', 'opus', 'wav', 'flac', 'aac']);
const IMAGE_EXTS = new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'svg', 'bmp']);
const TEXT_EXTS = new Set(['md', 'markdown', 'txt']);
const CONTENT_TYPES = {
mp4: 'video/mp4', webm: 'video/webm', mov: 'video/quicktime',
mkv: 'video/x-matroska', avi: 'video/x-msvideo', m4v: 'video/mp4', ogv: 'video/ogg',
};
export default function PreviewPanel({ archiveId, entry, detail, fullPage, onXArticle }) {
if (!entry) {
return (
<div className="preview-panel preview-panel--empty">
<span style={{ color: 'var(--muted)', fontFamily: 'var(--sans)', fontSize: '0.9rem' }}>
Select an entry to preview
</span>
</div>
);
}
if (!detail) {
return (
<div className="preview-panel preview-panel--loading">
<span style={{ color: 'var(--muted)', fontFamily: 'var(--sans)', fontSize: '0.9rem' }}>
Loading…
</span>
</div>
);
}
const { summary, artifacts } = detail;
const entryUid = summary.entry_uid;
const entityKind = summary.entity_kind;
// 1. Tweet / tweet thread
if (entityKind === 'tweet' || entityKind === 'tweet_thread') {
const tweetPreview = (
<TweetPreview
archiveId={archiveId}
entryUid={entryUid}
artifacts={artifacts}
entityKind={entityKind}
originalUrl={summary.original_url}
fullPage={fullPage}
onXArticle={onXArticle}
/>
);
// In fullPage mode the parent (PreviewPage) is the scroll container;
// skip preview-tweet-wrap to avoid nested overflow/scroll.
if (fullPage) return tweetPreview;
return <div className="preview-tweet-wrap">{tweetPreview}</div>;
}
// 2. Find primary_media artifact
const primaryMediaIndex = artifacts.findIndex(a => a.artifact_role === 'primary_media');
if (primaryMediaIndex === -1) {
return (
<div className="preview-panel preview-panel--no-preview">
<span style={{ color: 'var(--muted)', fontFamily: 'var(--sans)', fontSize: '0.9rem' }}>
No preview available
</span>
{artifacts.length > 0 && (
<ul
style={{
marginTop: '12px',
paddingLeft: '20px',
fontSize: '0.8rem',
color: 'var(--muted-2)',
fontFamily: 'var(--sans)',
}}
>
{artifacts.map((a, i) => (
<li key={i}>{a.artifact_role}: {a.relpath}</li>
))}
</ul>
)}
</div>
);
}
const primaryArtifact = artifacts[primaryMediaIndex];
const primaryMediaUrl = `/api/archives/${archiveId}/entries/${entryUid}/artifacts/${primaryMediaIndex}`;
const ext = primaryArtifact.relpath.split('.').pop().toLowerCase();
// 3. Video
if (VIDEO_EXTS.has(ext)) {
return (
<div className="preview-panel">
<VideoPreview
src={primaryMediaUrl}
contentType={CONTENT_TYPES[ext] || 'video/mp4'}
/>
</div>
);
}
// 4. Audio — inline player (AudioBar handles persistent playback via rail Play button)
if (AUDIO_EXTS.has(ext)) {
return (
<div className="preview-panel preview-panel--audio" style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: '12px', padding: '24px', fontFamily: 'var(--sans)' }}>
<span style={{ fontSize: '2rem' }}>🎵</span>
<span style={{ color: 'var(--ink)', fontSize: '0.95rem', fontWeight: 600 }}>
{summary.title || entryUid}
</span>
<audio src={primaryMediaUrl} controls style={{ marginTop: '8px', width: '100%', maxWidth: '400px' }} />
</div>
);
}
// 5. PDF
if (ext === 'pdf') {
return (
<div className="preview-panel" style={{ flex: 1, minHeight: 0 }}>
<IframePreview src={primaryMediaUrl} type="pdf"
title={summary.title} originalUrl={summary.original_url} />
</div>
);
}
// 6. HTML page
if (ext === 'html' || ext === 'htm') {
return (
<div className="preview-panel" style={{ flex: 1, minHeight: 0 }}>
<IframePreview src={primaryMediaUrl} type="page"
title={summary.title} originalUrl={summary.original_url} />
</div>
);
}
// 6b. Plain text / Markdown
if (TEXT_EXTS.has(ext)) {
return (
<div className="preview-panel" style={{ flex: 1, minHeight: 0, overflow: 'auto' }}>
<TextPreview
src={primaryMediaUrl}
mime={primaryArtifact.mime_type}
title={summary.title}
/>
</div>
);
}
// 7. Image
if (IMAGE_EXTS.has(ext)) {
return (
<div className="preview-panel" style={{ height: '100%' }}>
<ImagePreview src={primaryMediaUrl} alt={summary.title || 'Image'} />
</div>
);
}
// 8. Fallback
return (
<div className="preview-panel preview-panel--no-preview">
<span style={{ color: 'var(--muted)', fontFamily: 'var(--sans)', fontSize: '0.9rem' }}>
No preview available
</span>
<ul
style={{
marginTop: '12px',
paddingLeft: '20px',
fontSize: '0.8rem',
color: 'var(--muted-2)',
fontFamily: 'var(--sans)',
}}
>
{artifacts.map((a, i) => (
<li key={i}>{a.artifact_role}: {a.relpath}</li>
))}
</ul>
</div>
);
}