2019-06-11 20:10:58 +02:00
|
|
|
// @flow
|
2019-06-17 22:32:38 +02:00
|
|
|
import type { Node } from 'react';
|
2019-06-27 08:18:45 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2019-06-17 22:32:38 +02:00
|
|
|
import moment from 'moment';
|
2019-06-27 08:18:45 +02:00
|
|
|
import usePersistedState from 'util/use-persisted-state';
|
2019-06-11 20:10:58 +02:00
|
|
|
import { FormField } from 'component/common/form';
|
2019-06-19 07:05:43 +02:00
|
|
|
import ClaimList from 'component/claimList';
|
2019-06-17 22:32:38 +02:00
|
|
|
import Tag from 'component/tag';
|
2019-06-27 08:18:45 +02:00
|
|
|
import ClaimPreview from 'component/claimPreview';
|
2019-06-11 20:10:58 +02:00
|
|
|
|
2019-06-27 08:18:45 +02:00
|
|
|
const PAGE_SIZE = 20;
|
2019-06-11 20:10:58 +02:00
|
|
|
const TIME_DAY = 'day';
|
|
|
|
const TIME_WEEK = 'week';
|
|
|
|
const TIME_MONTH = 'month';
|
|
|
|
const TIME_YEAR = 'year';
|
|
|
|
const TIME_ALL = 'all';
|
2019-06-11 20:56:23 +02:00
|
|
|
const SEARCH_SORT_YOU = 'you';
|
|
|
|
const SEARCH_SORT_ALL = 'everyone';
|
2019-06-11 20:10:58 +02:00
|
|
|
const TYPE_TRENDING = 'trending';
|
|
|
|
const TYPE_TOP = 'top';
|
|
|
|
const TYPE_NEW = 'new';
|
2019-06-11 20:56:23 +02:00
|
|
|
const SEARCH_FILTER_TYPES = [SEARCH_SORT_YOU, SEARCH_SORT_ALL];
|
|
|
|
const SEARCH_TYPES = ['trending', 'top', 'new'];
|
|
|
|
const SEARCH_TIMES = [TIME_DAY, TIME_WEEK, TIME_MONTH, TIME_YEAR, TIME_ALL];
|
2019-06-11 20:10:58 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
uris: Array<string>,
|
|
|
|
doClaimSearch: (number, {}) => void,
|
|
|
|
injectedItem: any,
|
|
|
|
tags: Array<string>,
|
|
|
|
loading: boolean,
|
|
|
|
personal: boolean,
|
2019-06-17 22:32:38 +02:00
|
|
|
doToggleTagFollow: string => void,
|
|
|
|
meta?: Node,
|
2019-06-11 20:10:58 +02:00
|
|
|
};
|
|
|
|
|
2019-06-19 07:05:43 +02:00
|
|
|
function ClaimListDiscover(props: Props) {
|
2019-06-17 22:32:38 +02:00
|
|
|
const { doClaimSearch, uris, tags, loading, personal, injectedItem, meta } = props;
|
2019-06-11 20:56:23 +02:00
|
|
|
const [personalSort, setPersonalSort] = usePersistedState('file-list-trending:personalSort', SEARCH_SORT_YOU);
|
2019-06-11 20:10:58 +02:00
|
|
|
const [typeSort, setTypeSort] = usePersistedState('file-list-trending:typeSort', TYPE_TRENDING);
|
|
|
|
const [timeSort, setTimeSort] = usePersistedState('file-list-trending:timeSort', TIME_WEEK);
|
2019-06-27 08:18:45 +02:00
|
|
|
const [page, setPage] = useState(1);
|
2019-06-11 20:10:58 +02:00
|
|
|
|
|
|
|
const toCapitalCase = string => string.charAt(0).toUpperCase() + string.slice(1);
|
|
|
|
const tagsString = tags.join(',');
|
|
|
|
useEffect(() => {
|
2019-06-27 08:18:45 +02:00
|
|
|
const options: {
|
|
|
|
page_size: number,
|
|
|
|
any_tags?: Array<string>,
|
|
|
|
order_by?: Array<string>,
|
|
|
|
release_time?: string,
|
|
|
|
} = { page_size: PAGE_SIZE, page };
|
2019-06-11 20:10:58 +02:00
|
|
|
const newTags = tagsString.split(',');
|
|
|
|
|
2019-06-17 22:32:38 +02:00
|
|
|
if ((newTags && !personal) || (newTags && personal && personalSort === SEARCH_SORT_YOU)) {
|
2019-06-11 20:10:58 +02:00
|
|
|
options.any_tags = newTags;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeSort === TYPE_TRENDING) {
|
|
|
|
options.order_by = ['trending_global', 'trending_mixed'];
|
|
|
|
} else if (typeSort === TYPE_NEW) {
|
|
|
|
options.order_by = ['release_time'];
|
|
|
|
} else if (typeSort === TYPE_TOP) {
|
|
|
|
options.order_by = ['effective_amount'];
|
|
|
|
if (timeSort !== TIME_ALL) {
|
|
|
|
const time = Math.floor(
|
|
|
|
moment()
|
|
|
|
.subtract(1, timeSort)
|
|
|
|
.unix()
|
|
|
|
);
|
|
|
|
options.release_time = `>${time}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doClaimSearch(20, options);
|
2019-06-27 08:18:45 +02:00
|
|
|
}, [personal, personalSort, typeSort, timeSort, doClaimSearch, page, tagsString]);
|
2019-06-11 20:10:58 +02:00
|
|
|
|
|
|
|
const header = (
|
2019-06-17 22:32:38 +02:00
|
|
|
<h1 className="card__title--flex">
|
|
|
|
<FormField
|
2019-06-28 09:27:55 +02:00
|
|
|
className="claim-list__dropdown"
|
2019-06-17 22:32:38 +02:00
|
|
|
type="select"
|
|
|
|
name="trending_sort"
|
|
|
|
value={typeSort}
|
|
|
|
onChange={e => setTypeSort(e.target.value)}
|
|
|
|
>
|
|
|
|
{SEARCH_TYPES.map(type => (
|
|
|
|
<option key={type} value={type}>
|
|
|
|
{toCapitalCase(type)}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</FormField>
|
|
|
|
<span>{__('For')}</span>
|
2019-06-11 20:10:58 +02:00
|
|
|
{!personal && tags && tags.length ? (
|
2019-06-17 22:32:38 +02:00
|
|
|
tags.map(tag => <Tag key={tag} name={tag} disabled />)
|
2019-06-11 20:10:58 +02:00
|
|
|
) : (
|
|
|
|
<FormField
|
|
|
|
type="select"
|
|
|
|
name="trending_overview"
|
2019-06-28 09:27:55 +02:00
|
|
|
className="claim-list__dropdown"
|
2019-06-11 20:10:58 +02:00
|
|
|
value={personalSort}
|
2019-06-27 08:18:45 +02:00
|
|
|
onChange={e => {
|
|
|
|
setPage(1);
|
|
|
|
setPersonalSort(e.target.value);
|
|
|
|
}}
|
2019-06-11 20:10:58 +02:00
|
|
|
>
|
2019-06-11 20:56:23 +02:00
|
|
|
{SEARCH_FILTER_TYPES.map(type => (
|
2019-06-11 20:10:58 +02:00
|
|
|
<option key={type} value={type}>
|
|
|
|
{toCapitalCase(type)}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</FormField>
|
|
|
|
)}
|
2019-06-17 22:32:38 +02:00
|
|
|
</h1>
|
2019-06-11 20:10:58 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const headerAltControls = (
|
|
|
|
<React.Fragment>
|
2019-06-28 09:33:07 +02:00
|
|
|
{meta}
|
2019-06-11 20:10:58 +02:00
|
|
|
{typeSort === 'top' && (
|
|
|
|
<FormField
|
2019-06-28 09:27:55 +02:00
|
|
|
className="claim-list__dropdown"
|
2019-06-11 20:10:58 +02:00
|
|
|
type="select"
|
|
|
|
name="trending_time"
|
|
|
|
value={timeSort}
|
|
|
|
onChange={e => setTimeSort(e.target.value)}
|
|
|
|
>
|
2019-06-11 20:56:23 +02:00
|
|
|
{SEARCH_TIMES.map(time => (
|
2019-06-11 20:10:58 +02:00
|
|
|
<option key={time} value={time}>
|
|
|
|
{toCapitalCase(time)}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</FormField>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2019-06-17 22:32:38 +02:00
|
|
|
<div className="card">
|
2019-06-19 07:05:43 +02:00
|
|
|
<ClaimList
|
2019-06-17 22:32:38 +02:00
|
|
|
loading={loading}
|
|
|
|
uris={uris}
|
|
|
|
injectedItem={personalSort === SEARCH_SORT_YOU && injectedItem}
|
|
|
|
header={header}
|
|
|
|
headerAltControls={headerAltControls}
|
2019-06-27 08:18:45 +02:00
|
|
|
onScrollBottom={() => setPage(page + 1)}
|
2019-06-17 22:32:38 +02:00
|
|
|
/>
|
2019-06-27 08:18:45 +02:00
|
|
|
|
|
|
|
{loading && page > 1 && new Array(PAGE_SIZE).fill(1).map((x, i) => <ClaimPreview key={i} placeholder />)}
|
2019-06-17 22:32:38 +02:00
|
|
|
</div>
|
2019-06-11 20:10:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-19 07:05:43 +02:00
|
|
|
export default ClaimListDiscover;
|