Skip to content

Commit

Permalink
feat(explorer) : As a user, I want the Search feature to redirect me …
Browse files Browse the repository at this point in the history
…to the Subject page (#792)
  • Loading branch information
satyajeetkolhapure authored Nov 4, 2024
1 parent 5863e88 commit 316d91f
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions explorer/src/pages/Search/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand All @@ -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]);

Expand All @@ -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 (
<section className="container flex flex-col items-start gap-10 lg:mt-16 md:mt-8 mt-5">
{!isLoaded && (
Expand Down

0 comments on commit 316d91f

Please sign in to comment.