// @flow import { MAIN_CLASS } from 'component/page/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 'effects/use-persisted-state'; import debounce from 'util/debounce'; import ClaimPreviewTile from 'component/claimPreviewTile'; const DEBOUNCE_SCROLL_HANDLER_MS = 150; const SORT_NEW = 'new'; const SORT_OLD = 'old'; type Props = { uris: Array, header: Node | boolean, headerAltControls: Node, loading: boolean, type: string, empty?: string, defaultSort?: boolean, onScrollBottom?: any => void, page?: number, pageSize?: number, id?: string, // If using the default header, this is a unique ID needed to persist the state of the filter setting persistedStorageKey?: string, showHiddenByUser: boolean, showUnresolvedClaims?: boolean, renderProperties: ?(Claim) => Node, includeSupportAction?: boolean, hideBlock: boolean, injectedItem: ?Node, timedOutMessage?: Node, tileLayout?: boolean, }; export default function ClaimList(props: Props) { const { uris, headerAltControls, loading, persistedStorageKey, empty, defaultSort, type, header, onScrollBottom, pageSize, page, showHiddenByUser, showUnresolvedClaims, renderProperties, includeSupportAction, hideBlock, injectedItem, timedOutMessage, tileLayout = false, } = props; const [currentSort, setCurrentSort] = usePersistedState(persistedStorageKey, SORT_NEW); const timedOut = uris === null; const urisLength = (uris && uris.length) || 0; const sortedUris = (urisLength > 0 && (currentSort === SORT_NEW ? uris : uris.slice().reverse())) || []; function handleSortChange() { setCurrentSort(currentSort === SORT_NEW ? SORT_OLD : SORT_NEW); } useEffect(() => { const handleScroll = debounce(e => { if (page && pageSize && onScrollBottom) { const mainEl = document.querySelector(`.${MAIN_CLASS}`); if (mainEl && !loading && urisLength >= pageSize) { const contentWrapperAtBottomOfPage = mainEl.getBoundingClientRect().bottom - 0.5 <= window.innerHeight; if (contentWrapperAtBottomOfPage) { onScrollBottom(); } } } }, DEBOUNCE_SCROLL_HANDLER_MS); if (onScrollBottom) { window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); } }, [loading, onScrollBottom, urisLength, pageSize, page]); return tileLayout && !header ? (
{urisLength > 0 && uris.map(uri => )} {!timedOut && urisLength === 0 && !loading && (
{empty || __('No results')}
)} {timedOut && timedOutMessage &&
{timedOutMessage}
}
) : (
{header !== false && ( {header && (
{header} {loading && }
{headerAltControls} {defaultSort && ( )}
)}
)} {urisLength > 0 && ( )} {!timedOut && urisLength === 0 && !loading && (
{empty || __('No results')}
)} {!loading && timedOut && timedOutMessage &&
{timedOutMessage}
}
); }