From e9b7d40456a460b0075c895a4c504953dd0e7e46 Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Mon, 3 Aug 2020 13:57:14 -0400 Subject: [PATCH] redirect to claim if entering a valid claim_id in the search bar --- ui/page/search/view.jsx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ui/page/search/view.jsx b/ui/page/search/view.jsx index 35e3ac6ca..08901f632 100644 --- a/ui/page/search/view.jsx +++ b/ui/page/search/view.jsx @@ -3,7 +3,7 @@ import { SIMPLE_SITE } from 'config'; import * as ICONS from 'constants/icons'; import * as PAGES from 'constants/pages'; import React, { useEffect, Fragment } from 'react'; -import { regexInvalidURI, parseURI } from 'lbry-redux'; +import { Lbry, regexInvalidURI, parseURI } from 'lbry-redux'; import ClaimPreview from 'component/claimPreview'; import ClaimList from 'component/claimList'; import Page from 'component/page'; @@ -11,6 +11,8 @@ import SearchOptions from 'component/searchOptions'; import Button from 'component/button'; import ClaimUri from 'component/claimUri'; import Ads from 'web/component/ads'; +import { formatLbryUrlForWeb } from 'util/url'; +import { useHistory } from 'react-router'; type AdditionalOptions = { isBackgroundSearch: boolean, @@ -39,6 +41,7 @@ export default function SearchPage(props: Props) { showNsfw, isAuthenticated, } = props; + const { push } = useHistory(); const urlParams = new URLSearchParams(location.search); const urlQuery = urlParams.get('q') || ''; const additionalOptions: AdditionalOptions = { isBackgroundSearch: false }; @@ -56,6 +59,21 @@ export default function SearchPage(props: Props) { isValid = false; } + let claimId; + 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 modifiedUrlQuery = isValid && path ? path