// Entry detail screen — with threaded comments
function EntryScreen({ entry, onBack, user }) {
const { ENCOUNTER_TYPES } = window.ARCHIVE;
// Build the per-entry list of category metas — supports multi-category
// entries while staying compatible with legacy single-`type` entries.
const categoryIds = Array.isArray(entry.types) && entry.types.length > 0
? entry.types
: (entry.type ? [entry.type] : []);
const typeMetas = categoryIds.map(id => ENCOUNTER_TYPES.find(t => t.id === id)).filter(Boolean);
const typeMeta = typeMetas[0] || {};
const acc = window.entryAccent(entry);
const [vote, setVote] = React.useState(null);
const [newThread, setNewThread] = React.useState('');
const [threads, setThreads] = React.useState([]);
const [replyingTo, setReplyingTo] = React.useState(null);
const [replyBody, setReplyBody] = React.useState('');
const [witnesses, setWitnesses] = React.useState(entry.witnesses);
const [believeCount, setBelieveCount] = React.useState(entry.believe);
const [skepticalCount, setSkepticalCount] = React.useState(entry.skeptical);
// Attached files come from the single-entry endpoint (list responses
// don't include them). Starts with whatever the passed entry carries.
const [files, setFiles] = React.useState(Array.isArray(entry.files) ? entry.files : []);
const [thumbnail, setThumbnailUrl] = React.useState(entry.thumbnail || null);
const [copied, setCopied] = React.useState(false);
// The picture open in the full-size viewer (null = closed).
const [viewing, setViewing] = React.useState(null);
React.useEffect(() => {
if (!viewing) return;
const onKey = (e) => { if (e.key === 'Escape') setViewing(null); };
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [viewing]);
// Phones get the system share sheet; elsewhere the link is copied.
const share = async () => {
const url = window.location.origin + window.entryPath(entry);
try {
if (navigator.share) { await navigator.share({ title: entry.title, url }); return; }
await navigator.clipboard.writeText(url);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch (err) { /* share sheet dismissed */ }
};
const hasCoords = Number.isFinite(entry.lat) && Number.isFinite(entry.lng);
const authorName = entry.author_name || entry.author || 'anonymous';
// Load comments + full record (files) from API
React.useEffect(() => {
window.api.getComments(entry.id).then(data => {
setThreads(data.threads || []);
}).catch(err => console.warn('Could not load comments:', err));
window.api.getEntry(entry.id).then(full => {
if (Array.isArray(full.files)) setFiles(full.files);
if (full.thumbnail) setThumbnailUrl(full.thumbnail);
}).catch(err => console.warn('Could not load entry files:', err));
}, [entry.id]);
// Evidence = images/video; anything else lands in the documents table.
const isMedia = (f) => /^(image|video)\//.test(f.mime_type || f.mime || '');
const evidence = files.filter(isMedia);
const documents = files.filter(f => !isMedia(f));
const fmtSize = (s) => {
const n = parseInt(s, 10);
if (!Number.isFinite(n)) return '';
if (n >= 1024 * 1024) return (n / (1024 * 1024)).toFixed(1) + ' MB';
if (n >= 1024) return Math.round(n / 1024) + ' KB';
return n + ' B';
};
const openFile = async (f) => {
try {
const { url } = await window.api.get(`/entries/${entry.id}/files/${f.id}/url`);
window.open(url, '_blank', 'noopener');
} catch (err) { console.warn('Could not open file:', err); }
};
// The full account, split into paragraphs. Falls back to the summary
// for entries submitted without a long-form narrative.
const paragraphs = (entry.full_account || '')
.split(/\n+/).map(s => s.trim()).filter(Boolean);
const sig = (n) => (
{[1,2,3,4,5].map(i => )}
);
const postThread = async () => {
if (!newThread.trim()) return;
try {
const created = await window.api.postComment(entry.id, {
body: newThread,
author_name: user?.name || 'anonymous',
author_id: user?.id || null,
});
setThreads([{ ...created, author: created.author_name, time: 'just now', replies: [] }, ...threads]);
setNewThread('');
} catch (err) {
console.error('Post thread failed:', err);
}
};
const postReply = async (threadId) => {
if (!replyBody.trim()) return;
try {
const created = await window.api.postComment(entry.id, {
body: replyBody,
author_name: user?.name || 'anonymous',
author_id: user?.id || null,
parent_id: threadId,
});
setThreads(threads.map(t => t.id === threadId
? { ...t, replies: [...t.replies, { ...created, author: created.author_name, time: 'just now', replies: [] }] }
: t
));
setReplyBody('');
setReplyingTo(null);
} catch (err) {
console.error('Post reply failed:', err);
}
};
return (
{/* Meta header */}
← BACK TO ARCHIVE/ENTRY {entry.id}/FILED BY {authorName.toUpperCase()}◷ {window.formatDate(entry.date)}{entry.time ? ` · ${entry.time}` : ''}
{hasCoords && ⌖ {entry.lat.toFixed(3)}°, {entry.lng.toFixed(3)}°}
{/* Hero */}
{/* With a picture, its column is exactly as wide as the picture at 440px
high (capped at 60% of the page); the title panel takes the rest. */}