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

fix: app re-render issues caused by bad state handling #4146

Merged
merged 1 commit into from
Nov 27, 2024

Conversation

louis-menlo
Copy link
Contributor

@louis-menlo louis-menlo commented Nov 27, 2024

Describe Your Changes

Re-rendering across components in the app caused poor performance. This PR addresses numerous instances of poor state handling that lead to this issue.

JotaiProvider and ThemeProvider now load only once, which also the same issue before. These providers hold the main app states and themes, which could lead to unforeseen performance problems.

App appearance, theme, and show welcome screen toggle once on launch, causing screens to flash (attempt to display then disappear) due to initial storage state settings. These settings should persist on load.

Previously, the thread screen re-renders 16 times on launch
CleanShot 2024-11-27 at 22 53 45

Now it renders once (plus one due to Strict React debugging).
CleanShot 2024-11-27 at 22 47 42

Changes made

  1. Component Refactoring:

    • MainViewContainer: Moved the framer-motion animation logic from Layout to MainViewContainer and added memo for performance optimization.
    • ThreadScreen: Introduced ThreadPanels as a memoized component that conditionally renders OnDeviceStarterScreen or the thread panel components. Also, added WelcomeController to utilize the useStarterScreen hook.
    • Various components (ChatBody, ThreadCenterPanel, Thread) were memoized for performance optimization.
  2. Simplification and Separation:

    • CoreConfigurator: Created a new component to handle core and extension setup logic, migrating this functionality from Providers.
    • The configuration and setup logic in Providers was moved to CoreConfigurator.
  3. State Management Changes:

    • Replaced useAtom with useSetAtom in certain places, such as for setting state in Layout.
    • Implemented useMemo to optimize the computation of derived state, such as checking if any remote models are configured.
  4. Jotai Atom Enhancements:

    • Modified the atomWithStorage to include an onInit option for certain atoms across multiple files, ensuring initial state setup is handled correctly.
  5. Component Logic Adjustments:

    • OnDeviceStarterScreen: Removed the explicit props passing of extensionHasSettings and is now integrated with useStarterScreen.
    • Updated useStarterScreen to use useMemo for calculating isDownloadALocalModel and isShowStarterScreen flags, promoting efficient calculations and re-renders.
  6. Miscellaneous:

    • Imports and hooks were adjusted to ensure all components and logic are encapsulated appropriately.
    • Simplified some method calls and hooks usage that involves the Jotai state management.

Overall, these changes focus on enhancing performance by using React's memoization patterns, refactoring for better code organization, and improving the management of component state and data flow.

@louis-menlo louis-menlo requested a review from urmauur November 27, 2024 15:55
@github-actions github-actions bot added the type: bug Something isn't working label Nov 27, 2024
@louis-menlo louis-menlo merged commit cd1a274 into dev Nov 27, 2024
11 checks passed
@louis-menlo louis-menlo deleted the fix/app-rerender-issues branch November 27, 2024 16:34
@github-actions github-actions bot added this to the v0.5.10 milestone Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants