insomnia/packages/insomnia-app/app/ui/hooks/use-settings-side-effects.ts
Jack Kavanagh 3947bdc4aa
chore: move electron out of the renderer (#4406)
* fix lint

* fix types

* remove flush db.change event send

* guard against tests running electron functions

* copy preload to build output

* fix webview context menu

* fix context menu and plugin install

* move installPlugin to main context
2022-02-16 18:28:23 +00:00

57 lines
1.9 KiB
TypeScript

import { useEffect, useLayoutEffect } from 'react';
import { useSelector } from 'react-redux';
import { usePrevious } from 'react-use';
import { Settings } from '../../models/settings';
import { selectSettings } from '../redux/selectors';
const useRestartSetting = (setting: keyof Settings) => {
const settings = useSelector(selectSettings);
const nextValue = settings[setting];
const previousValue = usePrevious(nextValue);
useEffect(() => {
// for the first value only, the return of `usePrevious` is `undefined` since there's no "previous" value at the time of the first value.
if (previousValue === undefined) {
return;
}
// there's not been a change, so no need to take any action
if (nextValue === previousValue) {
return;
}
window.main.restart();
}, [nextValue, previousValue]);
};
const updateFontStyle = (key: string, value: string | null) => document?.querySelector('html')?.style.setProperty(key, value);
// as a general rule, if the body effect in this file is more than one line, extract into a separate function.
export const useSettingsSideEffects = () => {
const settings = useSelector(selectSettings);
useLayoutEffect(() => {
updateFontStyle('--font-default', settings.fontInterface);
}, [settings.fontInterface]);
useLayoutEffect(() => {
updateFontStyle('--font-monospace', settings.fontMonospace);
}, [settings.fontMonospace]);
useLayoutEffect(() => {
updateFontStyle('--font-ligatures', settings.fontVariantLigatures ? 'normal' : 'none');
}, [settings.fontVariantLigatures]);
useLayoutEffect(() => {
updateFontStyle('font-size', `${settings.fontSize}px`);
}, [settings.fontSize]);
useEffect(() => {
window.main.setMenuBarVisibility(!settings.autoHideMenuBar);
}, [settings.autoHideMenuBar]);
useRestartSetting('nunjucksPowerUserMode');
useRestartSetting('showVariableSourceAndValue');
};