Skip to content

Commit 0e24329

Browse files
committed
Removed ThemeRegistration mapping
#1504
1 parent 9ca3b4b commit 0e24329

5 files changed

Lines changed: 21 additions & 98 deletions

File tree

packages/components/src/theme/ThemeModel.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,3 @@ export interface ThemeRegistrationData {
2929
base: ThemeData[];
3030
custom: ThemeData[];
3131
}
32-
33-
export interface ThemeRegistrationStorageData {
34-
base: Map<string, ThemeData>;
35-
custom: Map<string, ThemeData>;
36-
}

packages/components/src/theme/ThemeUtils.test.ts

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
DEFAULT_PRELOAD_DATA_VARIABLES,
33
ThemeData,
4-
ThemeRegistrationStorageData,
4+
ThemeRegistrationData,
55
THEME_CACHE_LOCAL_STORAGE_KEY,
66
} from './ThemeModel';
77
import {
@@ -10,7 +10,6 @@ import {
1010
getDefaultBaseThemes,
1111
getThemeKey,
1212
getThemePreloadData,
13-
mapThemeRegistrationData,
1413
preloadTheme,
1514
setThemePreloadData,
1615
} from './ThemeUtils';
@@ -62,21 +61,18 @@ describe('getActiveThemes', () => {
6261
},
6362
} satisfies Record<string, ThemeData>;
6463

65-
const themeRegistration: ThemeRegistrationStorageData = {
66-
base: new Map([[mockTheme.base.themeKey, mockTheme.base]]),
67-
custom: new Map([[mockTheme.custom.themeKey, mockTheme.custom]]),
64+
const themeRegistration: ThemeRegistrationData = {
65+
base: [mockTheme.base],
66+
custom: [mockTheme.custom],
6867
};
6968

7069
it.each([null, mockTheme.customInvalid])(
7170
'should throw if base theme not found',
7271
customTheme => {
7372
expect(() =>
7473
getActiveThemes(customTheme?.themeKey ?? 'mock.themeKey', {
75-
base: new Map(),
76-
custom:
77-
customTheme == null
78-
? new Map()
79-
: new Map([[customTheme.themeKey, customTheme]]),
74+
base: [],
75+
custom: customTheme == null ? [] : [customTheme],
8076
})
8177
).toThrowError(`Default base theme 'default-dark' is not registered`);
8278
}
@@ -153,55 +149,6 @@ describe('getThemePreloadData', () => {
153149
);
154150
});
155151

156-
describe('mapThemeRegistrationData', () => {
157-
const baseThemeA: ThemeData = {
158-
name: 'Default Dark',
159-
themeKey: 'default-dark',
160-
styleContent: 'mock.base.styleContent',
161-
};
162-
163-
const baseThemeB: ThemeData = {
164-
name: 'Default Light',
165-
themeKey: 'default-light',
166-
styleContent: 'mock.base.styleContent',
167-
};
168-
169-
const customThemeA: ThemeData = {
170-
name: 'mock.custom.A',
171-
baseThemeKey: 'default-dark',
172-
themeKey: 'mock.custom.a',
173-
styleContent: 'mock.custom.styleContent',
174-
};
175-
176-
const customThemeB: ThemeData = {
177-
name: 'mock.custom.B',
178-
baseThemeKey: 'default-light',
179-
themeKey: 'mock.custom.b',
180-
styleContent: 'mock.custom.styleContent',
181-
};
182-
183-
it('should map theme registration data to storage data', () => {
184-
const base = [baseThemeA, baseThemeB];
185-
const custom = [customThemeA, customThemeB];
186-
187-
const actual = mapThemeRegistrationData({
188-
base,
189-
custom,
190-
});
191-
192-
expect(actual).toEqual({
193-
base: new Map([
194-
[baseThemeA.themeKey, baseThemeA],
195-
[baseThemeB.themeKey, baseThemeB],
196-
]),
197-
custom: new Map([
198-
[customThemeA.themeKey, customThemeA],
199-
[customThemeB.themeKey, customThemeB],
200-
]),
201-
});
202-
});
203-
});
204-
205152
describe('preloadTheme', () => {
206153
it.each([
207154
null,

packages/components/src/theme/ThemeUtils.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
ThemePreloadData,
1111
ThemePreloadStyleContent,
1212
ThemeRegistrationData,
13-
ThemeRegistrationStorageData,
1413
THEME_CACHE_LOCAL_STORAGE_KEY,
1514
} from './ThemeModel';
1615

@@ -40,19 +39,25 @@ export function calculatePreloadStyleContent(): ThemePreloadStyleContent {
4039
*/
4140
export function getActiveThemes(
4241
themeKey: string,
43-
themeRegistration: ThemeRegistrationStorageData
42+
themeRegistration: ThemeRegistrationData
4443
): [ThemeData] | [ThemeData, ThemeData] {
45-
const custom = themeRegistration.custom.get(themeKey);
44+
const custom = themeRegistration.custom.find(
45+
theme => theme.themeKey === themeKey
46+
);
4647

4748
const baseThemeKey = custom?.baseThemeKey ?? themeKey;
4849

49-
let base = themeRegistration.base.get(baseThemeKey);
50+
let base = themeRegistration.base.find(
51+
theme => theme.themeKey === baseThemeKey
52+
);
5053

5154
if (base == null) {
5255
log.error(
5356
`No registered base theme found for theme key: '${baseThemeKey}'`
5457
);
55-
base = themeRegistration.base.get(DEFAULT_DARK_THEME_KEY);
58+
base = themeRegistration.base.find(
59+
theme => theme.themeKey === DEFAULT_DARK_THEME_KEY
60+
);
5661

5762
assertNotNull(
5863
base,
@@ -99,21 +104,6 @@ export function getThemePreloadData(): ThemePreloadData | null {
99104
return null;
100105
}
101106

102-
/**
103-
* Map theme registration data to storage data.
104-
* @param themeRegistrationData
105-
*/
106-
export function mapThemeRegistrationData(
107-
themeRegistrationData: ThemeRegistrationData
108-
): ThemeRegistrationStorageData {
109-
const { base, custom } = themeRegistrationData;
110-
111-
return {
112-
base: new Map(base.map(theme => [theme.themeKey, theme])),
113-
custom: new Map(custom.map(theme => [theme.themeKey, theme])),
114-
};
115-
}
116-
117107
/**
118108
* Store theme preload data in local storage.
119109
* @param preloadData The preload data to set

packages/components/src/theme/useInitializeThemeContextValue.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import {
1010
calculatePreloadStyleContent,
1111
getActiveThemes,
12-
mapThemeRegistrationData,
1312
setThemePreloadData,
1413
} from './ThemeUtils';
1514

@@ -67,7 +66,7 @@ describe('useInitializeThemeContextValue', () => {
6766

6867
expect(getActiveThemes).toHaveBeenCalledWith(
6968
expectedThemeKey,
70-
mapThemeRegistrationData(themeRegistration)
69+
themeRegistration
7170
);
7271

7372
expect(setThemePreloadData).toHaveBeenCalledWith({

packages/components/src/theme/useInitializeThemeContextValue.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { useCallback, useEffect, useMemo, useState } from 'react';
22
import Log from '@deephaven/log';
33
import { ThemeContextValue } from './ThemeContext';
4-
import {
5-
DEFAULT_DARK_THEME_KEY,
6-
ThemeRegistrationData,
7-
ThemeRegistrationStorageData,
8-
} from './ThemeModel';
4+
import { DEFAULT_DARK_THEME_KEY, ThemeRegistrationData } from './ThemeModel';
95
import {
106
calculatePreloadStyleContent,
117
getActiveThemes,
128
getThemePreloadData,
13-
mapThemeRegistrationData,
149
setThemePreloadData,
1510
} from './ThemeUtils';
1611

@@ -25,7 +20,7 @@ export function useInitializeThemeContextValue(): ThemeContextValue {
2520
);
2621

2722
const [themeRegistration, setThemeRegistration] =
28-
useState<ThemeRegistrationStorageData | null>(null);
23+
useState<ThemeRegistrationData | null>(null);
2924

3025
const activeThemes = useMemo(
3126
() =>
@@ -40,11 +35,8 @@ export function useInitializeThemeContextValue(): ThemeContextValue {
4035
*/
4136
const registerThemes = useCallback(
4237
(themeRegistrationData: ThemeRegistrationData) => {
43-
const themeRegistrationStorage = mapThemeRegistrationData(
44-
themeRegistrationData
45-
);
46-
log.debug('Registering themes', themeRegistrationStorage);
47-
setThemeRegistration(themeRegistrationStorage);
38+
log.debug('Registering themes', themeRegistrationData);
39+
setThemeRegistration(themeRegistrationData);
4840
},
4941
[]
5042
);

0 commit comments

Comments
 (0)