fix: survey preview updation issues#3269
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ |
WalkthroughThe changes introduce a new Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
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 (
|
|
Thank you for following the naming conventions for pull request titles! 🙏 |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (2)
packages/surveys/src/components/general/Survey.tsx (2)
57-61: Approve the addition of the useEffect hook for survey synchronization.The new
useEffecthook effectively addresses the issue of survey preview not updating correctly after modifications. It ensures thatlocalSurveystate is synchronized with thesurveyprop, which is crucial for reflecting changes in the survey editor context.However, to optimize performance, consider memoizing the
surveyprop usinguseMemoif it's an object, especially if it's coming from a parent component that might re-render frequently. This would prevent unnecessary re-renders of theSurveycomponent.Consider adding a memoization for the
surveyprop:const memoizedSurvey = useMemo(() => survey, [JSON.stringify(survey)]); useEffect(() => { setlocalSurvey(memoizedSurvey); }, [memoizedSurvey]);This approach ensures that the effect only runs when the survey content actually changes, not just when the parent component re-renders.
Line range hint
1-486: Consider refactoring the Survey component for improved maintainability.While the current changes address the immediate issue, the
Surveycomponent appears to be quite large and complex. This could lead to maintenance challenges in the future. Consider the following suggestions for improving the overall structure:
- Break down the component into smaller, more manageable sub-components.
- Extract complex logic (e.g.,
evaluateLogicAndGetNextQuestionId) into separate utility functions.- Use custom hooks to encapsulate related state and effects (e.g., a
useSurveyNavigationhook).- Implement memoization for expensive computations to optimize performance.
Here's a high-level example of how you might start refactoring:
// Custom hook for survey navigation const useSurveyNavigation = (survey, initialQuestionId) => { // ... navigation state and logic }; // Custom hook for response handling const useSurveyResponses = (onResponse, onFinished) => { // ... response state and logic }; // Smaller, focused components const SurveyHeader = ({ showLanguageSwitch, onClose }) => { // ... header JSX }; const SurveyContent = ({ currentQuestion, onSubmit }) => { // ... content JSX }; const SurveyFooter = ({ isBrandingEnabled, showProgressBar }) => { // ... footer JSX }; // Main Survey component export const Survey = (props) => { const { currentQuestion, nextQuestion } = useSurveyNavigation(props.survey, props.startAtQuestionId); const { responseData, handleResponse } = useSurveyResponses(props.onResponse, props.onFinished); // ... rest of the component logic return ( <StackedCardsContainer {...containerProps}> <SurveyHeader {...headerProps} /> <SurveyContent currentQuestion={currentQuestion} onSubmit={handleResponse} /> <SurveyFooter {...footerProps} /> </StackedCardsContainer> ); };This structure would make the component easier to understand, test, and maintain. Consider implementing these changes in a future refactoring task.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- packages/surveys/src/components/general/Survey.tsx (1 hunks)
🔇 Additional comments (1)
packages/surveys/src/components/general/Survey.tsx (1)
Line range hint
1-486: Summary of Survey.tsx review
The addition of the
useEffecthook (lines 57-61) successfully addresses the issue of survey preview not updating correctly after modifications. This change is approved and aligns well with the PR objectives.A suggestion for memoizing the
surveyprop has been provided to optimize performance and prevent unnecessary re-renders.While not directly related to the current PR, a recommendation for refactoring the
Surveycomponent has been made to improve its maintainability and scalability in the future.Overall, the changes in this PR effectively solve the immediate problem. However, consider the suggested optimizations and potential refactoring in future iterations to ensure long-term maintainability of this critical component.
gupta-piyush19
left a comment
There was a problem hiding this comment.
LGTM 🚀
Changes look good and work great; thanks. 😊
What does this PR do?
Fixes survey preview not getting updated.
We recently added an advanced logic editor to Formbricks, which involved switching from using survey to localSurvey in survey.tsx. While localSurvey worked well for handling questions, it wasn’t reflecting changes made to the ending and welcome cards. To resolve this, we implemented a useEffect that keeps survey and localSurvey synchronized. This effect doesn’t run when a user takes a survey but is necessary in the survey editor, where modifications to the survey can occur.
How should this be tested?
Make changes to welcome card/ thank you card. Survey preview wont update
Checklist
Required
pnpm buildconsole.logsgit pull origin mainAppreciated
Summary by CodeRabbit
This improvement ensures users see the most current survey information without needing to refresh or navigate away.