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(web): use-atlas-spam-api #1789

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions web/src/hooks/useSpamEvidence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useQuery } from "@tanstack/react-query";
import { request } from "graphql-request";

import { isKlerosNeo, isKlerosUniversity, isTestnetDeployment } from "src/consts";
import { graphql } from "src/graphql";
import { isUndefined } from "src/utils";

const spamEvidenceQuery = graphql(`
query SpamEvidences($deployment: CourtV2Deployment!) {
courtv2EvidenceSpamsByDeployment(deployment: $deployment) {
disputeEvidenceIndex
dispute
}
}
`);

type SpamEvidences = {
courtv2EvidenceSpamsByDeployment: { disputeEvidenceIndex: string; dispute: string }[];
};

const getAtlasDeployment = () => {
if (isKlerosUniversity()) {
return "university";
} else if (isKlerosNeo()) {
return "beta";
} else if (isTestnetDeployment()) {
return "testnet";
} else {
return "devnet";
}
};
const atlasUri = import.meta.env.REACT_APP_ATLAS_URI;
Harman-singh-waraich marked this conversation as resolved.
Show resolved Hide resolved

export const useSpamEvidence = () => {
const isEnabled = !isUndefined(atlasUri);

const variables = { deployment: getAtlasDeployment() };
return useQuery<SpamEvidences>({
queryKey: [`evidenceSpamQuery`],
enabled: isEnabled,
staleTime: 60000,
queryFn: async () => await request(`${atlasUri}/graphql`, spamEvidenceQuery, variables),
});
Harman-singh-waraich marked this conversation as resolved.
Show resolved Hide resolved
};
31 changes: 24 additions & 7 deletions web/src/pages/Cases/CaseDetails/Evidence/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import { Button } from "@kleros/ui-components-library";

import DownArrow from "svgs/icons/arrow-down.svg";

import { useSpamEvidence } from "hooks/useSpamEvidence";

import { useDisputeDetailsQuery } from "queries/useDisputeDetailsQuery";
import { useEvidences } from "queries/useEvidences";

import { responsiveSize } from "styles/responsiveSize";

import { Divider } from "components/Divider";
import EvidenceCard from "components/EvidenceCard";
import { SkeletonEvidenceCard } from "components/StyledSkeleton";

import EvidenceSearch from "./EvidenceSearch";
import { Divider } from "components/Divider";
import { spamEvidencesIds } from "src/consts";

const Container = styled.div`
width: 100%;
Expand Down Expand Up @@ -79,6 +80,7 @@ const Evidence: React.FC = () => {
const [search, setSearch] = useState<string>();
const [debouncedSearch, setDebouncedSearch] = useState<string>();
const [showSpam, setShowSpam] = useState(false);
const { data: spamEvidences } = useSpamEvidence();

const { data } = useEvidences(disputeData?.dispute?.externalDisputeId?.toString(), debouncedSearch);

Expand All @@ -93,12 +95,31 @@ const Evidence: React.FC = () => {
latestEvidence.scrollIntoView({ behavior: "smooth" });
}, [ref]);

const flattenedSpamEvidences = useMemo(
() =>
spamEvidences?.courtv2EvidenceSpamsByDeployment.reduce<string[]>((acc, current) => {
if (current.dispute === id) {
acc.push(current.disputeEvidenceIndex);
return acc;
}
return acc;
}, []),
[id, spamEvidences]
);

const isSpam = useCallback(
(evidenceId: string) => {
return Boolean(flattenedSpamEvidences?.includes(evidenceId));
},
[flattenedSpamEvidences]
);

const evidences = useMemo(() => {
if (!data?.evidences) return;
const spamEvidences = data.evidences.filter((evidence) => isSpam(evidence.id));
const realEvidences = data.evidences.filter((evidence) => !isSpam(evidence.id));
return { realEvidences, spamEvidences };
}, [data]);
}, [data, isSpam]);

return (
<Container ref={ref}>
Expand Down Expand Up @@ -142,8 +163,4 @@ const Evidence: React.FC = () => {
);
};

const isSpam = (id: string) => {
return spamEvidencesIds.includes(id);
};

export default Evidence;
3 changes: 2 additions & 1 deletion web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"resolveJsonModule": true,
"target": "ES2020",
"lib": [
"ESNext.Array"
"ESNext.Array",
"dom"
],
"types": [
"vite/client",
Expand Down
Loading