-
Notifications
You must be signed in to change notification settings - Fork 60
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
chore(j-s): Redirect logged in user #16978
Conversation
WalkthroughThe changes in the Changes
Suggested labels
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? 🪧 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
Documentation and Community
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #16978 +/- ##
==========================================
+ Coverage 35.68% 36.54% +0.86%
==========================================
Files 6937 6902 -35
Lines 147046 144903 -2143
Branches 41822 41439 -383
==========================================
+ Hits 52477 52962 +485
+ Misses 94569 91941 -2628
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 227 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
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
🧹 Outside diff range and nitpick comments (2)
apps/judicial-system/web/src/routes/Shared/Login/Login.tsx (2)
72-84
: Consider adding loading state and error handling.While the redirection logic is correct, the user experience could be improved by:
- Adding a loading state during redirection
- Handling potential navigation errors
Here's a suggested improvement:
+ const [isRedirecting, setIsRedirecting] = useState(false) useEffect(() => { if (user) { + setIsRedirecting(true) let targetRoute = CASES_ROUTE if (isDefenceUser(user)) { targetRoute = DEFENDER_CASES_ROUTE } else if (isPrisonStaffUser(user)) { targetRoute = PRISON_CASES_ROUTE } else if (isCourtOfAppealsUser(user)) { targetRoute = COURT_OF_APPEAL_CASES_ROUTE } - router.push(targetRoute) + router.push(targetRoute).catch(console.error).finally(() => { + setIsRedirecting(false) + }) } }, [router, user]) + if (isRedirecting) { + return <LoadingScreen /> + }
72-84
: Consider extracting redirection logic into a custom hook.The role-based redirection logic could be reused in other components. Consider extracting it into a custom hook like
useRoleBasedRedirect
for better maintainability and reusability.Example implementation:
// hooks/useRoleBasedRedirect.ts export const useRoleBasedRedirect = (user: User | null) => { const router = useRouter() const [isRedirecting, setIsRedirecting] = useState(false) useEffect(() => { if (!user) return setIsRedirecting(true) const targetRoute = getTargetRouteForUser(user) router.push(targetRoute) .catch(console.error) .finally(() => setIsRedirecting(false)) }, [router, user]) return { isRedirecting } } // Usage in Login.tsx const { isRedirecting } = useRoleBasedRedirect(user)
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
apps/judicial-system/web/src/routes/Shared/Login/Login.tsx
(3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
apps/judicial-system/web/src/routes/Shared/Login/Login.tsx (1)
Pattern apps/**/*
: "Confirm that the code adheres to the following:
- NextJS best practices, including file structure, API routes, and static generation methods.
- Efficient state management and server-side rendering techniques.
- Optimal use of TypeScript for component and utility type safety."
🔇 Additional comments (3)
apps/judicial-system/web/src/routes/Shared/Login/Login.tsx (3)
1-1
: LGTM! Import statements are well-organized.
The new imports are logically grouped and necessary for implementing the redirection functionality.
Also applies to: 6-16, 21-21
31-31
: LGTM! Proper usage of React Context.
The user context is correctly destructured and follows React best practices.
72-84
: Verify redirection behavior in different scenarios.
The implementation aligns with the PR objectives, but let's verify the behavior in various scenarios:
- User directly accessing login page
- User clicking back button after login
- Race conditions during login/logout
#!/bin/bash
# Search for other components that might need similar redirection logic
rg -t typescript -t tsx "router\.(push|replace)" apps/judicial-system/web/src
# Look for potential race conditions in auth state management
ast-grep --pattern 'useEffect($$$, [$$$user$$$])'
Datadog ReportBranch report: ✅ 0 Failed, 335 Passed, 0 Skipped, 1m 19.32s Total Time 🔻 Code Coverage Decreases vs Default Branch (1)
|
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: 1
🧹 Outside diff range and nitpick comments (1)
apps/judicial-system/web/src/routes/Shared/Login/Login.tsx (1)
74-88
: Consider adding loading state during redirectionThe component might briefly show the login UI before redirecting. Consider adding a loading state to improve user experience.
Here's a suggested implementation:
+ const [isRedirecting, setIsRedirecting] = useState(false) useEffect(() => { if (user) { + setIsRedirecting(true) const redirectRoute = // ... existing logic ... router.push(redirectRoute) } }, [user]) // Early return for redirecting state + if (isRedirecting) { + return <LoadingScreen /> + } return ( // ... existing JSX ... )
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
apps/judicial-system/web/src/routes/Shared/Login/Login.tsx
(3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
apps/judicial-system/web/src/routes/Shared/Login/Login.tsx (1)
Pattern apps/**/*
: "Confirm that the code adheres to the following:
- NextJS best practices, including file structure, API routes, and static generation methods.
- Efficient state management and server-side rendering techniques.
- Optimal use of TypeScript for component and utility type safety."
🔇 Additional comments (3)
apps/judicial-system/web/src/routes/Shared/Login/Login.tsx (3)
1-1
: LGTM! Well-organized imports
The imports are properly organized and grouped by functionality, with clear separation between external dependencies, route constants, and internal utilities.
Also applies to: 6-24
33-33
: LGTM! Proper context usage
The UserContext is correctly accessed using the useContext hook, following React best practices.
74-88
: Verify redirection behavior for all user roles
Please ensure that the redirection logic is tested for all possible user roles and edge cases.
✅ Verification successful
Let me search for the Login component tests and role-based routing implementation to verify the redirection logic.
Let me search for the role check functions and route constants to verify the redirection logic completeness.
Based on the comprehensive code analysis, I can now provide a final response about the redirection logic in the Login component.
Redirection logic is correctly implemented for all user roles
The implementation correctly handles all user roles defined in the UserRole
enum and redirects users to their appropriate routes:
- Defence users ->
DEFENDER_CASES_ROUTE
- Prison staff users ->
PRISON_CASES_ROUTE
- Court of Appeals users ->
COURT_OF_APPEAL_CASES_ROUTE
- Admin users ->
USERS_ROUTE
- All other roles ->
CASES_ROUTE
The role-checking functions (isDefenceUser
, isPrisonStaffUser
, isCourtOfAppealsUser
) are consistently used throughout the codebase, and the route constants are properly defined and referenced. The redirection logic follows the same pattern in both the Login component and the Header component, ensuring consistent navigation behavior.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for related test files
fd -e test.tsx -e spec.tsx "Login" apps/judicial-system/web/src
# Look for existing role-based routing tests
rg -l "test|describe|it.*redirect.*role" apps/judicial-system/web/src
Length of output: 9433
Script:
#!/bin/bash
# Search for Login component tests
rg -A 10 "describe.*Login" apps/judicial-system/web/src
# Search for user role definitions and related functions
ast-grep --pattern 'enum UserRole {
$$$
}'
# Search for role check functions
ast-grep --pattern 'function isDefenceUser($_) {
$$$
}'
ast-grep --pattern 'function isPrisonStaffUser($_) {
$$$
}'
ast-grep --pattern 'function isCourtOfAppealsUser($_) {
$$$
}'
Length of output: 1810
Script:
#!/bin/bash
# Search for role check functions implementation
rg "isDefenceUser|isPrisonStaffUser|isCourtOfAppealsUser" -A 5 apps/judicial-system/web/src
# Search for route constants
rg "DEFENDER_CASES_ROUTE|PRISON_CASES_ROUTE|COURT_OF_APPEAL_CASES_ROUTE|USERS_ROUTE|CASES_ROUTE" apps/judicial-system/web/src
# Search for any Login component tests
rg -g "**/*Login*.{spec,test}.{ts,tsx}" . apps/judicial-system/web/src
Length of output: 55984
emailTail linkar á rettarvorslugatt.island.is sem tekur mann alltaf á forsíðu - þó maður sé loggaður inn.
What
Redirect logged in user to correct case list when going to log in screen
Why
So people don't log in more often than needed
Checklist:
Summary by CodeRabbit