Skip to content

Commit

Permalink
Clean-up UserThemeProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovNikita committed Apr 15, 2024
1 parent 09ab49d commit cf5c38c
Showing 1 changed file with 12 additions and 34 deletions.
46 changes: 12 additions & 34 deletions packages/uikit/src/providers/UserThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,33 @@
import { FC, PropsWithChildren, useEffect, useMemo } from 'react';
import { FC, PropsWithChildren, useMemo } from 'react';
import { DefaultTheme, ThemeProvider } from 'styled-components';
import { availableThemes, useMutateUserUIPreferences, useUserUIPreferences } from '../state/theme';
import { usePrevious } from '../hooks/usePrevious';
import { availableThemes, useUserUIPreferences } from '../state/theme';

export const UserThemeProvider: FC<
PropsWithChildren<{
displayType?: 'compact' | 'full-width';
isPro?: boolean;
isProSupported?: boolean;
}>
> = ({ children, displayType, isPro, isProSupported }) => {
const { data: uiPreferences, isFetched: isUIPreferencesLoaded } = useUserUIPreferences();
const { mutateAsync } = useMutateUserUIPreferences();
const isProPrev = usePrevious(isPro);

const [currentTheme, currentThemeName] = useMemo(() => {
let themeName = uiPreferences?.theme;
> = ({ children, displayType, isPro }) => {
const { data: uiPreferences } = useUserUIPreferences();

if (themeName === 'pro' && isPro === false) {
themeName = 'dark';
}

if (!themeName && isPro) {
themeName = 'pro';
}
const [currentTheme, _currentThemeName] = useMemo(() => {
let themeName: 'dark' | 'pro' = 'dark';
let theme = availableThemes[themeName];

if (isProPrev === false && isPro) {
themeName = 'pro';
if (isPro === true) {
themeName = (uiPreferences?.theme || 'pro') as 'dark' | 'pro';
theme = availableThemes[themeName];
}

themeName = themeName || 'dark';

const theme = availableThemes[themeName];

if (displayType) {
theme.displayType = displayType;
}

window.document.body.style.background = theme.backgroundPage;

return [theme, themeName];
}, [uiPreferences?.theme, displayType, isPro, isProPrev]);

useEffect(() => {
if (currentTheme && uiPreferences && currentThemeName !== uiPreferences.theme) {
mutateAsync({ theme: currentThemeName as 'dark' | 'pro' });
}
}, [mutateAsync, currentThemeName, uiPreferences]);

if (!isUIPreferencesLoaded || (isPro === undefined && isProSupported)) {
return <div></div>;
}
}, [uiPreferences?.theme, displayType, isPro]);

return <ThemeProvider theme={currentTheme as DefaultTheme}>{children}</ThemeProvider>;
};

0 comments on commit cf5c38c

Please sign in to comment.