Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/devel' into SHELL-191-update-nod…
Browse files Browse the repository at this point in the history
…e-to-v18
  • Loading branch information
beawar committed Mar 7, 2024
2 parents b5e197a + cd45f6b commit ce269a0
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 14 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"@tinymce/tinymce-react": "^4.3.2",
"@zextras/carbonio-design-system": "^4.2.0",
"@zextras/carbonio-ui-preview": "^1.2.3",
"darkreader": "^4.9.77",
"darkreader": "^4.9.79",
"history": "^5.3.0",
"i18next": "^22.5.1",
"i18next-http-backend": "^2.5.0",
Expand Down
190 changes: 190 additions & 0 deletions src/boot/use-get-primary-color.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/

import React from 'react';

import { act, screen } from '@testing-library/react';
import { produce } from 'immer';

import { useGetPrimaryColor } from './use-get-primary-color';
import { DARK_READER_PROP_KEY, SHELL_APP_ID } from '../constants';
import * as useLocalStorage from '../shell/hooks/useLocalStorage';
import { useAccountStore } from '../store/account';
import { useLoginConfigStore } from '../store/login/store';
import { setup } from '../test/utils';

const PrimaryColorComponent = (): React.JSX.Element => {
const primary = useGetPrimaryColor();
return <div>{`color: ${primary}`}</div>;
};

describe('Use get primary color', () => {
it('should return the carbonioWebUiPrimaryColor config when available and dark mode is disabled', async () => {
const localStorageColor = '#aabbaa';
const mockUseLocalStorage = jest.spyOn(useLocalStorage, 'useLocalStorage');

mockUseLocalStorage.mockReturnValue([{ light: localStorageColor }, jest.fn()]);

const carbonioWebUiPrimaryColorConfig = '#bbbbbb';
const carbonioWebUiDarkPrimaryColorConfig = '#cccccc';
useLoginConfigStore.setState((s) => ({
...s,
carbonioWebUiPrimaryColor: carbonioWebUiPrimaryColorConfig,
carbonioWebUiDarkPrimaryColor: carbonioWebUiDarkPrimaryColorConfig,
loaded: true
}));

useAccountStore.setState(
produce((state) => {
state.settings.props = [
{ name: DARK_READER_PROP_KEY, zimlet: SHELL_APP_ID, _content: 'disabled' }
];
})
);

setup(<PrimaryColorComponent />);

expect(await screen.findByText(`color: ${carbonioWebUiPrimaryColorConfig}`)).toBeVisible();
});

it('should return the carbonioWebUiDarkPrimaryColor config when available and dark mode is enabled', async () => {
const localStorageColor = '#aabbaa';
const mockUseLocalStorage = jest.spyOn(useLocalStorage, 'useLocalStorage');

mockUseLocalStorage.mockReturnValue([{ light: localStorageColor }, jest.fn()]);

const carbonioWebUiPrimaryColorConfig = '#bbbbbb';
const carbonioWebUiDarkPrimaryColorConfig = '#cccccc';
useLoginConfigStore.setState((s) => ({
...s,
carbonioWebUiPrimaryColor: carbonioWebUiPrimaryColorConfig,
carbonioWebUiDarkPrimaryColor: carbonioWebUiDarkPrimaryColorConfig,
loaded: true
}));

useAccountStore.setState(
produce((state) => {
state.settings.props = [
{ name: DARK_READER_PROP_KEY, zimlet: SHELL_APP_ID, _content: 'enabled' }
];
})
);

setup(<PrimaryColorComponent />);

expect(await screen.findByText(`color: ${carbonioWebUiDarkPrimaryColorConfig}`)).toBeVisible();
});

it('should return the carbonioWebUiPrimaryColor config when available and carbonioWebUiDarkPrimaryColor is not available and dark mode is enabled', async () => {
const localStorageColor = '#aabbaa';
const mockUseLocalStorage = jest.spyOn(useLocalStorage, 'useLocalStorage');

mockUseLocalStorage.mockReturnValue([{ light: localStorageColor }, jest.fn()]);

const carbonioWebUiPrimaryColorConfig = '#bbbbbb';
useLoginConfigStore.setState((s) => ({
...s,
carbonioWebUiPrimaryColor: carbonioWebUiPrimaryColorConfig,
loaded: true
}));

useAccountStore.setState(
produce((state) => {
state.settings.props = [
{ name: DARK_READER_PROP_KEY, zimlet: SHELL_APP_ID, _content: 'enabled' }
];
})
);

setup(<PrimaryColorComponent />);

expect(await screen.findByText(`color: ${carbonioWebUiPrimaryColorConfig}`)).toBeVisible();
});

it('should return the carbonioWebUiDarkPrimaryColor config when available and carbonioWebUiPrimaryColor is not available and dark mode is disabled', async () => {
const localStorageColor = '#aabbaa';
const mockUseLocalStorage = jest.spyOn(useLocalStorage, 'useLocalStorage');

mockUseLocalStorage.mockReturnValue([{ light: localStorageColor }, jest.fn()]);

const carbonioWebUiDarkPrimaryColorConfig = '#cccccc';
useLoginConfigStore.setState((s) => ({
...s,
carbonioWebUiDarkPrimaryColor: carbonioWebUiDarkPrimaryColorConfig,
loaded: true
}));

useAccountStore.setState(
produce((state) => {
state.settings.props = [
{ name: DARK_READER_PROP_KEY, zimlet: SHELL_APP_ID, _content: 'disabled' }
];
})
);

setup(<PrimaryColorComponent />);

expect(await screen.findByText(`color: ${carbonioWebUiDarkPrimaryColorConfig}`)).toBeVisible();
});

it('should use localStore color as fallback until config color is received', async () => {
const localStorageColor = '#aabbaa';
const mockUseLocalStorage = jest.spyOn(useLocalStorage, 'useLocalStorage');

mockUseLocalStorage.mockReturnValue([{ light: localStorageColor }, jest.fn()]);

setup(<PrimaryColorComponent />);

expect(await screen.findByText(`color: ${localStorageColor}`)).toBeVisible();

const carbonioWebUiPrimaryColorConfig = '#bbbbbb';
const carbonioWebUiDarkPrimaryColorConfig = '#cccccc';
act(() => {
useLoginConfigStore.setState((s) => ({
...s,
carbonioWebUiPrimaryColor: carbonioWebUiPrimaryColorConfig,
carbonioWebUiDarkPrimaryColor: carbonioWebUiDarkPrimaryColorConfig,
loaded: true
}));

useAccountStore.setState(
produce((state) => {
state.settings.props = [
{ name: DARK_READER_PROP_KEY, zimlet: SHELL_APP_ID, _content: 'disabled' }
];
})
);
});
expect(await screen.findByText(`color: ${carbonioWebUiPrimaryColorConfig}`)).toBeVisible();
});

it('should use localStore color as fallback until configs are received and return undefined if carbonioWebUiPrimaryColor is not received', async () => {
const localStorageColor = '#aabbaa';
const mockUseLocalStorage = jest.spyOn(useLocalStorage, 'useLocalStorage');

mockUseLocalStorage.mockReturnValue([{ light: localStorageColor }, jest.fn()]);

setup(<PrimaryColorComponent />);

expect(await screen.findByText(`color: ${localStorageColor}`)).toBeVisible();

act(() => {
useLoginConfigStore.setState((s) => ({
...s,
loaded: true
}));

useAccountStore.setState(
produce((state) => {
state.settings.props = [
{ name: DARK_READER_PROP_KEY, zimlet: SHELL_APP_ID, _content: 'disabled' }
];
})
);
});
expect(await screen.findByText(`color: undefined`)).toBeVisible();
});
});
14 changes: 5 additions & 9 deletions src/boot/use-get-primary-color.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
import { useEffect, useMemo } from 'react';

import { useTheme } from '@zextras/carbonio-design-system';
import { size } from 'lodash';

import { LOCAL_STORAGE_LAST_PRIMARY_KEY } from '../constants';
Expand All @@ -17,9 +16,9 @@ export function useGetPrimaryColor(): string | undefined {
const [localStorageLastPrimary, setLocalStorageLastPrimary] = useLocalStorage<
Partial<Record<'dark' | 'light', string>> | undefined
>(LOCAL_STORAGE_LAST_PRIMARY_KEY, undefined);
const { carbonioWebUiPrimaryColor, carbonioWebUiDarkPrimaryColor } = useLoginConfigStore();
const { carbonioWebUiPrimaryColor, carbonioWebUiDarkPrimaryColor, loaded } =
useLoginConfigStore();
const { darkModeEnabled, darkReaderStatus } = useDarkMode();
const theme = useTheme();

const primaryColor = useMemo(() => {
if (
Expand All @@ -31,24 +30,21 @@ export function useGetPrimaryColor(): string | undefined {
}
return carbonioWebUiPrimaryColor || carbonioWebUiDarkPrimaryColor;
}
if (localStorageLastPrimary && size(localStorageLastPrimary) > 0) {
if (localStorageLastPrimary && size(localStorageLastPrimary) > 0 && !loaded) {
return (
(darkModeEnabled && (localStorageLastPrimary.dark || localStorageLastPrimary.light)) ||
localStorageLastPrimary.light ||
localStorageLastPrimary.dark
);
}
if (theme) {
return theme.palette.primary.regular;
}
return undefined;
}, [
carbonioWebUiDarkPrimaryColor,
carbonioWebUiPrimaryColor,
darkModeEnabled,
darkReaderStatus,
localStorageLastPrimary,
theme
loaded,
localStorageLastPrimary
]);

useEffect(() => {
Expand Down

0 comments on commit ce269a0

Please sign in to comment.