Skip to content

Commit 8b6aa5d

Browse files
committed
feat: add computedAppTheme in user preferences
1 parent 56bcc58 commit 8b6aa5d

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/Shared/Providers/ThemeProvider/ThemeProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const ThemeProvider = ({ children }: ThemeProviderProps) => {
5050
}
5151

5252
const handleColorSchemeChange = () => {
53-
handleThemePreferenceChange(THEME_PREFERENCE_MAP.auto)
53+
handleThemePreferenceChange(THEME_PREFERENCE_MAP.auto, showThemeSwitcherDialog)
5454
}
5555

5656
useEffect(() => {
@@ -73,7 +73,7 @@ export const ThemeProvider = ({ children }: ThemeProviderProps) => {
7373
return () => {
7474
matchQuery.removeEventListener('change', handleColorSchemeChange)
7575
}
76-
}, [themeConfig.themePreference])
76+
}, [themeConfig.themePreference, showThemeSwitcherDialog])
7777

7878
const handleThemeSwitcherDialogVisibilityChange = (isVisible: boolean) => {
7979
setShowThemeSwitcherDialog(isVisible)

src/Shared/Services/common.service.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { ViewIsPipelineRBACConfiguredRadioTabs } from '@Shared/types'
18+
import { THEME_PREFERENCE_MAP } from '@Shared/Providers/ThemeProvider/types'
1819
import { get, getUrlWithSearchParams, post, ROUTES, showError } from '../../Common'
1920
import { USER_PREFERENCES_ATTRIBUTE_KEY } from './constants'
2021
import {
@@ -23,7 +24,8 @@ import {
2324
GetResourceApiUrlProps,
2425
GetUserPreferencesParsedDTO,
2526
GetUserPreferencesQueryParamsType,
26-
UpdateUserPreferencesParsedValueType,
27+
UpdatedUserPreferencesType,
28+
UserPreferencesPayloadValueType,
2729
UpdateUserPreferencesPayloadType,
2830
UserPreferencesType,
2931
} from './types'
@@ -58,20 +60,25 @@ export const getUserPreferences = async (): Promise<UserPreferencesType> => {
5860

5961
return {
6062
pipelineRBACViewSelectedTab,
61-
themePreference: parsedResult.themePreference,
63+
themePreference:
64+
parsedResult.computedAppTheme === 'system-dark' || parsedResult.computedAppTheme === 'system-light'
65+
? THEME_PREFERENCE_MAP.auto
66+
: parsedResult.computedAppTheme,
6267
}
6368
}
6469

6570
export const updateUserPreferences = async (
66-
updatedUserPreferences: UserPreferencesType,
71+
updatedUserPreferences: UpdatedUserPreferencesType,
6772
shouldThrowError: boolean = false,
6873
): Promise<boolean> => {
6974
try {
70-
const value: UpdateUserPreferencesParsedValueType = {
75+
const { themePreference, appTheme } = updatedUserPreferences
76+
77+
const value: UserPreferencesPayloadValueType = {
7178
viewPermittedEnvOnly:
7279
updatedUserPreferences.pipelineRBACViewSelectedTab ===
7380
ViewIsPipelineRBACConfiguredRadioTabs.ACCESS_ONLY,
74-
themePreference: updatedUserPreferences.themePreference,
81+
computedAppTheme: themePreference === THEME_PREFERENCE_MAP.auto ? `system-${appTheme}` : appTheme,
7582
}
7683

7784
const payload: UpdateUserPreferencesPayloadType = {

src/Shared/Services/types.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { MainContext } from '@Shared/Providers'
18-
import { ThemePreferenceType } from '@Shared/Providers/ThemeProvider/types'
18+
import { AppThemeType, ThemeConfigType, ThemePreferenceType } from '@Shared/Providers/ThemeProvider/types'
1919
import { getUrlWithSearchParams } from '../../Common'
2020
import { PolicyKindType, ResourceKindType, ResourceVersionType, ViewIsPipelineRBACConfiguredRadioTabs } from '../types'
2121
import { USER_PREFERENCES_ATTRIBUTE_KEY } from './constants'
@@ -63,18 +63,26 @@ export interface GetUserPreferencesQueryParamsType {
6363
export interface GetUserPreferencesParsedDTO {
6464
viewPermittedEnvOnly?: boolean
6565
/**
66-
* Preferred theme for the user
67-
* If null, would forcibly show user theme switcher dialog for user to select
66+
* Computed app theme for the user
67+
*
68+
* Could be 'light' | 'dark' | 'system-light' | 'system-dark'
6869
*/
69-
themePreference: ThemePreferenceType | null
70+
computedAppTheme: AppThemeType | `system-${AppThemeType}`
7071
}
7172

72-
export interface UpdateUserPreferencesParsedValueType extends GetUserPreferencesParsedDTO {}
73+
export interface UserPreferencesPayloadValueType extends GetUserPreferencesParsedDTO {}
7374

7475
export interface UpdateUserPreferencesPayloadType extends Pick<GetUserPreferencesQueryParamsType, 'key'> {
7576
value: string
7677
}
7778

78-
export interface UserPreferencesType extends Pick<GetUserPreferencesParsedDTO, 'themePreference'> {
79+
export interface UserPreferencesType {
80+
/**
81+
* Preferred theme for the user
82+
* If null, would forcibly show user theme switcher dialog for user to select
83+
*/
84+
themePreference: ThemePreferenceType | null
7985
pipelineRBACViewSelectedTab: ViewIsPipelineRBACConfiguredRadioTabs
8086
}
87+
88+
export interface UpdatedUserPreferencesType extends UserPreferencesType, Pick<ThemeConfigType, 'appTheme'> {}

0 commit comments

Comments
 (0)