Skip to content

Commit a393816

Browse files
authored
Merge pull request #4081 from RedisInsight/fe/feature/RI-6156_Change_theme_for_proxy
#RIVS-6156 - Change theme key of local storage
2 parents 43e29ae + c9c590c commit a393816

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

redisinsight/ui/src/contexts/themeContext.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react'
2+
import { ipcThemeChange } from 'uiSrc/electron/utils'
23
import { BrowserStorageItem, Theme, THEMES, THEME_MATCH_MEDIA_DARK } from '../constants'
34
import { localStorageService, themeService } from '../services'
45

56
interface Props {
6-
children: React.ReactNode;
7+
children: React.ReactNode
78
}
89

910
const THEME_NAMES = THEMES.map(({ value }) => value)
@@ -36,14 +37,15 @@ export class ThemeProvider extends React.Component<Props> {
3637
}
3738
}
3839

39-
getSystemTheme = () => (window.matchMedia && window.matchMedia(THEME_MATCH_MEDIA_DARK).matches ? Theme.Dark : Theme.Light)
40+
getSystemTheme = () => (window.matchMedia?.(THEME_MATCH_MEDIA_DARK)?.matches ? Theme.Dark : Theme.Light)
4041

4142
changeTheme = (themeValue: any) => {
4243
let actualTheme = themeValue
4344
if (themeValue === Theme.System) {
4445
actualTheme = this.getSystemTheme()
4546
}
46-
window.app?.ipc?.invoke?.('theme:change', themeValue)
47+
48+
ipcThemeChange(themeValue)
4749

4850
this.setState({ theme: actualTheme, usingSystemTheme: themeValue === Theme.System }, () => {
4951
themeService.applyTheme(themeValue)

redisinsight/ui/src/electron/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import { ipcCheckUpdates, ipcSendEvents } from './ipcCheckUpdates'
22

33
export * from './ipcAuth'
44
export * from './ipcAppRestart'
5+
export * from './ipcThemeChange'
56

67
export { ipcCheckUpdates, ipcSendEvents }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { IpcInvokeEvent } from '../constants'
2+
3+
export const ipcThemeChange = async (value: string) => {
4+
await window.app?.ipc?.invoke?.(
5+
IpcInvokeEvent.themeChange,
6+
value,
7+
)
8+
}

redisinsight/ui/src/services/storage.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import { isObjectLike } from 'lodash'
2+
import { Maybe } from 'uiSrc/utils'
23
import BrowserStorageItem from '../constants/storage'
34

45
class StorageService {
56
private storage: Storage
67

7-
constructor(storage: Storage) {
8+
private envKey: Maybe<string>
9+
10+
constructor(storage: Storage, envKey?: string) {
811
this.storage = storage
12+
this.envKey = envKey
13+
}
14+
15+
private getKey(itemName: string): string {
16+
return this.envKey ? `${this.envKey}_${itemName}` : itemName
917
}
1018

1119
get(itemName: string = '') {
20+
const key = this.getKey(itemName)
1221
let item
1322
try {
14-
item = this.storage.getItem(itemName)
23+
item = this.storage.getItem(key)
1524
} catch (error) {
1625
console.error(`getItem from storage error: ${error}`)
1726
}
@@ -26,28 +35,28 @@ class StorageService {
2635
return null
2736
}
2837

29-
getAll() {
30-
return this.storage
31-
}
32-
3338
set(itemName: string = '', item: any) {
3439
try {
40+
const key = this.getKey(itemName)
3541
if (isObjectLike(item)) {
36-
this.storage.setItem(itemName, JSON.stringify(item))
42+
this.storage.setItem(key, JSON.stringify(item))
3743
} else {
38-
this.storage.setItem(itemName, item)
44+
this.storage.setItem(key, item)
3945
}
4046
} catch (error) {
4147
console.error(`setItem to storage error: ${error}`)
4248
}
4349
}
4450

4551
remove(itemName: string = '') {
46-
this.storage.removeItem(itemName)
52+
const key = this.getKey(itemName)
53+
this.storage.removeItem(key)
4754
}
4855
}
49-
export const localStorageService = new StorageService(localStorage)
50-
export const sessionStorageService = new StorageService(sessionStorage)
56+
const envKey = window.__RI_PROXY_PATH__
57+
58+
export const localStorageService = new StorageService(localStorage, envKey)
59+
export const sessionStorageService = new StorageService(sessionStorage, envKey)
5160

5261
export const getObjectStorageField = (itemName = '', field = '') => {
5362
try {

redisinsight/ui/src/services/theme.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ class ThemeService {
1313
}
1414

1515
applyTheme(newTheme: Theme) {
16-
const actualTheme = newTheme
16+
let actualTheme = newTheme
17+
1718
if (newTheme === Theme.System) {
18-
if (window.matchMedia && window.matchMedia(THEME_MATCH_MEDIA_DARK).matches) {
19-
newTheme = Theme.Dark
19+
if (window.matchMedia?.(THEME_MATCH_MEDIA_DARK)?.matches) {
20+
actualTheme = Theme.Dark
2021
} else {
21-
newTheme = Theme.Light
22+
actualTheme = Theme.Light
2223
}
2324
}
2425

2526
const sheet = new CSSStyleSheet()
26-
sheet?.replaceSync(this.themes[newTheme])
27+
sheet?.replaceSync(this.themes[actualTheme])
2728

2829
document.adoptedStyleSheets = [sheet]
2930

3031
localStorageService.set(BrowserStorageItem.theme, actualTheme)
31-
document.body.classList.value = `theme_${newTheme}`
32+
document.body.classList.value = `theme_${actualTheme}`
3233
}
3334

3435
static getTheme() {

0 commit comments

Comments
 (0)