import './lists.css'; import { Menu, MenuItem } from '@szhsin/react-menu'; import { useEffect, useRef, useState } from 'preact/hooks'; import { InView } from 'react-intersection-observer'; import { useNavigate, useParams } from 'react-router-dom'; import { useSnapshot } from 'valtio'; import AccountBlock from '../components/account-block'; import Icon from '../components/icon'; import Link from '../components/link'; import ListAddEdit from '../components/list-add-edit'; import Menu2 from '../components/menu2'; import MenuConfirm from '../components/menu-confirm'; import Modal from '../components/modal'; import Timeline from '../components/timeline'; import { api } from '../utils/api'; import { filteredItems } from '../utils/filters'; import states, { saveStatus } from '../utils/states'; import useTitle from '../utils/useTitle'; const LIMIT = 20; function List(props) { const snapStates = useSnapshot(states); const { masto, instance } = api(); const id = props?.id || useParams()?.id; // const navigate = useNavigate(); const latestItem = useRef(); // const [reloadCount, reload] = useReducer((c) => c + 1, 0); const listIterator = useRef(); async function fetchList(firstLoad) { if (firstLoad || !listIterator.current) { listIterator.current = masto.v1.timelines.list.$select(id).list({ limit: LIMIT, }); } const results = await listIterator.current.next(); let { value } = results; if (value?.length) { if (firstLoad) { latestItem.current = value[0].id; } value = filteredItems(value, 'home'); value.forEach((item) => { saveStatus(item, instance); }); } return { ...results, value, }; } async function checkForUpdates() { try { const results = await masto.v1.timelines.list.$select(id).list({ limit: 1, since_id: latestItem.current, }); let { value } = results; value = filteredItems(value, 'home'); if (value?.length) { return true; } return false; } catch (e) { return false; } } const [list, setList] = useState({ title: 'List' }); // const [title, setTitle] = useState(`List`); useTitle(list.title, `/l/:id`); useEffect(() => { (async () => { try { const list = await masto.v1.lists.$select(id).fetch(); setList(list); // setTitle(list.title); } catch (e) { console.error(e); } })(); }, [id]); const [showListAddEditModal, setShowListAddEditModal] = useState(false); const [showManageMembersModal, setShowManageMembersModal] = useState(false); return ( <> } headerEnd={ } > setShowListAddEditModal({ list, }) } > Edit setShowManageMembersModal(true)}> Manage members } /> {showListAddEditModal && ( { if (e.target === e.currentTarget) { setShowListAddEditModal(false); } }} > { if (result.state === 'success' && result.list) { setList(result.list); // reload(); } else if (result.state === 'deleted') { // navigate('/l'); location.hash = '/l'; } setShowListAddEditModal(false); }} /> )} {showManageMembersModal && ( { if (e.target === e.currentTarget) { setShowManageMembersModal(false); } }} > setShowManageMembersModal(false)} /> )} ); } const MEMBERS_LIMIT = 40; function ListManageMembers({ listID, onClose }) { // Show list of members with [Remove] button // API only returns 40 members at a time, so this need to be paginated with infinite scroll // Show [Add] button after removing a member const { masto, instance } = api(); const [members, setMembers] = useState([]); const [uiState, setUIState] = useState('default'); const [showMore, setShowMore] = useState(false); const membersIterator = useRef(); async function fetchMembers(firstLoad) { setShowMore(false); setUIState('loading'); (async () => { try { if (firstLoad || !membersIterator.current) { membersIterator.current = masto.v1.lists .$select(listID) .accounts.list({ limit: MEMBERS_LIMIT, }); } const results = await membersIterator.current.next(); let { done, value } = results; if (value?.length) { if (firstLoad) { setMembers(value); } else { setMembers(members.concat(value)); } setShowMore(!done); } else { setShowMore(false); } setUIState('default'); } catch (e) { setUIState('error'); } })(); } useEffect(() => { fetchMembers(true); }, []); return (
{!!onClose && ( )}

Manage members

    {members.map((member) => (
  • ))} {showMore && uiState === 'default' && ( inView && fetchMembers()}> )}
); } function RemoveAddButton({ account, listID }) { const { masto } = api(); const [uiState, setUIState] = useState('default'); const [removed, setRemoved] = useState(false); return ( Remove @{account.username} from list?} align="end" menuItemClassName="danger" onClick={() => { if (removed) { setUIState('loading'); (async () => { try { await masto.v1.lists.$select(listID).accounts.create({ accountIds: [account.id], }); setUIState('default'); setRemoved(false); } catch (e) { setUIState('error'); } })(); } else { // const yes = confirm(`Remove ${account.username} from this list?`); // if (!yes) return; setUIState('loading'); (async () => { try { await masto.v1.lists.$select(listID).accounts.remove({ accountIds: [account.id], }); setUIState('default'); setRemoved(true); } catch (e) { setUIState('error'); } })(); } }} > ); } export default List;