Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 { 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 @@
[FeatureFlags.cloudSso]: cloudSsoFeature,
[FeatureFlags.envDependent]: envDependentFeature,
} = useSelector(appFeatureFlagsFeaturesSelector)
const { changeTheme } = useContext(ThemeContext)
const { pathname } = useLocation()

const dispatch = useDispatch()
Expand Down Expand Up @@ -210,8 +208,8 @@

const checkAndSetTheme = () => {
const theme = config?.theme
if (theme && localStorageService.get(BrowserStorageItem.theme) !== theme)
themeService.applyTheme(theme as Theme)
changeTheme(theme)

Check warning on line 212 in redisinsight/ui/src/components/config/Config.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 212 in redisinsight/ui/src/components/config/Config.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
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
24 changes: 19 additions & 5 deletions redisinsight/ui/src/contexts/themeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@

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,14 +49,20 @@
constructor(props: any) {
super(props)

const queryTheme = getQueryTheme()
const storedThemeValue = localStorageService.get(BrowserStorageItem.theme)
const theme =
!storedThemeValue || !THEME_NAMES.includes(storedThemeValue)
? defaultState.theme
: storedThemeValue

let theme = defaultState.theme

if (queryTheme) {
theme = queryTheme

Check warning on line 58 in redisinsight/ui/src/contexts/themeContext.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} else if (storedThemeValue && THEME_NAMES.includes(storedThemeValue)) {

Check warning on line 59 in redisinsight/ui/src/contexts/themeContext.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
theme = storedThemeValue

Check warning on line 60 in redisinsight/ui/src/contexts/themeContext.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 61 in redisinsight/ui/src/contexts/themeContext.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 61 in redisinsight/ui/src/contexts/themeContext.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const usingSystemTheme = theme === Theme.System

themeService.applyTheme(theme)
themeService.applyTheme(theme as Theme)

this.state = {
theme: theme === Theme.System ? this.getSystemTheme() : theme,
Expand Down
Loading