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

refactor(web): courts-stat #1826

Merged
merged 3 commits into from
Jan 9, 2025
Merged

refactor(web): courts-stat #1826

merged 3 commits into from
Jan 9, 2025

Conversation

Harman-singh-waraich
Copy link
Contributor

@Harman-singh-waraich Harman-singh-waraich commented Jan 9, 2025

PR-Codex overview

This PR primarily focuses on enhancing the Stats component within the CourtDetails page by adding new features and improving the structure of the code, particularly around the handling of coin prices and displaying statistics.

Detailed summary

  • Removed the Stats.tsx file.
  • Introduced a new type Prices in useCoinPrice.tsx.
  • Updated useCoinPrice to use the new Prices type.
  • Added Stats and StatsContent components in Stats/index.tsx and Stats/StatsContent.tsx.
  • Implemented styled components for layout and design.
  • Integrated coin price fetching in the stats display.
  • Created a new stats.ts file to define statistical metrics with associated icons and formatting functions.

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

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new statistics view for court details with responsive design.
    • Added a new StatsContent component for displaying court-related statistics.
    • Implemented a new Stats component that conditionally renders statistics based on device type.
  • Bug Fixes

    • Removed outdated Stats component to streamline the statistics display.
  • Refactor

    • Restructured statistics components for improved modularity and maintainability.
    • Enhanced statistics display with more flexible data presentation.
  • Improvements

    • Implemented conditional rendering based on device type.
    • Added support for displaying court statistics with dynamic data retrieval.

Copy link
Contributor

coderabbitai bot commented Jan 9, 2025

Walkthrough

The pull request introduces a comprehensive refactoring of the court statistics display functionality in the web application. The changes involve removing the existing Stats.tsx file and creating a new modular structure with separate files for statistics rendering. The new implementation includes StatsContent.tsx for displaying court statistics, index.tsx for responsive rendering, and stats.ts for defining the statistical data structure and retrieval logic. Additionally, minor adjustments were made to import statements in the CourtDetails component.

Changes

File Change Summary
web/src/pages/Courts/CourtDetails/Stats.tsx Entire file removed
web/src/pages/Courts/CourtDetails/Stats/StatsContent.tsx New component added for rendering court statistics with dynamic data display
web/src/pages/Courts/CourtDetails/Stats/index.tsx New responsive Stats component with desktop and mobile rendering logic
web/src/pages/Courts/CourtDetails/Stats/stats.ts New file defining statistics interface and configuration
web/src/pages/Courts/CourtDetails/index.tsx Minor import statement adjustment for Divider component

Possibly related PRs

Suggested Labels

Type: Enhancement :sparkles:, Type: UX, Package: Web

Suggested Reviewers

  • alcercu

Poem

🐰 Stats reborn, a rabbit's delight,
Code refactored, components shine bright.
Modular magic, data takes flight,
From old to new, with algorithmic might,
A statistical dance of pure insight! 📊✨

Finishing Touches

  • 📝 Generate Docstrings

🪧 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. (Beta)
  • @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 Jan 9, 2025

Deploy Preview for kleros-v2-testnet ready!

Name Link
🔨 Latest commit 743b960
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-testnet/deploys/677fdda587a76200081a021f
😎 Deploy Preview https://deploy-preview-1826--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 Jan 9, 2025

Deploy Preview for kleros-v2-testnet-devtools ready!

Name Link
🔨 Latest commit 743b960
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-testnet-devtools/deploys/677fdda5864eef00088585a8
😎 Deploy Preview https://deploy-preview-1826--kleros-v2-testnet-devtools.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 Jan 9, 2025

Deploy Preview for kleros-v2-university ready!

Name Link
🔨 Latest commit 743b960
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-university/deploys/677fdda5c25fae0008197fcc
😎 Deploy Preview https://deploy-preview-1826--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
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: 4

🧹 Nitpick comments (2)
web/src/pages/Courts/CourtDetails/Stats/index.tsx (1)

66-74: Improve type safety in StyledAccordion

The component is using a default expanded index without type checking.

Consider using a type-safe approach:

-      defaultExpanded={0}
+      defaultExpanded={0 as const}
web/src/pages/Courts/CourtDetails/Stats/StatsContent.tsx (1)

65-76: Reduce code duplication in stat rendering

The same mapping logic is repeated three times with different slices of the stats array.

Extract the mapping logic into a reusable component:

interface StatSectionProps {
  stats: typeof stats;
  startIndex: number;
  endIndex: number;
  court: CourtDetailsQuery["court"];
  pricesData: PricesData;
  coinIds: string[];
}

const StatSection: React.FC<StatSectionProps> = ({ stats, startIndex, endIndex, court, pricesData, coinIds }) => (
  <StyledCard>
    {stats.slice(startIndex, endIndex).map(({ title, coinId, getText, getSubtext, color, icon }) => {
      const coinPrice = !isUndefined(pricesData) ? pricesData[coinIds[coinId!]]?.price : undefined;
      return (
        <StatDisplay
          key={title}
          {...{ title, color, icon }}
          text={court ? getText(court) : <StyledSkeleton />}
          subtext={calculateSubtextRender(court, getSubtext, coinPrice)}
          isSmallDisplay={true}
        />
      );
    })}
  </StyledCard>
);

Also applies to: 85-96, 105-116

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between dae34a1 and faa026b.

📒 Files selected for processing (5)
  • web/src/pages/Courts/CourtDetails/Stats.tsx (0 hunks)
  • web/src/pages/Courts/CourtDetails/Stats/StatsContent.tsx (1 hunks)
  • web/src/pages/Courts/CourtDetails/Stats/index.tsx (1 hunks)
  • web/src/pages/Courts/CourtDetails/Stats/stats.ts (1 hunks)
  • web/src/pages/Courts/CourtDetails/index.tsx (1 hunks)
💤 Files with no reviewable changes (1)
  • web/src/pages/Courts/CourtDetails/Stats.tsx
✅ Files skipped from review due to trivial changes (1)
  • web/src/pages/Courts/CourtDetails/index.tsx
⏰ Context from checks skipped due to timeout of 90000ms (19)
  • GitHub Check: Redirect rules - kleros-v2-university
  • GitHub Check: Header rules - kleros-v2-university
  • GitHub Check: Pages changed - kleros-v2-university
  • GitHub Check: Redirect rules - kleros-v2-testnet-devtools
  • GitHub Check: Header rules - kleros-v2-testnet-devtools
  • GitHub Check: Pages changed - kleros-v2-testnet-devtools
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Copy labels from linked issues
  • GitHub Check: SonarCloud
  • GitHub Check: dependency-review
  • GitHub Check: Copy labels from linked issues
  • GitHub Check: contracts-testing
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Mend Security Check
🔇 Additional comments (2)
web/src/pages/Courts/CourtDetails/Stats/stats.ts (2)

96-96: Handle potential negative case count

The calculation data?.numberDisputes - data?.numberClosedDisputes could result in a negative number if the data is inconsistent.

Add a safety check:

-    getText: (data) => data?.numberDisputes - data?.numberClosedDisputes,
+    getText: (data) => Math.max(0, (data?.numberDisputes ?? 0) - (data?.numberClosedDisputes ?? 0)),

67-68: Add null check for feeForJuror

The code assumes data?.feeForJuror is always defined.

Add a null check or provide a default value:

-      const jurorReward = formatUnitsWei(data?.feeForJuror);
+      const jurorReward = formatUnitsWei(data?.feeForJuror ?? '0');

Copy link

netlify bot commented Jan 9, 2025

Deploy Preview for kleros-v2-neo ready!

Name Link
🔨 Latest commit 743b960
🔍 Latest deploy log https://app.netlify.com/sites/kleros-v2-neo/deploys/677fdda5549cb50008d92bc1
😎 Deploy Preview https://deploy-preview-1826--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
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

🧹 Nitpick comments (3)
web/src/hooks/useCoinPrice.tsx (3)

9-11: Add JSDoc documentation for the Prices type.

While the type definition is well-structured, adding documentation would help consumers understand the expected format of coin IDs and the meaning of the price value.

+/**
+ * Represents a mapping of coin IDs to their current prices
+ * @example
+ * {
+ *   "coingecko:ethereum": { price: 2000.50 },
+ *   "coingecko:bitcoin": { price: 35000.00 }
+ * }
+ */
 export type Prices = {
   [coinId: string]: { price: number };
 };

16-16: Improve query key structure to avoid collisions.

The current query key might cause collisions if multiple instances of the hook are used with different coin IDs. Consider using an array-based key structure.

-    queryKey: [`coinPrice${coinIds}`],
+    queryKey: ['coinPrice', ...coinIds],

Line range hint 16-20: Consider enhancing error handling.

The hook returns isError but doesn't provide error details. Consider adding error type information and returning the error message for better error handling in consuming components.

-  const { data: prices, isError } = useQuery<Prices>({
+  const { data: prices, isError, error } = useQuery<Prices, Error>({
     queryKey: [`coinPrice${coinIds}`],
     enabled: isEnabled,
     queryFn: async () => fetchCoinPrices(coinIds),
   });
   return {
     prices,
     isError,
+    error: error?.message
   };
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between faa026b and 721d029.

📒 Files selected for processing (3)
  • web/src/hooks/useCoinPrice.tsx (1 hunks)
  • web/src/pages/Courts/CourtDetails/Stats/StatsContent.tsx (1 hunks)
  • web/src/pages/Courts/CourtDetails/Stats/stats.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • web/src/pages/Courts/CourtDetails/Stats/stats.ts
  • web/src/pages/Courts/CourtDetails/Stats/StatsContent.tsx
⏰ Context from checks skipped due to timeout of 90000ms (17)
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Header rules - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Pages changed - kleros-v2-testnet
  • GitHub Check: Redirect rules - kleros-v2-testnet-devtools
  • GitHub Check: Header rules - kleros-v2-testnet-devtools
  • GitHub Check: Pages changed - kleros-v2-testnet-devtools
  • GitHub Check: Redirect rules - kleros-v2-university
  • GitHub Check: Header rules - kleros-v2-university
  • GitHub Check: Pages changed - kleros-v2-university
  • GitHub Check: Copy labels from linked issues
  • GitHub Check: dependency-review
  • GitHub Check: contracts-testing
  • GitHub Check: SonarCloud
  • GitHub Check: Analyze (javascript)

Copy link
Contributor

@alcercu alcercu left a comment

Choose a reason for hiding this comment

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

lgtm

@alcercu alcercu enabled auto-merge January 9, 2025 14:31
Copy link

codeclimate bot commented Jan 9, 2025

Code Climate has analyzed commit 743b960 and detected 8 issues on this pull request.

Here's the issue category breakdown:

Category Count
Duplication 8

View more on Code Climate.

Copy link

sonarqubecloud bot commented Jan 9, 2025

@alcercu alcercu added this pull request to the merge queue Jan 9, 2025
Merged via the queue into dev with commit f39ef4a Jan 9, 2025
18 of 24 checks passed
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.

2 participants