Skip to content

Commit

Permalink
fix: apply dark mode preview when changed to initial value
Browse files Browse the repository at this point in the history
refs: IRIS-3481 (#163)
  • Loading branch information
CataldoMazzilli authored Nov 21, 2022
1 parent e75dca6 commit e99e80f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const EMAIL_VALIDATION_REGEX =

export const ROOT_NAME = 'USER_ROOT';

export const DR_VALUES = ['auto', 'enabled', 'disabled'];
export const DARK_READER_VALUES = ['auto', 'enabled', 'disabled'] as const;

export const FOLDER_VIEW = {
search_folder: 'search folder',
Expand Down
27 changes: 16 additions & 11 deletions src/settings/components/general-settings/appearance-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { FormSubSection, Select } from '@zextras/carbonio-design-system';
import { FormSubSection, Select, SelectProps } from '@zextras/carbonio-design-system';
import { find } from 'lodash';
import React, { FC, useCallback, useContext, useMemo } from 'react';
import { ThemeCallbacksContext } from '../../../boot/theme-provider';
import { DR_VALUES, SHELL_APP_ID } from '../../../constants';
import { SHELL_APP_ID } from '../../../constants';
import { getT } from '../../../store/i18n';
import { themeSubSection } from '../../general-settings-sub-sections';
import { useDarkReaderResultValue } from '../../../custom-hooks/useDarkReaderResultValue';
import { DarkReaderPropValues, isDarkReaderPropValues } from '../../../../types';

const AppearanceSettings: FC<{
addMod: (type: 'prefs' | 'props', key: string, value: { value: any; app: string }) => void;
}> = ({ addMod }) => {
removeMod: (type: 'prefs' | 'props', key: string) => void;
}> = ({ addMod, removeMod }) => {
const { setDarkReaderState } = useContext(ThemeCallbacksContext);

const darkReaderResultValue = useDarkReaderResultValue();

const t = getT();
const items = useMemo(
const items = useMemo<Array<{ label: string; value: DarkReaderPropValues }>>(
() => [
{
label: t('settings.general.theme_auto', 'Auto'),
Expand All @@ -42,14 +44,18 @@ const AppearanceSettings: FC<{
() => find(items, { value: darkReaderResultValue }),
[darkReaderResultValue, items]
);
const onSelectionChange = useCallback(
(v) => {
if (DR_VALUES.includes(v) && v !== darkReaderResultValue) {
setDarkReaderState(v);
addMod('props', 'zappDarkreaderMode', { app: SHELL_APP_ID, value: v });
const onSelectionChange = useCallback<NonNullable<SelectProps['onChange']>>(
(value) => {
if (isDarkReaderPropValues(value)) {
setDarkReaderState(value);
if (value !== darkReaderResultValue) {
addMod('props', 'zappDarkreaderMode', { app: SHELL_APP_ID, value });
} else {
removeMod('props', 'zappDarkreaderMode');
}
}
},
[addMod, darkReaderResultValue, setDarkReaderState]
[addMod, darkReaderResultValue, removeMod, setDarkReaderState]
);
const subSection = useMemo(() => themeSubSection(t), [t]);
return (
Expand All @@ -62,7 +68,6 @@ const AppearanceSettings: FC<{
<Select
items={items}
selection={defaultSelection}
defaultSelection={defaultSelection}
label={t('settings.general.dark_mode', 'Dark Mode')}
onChange={onSelectionChange}
/>
Expand Down
22 changes: 20 additions & 2 deletions src/settings/general-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Container, useSnackbar } from '@zextras/carbonio-design-system';
import { includes, isEmpty } from 'lodash';
import { includes, isEmpty, size } from 'lodash';
import React, { FC, useCallback, useMemo, useState } from 'react';
import { Mods } from '../../types';
import { editSettings } from '../network/edit-settings';
Expand Down Expand Up @@ -34,6 +34,24 @@ const GeneralSettings: FC = () => {
}
}));
}, []);
// TODO update type
const removeMod = useCallback((type: 'props' | 'prefs', key: string) => {
setMods((prevState) => {
const prevType = prevState[type];
if (prevType && prevType[key] !== undefined) {
const nextState = { ...prevState, [type]: { ...prevState[type] } };
const nextType = nextState[type];
if (nextType && nextType[key] !== undefined) {
delete nextType[key];
}
if (size(nextState[type]) === 0) {
delete nextState[type];
}
return nextState;
}
return prevState;
});
}, []);
const createSnackbar = useSnackbar();

const onSave = useCallback(() => {
Expand Down Expand Up @@ -78,7 +96,7 @@ const GeneralSettings: FC = () => {
padding={{ all: 'medium' }}
style={{ overflow: 'auto' }}
>
<AppearanceSettings addMod={addMod} />
<AppearanceSettings addMod={addMod} removeMod={removeMod} />
<LanguageAndTimeZoneSettings
settings={settings}
addMod={addMod}
Expand Down
3 changes: 2 additions & 1 deletion src/store/login/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export const useLoginConfigStore = create<LoginConfigStore>(() => ({
carbonioWebUIDarkMode: undefined,
carbonioWebUiAppLogo: undefined,
carbonioWebUiDarkAppLogo: undefined,
carbonioWebUiTitle: undefined
carbonioWebUiTitle: undefined,
carbonioWebUiFavicon: undefined
}));
12 changes: 11 additions & 1 deletion types/misc/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Store } from '@reduxjs/toolkit';
import { To } from 'history';
import { ComponentType } from 'react';
import { CarbonioModule, PanelMode } from '../apps';
import { DARK_READER_VALUES } from '../../src/constants';

// eslint-disable-next-line no-shadow
export enum JSNS {
Expand All @@ -18,7 +19,16 @@ export enum JSNS {
SYNC = 'urn:zimbraSync'
}

export type DarkReaderPropValues = 'auto' | 'enabled' | 'disabled';
export type DarkReaderPropValues = typeof DARK_READER_VALUES[number];

export function isDarkReaderPropValues(
maybeDarkReaderPropValue: unknown
): maybeDarkReaderPropValue is DarkReaderPropValues {
return (
typeof maybeDarkReaderPropValue === 'string' &&
DARK_READER_VALUES.includes(maybeDarkReaderPropValue)
);
}

// eslint-disable-next-line @typescript-eslint/ban-types
export type PackageDependentFunction = (app: string) => Function;
Expand Down

0 comments on commit e99e80f

Please sign in to comment.