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-10-21 00:41:28 +02:00
|
|
|
import { MAIN_CLASS } from 'component/page/view';
|
2019-06-17 22:32:38 +02:00
|
|
|
import type { Node } from 'react';
|
2019-07-23 10:05:51 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2019-06-11 20:10:58 +02:00
|
|
|
import classnames from 'classnames';
|
2019-06-28 09:27:55 +02:00
|
|
|
import ClaimPreview from 'component/claimPreview';
|
2019-06-11 20:10:58 +02:00
|
|
|
import Spinner from 'component/spinner';
|
|
|
|
import { FormField } from 'component/common/form';
|
2019-09-27 20:56:15 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2019-06-11 20:10:58 +02:00
|
|
|
|
|
|
|
const SORT_NEW = 'new';
|
|
|
|
const SORT_OLD = 'old';
|
2019-10-21 00:41:28 +02:00
|
|
|
const PADDING_ALLOWANCE = 100;
|
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,
|
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-07-01 06:35:36 +02:00
|
|
|
page?: number,
|
2019-07-02 04:54:11 +02:00
|
|
|
pageSize?: number,
|
2019-07-23 10:05:51 +02:00
|
|
|
id?: string,
|
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,
|
2019-08-02 02:56:25 +02:00
|
|
|
showHiddenByUser: boolean,
|
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,
|
|
|
|
loading,
|
|
|
|
persistedStorageKey,
|
|
|
|
empty,
|
2019-06-28 09:33:07 +02:00
|
|
|
defaultSort,
|
2019-06-27 08:18:45 +02:00
|
|
|
type,
|
|
|
|
header,
|
|
|
|
onScrollBottom,
|
2019-07-02 04:54:11 +02:00
|
|
|
pageSize,
|
2019-07-23 10:05:51 +02:00
|
|
|
page,
|
|
|
|
id,
|
2019-08-02 02:56:25 +02:00
|
|
|
showHiddenByUser,
|
2019-06-27 08:18:45 +02:00
|
|
|
} = props;
|
2019-07-23 10:05:51 +02:00
|
|
|
const [scrollBottomCbMap, setScrollBottomCbMap] = useState({});
|
2019-06-11 20:38:08 +02:00
|
|
|
const [currentSort, setCurrentSort] = usePersistedState(persistedStorageKey, SORT_NEW);
|
2019-07-17 22:49:06 +02:00
|
|
|
const urisLength = (uris && uris.length) || 0;
|
|
|
|
const sortedUris = (urisLength > 0 && (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);
|
2017-04-30 18:01:43 +02:00
|
|
|
}
|
2017-04-23 18:10:45 +02:00
|
|
|
|
2019-07-23 10:05:51 +02:00
|
|
|
useEffect(() => {
|
|
|
|
setScrollBottomCbMap({});
|
2019-09-26 18:07:11 +02:00
|
|
|
}, [id, setScrollBottomCbMap]);
|
2019-07-23 10:05:51 +02:00
|
|
|
|
2019-06-27 08:18:45 +02:00
|
|
|
useEffect(() => {
|
2019-06-28 09:33:07 +02:00
|
|
|
function handleScroll(e) {
|
2019-07-23 10:05:51 +02:00
|
|
|
if (page && pageSize && onScrollBottom && !scrollBottomCbMap[page]) {
|
2019-06-28 09:33:07 +02:00
|
|
|
const x = document.querySelector(`.${MAIN_WRAPPER_CLASS}`);
|
2019-10-21 00:41:28 +02:00
|
|
|
const mc = document.querySelector(`.${MAIN_CLASS}`);
|
2019-06-28 09:33:07 +02:00
|
|
|
|
2019-10-21 00:41:28 +02:00
|
|
|
if (
|
|
|
|
x &&
|
|
|
|
mc &&
|
|
|
|
(window.scrollY + window.innerHeight >= x.offsetHeight ||
|
|
|
|
x.offsetHeight - mc.offsetHeight > PADDING_ALLOWANCE)
|
|
|
|
) {
|
2019-07-02 04:54:11 +02:00
|
|
|
if (!loading && urisLength >= pageSize) {
|
2019-06-28 09:33:07 +02:00
|
|
|
onScrollBottom();
|
2019-07-23 10:05:51 +02:00
|
|
|
|
|
|
|
// Save that we've fetched this page to avoid weird stuff happening with fast scrolling
|
|
|
|
setScrollBottomCbMap({ ...scrollBottomCbMap, [page]: true });
|
2019-06-28 09:33:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 08:18:45 +02:00
|
|
|
if (onScrollBottom) {
|
|
|
|
window.addEventListener('scroll', handleScroll);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('scroll', handleScroll);
|
|
|
|
};
|
|
|
|
}
|
2019-07-23 10:05:51 +02:00
|
|
|
}, [loading, onScrollBottom, urisLength, pageSize, page, setScrollBottomCbMap]);
|
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 && (
|
2019-06-28 09:27:55 +02:00
|
|
|
<div className={classnames('claim-list__header', { 'claim-list__header--small': type === 'small' })}>
|
2019-06-28 09:33:07 +02:00
|
|
|
{header}
|
2019-10-10 19:39:44 +02:00
|
|
|
{loading && <Spinner 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>
|
|
|
|
)}
|
2019-08-19 06:49:54 +02:00
|
|
|
|
2019-07-17 22:49:06 +02:00
|
|
|
{urisLength > 0 && (
|
2019-07-21 23:31:22 +02:00
|
|
|
<ul className="ul--no-style">
|
2019-06-11 20:10:58 +02:00
|
|
|
{sortedUris.map((uri, index) => (
|
2019-08-19 06:49:54 +02:00
|
|
|
<ClaimPreview key={uri} uri={uri} type={type} showUserBlocked={showHiddenByUser} />
|
2019-06-11 20:10:58 +02:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
)}
|
2019-09-26 18:07:11 +02:00
|
|
|
|
2019-07-21 23:31:22 +02:00
|
|
|
{urisLength === 0 && !loading && <p className="main--empty empty">{empty || __('No results')}</p>}
|
2019-06-11 20:10:58 +02:00
|
|
|
</section>
|
|
|
|
);
|
2017-04-30 18:01:43 +02:00
|
|
|
}
|