2018-03-26 14:32:43 -07:00
|
|
|
// @flow
|
2020-08-03 16:14:43 -04:00
|
|
|
import { SIMPLE_SITE, SHOW_ADS } from 'config';
|
2020-10-28 15:18:58 -04:00
|
|
|
import React, { useEffect } from 'react';
|
2020-10-29 23:56:01 -04:00
|
|
|
import { Lbry, parseURI, isNameValid } from 'lbry-redux';
|
2019-06-19 01:05:43 -04:00
|
|
|
import ClaimList from 'component/claimList';
|
2018-03-26 14:32:43 -07:00
|
|
|
import Page from 'component/page';
|
2019-02-18 12:24:56 -05:00
|
|
|
import SearchOptions from 'component/searchOptions';
|
2020-05-07 14:44:11 -04:00
|
|
|
import Ads from 'web/component/ads';
|
2020-10-28 15:18:58 -04:00
|
|
|
import SearchTopClaim from 'component/searchTopClaim';
|
2020-08-03 13:57:14 -04:00
|
|
|
import { formatLbryUrlForWeb } from 'util/url';
|
|
|
|
import { useHistory } from 'react-router';
|
2021-03-26 16:33:30 +08:00
|
|
|
import { SEARCH_PAGE_SIZE } from 'constants/search';
|
2017-05-05 15:01:16 +07:00
|
|
|
|
2019-06-11 14:10:58 -04:00
|
|
|
type Props = {
|
2021-07-13 14:28:09 +08:00
|
|
|
urlQuery: string,
|
|
|
|
searchOptions: SearchOptions,
|
|
|
|
search: (string, SearchOptions) => void,
|
2019-07-20 20:23:29 -03:00
|
|
|
isSearching: boolean,
|
2019-06-11 14:10:58 -04:00
|
|
|
uris: Array<string>,
|
2020-03-26 17:47:07 -04:00
|
|
|
isAuthenticated: boolean,
|
2021-03-26 16:33:30 +08:00
|
|
|
hasReachedMaxResultsLength: boolean,
|
2019-06-11 14:10:58 -04:00
|
|
|
};
|
2018-03-26 14:32:43 -07:00
|
|
|
|
2019-03-28 12:53:13 -04:00
|
|
|
export default function SearchPage(props: Props) {
|
2021-07-13 14:28:09 +08:00
|
|
|
const { urlQuery, searchOptions, search, uris, isSearching, isAuthenticated, hasReachedMaxResultsLength } = props;
|
2020-08-03 13:57:14 -04:00
|
|
|
const { push } = useHistory();
|
2021-03-26 16:33:30 +08:00
|
|
|
const [from, setFrom] = React.useState(0);
|
2021-01-08 19:02:40 -03:00
|
|
|
|
2021-03-24 13:55:46 +08:00
|
|
|
const modifiedUrlQuery = urlQuery.trim().replace(/\s+/g, '').replace(/:/g, '#');
|
2020-10-28 15:18:58 -04:00
|
|
|
const uriFromQuery = `lbry://${modifiedUrlQuery}`;
|
|
|
|
|
|
|
|
let streamName;
|
2020-09-11 01:12:05 +08:00
|
|
|
let isValid = true;
|
2020-03-10 10:08:27 -04:00
|
|
|
try {
|
2020-10-28 15:18:58 -04:00
|
|
|
({ streamName } = parseURI(uriFromQuery));
|
2020-09-11 01:12:05 +08:00
|
|
|
if (!isNameValid(streamName)) {
|
|
|
|
isValid = false;
|
|
|
|
}
|
2020-03-10 10:08:27 -04:00
|
|
|
} catch (e) {
|
|
|
|
isValid = false;
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:57:14 -04:00
|
|
|
let claimId;
|
2020-10-28 15:18:58 -04:00
|
|
|
// Navigate directly to a claim if a claim_id is pasted into the search bar
|
2020-08-03 13:57:14 -04:00
|
|
|
if (!/\s/.test(urlQuery) && urlQuery.length === 40) {
|
|
|
|
try {
|
|
|
|
const dummyUrlForClaimId = `x#${urlQuery}`;
|
|
|
|
({ claimId } = parseURI(dummyUrlForClaimId));
|
2021-03-24 13:55:46 +08:00
|
|
|
Lbry.claim_search({ claim_id: claimId }).then((res) => {
|
2020-08-03 13:57:14 -04:00
|
|
|
if (res.items && res.items.length) {
|
|
|
|
const claim = res.items[0];
|
|
|
|
const url = formatLbryUrlForWeb(claim.canonical_url);
|
|
|
|
push(url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
2020-12-03 16:31:38 -05:00
|
|
|
const stringifiedSearchOptions = JSON.stringify(searchOptions);
|
2021-07-13 14:28:09 +08:00
|
|
|
|
2019-03-28 12:53:13 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (urlQuery) {
|
2021-07-13 14:28:09 +08:00
|
|
|
const searchOptions = JSON.parse(stringifiedSearchOptions);
|
|
|
|
search(urlQuery, { ...searchOptions, from: from });
|
2018-08-24 17:25:18 -04:00
|
|
|
}
|
2021-07-13 14:28:09 +08:00
|
|
|
}, [search, urlQuery, stringifiedSearchOptions, from]);
|
2019-03-28 12:53:13 -04:00
|
|
|
|
2021-03-26 16:33:30 +08:00
|
|
|
function loadMore() {
|
|
|
|
if (!isSearching && !hasReachedMaxResultsLength) {
|
|
|
|
setFrom(from + SEARCH_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 18:52:15 +08:00
|
|
|
function resetPage() {
|
|
|
|
setFrom(0);
|
|
|
|
}
|
|
|
|
|
2019-03-28 12:53:13 -04:00
|
|
|
return (
|
2019-05-06 22:35:04 -04:00
|
|
|
<Page>
|
2019-04-04 17:05:23 -04:00
|
|
|
<section className="search">
|
|
|
|
{urlQuery && (
|
2020-10-28 15:18:58 -04:00
|
|
|
<>
|
2020-12-22 10:16:39 -05:00
|
|
|
{isValid && <SearchTopClaim query={modifiedUrlQuery} isSearching={isSearching} />}
|
2020-01-02 17:30:58 -05:00
|
|
|
<ClaimList
|
|
|
|
uris={uris}
|
|
|
|
loading={isSearching}
|
2021-03-26 16:33:30 +08:00
|
|
|
onScrollBottom={loadMore}
|
|
|
|
// 'page' is 1-indexed; It's not the same as 'from', but it just
|
|
|
|
// needs to be unique to indicate when a fetch is needed.
|
|
|
|
page={from + 1}
|
|
|
|
pageSize={SEARCH_PAGE_SIZE}
|
2021-04-07 18:52:15 +08:00
|
|
|
header={
|
|
|
|
<SearchOptions
|
|
|
|
simple={SIMPLE_SITE}
|
2021-07-13 14:28:09 +08:00
|
|
|
additionalOptions={searchOptions}
|
2021-04-07 18:52:15 +08:00
|
|
|
onSearchOptionsChanged={resetPage}
|
|
|
|
/>
|
|
|
|
}
|
2021-01-26 18:45:34 -05:00
|
|
|
injectedItem={
|
|
|
|
SHOW_ADS && IS_WEB ? (SIMPLE_SITE ? false : !isAuthenticated && <Ads small type={'video'} />) : false
|
|
|
|
}
|
2020-01-02 17:30:58 -05:00
|
|
|
/>
|
|
|
|
|
2020-10-28 15:18:58 -04:00
|
|
|
<div className="main--empty help">{__('These search results are provided by LBRY, Inc.')}</div>
|
|
|
|
</>
|
2019-04-04 17:05:23 -04:00
|
|
|
)}
|
|
|
|
</section>
|
2019-03-28 12:53:13 -04:00
|
|
|
</Page>
|
|
|
|
);
|
2017-05-05 15:01:16 +07:00
|
|
|
}
|