lbry-desktop/src/ui/component/claimList/view.jsx

114 lines
3.3 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2019-06-27 08:18:45 +02:00
import { MAIN_WRAPPER_CLASS } from 'component/app/view';
2019-06-17 22:32:38 +02:00
import type { Node } from 'react';
2019-06-27 08:18:45 +02:00
import React, { useEffect } from 'react';
2019-06-11 20:10:58 +02:00
import classnames from 'classnames';
import ClaimPreview from 'component/claimPreview';
2019-06-11 20:10:58 +02:00
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';
2018-03-26 23:32:43 +02:00
type Props = {
2019-06-11 20:10:58 +02:00
uris: Array<string>,
2019-06-19 07:05:43 +02:00
header: Node | boolean,
2019-06-17 22:32:38 +02:00
headerAltControls: Node,
injectedItem?: Node,
2019-06-11 20:10:58 +02:00
loading: boolean,
2019-06-19 07:05:43 +02:00
type: string,
2019-06-11 20:10:58 +02:00
empty?: string,
2019-06-28 09:33:07 +02:00
defaultSort?: boolean,
2019-06-27 08:18:45 +02:00
onScrollBottom?: any => void,
2019-06-11 20:10:58 +02:00
// If using the default header, this is a unique ID needed to persist the state of the filter setting
persistedStorageKey?: string,
2018-03-26 23:32:43 +02:00
};
2019-06-19 07:05:43 +02:00
export default function ClaimList(props: Props) {
2019-06-27 08:18:45 +02:00
const {
uris,
headerAltControls,
injectedItem,
loading,
persistedStorageKey,
empty,
2019-06-28 09:33:07 +02:00
defaultSort,
2019-06-27 08:18:45 +02:00
type,
header,
onScrollBottom,
} = props;
2019-06-11 20:38:08 +02:00
const [currentSort, setCurrentSort] = usePersistedState(persistedStorageKey, SORT_NEW);
2019-06-11 20:10:58 +02:00
const hasUris = uris && !!uris.length;
const sortedUris = (hasUris && (currentSort === SORT_NEW ? uris : uris.slice().reverse())) || [];
2018-03-26 23:32:43 +02:00
2019-06-11 20:10:58 +02:00
function handleSortChange() {
setCurrentSort(currentSort === SORT_NEW ? SORT_OLD : SORT_NEW);
}
2019-06-28 09:33:07 +02:00
const urisLength = uris && uris.length;
2019-06-27 08:18:45 +02:00
useEffect(() => {
2019-06-28 09:33:07 +02:00
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();
}
}
}
}
2019-06-27 08:18:45 +02:00
if (onScrollBottom) {
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
}
2019-06-28 09:33:07 +02:00
}, [loading, onScrollBottom, urisLength]);
2019-06-27 08:18:45 +02:00
2019-06-11 20:10:58 +02:00
return (
2019-06-28 09:33:07 +02:00
<section
className={classnames('claim-list', {
'claim-list--small': type === 'small',
})}
>
2019-06-19 07:05:43 +02:00
{header !== false && (
<div className={classnames('claim-list__header', { 'claim-list__header--small': type === 'small' })}>
2019-06-28 09:33:07 +02:00
{header}
2019-06-11 20:10:58 +02:00
{loading && <Spinner light type="small" />}
2019-06-28 09:33:07 +02:00
<div className="claim-list__alt-controls">
{headerAltControls}
{defaultSort && (
<FormField
className="claim-list__dropdown"
type="select"
name="file_sort"
value={currentSort}
onChange={handleSortChange}
>
<option value={SORT_NEW}>{__('Newest First')}</option>
<option value={SORT_OLD}>{__('Oldest First')}</option>
</FormField>
)}
</div>
2019-06-11 20:10:58 +02:00
</div>
)}
{hasUris && (
<ul>
{sortedUris.map((uri, index) => (
<React.Fragment key={uri}>
<ClaimPreview uri={uri} type={type} />
2019-06-27 08:18:45 +02:00
{index === 4 && injectedItem && <li className="claim-preview--injected">{injectedItem}</li>}
2019-06-11 20:10:58 +02:00
</React.Fragment>
))}
</ul>
)}
2019-06-11 20:22:10 +02:00
{!hasUris && !loading && <h2 className="main--empty empty">{empty || __('No results')}</h2>}
2019-06-11 20:10:58 +02:00
</section>
);
}