// @flow import { MAIN_WRAPPER_CLASS } from 'component/app/view'; import type { Node } from 'react'; import React, { useEffect } from 'react'; import classnames from 'classnames'; import ClaimPreview from 'component/claimPreview'; import Spinner from 'component/spinner'; import { FormField } from 'component/common/form'; import usePersistedState from 'util/use-persisted-state'; const SORT_NEW = 'new'; const SORT_OLD = 'old'; type Props = { uris: Array, header: Node | boolean, headerAltControls: Node, injectedItem?: Node, loading: boolean, type: string, empty?: string, defaultSort?: boolean, onScrollBottom?: any => void, // If using the default header, this is a unique ID needed to persist the state of the filter setting persistedStorageKey?: string, }; export default function ClaimList(props: Props) { const { uris, headerAltControls, injectedItem, loading, persistedStorageKey, empty, defaultSort, type, header, onScrollBottom, } = props; const [currentSort, setCurrentSort] = usePersistedState(persistedStorageKey, SORT_NEW); const hasUris = uris && !!uris.length; const sortedUris = (hasUris && (currentSort === SORT_NEW ? uris : uris.slice().reverse())) || []; function handleSortChange() { setCurrentSort(currentSort === SORT_NEW ? SORT_OLD : SORT_NEW); } const urisLength = uris && uris.length; useEffect(() => { function handleScroll(e) { if (onScrollBottom) { const x = document.querySelector(`.${MAIN_WRAPPER_CLASS}`); if (x && window.scrollY + window.innerHeight >= x.offsetHeight) { // fix this if (!loading && urisLength > 19) { onScrollBottom(); } } } } if (onScrollBottom) { window.addEventListener('scroll', handleScroll); return () => { window.removeEventListener('scroll', handleScroll); }; } }, [loading, onScrollBottom, urisLength]); return (
{header !== false && (
{header} {loading && }
{headerAltControls} {defaultSort && ( )}
)} {hasUris && ( )} {!hasUris && !loading &&

{empty || __('No results')}

}
); }