// @flow import { MAIN_CLASS } from 'constants/classnames'; 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 useLastVisibleItem from 'effects/use-last-visible-item'; import debounce from 'util/debounce'; import ClaimPreviewTile from 'component/claimPreviewTile'; const Draggable = React.lazy(() => // $FlowFixMe import('react-beautiful-dnd' /* webpackChunkName: "dnd" */).then((module) => ({ default: module.Draggable })) ); const DEBOUNCE_SCROLL_HANDLER_MS = 150; const SORT_NEW = 'new'; const SORT_OLD = 'old'; type Props = { uris: Array, prefixUris?: Array, header: Node | boolean, headerAltControls: Node, loading: boolean, useLoadingSpinner?: boolean, // use built-in spinner when 'loading' is true. Else, roll your own at client-side. type: string, activeUri?: string, empty?: string, defaultSort?: boolean, onScrollBottom?: (any) => void, page?: number, pageSize?: number, // 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, renderActions?: (Claim) => ?Node, renderProperties?: (Claim) => ?Node, includeSupportAction?: boolean, injectedItem?: { node: Node, index?: number, replace?: boolean }, timedOutMessage?: Node, tileLayout?: boolean, searchInLanguage: boolean, hideMenu?: boolean, claimSearchByQuery: { [string]: Array }, claimsByUri: { [string]: any }, collectionId?: string, fypId?: string, showNoSourceClaims?: boolean, onClick?: (e: any, claim?: ?Claim, index?: number) => void, maxClaimRender?: number, loadedCallback?: (number) => void, swipeLayout: boolean, showEdit?: boolean, droppableProvided?: any, unavailableUris?: Array, showMemberBadge?: boolean, }; export default function ClaimList(props: Props) { const { activeUri, uris, prefixUris, headerAltControls, loading, useLoadingSpinner, persistedStorageKey, empty, defaultSort, type, header, onScrollBottom, pageSize, page, showHiddenByUser, showUnresolvedClaims, includeSupportAction, injectedItem, timedOutMessage, tileLayout = false, renderActions, renderProperties, searchInLanguage, hideMenu, collectionId, fypId, showNoSourceClaims, onClick, maxClaimRender, loadedCallback, swipeLayout = false, showEdit, droppableProvided, unavailableUris, showMemberBadge, } = props; const [currentSort, setCurrentSort] = usePersistedState(persistedStorageKey, SORT_NEW); // Resolve the index for injectedItem, if provided; else injectedIndex will be 'undefined'. const listRef = React.useRef(); const injectedIndex = useLastVisibleItem(injectedItem, listRef); // Exclude prefix uris in these results variables. We don't want to show // anything if the search failed or timed out. const timedOut = uris === null; const urisLength = (uris && uris.length) || 0; let tileUris = (prefixUris || []).concat(uris || []); if (prefixUris && prefixUris.length) tileUris.splice(prefixUris.length * -1, prefixUris.length); const totalLength = tileUris.length; if (maxClaimRender) tileUris = tileUris.slice(0, maxClaimRender); const sortedUris = (urisLength > 0 && (currentSort === SORT_NEW ? tileUris : tileUris.slice().reverse())) || []; React.useEffect(() => { if (typeof loadedCallback === 'function') loadedCallback(totalLength); }, [totalLength]); // eslint-disable-line react-hooks/exhaustive-deps const noResultMsg = searchInLanguage ? __('No results. Contents may be hidden by the Language filter.') : __('No results'); function handleSortChange() { setCurrentSort(currentSort === SORT_NEW ? SORT_OLD : SORT_NEW); } const handleClaimClicked = React.useCallback( (e, claim, index) => { if (onClick) { onClick(e, claim, index); } }, [onClick] ); const customShouldHide = React.useCallback((claim: StreamClaim) => { // Hack to hide spee.ch thumbnail publishes // If it meets these requirements, it was probably uploaded here: // https://github.com/lbryio/lbry-redux/blob/master/src/redux/actions/publish.js#L74-L79 return claim.name.length === 24 && !claim.name.includes(' ') && claim.value.author === 'Spee.ch'; }, []); // @if process.env.NODE_ENV!='production' if (injectedItem && injectedItem.replace) { throw new Error('claimList: "injectedItem.replace" is not implemented yet'); } // @endif useEffect(() => { const handleScroll = debounce((e) => { if (page && pageSize && onScrollBottom) { const mainEl = document.querySelector(`.${MAIN_CLASS}`); if (mainEl && !loading && urisLength >= pageSize) { const ROUGH_TILE_HEIGHT_PX = 200; const mainBoundingRect = mainEl.getBoundingClientRect(); const contentWrapperAtBottomOfPage = mainBoundingRect.bottom - ROUGH_TILE_HEIGHT_PX <= 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]); const getClaimPreview = (uri: string, index: number, draggableProvided?: any) => ( ); const getInjectedItem = (index) => { if (injectedItem && injectedItem.node && injectedIndex === index) { return injectedItem.node; } return null; }; return tileLayout && !header ? ( <>
{urisLength > 0 && tileUris.map((uri, index) => ( {getInjectedItem(index)} ))} {!timedOut && urisLength === 0 && !loading &&
{empty || noResultMsg}
} {timedOut && timedOutMessage &&
{timedOutMessage}
}
{loading && useLoadingSpinner && (
)} ) : (
{header !== false && ( {header && (
{header} {loading && } {(headerAltControls || defaultSort) && (
{headerAltControls} {defaultSort && ( )}
)}
)}
)} {urisLength > 0 && (
    {droppableProvided ? ( <> {sortedUris.map((uri, index) => ( {(draggableProvided, draggableSnapshot) => { // Restrict dragging to vertical axis // https://github.com/atlassian/react-beautiful-dnd/issues/958#issuecomment-980548919 let transform = draggableProvided.draggableProps.style.transform; if (draggableSnapshot.isDragging && transform) { transform = transform.replace(/\(.+,/, '(0,'); } const style = { ...draggableProvided.draggableProps.style, transform, }; return (
  • {/* https://github.com/atlassian/react-beautiful-dnd/issues/1756 */}
    {getClaimPreview(uri, index, draggableProvided)}
  • ); }}
    ))} {droppableProvided.placeholder} ) : ( sortedUris.map((uri, index) => ( {getInjectedItem(index)} {getClaimPreview(uri, index)} )) )}
)} {!timedOut && urisLength === 0 && !loading &&
{empty || noResultMsg}
} {!loading && timedOut && timedOutMessage &&
{timedOutMessage}
} {loading && useLoadingSpinner && (
)}
); }