Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(explorer) : As a user, I want the Search feature to redirect me to the Subject page #792

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading