Reduce toasts when switching themes
This commit is contained in:
@@ -196,8 +196,14 @@ const getFontsFromTheme = (obj: Record<string, any>, fonts: string[] = []): stri
|
|||||||
};
|
};
|
||||||
|
|
||||||
const loadedFonts: string[] = [];
|
const loadedFonts: string[] = [];
|
||||||
|
|
||||||
|
export const hasMissingFonts = (theme: ThemeOptions): boolean => {
|
||||||
|
const themeFonts = getFontsFromTheme(theme.typography ?? {});
|
||||||
|
return !!themeFonts.length && themeFonts.some((font) => !loadedFonts.includes(font));
|
||||||
|
};
|
||||||
|
|
||||||
export const loadThemeFonts = async (theme: ThemeOptions): Promise<void> => {
|
export const loadThemeFonts = async (theme: ThemeOptions): Promise<void> => {
|
||||||
const themeFonts = getFontsFromTheme(theme);
|
const themeFonts = getFontsFromTheme(theme.typography ?? {});
|
||||||
const missingThemeFonts = themeFonts.filter((font) => !loadedFonts.includes(font));
|
const missingThemeFonts = themeFonts.filter((font) => !loadedFonts.includes(font));
|
||||||
|
|
||||||
if (!missingThemeFonts.length) {
|
if (!missingThemeFonts.length) {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import Tooltip from '@mui/material/Tooltip';
|
|||||||
import EditIcon from '@mui/icons-material/Edit';
|
import EditIcon from '@mui/icons-material/Edit';
|
||||||
import { bindDialog, usePopupState } from 'material-ui-popup-state/hooks';
|
import { bindDialog, usePopupState } from 'material-ui-popup-state/hooks';
|
||||||
import { ThemeModeContext } from '@/components/context/ThemeModeContext.tsx';
|
import { ThemeModeContext } from '@/components/context/ThemeModeContext.tsx';
|
||||||
import { AppTheme, loadThemeFonts } from '@/lib/ui/AppThemes.ts';
|
import { AppTheme, hasMissingFonts, loadThemeFonts } from '@/lib/ui/AppThemes.ts';
|
||||||
import { createTheme } from '@/theme.ts';
|
import { createTheme } from '@/theme.ts';
|
||||||
import { ThemeCreationDialog } from '@/screens/settings/appearance/theme/CreateThemeDialog.tsx';
|
import { ThemeCreationDialog } from '@/screens/settings/appearance/theme/CreateThemeDialog.tsx';
|
||||||
import { makeToast } from '@/components/util/Toast';
|
import { makeToast } from '@/components/util/Toast';
|
||||||
@@ -84,12 +84,15 @@ export const ThemePreview = ({ theme, onDelete }: { theme: AppTheme; onDelete: (
|
|||||||
backgroundColor: 'background.default',
|
backgroundColor: 'background.default',
|
||||||
}}
|
}}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
const needToLoadFonts = hasMissingFonts(theme.muiTheme);
|
||||||
|
if (!needToLoadFonts) {
|
||||||
|
setAppTheme(theme.id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
makeToast(t('settings.appearance.theme.select.fonts.loading'), 'info');
|
makeToast(t('settings.appearance.theme.select.fonts.loading'), 'info');
|
||||||
loadThemeFonts(theme.muiTheme)
|
loadThemeFonts(theme.muiTheme)
|
||||||
.then(() => {
|
.then(() => setAppTheme(theme.id))
|
||||||
setAppTheme(theme.id);
|
|
||||||
makeToast(t('settings.appearance.theme.select.success'), 'success');
|
|
||||||
})
|
|
||||||
.catch(() => makeToast(t('settings.appearance.theme.select.fonts.error'), 'error'));
|
.catch(() => makeToast(t('settings.appearance.theme.select.fonts.error'), 'error'));
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user