Skip to content

Commit

Permalink
Allow v8/v9 as default setting (#9429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maosaic authored Feb 27, 2025
1 parent ca8f0b1 commit 14a6cfa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
10 changes: 10 additions & 0 deletions packages/osd-ui-shared-deps/theme_config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export const themeVersionLabelMap: Record<string, string>;
*/
export const themeVersionValueMap: Record<string, string>;

/**
* List of theme schema values
*/
export const themeSchemaValues: string[];

/**
* List of theme options
*/
export const themeOptions: string[];

/**
* Theme CSS distributable filenames by themeVersion and themeMode
* Note: used by bootstrap template
Expand Down
9 changes: 9 additions & 0 deletions packages/osd-ui-shared-deps/theme_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ const THEME_VERSION_VALUE_MAP = {
const THEME_VERSIONS = Object.keys(THEME_VERSION_LABEL_MAP);
const THEME_TAGS = THEME_VERSIONS.flatMap((v) => THEME_MODES.map((m) => `${v}${m}`));

// Setup theme options to be backwards compatible with the fact that v8 was persisted with its
// label rather than with the correct themeVersion value
const THEME_SCHEMA_VALUES = THEME_VERSIONS.concat(THEME_VERSION_LABEL_MAP.v8);
const THEME_OPTIONS = THEME_VERSIONS.map((v) => (v !== 'v8' ? v : THEME_VERSION_LABEL_MAP.v8));

exports.themeVersionLabelMap = THEME_VERSION_LABEL_MAP;

exports.themeVersionValueMap = THEME_VERSION_VALUE_MAP;

exports.themeTags = THEME_TAGS;

exports.themeSchemaValues = THEME_SCHEMA_VALUES;

exports.themeOptions = THEME_OPTIONS;

exports.themeCssDistFilenames = THEME_VERSIONS.reduce((map, v) => {
map[v] = THEME_MODES.reduce((acc, m) => {
acc[m] = `osd-ui-shared-deps.${v}.${m}.css`;
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/ui_settings/settings/theme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe('theme settings', () => {

it('should only accept valid values', () => {
expect(() => validate('v7')).not.toThrow();
expect(() => validate('v8')).not.toThrow();
expect(() => validate('v9')).not.toThrow();
expect(() => validate('Next (preview)')).not.toThrow();
expect(() => validate('v12')).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
Expand Down
12 changes: 3 additions & 9 deletions src/core/server/ui_settings/settings/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,11 @@

import { schema } from '@osd/config-schema';
import { i18n } from '@osd/i18n';
import { themeVersionLabelMap } from '@osd/ui-shared-deps';
import { themeVersionLabelMap, themeSchemaValues, themeOptions } from '@osd/ui-shared-deps';
import type { Type } from '@osd/config-schema';
import { UiSettingsParams } from '../../../types';
import { DEFAULT_THEME_VERSION } from '../ui_settings_config';

// Setup theme options to be backwards compatible with the fact that v8 was persisted with its
// label rather than with the correct themeVersion value
const THEME_VERSIONS = Object.keys(themeVersionLabelMap);
const THEME_SCHEMA_VALUES = THEME_VERSIONS.concat(themeVersionLabelMap.v8);
const THEME_OPTIONS = THEME_VERSIONS.map((v) => (v !== 'v8' ? v : themeVersionLabelMap.v8));

export const getThemeSettings = (): Record<string, UiSettingsParams> => {
return {
'theme:enableUserControl': {
Expand Down Expand Up @@ -79,7 +73,7 @@ export const getThemeSettings = (): Record<string, UiSettingsParams> => {
? themeVersionLabelMap[DEFAULT_THEME_VERSION]
: DEFAULT_THEME_VERSION,
type: 'select',
options: THEME_OPTIONS,
options: themeOptions,
optionLabels: themeVersionLabelMap,
description: i18n.translate('core.ui_settings.params.themeVersionText', {
defaultMessage: `<p>Switch between the themes used for the current and next versions of OpenSearch Dashboards. A page refresh is required for the setting to be applied.</p><p><a href="{href}">{linkText}</a></p>`,
Expand All @@ -91,7 +85,7 @@ export const getThemeSettings = (): Record<string, UiSettingsParams> => {
requiresPageReload: true,
preferBrowserSetting: true,
category: ['appearance'],
schema: schema.oneOf(THEME_SCHEMA_VALUES.map((v) => schema.literal(v)) as [Type<string>]),
schema: schema.oneOf(themeSchemaValues.map((v) => schema.literal(v)) as [Type<string>]),
},
};
};
5 changes: 3 additions & 2 deletions src/core/server/ui_settings/ui_settings_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
* under the License.
*/

import { schema, TypeOf } from '@osd/config-schema';
import { schema, TypeOf, Type } from '@osd/config-schema';
import { themeOptions } from '@osd/ui-shared-deps';
import { ConfigDeprecationProvider } from 'src/core/server';
import { ServiceConfigDescriptor } from '../internal_types';

Expand Down Expand Up @@ -59,7 +60,7 @@ const configSchema = schema.object({
defaults: schema.object({
'theme:darkMode': schema.maybe(schema.boolean({ defaultValue: false })),
'theme:version': schema.maybe(
schema.oneOf([schema.literal('v7'), schema.literal('Next (preview)')], {
schema.oneOf(themeOptions.map((option) => schema.literal(option)) as [Type<string>], {
defaultValue: 'Next (preview)',
})
),
Expand Down

0 comments on commit 14a6cfa

Please sign in to comment.