-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor(frontend)/cooperative page #39
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(frontend)/cooperative page #39
Conversation
WalkthroughThis pull request introduces several refinements across multiple frontend components, focusing on improving code organization, state management, and user interaction. The changes span various files, including pre-commit hooks, button components, dropdown implementations, and report-related sections. The modifications primarily involve updating import statements, refactoring state management, enhancing component flexibility, and implementing more dynamic styling using libraries like Changes
Sequence DiagramsequenceDiagram
participant User
participant Dropdown
participant ReportSection
User->>Dropdown: Open/Close
Dropdown-->>User: Toggle Visibility
User->>Dropdown: Select Report Options
Dropdown->>ReportSection: Fetch Reports
ReportSection-->>User: Display Reports
Poem
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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 (6)
frontend/src/components/Dropdown/DropdownItemButton.tsx (1)
17-21
: Consider extracting styles to constants for better maintainability.While the inline styles are clear, extracting them to named constants would improve maintainability and reusability. This is especially useful for styles that might be shared across similar components.
+const baseStyles = "w-full rounded-lg text-left py-3 px-4"; +const activeStyles = "text-primary-blue bg-background-2"; +const inactiveStyles = "text-secondary-text"; +const hoverStyles = "hover:text-primary-blue hover:bg-background-2 cursor-pointer"; <button className={clsx( - "w-full rounded-lg text-left py-3 px-4", - value === item.value - ? "text-primary-blue bg-background-2" - : "text-secondary-text", - "hover:text-primary-blue hover:bg-background-2 cursor-pointer" + baseStyles, + value === item.value ? activeStyles : inactiveStyles, + hoverStyles )}frontend/src/components/ExternalLink.tsx (1)
Line range hint
27-31
: Convert Image width and height props to numbers.The Next.js Image component expects numeric values for width and height props.
<Image src={LinkArrow} - width="24" - height="24" + width={24} + height={24} alt="Arrow link image" className="inline" />frontend/src/components/Cooperative/ReportSection/ReportCard.tsx (1)
33-36
: Consider extracting common class names into Tailwind components.The repeated use of complex class combinations could be simplified by extracting them into Tailwind components.
Example in your
tailwind.config.js
:+ theme: { + extend: { + components: { + 'report-card': 'bg-background-2 md:pb-7 flex flex-col md:flex-row gap-16 justify-between', + 'report-image': 'relative w-32 h-32 md:w-56 md:h-56 flex-shrink-0 hidden md:block' + } + } + }Then in your component:
- <div className={clsx( - "bg-background-2 md:pb-7", - "flex flex-col md:flex-row gap-16 justify-between" - )}> + <div className="report-card">Also applies to: 63-65
frontend/src/components/Dropdown/index.tsx (1)
60-61
: Consider extracting magic numbers into constants.The max-height and width values could be extracted into named constants for better maintainability.
+ const DROPDOWN_MAX_HEIGHT = 300; + const DROPDOWN_WIDTH = { + mobile: 200, + desktop: 348 + }; <div className={clsx( "absolute bg-background-1 z-10 mt-2 rounded-2xl border border-stroke", - "flex flex-col gap-4 p-[10px] max-h-[300px] w-[200px] md:w-[348px]", + "flex flex-col gap-4 p-[10px]", + `max-h-[${DROPDOWN_MAX_HEIGHT}px]`, + `w-[${DROPDOWN_WIDTH.mobile}px] md:w-[${DROPDOWN_WIDTH.desktop}px]`, isOpen ? "visible" : "hidden" )} >frontend/src/components/Cooperative/ReportSection/DropdownContainer.tsx (2)
48-50
: Consider combining year and month state.The separate states for year and month could be combined into a single state object to avoid synchronization issues.
- const [year, setYear] = useState<number>(firstYear); - const [month, setMonth] = useState<string>(); + const [selected, setSelected] = useState<{ + year: number; + month?: string; + }>({ + year: firstYear + }); - useEffect(() => { - setMonth(undefined); - }, [year]); // Then update the handlers - onChange={(val) => setYear(Number(val))} + onChange={(val) => setSelected(prev => ({ + year: Number(val), + month: undefined + }))}Also applies to: 63-69
71-77
: Consider memoizing the selectedReport calculation.The report finding logic could be memoized to avoid recalculation on every render.
+ const selectedReport = useMemo(() => + reports.find( + (report) => + (isMonthInfo ? report.month === month : true) && report.year === year + ), + [isMonthInfo, month, year, reports] + ); useEffect(() => { - const selectedReport = reports.find( - (report) => - (isMonthInfo ? report.month === month : true) && report.year === year - ); setReportUrl(selectedReport?.url); - }, [isMonthInfo, month, year, reports, setReportUrl]); + }, [selectedReport, setReportUrl]);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
yarn.lock
is excluded by!**/yarn.lock
,!**/*.lock
📒 Files selected for processing (11)
frontend/.husky/pre-commit
(1 hunks)frontend/src/components/Button.tsx
(3 hunks)frontend/src/components/Cooperative/MemberSection/LearnMore.tsx
(1 hunks)frontend/src/components/Cooperative/ReportSection/DropdownContainer.tsx
(2 hunks)frontend/src/components/Cooperative/ReportSection/ReportCard.tsx
(3 hunks)frontend/src/components/Cooperative/ReportSection/index.tsx
(2 hunks)frontend/src/components/Cooperative/hero.tsx
(2 hunks)frontend/src/components/Dropdown/DropdownItemButton.tsx
(2 hunks)frontend/src/components/Dropdown/index.tsx
(3 hunks)frontend/src/components/ExternalLink.tsx
(2 hunks)frontend/src/pages/cooperative.tsx
(1 hunks)
✅ Files skipped from review due to trivial changes (3)
- frontend/.husky/pre-commit
- frontend/src/components/Cooperative/MemberSection/LearnMore.tsx
- frontend/src/pages/cooperative.tsx
🧰 Additional context used
📓 Learnings (2)
frontend/src/components/Cooperative/ReportSection/index.tsx (1)
Learnt from: Harman-singh-waraich
PR: kleros/kleros-website-v2#37
File: frontend/src/components/Cooperative/ReportSection/index.tsx:25-31
Timestamp: 2025-01-07T09:31:13.200Z
Learning: In the cooperative page's ReportSection component, the report data is static and doesn't change dynamically, making it safe to use array indices as React keys in the reports mapping.
frontend/src/components/Cooperative/ReportSection/DropdownContainer.tsx (1)
Learnt from: Harman-singh-waraich
PR: kleros/kleros-website-v2#37
File: frontend/src/components/Cooperative/ReportSection/DropdownContainer.tsx:65-71
Timestamp: 2025-01-07T09:32:38.688Z
Learning: In React's useEffect hook, dependencies that are guaranteed to remain static (not change between renders) can be safely omitted from the dependency array, even if the exhaustive-deps lint rule suggests including them. However, this should be done with caution and proper documentation to explain why the dependencies are intentionally omitted.
🔇 Additional comments (8)
frontend/src/components/Button.tsx (1)
10-11
: Well-structured button component with proper TypeScript support!Great improvements:
- Proper TypeScript support by extending React.ComponentProps
- Clean implementation of disabled state
- Correct props spreading
Also applies to: 21-21, 32-33, 39-39
frontend/src/components/ExternalLink.tsx (1)
20-23
: Clean implementation of clsx for className management!The refactoring improves readability and maintainability.
frontend/src/components/Cooperative/ReportSection/index.tsx (1)
Line range hint
17-26
: Well-implemented performance optimization and styling improvements!Great improvements:
- Proper memoization with useCallback
- Clean className management with clsx
- Correct dependency array usage
Note: Based on the retrieved learning, using array indices as keys is safe here since the report data is static.
Also applies to: 29-32
frontend/src/components/Cooperative/hero.tsx (1)
7-7
: LGTM! Import path and Button usage improvements.The changes align with best practices:
- Using absolute imports for better maintainability
- Leveraging the Button component's built-in styling instead of passing custom classes
Also applies to: 29-31
frontend/src/components/Cooperative/ReportSection/ReportCard.tsx (2)
1-9
: LGTM! Import organization improvements.Imports are now better organized with React hooks first, followed by third-party and local imports.
53-57
: LGTM! Enhanced button accessibility.Good addition of the disabled state when reportUrl is undefined, improving user experience.
frontend/src/components/Dropdown/index.tsx (1)
1-9
: LGTM! Improved state management with specialized hooks.Good use of
useToggle
anduseClickAway
from react-use to simplify the implementation.Also applies to: 23-23, 31-33
frontend/src/components/Cooperative/ReportSection/DropdownContainer.tsx (1)
14-14
: LGTM! Improved type safety and data organization.Good addition of
IProcessedReports
type and refactored report processing logic for better maintainability.Also applies to: 22-32
There was a problem hiding this 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 (2)
frontend/src/components/Button.tsx (2)
10-11
: Style definitions look good with room for minor enhancement!The styling implementation is clean and follows best practices. The disabled state and hover effects are well-defined.
Consider adding disabled states to secondary and tertiary variants for consistency:
const secondaryStyle = clsx( baseStyle, "bg-transparent border-2 border-white", - "hover:bg-primary-blue hover:border-primary-blue hover:text-background-2" + "hover:bg-primary-blue hover:border-primary-blue hover:text-background-2", + "disabled:bg-transparent disabled:border-stroke disabled:text-stroke" ); const tertiaryStyle = clsx( baseStyle, "bg-transparent border-2 border-white", - "hover:bg-white/10" + "hover:bg-white/10", + "disabled:border-stroke disabled:text-stroke" );Also applies to: 15-18, 20-24
38-48
: Clean implementation with proper props handling!The component implementation is well-structured with proper conditional rendering of styles and correct props handling.
Consider being more explicit about button-specific props types:
- ...props + ...props }: IButton & { + type?: 'button' | 'submit' | 'reset'; + disabled?: boolean; + form?: string; }) => (This would make the most common button props more discoverable in TypeScript.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
frontend/src/components/Button.tsx
(1 hunks)
🔇 Additional comments (1)
frontend/src/components/Button.tsx (1)
27-31
: Great type safety improvement!Extending
React.ComponentProps<"button">
is an excellent practice that ensures type safety while maintaining full compatibility with native button attributes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Summary by CodeRabbit
Release Notes
New Features
Improvements
clsx
.Performance
User Experience