From 59d86fcea4a0265698105b49944d911b0902ef42 Mon Sep 17 00:00:00 2001 From: Satyajeet Kolhapure Date: Fri, 1 Nov 2024 12:26:17 +0000 Subject: [PATCH] redirected to subject page when only attestations found in search result --- explorer/src/pages/Search/index.tsx | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/explorer/src/pages/Search/index.tsx b/explorer/src/pages/Search/index.tsx index ae849dcb..6dc34bb9 100644 --- a/explorer/src/pages/Search/index.tsx +++ b/explorer/src/pages/Search/index.tsx @@ -1,6 +1,6 @@ import { useEffect, useMemo, useState } from "react"; import { Trans } from "react-i18next"; -import { useSearchParams } from "react-router-dom"; +import { useNavigate, useSearchParams } from "react-router-dom"; import archive from "@/assets/icons/archive.svg"; import magnifyingGlass from "@/assets/icons/magnifying-glass.svg"; @@ -10,6 +10,7 @@ import { DEFAULT_SEARCH_ELEMENTS } from "@/constants/components"; import { EQueryParams } from "@/enums/queryParams"; import { Page, SearchElementProps } from "@/interfaces/components"; import { useNetworkContext } from "@/providers/network-provider/context.ts"; +import { CHAIN_ID_ROUTE, toAttestationsBySubject } from "@/routes/constants"; import { parseSearch } from "@/utils/searchUtils"; import { SearchAttestationsReceived } from "./components/SearchAttestationsReceived"; @@ -19,10 +20,11 @@ import { SearchSchemas } from "./components/SearchSchemas"; //todo: load more and loading for child export const Search = () => { + const navigate = useNavigate(); const [searchParams] = useSearchParams(); const search = searchParams.get(EQueryParams.SEARCH_QUERY); const { - network: { prefix }, + network: { prefix, network }, } = useNetworkContext(); const parsedString = useMemo(() => parseSearch(search, prefix), [search, prefix]); @@ -38,12 +40,31 @@ export const Search = () => { setCount(newCount); setIsLoaded(newIsLoaded); setNotFound(newIsLoaded && newCount === 0); - }, [searchElements]); + if (isOnlyAttestationsFound(searchElements)) { + navigate(toAttestationsBySubject(search ?? "").replace(CHAIN_ID_ROUTE, network), { + state: { from: location.pathname }, + }); + } + }, [searchElements, navigate, network, search]); const updateSearchElement = (page: Page, count: number, loaded: boolean) => { setSearchElements((prev) => ({ ...prev, [page]: { loaded, count } })); }; + const isOnlyAttestationsFound = (searchElements: SearchElementProps) => { + const { attestation, module, portal, schema } = searchElements; + return ( + attestation.loaded && + module.loaded && + portal.loaded && + schema.loaded && + attestation.count > 0 && + module.count === 0 && + portal.count === 0 && + schema.count === 0 + ); + }; + return (
{!isLoaded && (