lbry-desktop/ui/page/search/view.jsx

106 lines
3.2 KiB
React
Raw Permalink Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import React, { useEffect } from 'react';
import Lbry from 'lbry';
import { parseURI, isNameValid } from 'util/lbryURI';
2019-06-19 07:05:43 +02:00
import ClaimList from 'component/claimList';
2018-03-26 23:32:43 +02:00
import Page from 'component/page';
2019-02-18 18:24:56 +01:00
import SearchOptions from 'component/searchOptions';
import SearchTopClaim from 'component/searchTopClaim';
import { formatLbryUrlForWeb } from 'util/url';
import { useHistory } from 'react-router';
2021-03-26 09:33:30 +01:00
import { SEARCH_PAGE_SIZE } from 'constants/search';
2017-05-05 10:01:16 +02:00
2019-06-11 20:10:58 +02:00
type Props = {
urlQuery: string,
searchOptions: SearchOptions,
search: (string, SearchOptions) => void,
isSearching: boolean,
2019-06-11 20:10:58 +02:00
uris: Array<string>,
2020-03-26 22:47:07 +01:00
isAuthenticated: boolean,
2021-03-26 09:33:30 +01:00
hasReachedMaxResultsLength: boolean,
2019-06-11 20:10:58 +02:00
};
2018-03-26 23:32:43 +02:00
2019-03-28 17:53:13 +01:00
export default function SearchPage(props: Props) {
2021-10-19 01:37:58 +02:00
const { urlQuery, searchOptions, search, uris, isSearching, hasReachedMaxResultsLength } = props;
const { push } = useHistory();
2021-03-26 09:33:30 +01:00
const [from, setFrom] = React.useState(0);
2021-01-08 23:02:40 +01:00
const modifiedUrlQuery = urlQuery.trim().replace(/\s+/g, '').replace(/:/g, '#');
const uriFromQuery = `lbry://${modifiedUrlQuery}`;
let streamName;
2021-10-20 18:47:58 +02:00
let channelName;
let isValid = true;
try {
2021-10-20 18:47:58 +02:00
({ streamName, channelName } = parseURI(uriFromQuery));
if (
(!streamName && !channelName) ||
(streamName && !isNameValid(streamName)) ||
(channelName && !isNameValid(channelName))
) {
isValid = false;
}
} catch (e) {
isValid = false;
}
let claimId;
// Navigate directly to a claim if a claim_id is pasted into the search bar
if (!/\s/.test(urlQuery) && urlQuery.length === 40) {
try {
const dummyUrlForClaimId = `x#${urlQuery}`;
({ claimId } = parseURI(dummyUrlForClaimId));
Lbry.claim_search({ claim_id: claimId }).then((res) => {
if (res.items && res.items.length) {
const claim = res.items[0];
const url = formatLbryUrlForWeb(claim.canonical_url);
push(url);
}
});
} catch (e) {}
}
const stringifiedSearchOptions = JSON.stringify(searchOptions);
2019-03-28 17:53:13 +01:00
useEffect(() => {
if (urlQuery) {
const searchOptions = JSON.parse(stringifiedSearchOptions);
search(urlQuery, { ...searchOptions, from: from });
2018-08-24 23:25:18 +02:00
}
}, [search, urlQuery, stringifiedSearchOptions, from]);
2019-03-28 17:53:13 +01:00
2021-03-26 09:33:30 +01:00
function loadMore() {
if (!isSearching && !hasReachedMaxResultsLength) {
setFrom(from + SEARCH_PAGE_SIZE);
}
}
function resetPage() {
setFrom(0);
}
2019-03-28 17:53:13 +01:00
return (
<Page className="searchPage-wrapper">
2019-04-04 23:05:23 +02:00
<section className="search">
{urlQuery && (
<>
{isValid && <SearchTopClaim query={modifiedUrlQuery} isSearching={isSearching} />}
2020-01-02 23:30:58 +01:00
<ClaimList
uris={uris}
loading={isSearching}
2021-03-26 09:33:30 +01: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-10-19 01:37:58 +02:00
header={<SearchOptions additionalOptions={searchOptions} onSearchOptionsChanged={resetPage} />}
2020-01-02 23:30:58 +01:00
/>
<div className="main--empty help">{__('These search results are provided by LBRY, Inc.')}</div>
</>
2019-04-04 23:05:23 +02:00
)}
</section>
2019-03-28 17:53:13 +01:00
</Page>
);
2017-05-05 10:01:16 +02:00
}