geido commented on code in PR #33420:
URL: https://github.com/apache/superset/pull/33420#discussion_r2113701297


##########
superset-frontend/packages/superset-ui-core/src/theme/ThemeController.tsx:
##########
@@ -0,0 +1,210 @@
+/**
+ * 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 { theme as antdThemeImport } from 'antd';
+import { Theme } from './Theme';
+import { AnyThemeConfig } from './types';
+
+export interface ThemeStorage {
+  getItem(key: string): string | null;
+  setItem(key: string, value: string): void;
+  removeItem(key: string): void;
+}
+
+export class LocalStorageAdapter implements ThemeStorage {
+  getItem(key: string): string | null {
+    return localStorage.getItem(key);
+  }
+
+  setItem(key: string, value: string): void {
+    localStorage.setItem(key, value);
+  }
+
+  removeItem(key: string): void {
+    localStorage.removeItem(key);
+  }
+}
+
+export interface ThemeControllerOptions {
+  storage?: ThemeStorage;
+  storageKey?: string;
+  defaultTheme?: AnyThemeConfig;
+  onChange?: (theme: Theme) => void;
+}
+
+export class ThemeController {
+  private static instance: ThemeController | null = null;
+
+  private theme: Theme;
+
+  private storage: ThemeStorage;
+
+  private storageKey: string;
+
+  private defaultTheme: AnyThemeConfig;
+
+  private customizations: AnyThemeConfig = {};
+
+  private onChangeCallbacks: Set<(theme: Theme) => void> = new Set();
+
+  constructor(options: ThemeControllerOptions = {}) {
+    this.storage = options.storage || new LocalStorageAdapter();
+    this.storageKey = options.storageKey || 'superset-theme';
+    this.defaultTheme = options.defaultTheme || {};
+
+    const savedThemeJson = this.storage.getItem(this.storageKey);
+    let initialCustomizations: AnyThemeConfig = {};
+
+    if (savedThemeJson) {
+      try {
+        initialCustomizations = JSON.parse(savedThemeJson);
+        this.customizations = initialCustomizations;
+      } catch (e) {
+        console.error('Failed to parse saved theme:', e);
+        initialCustomizations = {};
+      }
+    }
+
+    this.theme = Theme.fromConfig(initialCustomizations);
+
+    if (options.onChange) {
+      this.onChangeCallbacks.add(options.onChange);
+    }
+  }
+
+  /**
+   * Get the singleton instance of ThemeController
+   */
+  public static getInstance(options?: ThemeControllerOptions): ThemeController 
{
+    if (!ThemeController.instance) {
+      ThemeController.instance = new ThemeController(options);
+    } else if (options) {
+      if (options.storage) {
+        ThemeController.instance.storage = options.storage;
+      }
+      if (options.storageKey) {
+        ThemeController.instance.storageKey = options.storageKey;
+      }
+      if (options.defaultTheme) {
+        ThemeController.instance.defaultTheme = options.defaultTheme;
+      }
+      if (options.onChange) {
+        ThemeController.instance.onChangeCallbacks.add(options.onChange);
+      }
+    }
+    return ThemeController.instance;
+  }
+
+  /**
+   * Check if the current user can update the theme
+   * This is a placeholder method
+   */
+  public canUpdateTheme(): boolean {

Review Comment:
   What is missing to implement these permission checks? Also, wondering if we 
really want these checks to be within the controller. Should the controller be 
caring about permissions or do permissions belong to the application layer 
where the controller is used? I tend to prefer the latter. Thoughts?



-- 
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