Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): give call, order and product their own pie colors
Yudhi Armyndharisd3a99e8ae765
diff --git a/dashboard/src/components/DashboardCharts.test.ts b/dashboard/src/components/DashboardCharts.test.tsindex 9932c2519..34c1edf7b 100644--- a/dashboard/src/components/DashboardCharts.test.ts+++ b/dashboard/src/components/DashboardCharts.test.ts@@ -1,7 +1,8 @@// The "Messages by type" pie names each slice in its legend. Those names are message type keys from-// the stats API (voice, masked, unknown), which must read as words, not as the raw keys.+// the stats API (voice, masked, unknown), which must read as words, not as the raw keys. Each type+// also keeps a color of its own, so no two slices can look alike.import '../test-helpers/register-hooks.ts';-import { test, before, after } from 'node:test';+import { test, before, afterEach } from 'node:test';import assert from 'node:assert/strict';import { createElement } from 'react';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';@@ -13,6 +14,7 @@ function jsonResponse(data: unknown, status = 200): Response {let rtl: typeof import('@testing-library/react');let DashboardCharts: (typeof import('./DashboardCharts.tsx'))['DashboardCharts'];let queryClient: QueryClient | undefined;+let byType: Record<string, number> = {};before(async () => {const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');@@ -29,22 +31,26 @@ before(async () => {unobserve(): void {}disconnect(): void {}};- globalThis.fetch = (() =>- Promise.resolve(- jsonResponse({ timeSeries: [], byType: { voice: 3, masked: 2, unknown: 1 }, topChats: [] }),- )) as typeof fetch;+ globalThis.fetch = (() => Promise.resolve(jsonResponse({ timeSeries: [], byType, topChats: [] }))) as typeof fetch;const { i18nReady } = await import('../i18n/index.ts');await i18nReady;rtl = await import('@testing-library/react');({ DashboardCharts } = await import('./DashboardCharts.tsx'));});-after(() => {+afterEach(() => {rtl.cleanup();queryClient?.clear();});+function byTypeCard(): Promise<HTMLElement> {+ queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });+ rtl.render(createElement(QueryClientProvider, { client: queryClient }, createElement(DashboardCharts)));+ return rtl.screen.findByText('Messages by type').then(n => n.closest('.chart-card') as HTMLElement);+}+test('the by-type pie legend names message types in words', async () => {+ byType = { voice: 3, masked: 2, unknown: 1 };queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });rtl.render(createElement(QueryClientProvider, { client: queryClient }, createElement(DashboardCharts)));@@ -54,3 +60,18 @@ test('the by-type pie legend names message types in words', async () => {assert.deepEqual(legend.sort(), ['Hidden message', 'Unknown type', 'Voice message']);});});++test('every message type gets a slice color no other type uses', async () => {+ const { MESSAGE_TYPES } = await import('../services/api.ts');+ // Plus one type this build has no color for, as a newer gateway could send.+ const types = [...MESSAGE_TYPES, 'future-type'];+ byType = Object.fromEntries(types.map((type, i) => [type, i + 1]));+ const card = await byTypeCard();+ await rtl.waitFor(() => {+ const fills = Array.from(card.querySelectorAll('.recharts-legend-item .recharts-legend-icon')).map(n =>+ n.getAttribute('fill'),+ );+ assert.equal(fills.length, types.length);+ assert.equal(new Set(fills).size, types.length, `shared colors: ${fills.join(', ')}`);+ });+});diff --git a/dashboard/src/components/DashboardCharts.tsx b/dashboard/src/components/DashboardCharts.tsxindex 4c8a186eb..d193483b6 100644--- a/dashboard/src/components/DashboardCharts.tsx+++ b/dashboard/src/components/DashboardCharts.tsx@@ -17,16 +17,18 @@ import {} from 'recharts';import { BarChart3 } from 'lucide-react';import { useStatsMessagesQuery } from '../hooks/queries';-import type { StatsPeriod } from '../services/api';+import type { MessageType, StatsPeriod } from '../services/api';import { formatTick } from '../utils/chartTicks';import { messageTypeLabelKey } from '../utils/enumLabels';import './DashboardCharts.css';const PERIODS: StatsPeriod[] = ['24h', '7d', '30d'];-// Stable, distinct color per message type (recharts needs literal colors). Keyed by type name —-// not array index — so two types can never share a color, and a slice keeps its color even when the-// set of present types changes between requests. Covers every type mapMessageType() can emit.+// Stable, distinct color per message type (recharts needs literal colors). Keyed by type name, not+// array index, so a slice keeps its color even when the set of present types changes between requests.+// Covers every MESSAGE_TYPES value (the `satisfies` fails the build when one is missing). No color+// repeats here or in the fallback palette, so no known type shares its color, not even with a type+// that falls back.const TYPE_COLORS: Record<string, string> = {text: '#25d366',image: '#3b82f6',@@ -38,13 +40,16 @@ const TYPE_COLORS: Record<string, string> = {sticker: '#ef4444',location: '#84cc16',poll: '#6366f1',+ call: '#0ea5e9',revoked: '#f43f5e',+ order: '#f97316',+ product: '#d946ef',masked: '#8b5cf6',unknown: '#64748b',-};+} satisfies Record<MessageType, string>;-// Deterministic fallback for any unmapped type, so its color is stable across renders.-const FALLBACK_COLORS = ['#0ea5e9', '#d946ef', '#f97316', '#10b981', '#6366f1', '#eab308'];+// Deterministic fallback for a type this build does not know yet, so its color is stable across renders.+const FALLBACK_COLORS = ['#10b981', '#eab308'];function colorForType(name: string): string {if (TYPE_COLORS[name]) return TYPE_COLORS[name];let hash = 0;