|
| 1 | +// biome-ignore-all lint/suspicious/noExplicitAny: we have not kept old schema for types, so we need to use any here |
1 | 2 | import { type ConfigSchema, configSchema } from '@digdir/designsystemet'; |
2 | 3 | import { |
3 | 4 | Button, |
@@ -31,7 +32,10 @@ export function ConfigPaste() { |
31 | 32 | } |
32 | 33 |
|
33 | 34 | try { |
34 | | - const parsed = JSON.parse(configText); |
| 35 | + const parsed = isConfigWithOldColorSchema(configText) |
| 36 | + ? migrateConfigWithOldColorSchema(configText) |
| 37 | + : JSON.parse(configText); |
| 38 | + |
35 | 39 | const validated = configSchema.parse(parsed); |
36 | 40 | setValidatedConfig(validated); |
37 | 41 |
|
@@ -107,29 +111,9 @@ export function ConfigPaste() { |
107 | 111 | {themeName} |
108 | 112 | </Paragraph> |
109 | 113 | <div className={classes.colorPreview}> |
110 | | - {themeConfig.colors.main && |
111 | | - Object.values(themeConfig.colors.main) |
112 | | - .slice(0, 3) |
113 | | - .map((color, idx) => ( |
114 | | - <div |
115 | | - key={idx} |
116 | | - className={classes.colorDot} |
117 | | - style={{ backgroundColor: color }} |
118 | | - title={color} |
119 | | - /> |
120 | | - ))} |
121 | | - {themeConfig.colors.neutral && ( |
122 | | - <div |
123 | | - className={classes.colorDot} |
124 | | - style={{ |
125 | | - backgroundColor: themeConfig.colors.neutral, |
126 | | - }} |
127 | | - title={themeConfig.colors.neutral} |
128 | | - /> |
129 | | - )} |
130 | | - {themeConfig.colors?.support && |
131 | | - Object.values(themeConfig.colors.support) |
132 | | - .slice(0, 3) |
| 114 | + {themeConfig.colors && |
| 115 | + Object.values(themeConfig.colors) |
| 116 | + .slice(0, 7) |
133 | 117 | .map((color, idx) => ( |
134 | 118 | <div |
135 | 119 | key={idx} |
@@ -160,3 +144,59 @@ export function ConfigPaste() { |
160 | 144 | </div> |
161 | 145 | ); |
162 | 146 | } |
| 147 | + |
| 148 | +// Temporary migration functions for old color schema, to be removed in future versions |
| 149 | +const isOldColorSchema = (theme: any): boolean => { |
| 150 | + return ( |
| 151 | + theme.colors && |
| 152 | + Object.keys(theme.colors).length === 3 && |
| 153 | + theme.colors.main && |
| 154 | + theme.colors.support && |
| 155 | + theme.colors.neutral |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +const isConfigWithOldColorSchema = (config: any): boolean => { |
| 160 | + const currentConfig = JSON.parse(config); |
| 161 | + |
| 162 | + if (!currentConfig.themes) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + return Object.values(currentConfig.themes).some(isOldColorSchema); |
| 167 | +}; |
| 168 | + |
| 169 | +const migrateConfigWithOldColorSchema = (config: string): string => { |
| 170 | + const currentConfig = JSON.parse(config); |
| 171 | + const updatedThemes: Record<string, any> = {}; |
| 172 | + |
| 173 | + if (currentConfig.themes) { |
| 174 | + for (const [themeName, _] of Object.entries(currentConfig.themes)) { |
| 175 | + const theme = currentConfig.themes[themeName]; |
| 176 | + |
| 177 | + if (isOldColorSchema(theme)) { |
| 178 | + const { main, support, neutral, ...restColors } = theme.colors; |
| 179 | + |
| 180 | + const updatedTheme = { |
| 181 | + ...theme, |
| 182 | + colors: { |
| 183 | + ...restColors, |
| 184 | + ...main, |
| 185 | + ...support, |
| 186 | + neutral, |
| 187 | + }, |
| 188 | + }; |
| 189 | + |
| 190 | + updatedThemes[themeName] = updatedTheme; |
| 191 | + } else { |
| 192 | + updatedThemes[themeName] = theme; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + const migratedConfig = { |
| 197 | + ...currentConfig, |
| 198 | + themes: updatedThemes, |
| 199 | + }; |
| 200 | + |
| 201 | + return migratedConfig; |
| 202 | +}; |
0 commit comments