import { useEffect, useRef, useState } from 'preact/hooks'; import Icon from '../components/Icon'; import Link from '../components/link'; import Loader from '../components/Loader'; import Status from '../components/status'; import { saveStatus } from '../utils/states'; import useTitle from '../utils/useTitle'; const LIMIT = 40; function Bookmarks() { useTitle('Bookmarks'); const [bookmarks, setBookmarks] = useState([]); const [uiState, setUIState] = useState('default'); const [showMore, setShowMore] = useState(false); const bookmarksIterator = useRef(masto.v1.bookmarks.list({ limit: LIMIT })); async function fetchBookmarks(firstLoad) { console.log('fetchBookmarks', firstLoad); if (firstLoad) { bookmarksIterator.current = masto.v1.bookmarks.list({ limit: LIMIT }); } const allBookmarks = await bookmarksIterator.current.next(); if (allBookmarks.value?.length) { const bookmarksValue = allBookmarks.value.map((status) => { saveStatus(status, { skipThreading: true, override: false, }); return status; }); if (firstLoad) { setBookmarks(bookmarksValue); } else { setBookmarks([...bookmarks, ...bookmarksValue]); } } return allBookmarks; } const loadBookmarks = (firstLoad) => { setUIState('loading'); (async () => { try { console.log('loadBookmarks', firstLoad); const { done } = await fetchBookmarks(firstLoad); console.log('loadBookmarks', firstLoad); setShowMore(!done); setUIState('default'); } catch (e) { console.error(e); setUIState('error'); } })(); }; useEffect(() => { loadBookmarks(true); }, []); const scrollableRef = useRef(null); return (
{ if (e.target === e.currentTarget) { scrollableRef.current?.scrollTo({ top: 0, behavior: 'smooth', }); } }} >

Bookmarks

{' '}
{!!bookmarks.length ? ( <> {showMore && ( )} ) : ( uiState !== 'loading' && (

No bookmarks yet. Go bookmark something!

) )} {uiState === 'loading' ? (
) : uiState === 'error' ? (

Unable to load bookmarks.

) : ( bookmarks.length && !showMore &&

The end.

)}
); } export default Bookmarks;