-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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] Save language preference in database #2043
base: main
Are you sure you want to change the base?
Conversation
@enzogms is attempting to deploy a commit to the Typebot Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughThe changes refactor how user locale preferences are handled across the application. In the client-side form, the Changes
Sequence Diagram(s)sequenceDiagram
participant U as User
participant F as UserPreferencesForm
participant A as updateUser
participant R as Router
U->>F: Selects language preference
F->>A: Calls updateUser({ preferredLanguage })
A-->>F: Returns updated state
F->>R: Replaces route with updated path
sequenceDiagram
participant Req as Request
participant SS as Server (getServerSideProps)
participant GS as getServerSession
participant RL as Redirect Logic
Req->>SS: Initiates request
SS->>GS: Retrieve session (with User and preferredLanguage)
GS-->>SS: Returns session data
SS->>RL: Compute preferredLanguagePath
RL-->>SS: Returns computed language path
SS->>Req: Issues redirect using computed path
✨ Finishing Touches
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
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 (2)
apps/builder/src/pages/typebots.tsx (2)
19-19
: Remove debugging console log before production.This debug log statement should be removed before merging to production. While helpful during development, it could expose sensitive user information in logs.
- console.log("🚀 ~ session:", session);
14-27
: Consider extracting the language preference logic to a utility function.This language preference determination logic might be reused across different pages. Consider extracting it to a shared utility function to avoid duplication and improve maintainability.
// Proposed utility in a shared location (e.g., src/utils/language.ts) export const getPreferredLanguagePath = ( user: User | undefined, contextLocale: string | undefined, defaultLocale: string | undefined ): string | undefined => { return user?.preferredLanguage && user.preferredLanguage !== defaultLocale ? user.preferredLanguage : contextLocale !== defaultLocale ? contextLocale : undefined; };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
apps/builder/src/pages/index.tsx
(3 hunks)apps/builder/src/pages/typebots.tsx
(3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- apps/builder/src/pages/index.tsx
🔇 Additional comments (4)
apps/builder/src/pages/typebots.tsx (4)
1-1
: Import additions look good.The additional imports support the new user language preference functionality. The
User
type is correctly imported from the schemas package, and the session-related imports from next-auth are appropriate for server-side session handling.Also applies to: 3-3, 5-5
14-18
: Session retrieval implementation is correct.The implementation properly retrieves the server session and casts it to include the user with their preferences. This allows access to the user's preferred language property for the redirection logic.
21-27
: Language preference logic is well-implemented.The logic correctly prioritizes:
- User's stored preferred language (if set and different from default)
- Request context locale (if different from default)
- Falls back to undefined when appropriate
The null-safety with optional chaining is properly implemented.
39-41
: Redirect path construction looks good.The conditional path construction properly incorporates the preferred language into the URL structure when available, maintaining the original redirect functionality when not.
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 (3)
apps/builder/src/pages/typebots.tsx (3)
14-18
: Consider using type guards instead of type assertion.The type assertion
as Session & { user: User }
works but could be more safely handled with proper type guards.- const session = (await getServerSession( - context.req, - context.res, - createAuthConfig({}), - )) as Session & { user: User }; + const session = await getServerSession( + context.req, + context.res, + createAuthConfig({}), + ); + + // Type guard to ensure session and user exist + const userSession = session && 'user' in session ? + { ...session, user: session.user as User } : null;
38-40
: Consider adding error handling for URL construction.The URL construction works as implemented, but consider adding error handling for malformed paths or using a URL construction utility for more robustness.
- destination: preferredLanguagePath - ? `/${preferredLanguagePath}/${redirectPath}` - : `/${redirectPath}`, + destination: preferredLanguagePath + ? `/${preferredLanguagePath}/${redirectPath.startsWith('/') ? redirectPath.substring(1) : redirectPath}` + : `/${redirectPath.startsWith('/') ? redirectPath.substring(1) : redirectPath}`,
14-40
: Add error handling for session retrieval.There's no error handling for the
getServerSession
call. If session retrieval fails, it might cause unexpected behavior.- const session = (await getServerSession( - context.req, - context.res, - createAuthConfig({}), - )) as Session & { user: User }; + let session; + try { + session = (await getServerSession( + context.req, + context.res, + createAuthConfig({}), + )) as Session & { user: User }; + } catch (error) { + console.error('Failed to retrieve session:', error); + // Provide fallback behavior or decide how to handle the error + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
apps/builder/src/pages/typebots.tsx
(3 hunks)
🔇 Additional comments (3)
apps/builder/src/pages/typebots.tsx (3)
3-3
: Clean import of the User type.The User type import is appropriately added to support the new language preference feature.
5-5
: Good addition of next-auth imports.Properly importing both the Session type and getServerSession function for authentication handling.
20-26
: Good implementation of preferred language logic.This logic correctly determines the language path by checking the user's preferred language first, then falling back to context locale if needed.
Closes 2013