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

Conversation

Harman-singh-waraich
Copy link
Contributor

@Harman-singh-waraich Harman-singh-waraich commented Dec 13, 2024

PR-Codex overview

This PR introduces a new useSpamEvidence hook to fetch spam evidence data and integrates it into the Evidence component. It enhances the functionality to filter and identify spam evidences based on the fetched data.

Detailed summary

  • Updated tsconfig.json to include dom in lib.
  • Added useSpamEvidence hook in web/src/hooks/useSpamEvidence.ts.
  • Implemented GraphQL query for spam evidences.
  • Integrated spam evidence fetching in Evidence component.
  • Replaced the old spam checking logic with the new hook.
  • Added memoization for spam evidence filtering.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Summary by CodeRabbit

  • New Features

    • Introduced a custom hook useSpamEvidence for fetching spam evidence data.
    • Enhanced the Evidence component to dynamically classify and display spam evidence.
  • Bug Fixes

    • Improved the logic for determining spam evidence, replacing static IDs with dynamic filtering.
  • Chores

    • Updated TypeScript configuration to include DOM-related types.

Copy link
Contributor

coderabbitai bot commented Dec 13, 2024

Walkthrough

This pull request introduces a new custom React hook, useSpamEvidence, which fetches spam evidence data from a GraphQL API using the @tanstack/react-query library. The hook determines the deployment type based on the environment and constructs a query to retrieve relevant spam evidence. Changes to the Evidence component utilize this hook to dynamically classify evidence as spam or real based on the fetched data. Additionally, the TypeScript configuration is updated to include DOM-related types.

Changes

File Change Summary
web/src/hooks/useSpamEvidence.ts Added a new hook useSpamEvidence that fetches spam evidence data from a GraphQL API.
web/src/pages/Cases/CaseDetails/Evidence/index.tsx Updated the Evidence component to use useSpamEvidence, redefined isSpam, and modified rendering logic for spam evidences.
web/tsconfig.json Modified the TypeScript configuration to extend from @kleros/kleros-v2-tsconfig/react-library.json and added "dom" to lib.

Possibly related PRs

  • fix(web): error-handling-in-submit-evidence-modal #1724: This PR enhances error handling in the SubmitEvidenceModal, which is part of the evidence submission process, potentially interacting with the spam evidence management introduced in the main PR.
  • feat(web): evidence moderation #1743: This PR directly relates to the Evidence component by implementing spam evidence filtering and management, which is closely tied to the functionality of the useSpamEvidence hook introduced in the main PR.

Suggested reviewers

  • alcercu
  • jaybuidl

🐰 In a world of spam and cheer,
A hook was born to bring us near.
With evidence fetched from afar,
We sort the truth, like a shining star.
So hop along, let's make it bright,
For every piece, we’ll get it right! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Dec 13, 2024

Deploy Preview for kleros-v2-testnet ready!

Name Link
🔨 Latest commit 01be41e
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-testnet/deploys/675c0f9e24ce220009859766
😎 Deploy Preview https://deploy-preview-1789--kleros-v2-testnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Dec 13, 2024

Deploy Preview for kleros-v2-neo ready!

Name Link
🔨 Latest commit 01be41e
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-neo/deploys/675c0f9e72eba20008271a7d
😎 Deploy Preview https://deploy-preview-1789--kleros-v2-neo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Dec 13, 2024

Deploy Preview for kleros-v2-testnet-devtools failed. Why did it fail? →

Name Link
🔨 Latest commit 01be41e
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-testnet-devtools/deploys/675c0f9e9c11ad00089b294a

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6abacc8 and 15430c7.

📒 Files selected for processing (3)
  • web/src/hooks/useSpamEvidence.ts (1 hunks)
  • web/src/pages/Cases/CaseDetails/Evidence/index.tsx (3 hunks)
  • web/tsconfig.json (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
web/src/pages/Cases/CaseDetails/Evidence/index.tsx

[error] 102-102: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)

🔇 Additional comments (1)
web/tsconfig.json (1)

71-71: LGTM

Adding "dom" to the lib array is appropriate to include DOM-related types.

web/src/pages/Cases/CaseDetails/Evidence/index.tsx Outdated Show resolved Hide resolved
web/src/hooks/useSpamEvidence.ts Show resolved Hide resolved
web/src/hooks/useSpamEvidence.ts Show resolved Hide resolved
Copy link

netlify bot commented Dec 13, 2024

Deploy Preview for kleros-v2-university ready!

Name Link
🔨 Latest commit 01be41e
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-university/deploys/675c0f9e6ae61b00081c8cf4
😎 Deploy Preview https://deploy-preview-1789--kleros-v2-university.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codeclimate bot commented Dec 13, 2024

Code Climate has analyzed commit 01be41e and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Style 2

View more on Code Climate.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
web/src/pages/Cases/CaseDetails/Evidence/index.tsx (3)

83-83: Consider adding error handling for the spam evidence API call

The hook usage should handle potential API failures to ensure graceful degradation.

-  const { data: spamEvidences } = useSpamEvidence();
+  const { data: spamEvidences, error: spamError } = useSpamEvidence();
+  if (spamError) {
+    console.error('Failed to fetch spam evidence:', spamError);
+    // Fallback to treating all evidence as non-spam
+  }

98-108: Consider simplifying the spam evidence processing

While the current implementation avoids the O(n²) spread operator issue, it could be further simplified for better readability.

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

117-122: Consider optimizing the evidence filtering logic

While the current implementation is correct, it could be optimized to avoid unnecessary array iterations.

   const evidences = useMemo(() => {
     if (!data?.evidences) return;
+    // Single pass through the array instead of two filter operations
+    const { spam, real } = data.evidences.reduce(
+      (acc, evidence) => {
+        if (isSpam(evidence.id)) {
+          acc.spam.push(evidence);
+        } else {
+          acc.real.push(evidence);
+        }
+        return acc;
+      },
+      { spam: [], real: [] }
+    );
-    const spamEvidences = data.evidences.filter((evidence) => isSpam(evidence.id));
-    const realEvidences = data.evidences.filter((evidence) => !isSpam(evidence.id));
-    return { realEvidences, spamEvidences };
+    return { realEvidences: real, spamEvidences: spam };
   }, [data, isSpam]);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 15430c7 and 01be41e.

📒 Files selected for processing (1)
  • web/src/pages/Cases/CaseDetails/Evidence/index.tsx (3 hunks)
🔇 Additional comments (3)
web/src/pages/Cases/CaseDetails/Evidence/index.tsx (3)

11-12: LGTM: Imports are properly organized

The new imports for useSpamEvidence hook and Divider component are correctly placed and used within the component.

Also applies to: 18-18


110-115: LGTM: Efficient implementation of isSpam check

The isSpam callback is properly memoized and implements an efficient check using array includes.


11-11: Verify the implementation of useSpamEvidence hook

Let's ensure the hook is properly implemented and the GraphQL query is correctly structured.

✅ Verification successful

Based on the results, I can generate the final response as we have sufficient information about the implementation of the useSpamEvidence hook and its usage.

The useSpamEvidence hook is properly implemented and integrated

The hook is well-structured with:

  • Proper GraphQL query definition with courtv2EvidenceSpamsByDeployment field
  • Correct type definitions for the response data
  • Deployment-aware configuration
  • Integration with React Query for data fetching
  • Proper usage in the Evidence component for spam detection
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check the implementation of useSpamEvidence hook
ast-grep --pattern 'export const useSpamEvidence = () => {
  $$$
}'

# Check for the GraphQL query definition
rg -A 10 'courtv2EvidenceSpamsByDeployment'

Length of output: 2813

Copy link

sonarcloud bot commented Dec 13, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant