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(ui): Fix out of sync situations while setting themes or font sizes #231

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 18 additions & 29 deletions src/contexts/UIPreferencesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const PREFERENCES_URL = '/api/v1/ui-preferences';

export type Theme = "light" | "dark" | "pastel" | "ocean";
export type PageSize = 10 | 20 | 30 | 40 | 50 | 100;
export type fontSize = "fs-6" | "fs-5" | "fs-4";
export type FontSize = "fs-6" | "fs-5" | "fs-4";

export interface UIPreferences {
get pageSize(): PageSize
get theme(): Theme
get bytesStringBase2(): boolean
get defaultSnapshotViewAll(): boolean
get fontSize(): fontSize
get fontSize(): FontSize
setTheme: (theme: Theme) => void
setPageSize: (pageSize: number) => void
setByteStringBase: (bytesStringBase2: String) => void
Expand All @@ -29,7 +29,7 @@ interface SerializedUIPreferences {
bytesStringBase2?: boolean
defaultSnapshotView?: boolean
theme: Theme
fontSize: fontSize
fontSize: FontSize
}

export interface UIPreferenceProviderProps {
Expand Down Expand Up @@ -66,13 +66,9 @@ function normalizePageSize(pageSize: number): PageSize {

export function UIPreferenceProvider(props: UIPreferenceProviderProps) {
const [preferences, setPreferences] = useState(DEFAULT_PREFERENCES);
/**
*
* @param theme
* @returns
*/

const setTheme = useCallback((theme: Theme) => setPreferences(oldPreferences => {
syncTheme(theme, oldPreferences.theme);
syncTheme(theme, oldPreferences.fontSize);
return { ...oldPreferences, theme };
}), []);

Expand All @@ -89,8 +85,8 @@ export function UIPreferenceProvider(props: UIPreferenceProviderProps) {
return { ...oldPreferences, input };
});

const setFontSize = useCallback((fontSize: fontSize) => setPreferences(oldPreferences => {
syncFontSize(fontSize, oldPreferences.fontSize);
const setFontSize = useCallback((fontSize: FontSize) => setPreferences(oldPreferences => {
syncTheme(oldPreferences.theme, fontSize);
return { ...oldPreferences, fontSize };
}), []);

Expand All @@ -100,6 +96,9 @@ export function UIPreferenceProvider(props: UIPreferenceProviderProps) {
if (!storedPreferences.theme || (storedPreferences.theme as string) === "") {
storedPreferences.theme = getDefaultTheme();
}
if (!storedPreferences.fontSize || (storedPreferences.fontSize as string) === "") {
storedPreferences.fontSize = DEFAULT_PREFERENCES.fontSize
}
if (!storedPreferences.pageSize || storedPreferences.pageSize === 0) {
storedPreferences.pageSize = DEFAULT_PREFERENCES.pageSize;
} else {
Expand All @@ -120,29 +119,19 @@ export function UIPreferenceProvider(props: UIPreferenceProviderProps) {
}, [preferences]);

/**
* Synchronizes the selected theme with the html class
*
* @param theme
* The theme to be set
*/
const syncTheme = (newTheme: Theme, oldTheme: Theme) => {
var doc = document.querySelector("html")!;
if (!doc.classList.replace(oldTheme, newTheme)) {
doc.classList.add(newTheme)
}
}

/**
* Synchronizes the selected theme with the html class
* Synchronizes the theme as well as the font size with the class.
* To prevent any situations, where the theme or the font size is not in sync with the class,
* we first remove every element and set them again.
*
* @param theme
* The theme to be set
* @param fontSize
* The font size to be set
*/
const syncFontSize = (newFontSize: fontSize, oldFontSize: fontSize) => {
const syncTheme = (theme: Theme, fontSize: FontSize) => {
var doc = document.querySelector("html")!;
if (!doc.classList.replace(oldFontSize, newFontSize)) {
doc.classList.add(newFontSize)
}
doc.classList.remove(...doc.classList);
doc.classList.add(theme, fontSize)
}

const providedValue = { ...preferences, setTheme, setPageSize, setByteStringBase, setDefaultSnapshotViewAll, setFontSize } as UIPreferences;
Expand Down
Loading