Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

finish redux integration

kruzhambusd87a819b2f49
diff --git a/src/redux/reducers/ui/index.ts b/src/redux/reducers/ui/index.ts
index 33bcb71a0..e3ff264fd 100644
--- a/src/redux/reducers/ui/index.ts
+++ b/src/redux/reducers/ui/index.ts
@@ -1,11 +1,15 @@
import { alertsActions } from './alerts'
import { cacheActions } from './cache'
import { miscActions } from './misc'
+import { projectsActions } from './projects'
+import { themeActions } from './theme'
const UIActions = {
...alertsActions,
...cacheActions,
...miscActions,
+ ...projectsActions,
+ ...themeActions,
}
export default UIActions
diff --git a/src/redux/reducers/ui/theme.ts b/src/redux/reducers/ui/theme.ts
index e69de29bb..f8b298b7a 100644
--- a/src/redux/reducers/ui/theme.ts
+++ b/src/redux/reducers/ui/theme.ts
@@ -0,0 +1,66 @@
+import { createSlice, PayloadAction } from '@reduxjs/toolkit'
+import _includes from 'lodash/includes'
+import { LS_THEME_SETTING, SUPPORTED_THEMES, THEME_TYPE } from 'redux/constants'
+
+const setThemeToDOM = (theme: string) => {
+ const root = window.document.documentElement
+ const isDark = theme === 'dark'
+
+ root.classList.remove(isDark ? 'light' : 'dark')
+ root.classList.add(theme)
+}
+
+const setTheme = (theme: string): string => {
+ setThemeToDOM(theme)
+ localStorage.setItem(LS_THEME_SETTING, theme)
+ return theme
+}
+
+const getInitialTheme = (): string => {
+ if (typeof window !== 'undefined' && window.localStorage) {
+ const lsTheme: any = window.localStorage.getItem(LS_THEME_SETTING)
+ if (_includes(SUPPORTED_THEMES, lsTheme)) {
+ setThemeToDOM(lsTheme)
+ return lsTheme
+ }
+
+ const userMedia = window.matchMedia('(prefers-color-scheme: dark)')
+ if (userMedia.matches) {
+ setThemeToDOM('dark')
+ return 'dark'
+ }
+ }
+
+ setThemeToDOM('light')
+ return 'light' // light theme as the default
+}
+
+interface IInitialState {
+ theme: string
+ type: string
+}
+
+const getInitialState = (): IInitialState => {
+ return {
+ theme: getInitialTheme(),
+ type: THEME_TYPE.classic,
+ }
+}
+
+const themeSlice = createSlice({
+ name: 'theme',
+ initialState: getInitialState(),
+ reducers: {
+ setTheme(state, { payload }: PayloadAction<string>) {
+ setTheme(payload)
+ state.theme = payload
+ },
+ setThemeType(state, { payload }: PayloadAction<string>) {
+ state.type = payload
+ },
+ },
+})
+
+export const themeActions = themeSlice.actions
+
+export default themeSlice.reducer