import React, { useState, useEffect, useCallback, useRef } from 'react' import { createRoot } from 'react-dom/client' import CommentsFeed from '../../components/comments/CommentsFeed' const FILTER_TABS = [ { key: 'all', label: 'All' }, { key: 'following', label: 'Following', authRequired: true }, { key: 'mine', label: 'My Comments', authRequired: true }, ] function LatestCommentsPage({ initialComments = [], initialMeta = {}, isAuthenticated = false }) { const [activeFilter, setActiveFilter] = useState('all') const [comments, setComments] = useState(initialComments) const [meta, setMeta] = useState(initialMeta) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) // Track if we've moved off the initial server-rendered data const initialized = useRef(false) const fetchComments = useCallback(async (filter, page = 1) => { setLoading(true) setError(null) try { const url = `/api/comments/latest?type=${encodeURIComponent(filter)}&page=${page}` const res = await fetch(url, { headers: { 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }, credentials: 'same-origin', }) if (res.status === 401) { setError('Please log in to view this feed.') setComments([]) setMeta({}) return } if (! res.ok) { setError('Failed to load comments. Please try again.') return } const json = await res.json() setComments(json.data ?? []) setMeta(json.meta ?? {}) } catch { setError('Network error. Please try again.') } finally { setLoading(false) } }, []) const handleFilterChange = (key) => { if (key === activeFilter) return setActiveFilter(key) initialized.current = true fetchComments(key, 1) } const handlePageChange = (page) => { initialized.current = true fetchComments(activeFilter, page) window.scrollTo({ top: 0, behavior: 'smooth' }) } return (
{/* Page header */}

Community

Latest Comments

Most recent artwork comments from the community.

{/* Filter tabs — pill style */}
{FILTER_TABS.map((tab) => { const disabled = tab.authRequired && !isAuthenticated const active = activeFilter === tab.key return ( ) })}
{/* Feed content */}
) } // Auto-mount when the Blade view provides #latest-comments-root const mountEl = document.getElementById('latest-comments-root') if (mountEl) { let props = {} try { const propsEl = document.getElementById('latest-comments-props') props = propsEl ? JSON.parse(propsEl.textContent || '{}') : {} } catch { props = {} } createRoot(mountEl).render() } export default LatestCommentsPage