import { useState, useEffect } from 'react' import { useParams, useNavigate, useLocation } from 'react-router-dom' import { getUserPosts, getUser, startDownload, getDownloadCursor } from '../api' import PostCard from '../components/PostCard' function decodeHTML(str) { if (!str) return str const el = document.createElement('textarea') el.innerHTML = str return el.value } import Spinner from '../components/Spinner' import LoadMoreButton from '../components/LoadMoreButton' export default function UserPosts() { const { userId } = useParams() const navigate = useNavigate() const location = useLocation() const [user, setUser] = useState(location.state?.user || null) const [posts, setPosts] = useState([]) const [tailMarker, setTailMarker] = useState(null) const [loading, setLoading] = useState(true) const [loadingMore, setLoadingMore] = useState(false) const [hasMore, setHasMore] = useState(true) const [error, setError] = useState(null) const [downloading, setDownloading] = useState(false) const [showDownloadMenu, setShowDownloadMenu] = useState(false) const [cursorInfo, setCursorInfo] = useState(null) const fetchCursor = () => { getDownloadCursor(userId).then((data) => { if (data && data.hasCursor) setCursorInfo(data) else setCursorInfo(null) }) } useEffect(() => { loadPosts() fetchCursor() // Fetch user info if not passed via location state if (!user) { getUser(userId).then((data) => { if (!data.error) { setUser(data) } }) } }, [userId]) const loadPosts = async () => { setLoading(true) setError(null) const data = await getUserPosts(userId) if (data.error) { setError(data.error) setLoading(false) return } const items = data.list || data || [] setPosts(items) setTailMarker(data.tailMarker || null) setHasMore(items.length > 0 && !!data.tailMarker) setLoading(false) } const loadMore = async () => { if (!tailMarker || loadingMore) return setLoadingMore(true) const data = await getUserPosts(userId, tailMarker) if (!data.error) { const items = data.list || data || [] setPosts((prev) => [...prev, ...items]) setTailMarker(data.tailMarker || null) setHasMore(items.length > 0 && !!data.tailMarker) } setLoadingMore(false) } const handleDownload = async (limit, resume) => { setShowDownloadMenu(false) setDownloading(true) const result = await startDownload(userId, limit, resume, user?.username) if (result.error) { console.error('Download failed:', result.error) } setTimeout(() => { setDownloading(false) fetchCursor() }, 2000) } return (
@{user.username}
{(user.postsCount !== undefined || user.mediasCount !== undefined) && ({user.postsCount !== undefined && `${user.postsCount.toLocaleString()} posts`} {user.postsCount !== undefined && user.mediasCount !== undefined && ' ยท '} {user.mediasCount !== undefined && `${user.mediasCount.toLocaleString()} media`}
)}{error}
No posts found