refactor(core): optimize state management and component rendering performance
Improve application stability and performance by optimizing Zustand store usage, implementing memoization patterns, and introducing persistent authentication. - **State Management**: - Refactor Zustand selectors to use `useShallow` and `getState()` to prevent unnecessary re-renders and infinite loops in hooks. - Implement `persist` middleware for `authStore` to maintain user sessions across restarts. - Introduce `buildStateCached` in `themeStore` to reduce redundant theme object computations. - **Performance & Rendering**: - Implement `useMemo` for stable object/array references in `ImageGallery` and list components to prevent expensive re-renders. - Replace inline arrow functions with `useCallback` in complex screens like `ChatScreen` and `MessageListScreen`. - Optimize `FlashList` usage by providing stable `key` and `extraData` props. - **Architecture**: - Decouple unread count fetching by introducing `useUnreadCountQuery` (React Query) for better caching and synchronization. - Add `useChannels` hook to centralize channel data fetching. - Refine `SessionGate` logic to allow immediate rendering of authenticated users while verifying in the background.
This commit is contained in:
@@ -34,6 +34,24 @@ function buildState(
|
||||
};
|
||||
}
|
||||
|
||||
let _cachedKey = '';
|
||||
let _cachedState: ReturnType<typeof buildState>;
|
||||
|
||||
function buildStateCached(
|
||||
preference: ThemePreference,
|
||||
systemScheme: ResolvedScheme
|
||||
): {
|
||||
resolvedScheme: ResolvedScheme;
|
||||
colors: AppColors;
|
||||
paperTheme: MD3Theme;
|
||||
} {
|
||||
const key = `${preference}:${systemScheme}`;
|
||||
if (_cachedKey === key && _cachedState) return _cachedState;
|
||||
_cachedKey = key;
|
||||
_cachedState = buildState(preference, systemScheme);
|
||||
return _cachedState;
|
||||
}
|
||||
|
||||
type ThemeState = {
|
||||
preference: ThemePreference;
|
||||
systemScheme: ResolvedScheme;
|
||||
@@ -58,14 +76,14 @@ export const useThemeStore = create<ThemeState>((set, get) => ({
|
||||
preference: 'system',
|
||||
systemScheme: initialSystemScheme,
|
||||
hydrated: false,
|
||||
...buildState('system', initialSystemScheme),
|
||||
...buildStateCached('system', initialSystemScheme),
|
||||
|
||||
setSystemScheme: (systemScheme) => {
|
||||
const { preference, systemScheme: cur } = get();
|
||||
if (cur === systemScheme) return;
|
||||
set({
|
||||
systemScheme,
|
||||
...buildState(preference, systemScheme),
|
||||
...buildStateCached(preference, systemScheme),
|
||||
});
|
||||
},
|
||||
|
||||
@@ -78,7 +96,7 @@ export const useThemeStore = create<ThemeState>((set, get) => ({
|
||||
const { systemScheme } = get();
|
||||
set({
|
||||
preference,
|
||||
...buildState(preference, systemScheme),
|
||||
...buildStateCached(preference, systemScheme),
|
||||
});
|
||||
},
|
||||
|
||||
@@ -97,7 +115,7 @@ export const useThemeStore = create<ThemeState>((set, get) => ({
|
||||
preference,
|
||||
hydrated: true,
|
||||
systemScheme,
|
||||
...buildState(preference, systemScheme),
|
||||
...buildStateCached(preference, systemScheme),
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user