bito-code-review[bot] commented on code in PR #37378:
URL: https://github.com/apache/superset/pull/37378#discussion_r2760490746


##########
superset-frontend/src/theme/utils/themeStructureValidation.ts:
##########
@@ -0,0 +1,116 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { AnyThemeConfig } from '@apache-superset/core/ui';
+import { isValidTokenName } from './antdTokenNames';
+
+export interface ValidationIssue {
+  tokenName: string;
+  severity: 'error' | 'warning';
+  message: string;
+}
+
+export interface ValidationResult {
+  valid: boolean; // false if ANY errors exist (warnings don't affect this)
+  errors: ValidationIssue[];
+  warnings: ValidationIssue[];
+}
+
+/**
+ * Validates theme structure and token names.
+ * - ERRORS block save/apply (invalid structure, empty themes)
+ * - WARNINGS allow save/apply but show in editor (unknown tokens, null values)
+ *
+ * This validation does NOT check token values - Ant Design handles that at 
runtime.
+ */
+export function validateTheme(themeConfig: AnyThemeConfig): ValidationResult {
+  const errors: ValidationIssue[] = [];
+  const warnings: ValidationIssue[] = [];
+
+  // ERROR: Null/invalid config
+  if (!themeConfig || typeof themeConfig !== 'object') {
+    errors.push({
+      tokenName: '_root',
+      severity: 'error',
+      message: 'Theme configuration must be a valid object',
+    });
+    return { valid: false, errors, warnings };
+  }
+
+  // ERROR: Empty theme (no tokens, no algorithm, no components)
+  const hasTokens =
+    themeConfig.token && Object.keys(themeConfig.token).length > 0;
+  const hasAlgorithm = Boolean(themeConfig.algorithm);
+  const hasComponents =
+    themeConfig.components && Object.keys(themeConfig.components).length > 0;
+
+  if (!hasTokens && !hasAlgorithm && !hasComponents) {
+    errors.push({
+      tokenName: '_root',
+      severity: 'error',
+      message:
+        'Theme cannot be empty. Add at least one token, algorithm, or 
component override.',
+    });
+    return { valid: false, errors, warnings };
+  }
+
+  // WARNING: Unknown token names (likely typos)
+  // Guard against non-object token values (e.g., string, array, number)
+  const rawToken = themeConfig.token;
+  const tokens =
+    rawToken && typeof rawToken === 'object' && !Array.isArray(rawToken)
+      ? rawToken
+      : {};
+
+  if (rawToken && tokens !== rawToken) {
+    errors.push({
+      tokenName: '_root',
+      severity: 'error',
+      message:
+        'Token configuration must be an object, not an array or primitive',
+    });
+    return { valid: false, errors, warnings };
+  }
+
+  Object.entries(tokens).forEach(([name, value]) => {

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Missing Components Structure Validation</b></div>
   <div id="fix">
   
   The validation checks token configuration structure but lacks the same guard 
for components, which could lead to inconsistent error handling and potential 
runtime issues if components is not an object. Add a guard after the token 
validation to ensure components is a valid object.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
     }
    
     // Guard against non-object components values
     const rawComponents = themeConfig.components;
     if (rawComponents && (typeof rawComponents !== 'object' || 
Array.isArray(rawComponents))) {
       errors.push({
         tokenName: '_root',
         severity: 'error',
         message: 'Components configuration must be an object, not an array or 
primitive',
       });
       return { valid: false, errors, warnings };
     }
    
     Object.entries(tokens).forEach(([name, value]) => {
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #484b31</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset-frontend/src/theme/ThemeController.ts:
##########
@@ -551,18 +562,32 @@ export class ThemeController {
       this.persistMode();
       this.notifyListeners();
     } catch (error) {
-      console.error('Failed to update theme:', error);
-      this.fallbackToDefaultMode();
+      await this.fallbackToDefaultMode();
     }
   }
 
   /**
-   * Fallback to default mode with error recovery.
+   * Fallback to default mode with runtime error recovery.
+   * Tries to fetch a fresh system default theme from the API.
    */
-  private fallbackToDefaultMode(): void {
+  private async fallbackToDefaultMode(): Promise<void> {

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Async fallback not awaited</b></div>
   <div id="fix">
   
   fallbackToDefaultMode is now async, but setThemeMode calls it without await, 
which may cause incomplete fallback operations.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #484b31</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset-frontend/src/theme/ThemeController.ts:
##########
@@ -535,7 +546,7 @@ export class ThemeController {
    * Updates the theme.
    * @param theme - The new theme to apply
    */
-  private updateTheme(theme?: AnyThemeConfig): void {
+  private async updateTheme(theme?: AnyThemeConfig): Promise<void> {

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Async method called without await</b></div>
   <div id="fix">
   
   updateTheme is now async, but callers like setTheme, setThemeMode, 
resetTheme, setTemporaryTheme, and handleSystemThemeChange do not await it, 
potentially causing theme updates to race with subsequent operations.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #484b31</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to