Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): shorten unnamed group ids on the Top chats chart

Yudhi Armyndharis6af315a61d9c
diff --git a/dashboard/src/components/DashboardCharts.tsx b/dashboard/src/components/DashboardCharts.tsx
index d193483b6..f4b79ef53 100644
--- a/dashboard/src/components/DashboardCharts.tsx
+++ b/dashboard/src/components/DashboardCharts.tsx
@@ -20,6 +20,7 @@ import { useStatsMessagesQuery } from '../hooks/queries';
import type { MessageType, StatsPeriod } from '../services/api';
import { formatTick } from '../utils/chartTicks';
import { messageTypeLabelKey } from '../utils/enumLabels';
+import { shortenLabel } from '../utils/shortenLabel';
import './DashboardCharts.css';
const PERIODS: StatsPeriod[] = ['24h', '7d', '30d'];
@@ -185,6 +186,9 @@ export function DashboardCharts() {
dataKey="name"
width={120}
tick={{ fontSize: 12, fill: 'var(--text-secondary)' }}
+ // A group has no name here, so its label is its numeric id, wider than the axis; the tooltip
+ // keeps the full label.
+ tickFormatter={(label: string) => shortenLabel(String(label))}
/>
<Tooltip />
<Bar dataKey="count" name={t('dashboard.charts.messages')} fill="#25d366" radius={[0, 4, 4, 0]} />
diff --git a/dashboard/src/utils/shortenLabel.test.ts b/dashboard/src/utils/shortenLabel.test.ts
new file mode 100644
index 000000000..51e813bd1
--- /dev/null
+++ b/dashboard/src/utils/shortenLabel.test.ts
@@ -0,0 +1,37 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { shortenLabel } from './shortenLabel.ts';
+
+test('a label that fits is returned as it is', () => {
+ assert.equal(shortenLabel('Anak Mama'), 'Anak Mama');
+ assert.equal(shortenLabel('Yudhi Armyndhar'), 'Yudhi Armyndhar');
+ assert.equal(shortenLabel('1234567890123456'), '1234567890123456');
+ assert.equal(shortenLabel(''), '');
+});
+
+test('a long id keeps both ends and never exceeds the width', () => {
+ const id = '120363251588904778';
+ const out = shortenLabel(id);
+ assert.equal(out, '12036325…8904778');
+ assert.equal(Array.from(out).length, 16);
+ assert.ok(out.startsWith(id.slice(0, 8)) && out.endsWith(id.slice(-7)));
+});
+
+test('a long name keeps its start', () => {
+ const out = shortenLabel('Kopi Opsis Pemasaran Bersama');
+ assert.equal(out, 'Kopi Opsis Pema…');
+ assert.equal(Array.from(out).length, 16);
+ assert.equal(shortenLabel('Yudhi Armyndharis'), 'Yudhi Armyndhar…');
+});
+
+test('a cut never splits an emoji', () => {
+ // The 15th UTF-16 unit of this label is the first half of an emoji.
+ const out = shortenLabel('Hustler Offer 🔥🔥🔥🔥🔥');
+ assert.equal(out, 'Hustler Offer 🔥…');
+ assert.equal(Array.from(out).length, 16);
+});
+
+test('the width can be set', () => {
+ assert.equal(shortenLabel('abcdefghij', 5), 'abcd…');
+ assert.equal(shortenLabel('1234567890', 5), '12…90');
+});
diff --git a/dashboard/src/utils/shortenLabel.ts b/dashboard/src/utils/shortenLabel.ts
new file mode 100644
index 000000000..ee18fcd9e
--- /dev/null
+++ b/dashboard/src/utils/shortenLabel.ts
@@ -0,0 +1,15 @@
+/**
+ * Fit a chart axis label into a fixed width. A long run of digits (a WhatsApp id that has no name) keeps both
+ * ends, because the tail is what tells two ids apart; any other label keeps its start. Characters are counted,
+ * not UTF-16 units, so an emoji at the cut is never split in half.
+ */
+export function shortenLabel(label: string, max = 16): string {
+ const chars = Array.from(label);
+ if (chars.length <= max) return label;
+ if (/^\d+$/.test(label)) {
+ const head = Math.ceil((max - 1) / 2);
+ const tail = max - 1 - head;
+ return `${chars.slice(0, head).join('')}…${chars.slice(chars.length - tail).join('')}`;
+ }
+ return `${chars.slice(0, max - 1).join('')}…`;
+}