Skip to content

Commit 455c45e

Browse files
authoredApr 2, 2025··
fix: allow to create the brand fallback urls when {} is defined in PARAGON_THEMES_URLS (#785)
1 parent e0656d1 commit 455c45e

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed
 

‎src/react/hooks/paragon/useParagonThemeUrls.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,42 @@ const useParagonThemeUrls = () => useMemo(() => {
102102

103103
// If we don't have the core default or any theme variants, use the PARAGON_THEME
104104
if (!coreCss.default || isEmptyObject(themeVariantsCss) || isEmptyObject(defaultThemeVariants)) {
105-
const localCoreUrl = PARAGON_THEME.paragon?.themeUrls?.core;
106-
const localThemeVariants = PARAGON_THEME.paragon?.themeUrls?.variants;
107-
const localDefaultThemeVariants = PARAGON_THEME.paragon?.themeUrls?.defaults;
105+
const localParagonCoreUrl = PARAGON_THEME?.paragon?.themeUrls?.core;
106+
const localParagonThemeVariants = PARAGON_THEME?.paragon?.themeUrls?.variants;
107+
const localParagonDefaultThemeVariants = PARAGON_THEME?.paragon?.themeUrls?.defaults;
108108

109-
if (isEmptyObject(localCoreUrl) || isEmptyObject(localThemeVariants)) {
109+
const localBrandCoreUrl = PARAGON_THEME?.brand?.themeUrls?.core;
110+
const localBrandThemeVariants = PARAGON_THEME?.brand?.themeUrls?.variants;
111+
const localBrandDefaultThemeVariants = PARAGON_THEME?.brand?.themeUrls?.defaults;
112+
113+
if (isEmptyObject(localParagonCoreUrl) || isEmptyObject(localParagonThemeVariants)) {
110114
return undefined;
111115
}
112116
if (!coreCss.default) {
113-
coreCss.default = fallbackThemeUrl(localCoreUrl?.fileName);
117+
coreCss.default = fallbackThemeUrl(localParagonCoreUrl?.fileName);
118+
}
119+
120+
if (!coreCss.brandOverride && localBrandCoreUrl) {
121+
coreCss.brandOverride = fallbackThemeUrl(localBrandCoreUrl?.fileName);
114122
}
115123

116124
if (isEmptyObject(themeVariantsCss)) {
117-
Object.entries(localThemeVariants).forEach(([themeVariant, { fileName, ...rest }]) => {
125+
Object.entries(localParagonThemeVariants).forEach(([themeVariant, { fileName, ...rest }]) => {
118126
themeVariantsCss[themeVariant] = {
119127
urls: { default: fallbackThemeUrl(fileName), ...rest.urls },
120128
};
121129
});
130+
131+
Object.entries(localBrandThemeVariants).forEach(([themeVariant, { fileName, ...rest }]) => {
132+
themeVariantsCss[themeVariant] = {
133+
urls: { brandOverride: fallbackThemeUrl(fileName), ...rest.urls, ...themeVariantsCss[themeVariant]?.urls },
134+
};
135+
});
122136
}
137+
123138
return {
124139
core: { urls: coreCss },
125-
defaults: defaultThemeVariants || localDefaultThemeVariants,
140+
defaults: defaultThemeVariants || { ...localParagonDefaultThemeVariants, ...localBrandDefaultThemeVariants },
126141
variants: themeVariantsCss,
127142
};
128143
}

‎src/react/hooks/paragon/useParagonThemeUrls.test.js

+47-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,53 @@ import { renderHook } from '@testing-library/react';
33
import useParagonThemeUrls from './useParagonThemeUrls';
44
import { mergeConfig } from '../../../config';
55

6+
Object.defineProperty(global, 'PARAGON_THEME', {
7+
value: {
8+
paragon: {
9+
version: '1.0.0',
10+
themeUrls: {
11+
core: {
12+
fileName: 'local-core.min.css',
13+
},
14+
defaults: {
15+
light: 'light',
16+
},
17+
variants: {
18+
light: {
19+
fileName: 'local-light.min.css',
20+
},
21+
},
22+
},
23+
},
24+
brand: {
25+
version: '1.0.0',
26+
themeUrls: {
27+
core: {
28+
fileName: 'brand-local-core.min.css',
29+
},
30+
defaults: {
31+
light: 'light',
32+
},
33+
variants: {
34+
light: {
35+
fileName: 'brand-local-light.min.css',
36+
},
37+
},
38+
},
39+
},
40+
},
41+
writable: true,
42+
});
43+
644
describe('useParagonThemeUrls', () => {
745
beforeEach(() => { jest.resetAllMocks(); });
846
it.each([
947
[undefined, undefined],
10-
[{}, { core: { urls: { default: '//localhost:8080/core.min.css', brandOverride: undefined } }, defaults: { light: 'light' }, variants: { light: { urls: { default: '//localhost:8080/light.min.css' } } } }],
48+
[{}, {
49+
core: { urls: { default: '//localhost:8080/local-core.min.css', brandOverride: '//localhost:8080/brand-local-core.min.css' } },
50+
defaults: { light: 'light' },
51+
variants: { light: { urls: { default: '//localhost:8080/local-light.min.css', brandOverride: '//localhost:8080/brand-local-light.min.css' } } },
52+
}],
1153
])('handles when `config.PARAGON_THEME_URLS` is not present (%s)', (paragonThemeUrls, expectedURLConfig) => {
1254
mergeConfig({ PARAGON_THEME_URLS: paragonThemeUrls });
1355
const { result } = renderHook(() => useParagonThemeUrls());
@@ -122,7 +164,7 @@ describe('useParagonThemeUrls', () => {
122164
expect.objectContaining({
123165
core: {
124166
urls: {
125-
default: '//localhost:8080/core.min.css',
167+
default: '//localhost:8080/local-core.min.css',
126168
brandOverride: 'brand-core.css',
127169
},
128170
},
@@ -132,7 +174,8 @@ describe('useParagonThemeUrls', () => {
132174
variants: {
133175
light: {
134176
urls: {
135-
default: '//localhost:8080/light.min.css',
177+
default: '//localhost:8080/local-light.min.css',
178+
brandOverride: '//localhost:8080/brand-local-light.min.css',
136179
},
137180
},
138181
},
@@ -155,7 +198,7 @@ describe('useParagonThemeUrls', () => {
155198
};
156199
const originalParagonTheme = global.PARAGON_THEME;
157200
Object.defineProperty(global, 'PARAGON_THEME', {
158-
value: 'mocked-theme',
201+
value: undefined,
159202
writable: true,
160203
});
161204
mergeConfig(config);

0 commit comments

Comments
 (0)
Please sign in to comment.