let currentJourney = null;
const urlParams = new URLSearchParams(window.location.search);
const journeyId = urlParams.get('id');
// ==================== LOAD JOURNEY ====================
async function loadJourney() {
if (!journeyId) {
window.location.href = 'blog-list.html';
return;
}
try {
const res = await fetch(`${API_BASE}/journeys/${journeyId}`, { credentials: 'include' });
if (!res.ok) throw new Error('Journey not found');
currentJourney = await res.json();
renderJourney();
loadComments();
} catch (err) {
showToast('Error loading journey', true);
setTimeout(() => window.location.href = 'blog-list.html', 2000);
}
}
function renderJourney() {
const container = document.getElementById('post-content');
const isOwner = currentUser && currentUser.id === currentJourney.owner_id;
const canEdit = canEditJourney();
// Build chapters from markers
let chaptersHtml = '';
if (currentJourney.markers && currentJourney.markers.length) {
chaptersHtml = '
';
currentJourney.markers.forEach((marker, idx) => {
const title = marker.title || `Chapter ${idx + 1}`;
const date = marker.date ? `
${new Date(marker.date).toLocaleDateString()}` : '';
const content = marker.description ? renderMarkdown(marker.description) : '';
marker.images = getMarkerImages(marker);
const images = getMarkerImagesHtml(marker.images, idx, canEdit);
chaptersHtml += `
${images}
${canEdit ? getImageToolsHtml(idx) : ''}
${content}
`;
});
chaptersHtml += '
';
} else {
chaptersHtml = 'No chapters yet.
';
}
container.innerHTML = `
${escapeHtml(currentJourney.title)}
${new Date(currentJourney.created_at).toLocaleDateString()}
${currentJourney.visibility === 'public' ? 'Public' : ''}
${currentJourney.visibility === 'shared' && !isOwner ? `${canEdit ? 'Shared edit' : 'Shared'}` : ''}
${currentJourney.image ? `
` : ''}
${renderMarkdown(currentJourney.description)}
${chaptersHtml}
${canEdit || isOwner ? `
${canEdit ? `
Edit` : ''}
${isOwner ? `
` : ''}
` : ''}
`;
if (isOwner) {
document.getElementById('delete-journey-btn')?.addEventListener('click', deleteJourney);
}
if (canEdit) {
attachChapterImageControls();
}
}
function getUploadUrl(path) {
if (!path) return '';
if (path.startsWith('http')) return sanitizeDisplayUrl(path);
return API_BASE.replace('/api', path);
}
function canEditJourney() {
if (!currentUser || !currentJourney) return false;
if (currentUser.id === currentJourney.owner_id) return true;
return currentJourney.visibility === 'shared'
&& Array.isArray(currentJourney.shared_edit)
&& currentJourney.shared_edit.includes(currentUser.id);
}
function canViewJourney() {
if (!currentUser || !currentJourney) return false;
if (currentUser.id === currentJourney.owner_id) return true;
if (currentJourney.visibility === 'public') return true;
return currentJourney.visibility === 'shared'
&& (
(Array.isArray(currentJourney.shared_read) && currentJourney.shared_read.includes(currentUser.id))
|| (Array.isArray(currentJourney.shared_edit) && currentJourney.shared_edit.includes(currentUser.id))
);
}
function getMarkerImages(marker) {
return marker.images || (marker.image ? [marker.image] : []);
}
function getMarkerImagesHtml(images, markerIndex, canEdit) {
if (!images || !images.length) return '';
const imageHtml = images.map((image, imageIndex) => {
const url = typeof image === 'string' ? image : image.url;
const alt = typeof image === 'string' ? 'Journey image' : image.originalName || 'Journey image';
const displayUrl = sanitizeDisplayUrl(getUploadUrl(url));
if (!displayUrl) return '';
return `
No comments yet. Be the first to comment!
${canComment && currentUser ? getCommentFormHtml() : (!currentUser ? '
${escapeHtml(comment.text)}