Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions redisinsight/ui/src/components/config/Config.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { useEffect } from 'react'
import { useContext, useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { useLocation } from 'react-router-dom'
import { isNumber } from 'lodash'
import { BrowserStorageItem, FeatureFlags, Theme } from 'uiSrc/constants'
import { BuildType } from 'uiSrc/constants/env'
import { BUILD_FEATURES } from 'uiSrc/constants/featuresHighlighting'
import {
localStorageService,
setObjectStorage,
themeService,
} from 'uiSrc/services'
import { localStorageService, setObjectStorage } from 'uiSrc/services'

import {
appFeatureFlagsFeaturesSelector,
Expand Down Expand Up @@ -44,6 +40,7 @@ import { fetchProfile } from 'uiSrc/slices/oauth/cloud'
import { fetchDBSettings } from 'uiSrc/slices/app/db-settings'
import { connectedInstanceSelector } from 'uiSrc/slices/instances/instances'
import { DatabaseSettingsData } from 'uiSrc/slices/interfaces'
import { ThemeContext } from 'uiSrc/contexts/themeContext'

const SETTINGS_PAGE_PATH = '/settings'
const Config = () => {
Expand All @@ -54,6 +51,7 @@ const Config = () => {
[FeatureFlags.cloudSso]: cloudSsoFeature,
[FeatureFlags.envDependent]: envDependentFeature,
} = useSelector(appFeatureFlagsFeaturesSelector)
const { changeTheme } = useContext(ThemeContext)
const { pathname } = useLocation()

const dispatch = useDispatch()
Expand Down Expand Up @@ -211,7 +209,7 @@ const Config = () => {
const checkAndSetTheme = () => {
const theme = config?.theme
if (theme && localStorageService.get(BrowserStorageItem.theme) !== theme)
themeService.applyTheme(theme as Theme)
changeTheme(theme)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing theme via changeTheme is the correct approach as it updates the theme provider and calls themeService.applyTheme internally

}

return null
Expand Down
15 changes: 13 additions & 2 deletions redisinsight/ui/src/contexts/themeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ interface Props {

const THEME_NAMES = THEMES.map(({ value }) => value)

const getQueryTheme = () => {
const queryThemeParam = new URLSearchParams(window.location.search)
.get('theme')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: It's not directly related to your changes, but I would suggest putting this "magic string" behind an enum.

We have similar logic in about 15 other places in the codebase where we use the same approach. I believe having a centralized location where we can view a list of all the available query parameters that Redis Insight accepts and handles will be beneficial. It will be both cleaner and also better in terms of self-explanatory "code documentation".

If you find this advice useful, we can log a separate tech debt ticket with low priority and reference it outside the scope of this change.

?.toUpperCase()

return THEMES.find(({ value }) => value === queryThemeParam)?.value
}

export const defaultState = {
theme: DEFAULT_THEME || Theme.System,
usingSystemTheme:
Expand All @@ -41,11 +49,14 @@ export class ThemeProvider extends React.Component<Props> {
constructor(props: any) {
super(props)

// theme query param can be used to override the local/default theme
const queryTheme = getQueryTheme()
const storedThemeValue = localStorageService.get(BrowserStorageItem.theme)
const theme =
!storedThemeValue || !THEME_NAMES.includes(storedThemeValue)
queryTheme ||
(!storedThemeValue || !THEME_NAMES.includes(storedThemeValue)
? defaultState.theme
: storedThemeValue
: storedThemeValue)
const usingSystemTheme = theme === Theme.System

themeService.applyTheme(theme)
Expand Down
Loading