fix: app re-render issues caused by bad state handling #4146
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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

Now it renders once (plus one due to Strict React debugging).

Changes made
Component Refactoring:
MainViewContainer
: Moved theframer-motion
animation logic fromLayout
toMainViewContainer
and addedmemo
for performance optimization.ThreadScreen
: IntroducedThreadPanels
as a memoized component that conditionally rendersOnDeviceStarterScreen
or the thread panel components. Also, addedWelcomeController
to utilize theuseStarterScreen
hook.ChatBody
,ThreadCenterPanel
,Thread
) were memoized for performance optimization.Simplification and Separation:
CoreConfigurator
: Created a new component to handle core and extension setup logic, migrating this functionality fromProviders
.Providers
was moved toCoreConfigurator
.State Management Changes:
useAtom
withuseSetAtom
in certain places, such as for setting state inLayout
.useMemo
to optimize the computation of derived state, such as checking if any remote models are configured.Jotai Atom Enhancements:
atomWithStorage
to include anonInit
option for certain atoms across multiple files, ensuring initial state setup is handled correctly.Component Logic Adjustments:
OnDeviceStarterScreen
: Removed the explicit props passing ofextensionHasSettings
and is now integrated withuseStarterScreen
.useStarterScreen
to useuseMemo
for calculatingisDownloadALocalModel
andisShowStarterScreen
flags, promoting efficient calculations and re-renders.Miscellaneous:
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.