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

Merged
merged 5 commits into from
Jan 23, 2025
Merged
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
42 changes: 42 additions & 0 deletions web/src/hooks/useSpamEvidence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useQuery } from "@tanstack/react-query";
import { gql, request } from "graphql-request";

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

const spamEvidenceQuery = gql`
query SpamEvidences($deployment: CourtV2Deployment!, $evidenceGroupId: String!) {
courtv2EvidenceSpamsByGroupId(deployment: $deployment, evidenceGroupId: $evidenceGroupId) {
evidenceIds
}
}
`;

type SpamEvidences = {
courtv2EvidenceSpamsByGroupId: { evidenceIds: 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 = (evidenceGroupId: string) => {
const isEnabled = !isUndefined(atlasUri) && !isUndefined(evidenceGroupId);

const variables = { deployment: getAtlasDeployment(), evidenceGroupId };
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
};
80 changes: 45 additions & 35 deletions web/src/pages/Cases/CaseDetails/Evidence/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import { Button } from "@kleros/ui-components-library";

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

import { spamEvidencesIds } from "consts/index";
import { useSpamEvidence } from "hooks/useSpamEvidence";

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

import { landscapeStyle } from "styles/landscapeStyle";

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

import EvidenceSearch from "./EvidenceSearch";
import { Divider } from "components/Divider";

const Container = styled.div`
width: 100%;
Expand Down Expand Up @@ -85,6 +85,7 @@ const Evidence: React.FC = () => {
const [search, setSearch] = useState<string>();
const [debouncedSearch, setDebouncedSearch] = useState<string>();
const [showSpam, setShowSpam] = useState(false);
const { data: spamEvidences } = useSpamEvidence(disputeData?.dispute?.externalDisputeId?.toString());
Harman-singh-waraich marked this conversation as resolved.
Show resolved Hide resolved

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

Expand All @@ -99,57 +100,66 @@ const Evidence: React.FC = () => {
latestEvidence.scrollIntoView({ behavior: "smooth" });
}, [ref]);

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

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}>
<EvidenceSearch {...{ search, setSearch, evidenceGroup: disputeData?.dispute?.externalDisputeId }} />
<ScrollButton small Icon={DownArrow} text="Scroll to latest" onClick={scrollToLatest} />
{evidences?.realEvidences ? (
evidences?.realEvidences.map(
({ evidence, sender, timestamp, transactionHash, name, description, fileURI, evidenceIndex }) => (
<EvidenceCard
key={timestamp}
index={parseInt(evidenceIndex)}
sender={sender?.id}
{...{ evidence, timestamp, transactionHash, name, description, fileURI }}
/>
)
)
) : (
<SkeletonEvidenceCard />
)}
{evidences?.spamEvidences.length !== 0 ? (
<>
<Divider />
{showSpam ? (
evidences?.spamEvidences.map(
({ evidence, sender, timestamp, transactionHash, name, description, fileURI, evidenceIndex }) => (
<EvidenceCard
key={timestamp}
index={parseInt(evidenceIndex)}
sender={sender?.id}
{...{ evidence, timestamp, transactionHash, name, description, fileURI }}
/>
)
{evidences?.realEvidences.map(
({ evidence, sender, timestamp, transactionHash, name, description, fileURI, evidenceIndex }) => (
<EvidenceCard
key={timestamp}
index={parseInt(evidenceIndex)}
sender={sender?.id}
{...{ evidence, timestamp, transactionHash, name, description, fileURI }}
/>
)
) : (
<SpamLabel onClick={() => setShowSpam(true)}>Show likely spam</SpamLabel>
)}
{spamEvidences && evidences?.spamEvidences.length !== 0 ? (
<>
<Divider />
{showSpam ? (
<>
<SpamLabel onClick={() => setShowSpam(false)}>Hide spam</SpamLabel>
{evidences?.spamEvidences.map(
({ evidence, sender, timestamp, transactionHash, name, description, fileURI, evidenceIndex }) => (
<EvidenceCard
key={timestamp}
index={parseInt(evidenceIndex)}
sender={sender?.id}
{...{ evidence, timestamp, transactionHash, name, description, fileURI }}
/>
)
)}
</>
) : (
<SpamLabel onClick={() => setShowSpam(true)}>Show likely spam</SpamLabel>
)}
</>
) : null}
</>
) : null}
) : (
<SkeletonEvidenceCard />
)}

{data && data.evidences.length === 0 ? <StyledLabel>There is no evidence submitted yet</StyledLabel> : null}
</Container>
);
};

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

export default Evidence;
Loading