1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/frontend/src/hooks/useThemeMode.ts
andreas-unleash 35d9a62d89
Soft landing page on projects or last viewed project (#2499)
Signed-off-by: andreas-unleash <andreas@getunleash.ai>

This PR introduces a soft landing page to the last viewed project or to
the project list (if there is more than 1 project)

Changes: 
- Replaced clearing of `storage` with clearing `cache` in logout.ts ::
REVERTED
- Root redirects to `projects` instead of `features`

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->

## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
2022-11-23 15:58:02 +02:00

41 lines
1.2 KiB
TypeScript

import UIContext, { themeMode } from 'contexts/UIContext';
import { useContext } from 'react';
import { setLocalStorageItem } from 'utils/storage';
import mainTheme from 'themes/theme';
import darkTheme from 'themes/dark-theme';
import { Theme } from '@emotion/react';
import useUiConfig from './api/getters/useUiConfig/useUiConfig';
interface IUseThemeModeOutput {
resolveTheme: () => Theme;
onSetThemeMode: () => void;
themeMode: themeMode;
}
export const useThemeMode = (): IUseThemeModeOutput => {
const { themeMode, setThemeMode } = useContext(UIContext);
const { uiConfig } = useUiConfig();
const key = `${uiConfig.baseUriPath}:unleash-theme`;
const resolveTheme = () => {
if (themeMode === 'light') {
return mainTheme;
}
return darkTheme;
};
const onSetThemeMode = () => {
setThemeMode((prev: themeMode) => {
if (prev === 'light') {
setLocalStorageItem(key, 'dark');
return 'dark';
}
setLocalStorageItem(key, 'light');
return 'light';
});
};
return { resolveTheme, onSetThemeMode, themeMode };
};