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


##########
superset-frontend/src/features/themes/ThemeColorPickers.tsx:
##########
@@ -0,0 +1,175 @@
+/**
+ * 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 { useMemo } from 'react';
+import { t } from '@apache-superset/core/translation';
+import { styled } from '@apache-superset/core/theme';
+import { Typography } from '@superset-ui/core/components/Typography';
+import ColorPickerControl from 
'src/explore/components/controls/ColorPickerControl';
+import type { ColorPickerValue } from 
'src/explore/components/controls/ColorPickerControl';
+
+/**
+ * Curated antd theme SEED tokens (plus a handful of the most load-bearing
+ * MapToken/AliasToken derivatives) surfaced as individual color pickers, so
+ * the common case -- "change the brand color" -- doesn't require pasting a
+ * hand-edited JSON blob. This intentionally does not attempt to cover the
+ * full antd token surface (100+ tokens): anything not listed here is still
+ * fully editable via the JSON textarea below, which remains the source of
+ * truth. Names are taken directly from antd's own `SeedToken` /
+ * `MapToken` types -- see `antd/es/theme/interface/{seeds,maps/colors}.d.ts`
+ * -- never invented.
+ */
+export const CURATED_COLOR_TOKENS = [
+  { key: 'colorPrimary', label: () => t('Primary') },
+  { key: 'colorSuccess', label: () => t('Success') },
+  { key: 'colorWarning', label: () => t('Warning') },
+  { key: 'colorError', label: () => t('Error') },
+  { key: 'colorInfo', label: () => t('Info') },
+  { key: 'colorLink', label: () => t('Link') },
+  { key: 'colorText', label: () => t('Text') },
+  { key: 'colorTextSecondary', label: () => t('Secondary text') },
+  { key: 'colorBgBase', label: () => t('Base background') },
+  { key: 'colorBgContainer', label: () => t('Container background') },
+  { key: 'colorBorder', label: () => t('Border') },
+] as const;
+
+export type CuratedColorToken = (typeof CURATED_COLOR_TOKENS)[number]['key'];
+
+interface ParsedThemeJson {
+  token?: Record<string, unknown>;
+  [key: string]: unknown;
+}
+
+const isPlainObject = (value: unknown): value is Record<string, unknown> =>
+  typeof value === 'object' && value !== null && !Array.isArray(value);
+
+/** Attempts to parse `jsonData` as a theme config object; returns `null`
+ * (never throws) when the JSON is empty, mid-edit, or otherwise invalid --
+ * matching the JSON-parse error handling already used elsewhere in
+ * ThemeModal (`formatJsonData`, `isValidJson`). A `token` that parses but
+ * isn't itself a plain object (a string, array, or number) also counts as
+ * invalid here, since `patchThemeJsonToken` below spreads it -- and
+ * spreading a string or array silently rewrites it into numeric-keyed
+ * junk rather than the token map callers expect. */
+export const tryParseThemeJson = (
+  jsonData: string | undefined,
+): ParsedThemeJson | null => {
+  if (!jsonData?.trim()) return null;
+  try {
+    const parsed = JSON.parse(jsonData);
+    if (!isPlainObject(parsed)) return null;
+    if (
+      'token' in parsed &&
+      parsed.token !== undefined &&
+      !isPlainObject(parsed.token)
+    ) {
+      return null;
+    }
+    return parsed;
+  } catch {
+    return null;
+  }
+};
+
+/** Patches a single curated token into `jsonData`'s `token` object,
+ * preserving every other key (including tokens this UI doesn't curate) and
+ * re-serializing with the same 2-space indent used throughout this modal. */
+export const patchThemeJsonToken = (
+  jsonData: string,
+  key: CuratedColorToken,
+  value: string,
+): string => {
+  const parsed = tryParseThemeJson(jsonData) ?? {};
+  const next = {
+    ...parsed,
+    token: {
+      ...parsed.token,
+      [key]: value,
+    },
+  };
+  return JSON.stringify(next, null, 2);

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>JSON key reordering on edit</b></div>
   <div id="fix">
   
   `patchThemeJsonToken` spreads `{...parsed, token: {...}}`, which moves 
`token` to the front of the object (verified: keys become 
`['token','components']`). Since the JSON textarea is the source of truth, 
every picker change reorders the JSON keys and produces diff noise on each 
edit. Preserve original key order when writing the patched object.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #5bd339</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/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/utils.ts:
##########
@@ -74,3 +76,91 @@ export const getHeaderFontSize = (proportionValue: number) =>
 export const getComparisonFontSize = (proportionValue: number) =>
   comparisonFontSizesMapping[proportionValue] ??
   sharedFontSizes[sharedFontSizes.length - 1];
+
+export interface ComparisonColorTokens {
+  /** Color for the arrow indicator and (when the symbol is index 0) text. */
+  text: string;
+  /** Background color for the increase/decrease pill. */
+  background: string;
+  /** Foreground color for the increase/decrease pill's text. */
+  strongText: string;
+}
+
+/**
+ * Resolves the increase/decrease colors to use for rendering, given the
+ * chart's current `increaseColor` / `decreaseColor` (from the
+ * `ColorPickerControl`s added after this became customizable) and the
+ * legacy `comparisonColorScheme` field.
+ *
+ * Charts saved before `increaseColor` / `decreaseColor` existed only have
+ * `comparisonColorScheme`, a 2-choice select ('Green' | 'Red') where 'Green'
+ * meant "green for increase, red for decrease" and 'Red' meant the reverse.
+ * Both legacy choices map onto the same 'Green' | 'Red' semantic token names
+ * used by the new controls' presets, so resolving through it here
+ * reproduces the exact old behavior (including the reversed case) without a
+ * data migration.
+ */
+export const resolveComparisonColorKeys = (
+  comparisonColorScheme: string | undefined,
+  increaseColor: string | undefined,
+  decreaseColor: string | undefined,
+): { increaseColor: string; decreaseColor: string } => {
+  const legacyReversed = comparisonColorScheme === ColorSchemeEnum.Red;
+  return {
+    increaseColor:
+      increaseColor ??
+      (legacyReversed ? ColorSchemeEnum.Red : ColorSchemeEnum.Green),
+    decreaseColor:
+      decreaseColor ??
+      (legacyReversed ? ColorSchemeEnum.Green : ColorSchemeEnum.Red),
+  };
+};
+
+/**
+ * Resolves a single color value (semantic token name or literal hex from
+ * the color picker) to the (arrow/text, background, strong-text) triad used
+ * across the comparison pills. 'Green' / 'Red' keep using the paired
+ * success/error theme tokens exactly as before these colors were
+ * customizable; any other value is either a theme token name (e.g.
+ * 'colorPrimary', emitted by the picker's `resolveThemeTokens` option) or a
+ * literal hex -- 6-digit, or 8-digit when the alpha-enabled picker is used
+ * -- in which case the background is a light (~10% opacity) tint of that
+ * same color.
+ */
+export const getComparisonColorTokens = (
+  colorValue: string,
+  theme: SupersetTheme,
+): ComparisonColorTokens => {
+  if (colorValue === ColorSchemeEnum.Green) {
+    return {
+      text: theme.colorSuccess,
+      background: theme.colorSuccessBg,
+      strongText: theme.colorSuccessText,
+    };
+  }
+  if (colorValue === ColorSchemeEnum.Red) {
+    return {
+      text: theme.colorError,
+      background: theme.colorErrorBg,
+      strongText: theme.colorErrorText,
+    };
+  }
+  const themeColors = theme as unknown as Record<string, string>;
+  const resolvedColor = Object.prototype.hasOwnProperty.call(
+    themeColors,
+    colorValue,
+  )
+    ? themeColors[colorValue]
+    : colorValue;
+  // An 8-digit hex (alpha-enabled picker) already carries its own alpha
+  // channel; strip it before appending the tint suffix below so the
+  // background stays a valid 8-digit hex instead of stacking a second one.
+  const opaqueColor = /^#[0-9a-f]{8}$/i.test(resolvedColor)
+    ? resolvedColor.slice(0, 7)
+    : resolvedColor;
+  return {
+    text: resolvedColor,
+    background: `${opaqueColor}1A`,

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Magic tint suffix '1A'</b></div>
   <div id="fix">
   
   `background: \`${opaqueColor}1A\`` encodes the pill-tint business rule (~10% 
alpha, per the comment on lines 126-128) as a bare hex fragment. A named 
constant like `COMPARISON_TINT_ALPHA_HEX = '1A'` would make the tint level 
searchable and adjustable; today a designer tweak means editing an opaque 
literal. Minimal fix: add the named constant and an inline comment at this line.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #5bd339</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