amaannawab923 commented on code in PR #36416:
URL: https://github.com/apache/superset/pull/36416#discussion_r2665802038
##########
superset-frontend/packages/superset-ui-core/src/currency-format/utils.ts:
##########
@@ -25,21 +25,92 @@ import {
QueryFormMetric,
ValueFormatter,
} from '@superset-ui/core';
+import { normalizeCurrency, hasMixedCurrencies } from './CurrencyFormatter';
+
+/**
+ * Detect single currency in data. Returns ISO 4217 code if uniform, null if
mixed.
+ */
+export const analyzeCurrencyInData = (
+ data: Record<string, any>[],
+ currencyColumn: string | undefined,
+): string | null => {
+ if (!currencyColumn || !data || data.length === 0) {
+ return null;
+ }
+
+ const currencies = data
+ .map(row => row[currencyColumn])
+ .filter(val => val !== null && val !== undefined);
+
+ if (currencies.length === 0) {
+ return null;
+ }
+
+ if (hasMixedCurrencies(currencies)) {
+ return null;
+ }
+
+ return normalizeCurrency(currencies[0]);
+};
+
+/** Resolve AUTO currency to detected value, null (mixed), or passthrough
original. */
+export const resolveAutoCurrency = (
+ currencyFormat: Currency | undefined,
+ backendDetected: string | null | undefined,
+ data?: Record<string, any>[],
+ currencyCodeColumn?: string,
+): Currency | undefined | null => {
+ if (currencyFormat?.symbol !== 'AUTO') return currencyFormat;
+
+ const detectedCurrency =
+ backendDetected ??
+ (data && currencyCodeColumn
+ ? analyzeCurrencyInData(data, currencyCodeColumn)
+ : null);
+
+ if (detectedCurrency) {
+ return {
+ symbol: detectedCurrency,
+ symbolPosition: currencyFormat.symbolPosition,
+ };
+ }
+ return null; // Mixed currencies
+};
export const buildCustomFormatters = (
metrics: QueryFormMetric | QueryFormMetric[] | undefined,
savedCurrencyFormats: Record<string, Currency>,
savedColumnFormats: Record<string, string>,
d3Format: string | undefined,
- currencyFormat: Currency | undefined,
+ currencyFormat: Currency | undefined | null,
+ data?: Record<string, any>[],
+ currencyCodeColumn?: string,
) => {
const metricsArray = ensureIsArray(metrics);
+
+ let resolvedCurrency = currencyFormat;
+ if (currencyFormat?.symbol === 'AUTO' && data && currencyCodeColumn) {
+ const detectedCurrency = analyzeCurrencyInData(data, currencyCodeColumn);
+ if (detectedCurrency) {
+ resolvedCurrency = {
+ symbol: detectedCurrency,
+ symbolPosition: currencyFormat.symbolPosition,
+ };
+ } else {
+ resolvedCurrency = null;
+ }
+ }
+
return metricsArray.reduce((acc, metric) => {
if (isSavedMetric(metric)) {
const actualD3Format = d3Format ?? savedColumnFormats[metric];
- const actualCurrencyFormat = currencyFormat?.symbol
- ? currencyFormat
- : savedCurrencyFormats[metric];
+ // null means explicitly no currency (from AUTO mixed detection)
+ const actualCurrencyFormat =
+ resolvedCurrency === null
+ ? undefined
+ : resolvedCurrency?.symbol
+ ? resolvedCurrency
+ : savedCurrencyFormats[metric];
Review Comment:
too many ternary , impacts readability , can we extract it to a function
--
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]