2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2020-08-03 22:14:43 +02:00
|
|
|
import { SIMPLE_SITE, SHOW_ADS } from 'config';
|
2019-06-11 20:10:58 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-10-28 20:18:58 +01:00
|
|
|
import React, { useEffect } from 'react';
|
2020-10-30 04:56:01 +01:00
|
|
|
import { Lbry, parseURI, isNameValid } from 'lbry-redux';
|
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';
|
2019-04-19 20:02:46 +02:00
|
|
|
import Button from 'component/button';
|
2020-05-07 20:44:11 +02:00
|
|
|
import Ads from 'web/component/ads';
|
2020-10-28 20:18:58 +01:00
|
|
|
import SearchTopClaim from 'component/searchTopClaim';
|
2020-08-03 19:57:14 +02:00
|
|
|
import { formatLbryUrlForWeb } from 'util/url';
|
|
|
|
import { useHistory } from 'react-router';
|
2020-12-04 01:10:23 +01:00
|
|
|
import ClaimPreview from 'component/claimPreview';
|
2017-05-05 10:01:16 +02:00
|
|
|
|
2020-01-30 08:09:12 +01:00
|
|
|
type AdditionalOptions = {
|
|
|
|
isBackgroundSearch: boolean,
|
|
|
|
nsfw?: boolean,
|
|
|
|
};
|
|
|
|
|
2019-06-11 20:10:58 +02:00
|
|
|
type Props = {
|
2020-01-30 08:09:12 +01:00
|
|
|
search: (string, AdditionalOptions) => void,
|
2020-12-03 22:31:38 +01:00
|
|
|
searchOptions: {},
|
2019-07-21 01:23:29 +02:00
|
|
|
isSearching: boolean,
|
2019-06-11 20:10:58 +02:00
|
|
|
location: UrlLocation,
|
|
|
|
uris: Array<string>,
|
2021-03-03 20:23:42 +01:00
|
|
|
onFeedbackNegative: (string) => void,
|
|
|
|
onFeedbackPositive: (string) => void,
|
2020-01-30 08:09:12 +01:00
|
|
|
showNsfw: boolean,
|
2020-03-26 22:47:07 +01:00
|
|
|
isAuthenticated: 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) {
|
2020-03-26 22:47:07 +01:00
|
|
|
const {
|
|
|
|
search,
|
|
|
|
uris,
|
|
|
|
onFeedbackPositive,
|
|
|
|
onFeedbackNegative,
|
|
|
|
location,
|
|
|
|
isSearching,
|
2021-03-03 20:23:42 +01:00
|
|
|
// showNsfw,
|
2020-03-26 22:47:07 +01:00
|
|
|
isAuthenticated,
|
2020-12-03 22:31:38 +01:00
|
|
|
searchOptions,
|
2020-03-26 22:47:07 +01:00
|
|
|
} = props;
|
2020-08-03 19:57:14 +02:00
|
|
|
const { push } = useHistory();
|
2019-06-20 02:57:51 +02:00
|
|
|
const urlParams = new URLSearchParams(location.search);
|
2020-02-11 19:36:17 +01:00
|
|
|
const urlQuery = urlParams.get('q') || '';
|
2020-01-30 08:09:12 +01:00
|
|
|
const additionalOptions: AdditionalOptions = { isBackgroundSearch: false };
|
2021-01-08 23:02:40 +01:00
|
|
|
|
2021-03-03 20:23:42 +01:00
|
|
|
additionalOptions['nsfw'] = false;
|
2019-04-15 06:07:23 +02:00
|
|
|
|
2021-03-03 20:23:42 +01:00
|
|
|
const modifiedUrlQuery = urlQuery.trim().replace(/\s+/g, '').replace(/:/g, '#');
|
2020-10-28 20:18:58 +01:00
|
|
|
const uriFromQuery = `lbry://${modifiedUrlQuery}`;
|
|
|
|
|
|
|
|
let streamName;
|
2020-09-10 19:12:05 +02:00
|
|
|
let isValid = true;
|
2020-03-10 15:08:27 +01:00
|
|
|
try {
|
2020-10-28 20:18:58 +01:00
|
|
|
({ streamName } = parseURI(uriFromQuery));
|
2020-09-10 19:12:05 +02:00
|
|
|
if (!isNameValid(streamName)) {
|
|
|
|
isValid = false;
|
|
|
|
}
|
2020-03-10 15:08:27 +01:00
|
|
|
} catch (e) {
|
|
|
|
isValid = false;
|
|
|
|
}
|
|
|
|
|
2020-08-03 19:57:14 +02:00
|
|
|
let claimId;
|
2020-10-28 20:18:58 +01:00
|
|
|
// Navigate directly to a claim if a claim_id is pasted into the search bar
|
2020-08-03 19:57:14 +02:00
|
|
|
if (!/\s/.test(urlQuery) && urlQuery.length === 40) {
|
|
|
|
try {
|
|
|
|
const dummyUrlForClaimId = `x#${urlQuery}`;
|
|
|
|
({ claimId } = parseURI(dummyUrlForClaimId));
|
2021-03-03 20:23:42 +01:00
|
|
|
Lbry.claim_search({ claim_id: claimId }).then((res) => {
|
2020-08-03 19:57:14 +02:00
|
|
|
if (res.items && res.items.length) {
|
|
|
|
const claim = res.items[0];
|
|
|
|
const url = formatLbryUrlForWeb(claim.canonical_url);
|
|
|
|
push(url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
2020-10-22 22:05:58 +02:00
|
|
|
const stringifiedOptions = JSON.stringify(additionalOptions);
|
2020-12-03 22:31:38 +01:00
|
|
|
const stringifiedSearchOptions = JSON.stringify(searchOptions);
|
2019-03-28 17:53:13 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (urlQuery) {
|
2020-10-22 22:05:58 +02:00
|
|
|
const jsonOptions = JSON.parse(stringifiedOptions);
|
|
|
|
search(urlQuery, jsonOptions);
|
2018-08-24 23:25:18 +02:00
|
|
|
}
|
2020-12-03 22:31:38 +01:00
|
|
|
}, [search, urlQuery, stringifiedOptions, stringifiedSearchOptions]);
|
2019-03-28 17:53:13 +01:00
|
|
|
|
|
|
|
return (
|
2019-05-07 04:35:04 +02:00
|
|
|
<Page>
|
2019-04-04 23:05:23 +02:00
|
|
|
<section className="search">
|
|
|
|
{urlQuery && (
|
2020-10-28 20:18:58 +01:00
|
|
|
<>
|
2020-12-22 16:16:39 +01:00
|
|
|
{isValid && <SearchTopClaim query={modifiedUrlQuery} isSearching={isSearching} />}
|
2020-01-02 23:30:58 +01:00
|
|
|
<ClaimList
|
|
|
|
uris={uris}
|
|
|
|
loading={isSearching}
|
2020-10-23 21:26:39 +02:00
|
|
|
header={!SIMPLE_SITE && <SearchOptions additionalOptions={additionalOptions} />}
|
2021-01-27 00:45:34 +01:00
|
|
|
injectedItem={
|
|
|
|
SHOW_ADS && IS_WEB ? (SIMPLE_SITE ? false : !isAuthenticated && <Ads small type={'video'} />) : false
|
|
|
|
}
|
2020-01-02 23:30:58 +01:00
|
|
|
headerAltControls={
|
2021-03-03 20:23:42 +01:00
|
|
|
!SIMPLE_SITE && (
|
|
|
|
<>
|
|
|
|
<span>{__('Find what you were looking for?')}</span>
|
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
description={__('Yes')}
|
|
|
|
onClick={() => onFeedbackPositive(urlQuery)}
|
|
|
|
icon={ICONS.YES}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
description={__('No')}
|
|
|
|
onClick={() => onFeedbackNegative(urlQuery)}
|
|
|
|
icon={ICONS.NO}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
2020-01-02 23:30:58 +01:00
|
|
|
}
|
|
|
|
/>
|
2020-12-04 01:10:23 +01:00
|
|
|
{isSearching && new Array(5).fill(1).map((x, i) => <ClaimPreview key={i} placeholder="loading" />)}
|
2020-01-02 23:30:58 +01:00
|
|
|
|
2020-10-28 20:18: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
|
|
|
}
|