Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep stretched cut text at the start edge
Yudhi Armyndhariscc7131a29a51
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 756c6d074..07ecb1245 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Load every page of the chat list in the dashboard; an account with more than 1000 chats saw only the first 1000.- Cut a long chat name in the dashboard on the side its own script ends, so a Latin name on an Arabic or Hebrew page no longer loses its first letters.- Shorten the id of an unnamed group on the dashboard Top chats chart to its first and last digits, and fit the Number Checker phone placeholder in every language.+- Keep the dashboard charts left to right on right to left pages, where the Top chats labels were drawn across their bars.- Bound Redis cache commands and discard expired operations during reconnect.- Read the top chats of `GET /api/stats/messages` on SQLite through the `createdAt` index, where it scanned the whole messages table (3 s over 59,000 messages for a one-day window, with every request waiting), and skip the row lookup for group messages in the top chats of `GET /api/stats/sessions/{sessionId}`.- Preserve literal request values in custom ingress acknowledgments.diff --git a/dashboard/src/bidi.test.ts b/dashboard/src/bidi.test.tsindex 3774115e3..0acf69099 100644--- a/dashboard/src/bidi.test.ts+++ b/dashboard/src/bidi.test.ts@@ -41,7 +41,6 @@ test('text cut with an ellipsis takes its direction from its own characters', ()'.chats-page .room-contact-info h3','.chats-page .preview-filename','.chats-page .quote-body',- '.chats-page .replying-to-title','.chats-page .replying-to-body','.chats-page .group-info-member .name',];@@ -51,3 +50,33 @@ test('text cut with an ellipsis takes its direction from its own characters', ()assert.match(rule, /unicode-bidi:\s*plaintext/, selector);}});++test('the reply title is a sentence with a name inside, so it keeps the page direction', () => {+ // Read by its first strong letter, "{{name}} kişisine yanıt veriliyor" with an Arabic name would resolve right+ // to left and draw its words backwards.+ const rule = body('pages/Chats.css', '.chats-page .replying-to-title');+ assert.match(rule, /text-overflow:\s*ellipsis/);+ assert.doesNotMatch(rule, /unicode-bidi/);+});++test('cut text that a flex column stretches hugs the start edge, where its siblings are', () => {+ // unicode-bidi: plaintext also aligns the text by its own direction, so a stretched box would put a Latin name+ // at the left edge of an Arabic page while the lines beside it sit at the right.+ const hugging = [+ '.chats-page .chat-item-info > .chat-item-name',+ '.chats-page .chat-item-info > .chat-item-snippet',+ '.chats-page .room-contact-info h3',+ '.chats-page .preview-filename',+ '.chats-page .replying-to-body',+ ];+ for (const selector of hugging) {+ const rule = body('pages/Chats.css', selector);+ assert.match(rule, /align-self:\s*flex-start/, selector);+ assert.match(rule, /max-width:\s*100%/, `${selector} needs a cap, or the ellipsis never shows`);+ }+ assert.match(body('pages/Chats.css', '.chats-page .quote-body'), /width:\s*fit-content/);+});++test('chart plots stay left to right on a right to left page', () => {+ assert.match(body('components/DashboardCharts.css', '.dashboard-charts .recharts-surface'), /direction:\s*ltr/);+});diff --git a/dashboard/src/components/DashboardCharts.css b/dashboard/src/components/DashboardCharts.cssindex 305646f2b..da4a71b00 100644--- a/dashboard/src/components/DashboardCharts.css+++ b/dashboard/src/components/DashboardCharts.css@@ -103,3 +103,10 @@grid-column: auto;}}++/* Time runs left to right and numbers read left to right on every page. Under an inherited right to left+ direction recharts still places the axis labels for a left to right plot, but the end of each label is+ anchored on its other side, so the category labels were drawn across their own bars. */+.dashboard-charts .recharts-surface {+ direction: ltr;+}diff --git a/dashboard/src/components/DashboardCharts.tsx b/dashboard/src/components/DashboardCharts.tsxindex f4b79ef53..5ffb593e0 100644--- a/dashboard/src/components/DashboardCharts.tsx+++ b/dashboard/src/components/DashboardCharts.tsx@@ -20,7 +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 { isolate, shortenLabel } from '../utils/shortenLabel';import './DashboardCharts.css';const PERIODS: StatsPeriod[] = ['24h', '7d', '30d'];@@ -188,7 +188,7 @@ export function DashboardCharts() {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))}+ tickFormatter={(label: string) => isolate(shortenLabel(String(label)))}/><Tooltip /><Bar dataKey="count" name={t('dashboard.charts.messages')} fill="#25d366" radius={[0, 4, 4, 0]} />diff --git a/dashboard/src/pages/Chats.css b/dashboard/src/pages/Chats.cssindex 778184f8b..40d784f6d 100644--- a/dashboard/src/pages/Chats.css+++ b/dashboard/src/pages/Chats.css@@ -240,7 +240,10 @@/* Text that is cut with an ellipsis takes its direction from its own characters (unicode-bidi: plaintext).Under an inherited direction the ellipsis eats the wrong end of a name written the other way round: a- Latin name in an Arabic page lost its first letters and kept its last ("...hi Armyndharis"). */+ Latin name in an Arabic page lost its first letters and kept its last ("...hi Armyndharis").+ The same property aligns the text by its own direction, so a box that a flex column stretches across its+ width would put a Latin name at the left edge of an Arabic page while its siblings sit at the right. Those+ boxes hug the start edge instead (align-self: flex-start, capped by max-width so the ellipsis still shows). */.chats-page .chat-item-name {font-size: 0.9375rem;font-weight: 600;@@ -251,6 +254,14 @@unicode-bidi: plaintext;}+/* In the New chat list the name and the number are direct children of a stretched column; in the chat list+ they sit in rows that are as wide as their text, which needs nothing. */+.chats-page .chat-item-info > .chat-item-name,+.chats-page .chat-item-info > .chat-item-snippet {+ align-self: flex-start;+ max-width: 100%;+}+.chats-page .chat-item-time {font-size: 0.75rem;color: var(--text-muted);@@ -429,6 +440,8 @@overflow: hidden;text-overflow: ellipsis;unicode-bidi: plaintext;+ align-self: flex-start;+ max-width: 100%;}/* Prettified phone (or "Group" / "Private contact" label for non-personal JIDs). Primary subtitle@@ -1000,6 +1013,8 @@overflow: hidden;text-overflow: ellipsis;unicode-bidi: plaintext;+ align-self: flex-start;+ max-width: 100%;}.chats-page .preview-filesize {@@ -1476,6 +1491,7 @@text-overflow: ellipsis;unicode-bidi: plaintext;max-width: 250px;+ width: fit-content;}/* Inbound business prompt buttons (Baileys click-button) */@@ -1584,7 +1600,6 @@white-space: nowrap;overflow: hidden;text-overflow: ellipsis;- unicode-bidi: plaintext;}.chats-page .replying-to-body {@@ -1594,6 +1609,8 @@overflow: hidden;text-overflow: ellipsis;unicode-bidi: plaintext;+ align-self: flex-start;+ max-width: 100%;}.chats-page .btn-close-reply {diff --git a/dashboard/src/utils/shortenLabel.test.ts b/dashboard/src/utils/shortenLabel.test.tsindex 51e813bd1..93df1ad2a 100644--- a/dashboard/src/utils/shortenLabel.test.ts+++ b/dashboard/src/utils/shortenLabel.test.ts@@ -1,6 +1,6 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { shortenLabel } from './shortenLabel.ts';+import { isolate, shortenLabel } from './shortenLabel.ts';test('a label that fits is returned as it is', () => {assert.equal(shortenLabel('Anak Mama'), 'Anak Mama');@@ -35,3 +35,22 @@ test('the width can be set', () => {assert.equal(shortenLabel('abcdefghij', 5), 'abcd…');assert.equal(shortenLabel('1234567890', 5), '12…90');});++test('a legacy group id, two runs of digits joined by a hyphen, keeps both ends', () => {+ // Every legacy group of one creator starts with the same creator number, so the start alone cannot tell them apart.+ assert.equal(shortenLabel('6281234567890-1612345678'), '62812345…2345678');+ assert.equal(shortenLabel('6281234567890-1612345678', 8), '6281…678');+ assert.equal(shortenLabel('62812345678-12'), '62812345678-12');+});++test('a cut never splits what the eye reads as one character', () => {+ const family = '\u{1F468}\u200D\u{1F469}\u200D\u{1F467}';+ const flag = '\u{1F1EE}\u{1F1E9}';+ assert.equal(shortenLabel(`Tim ${family}${family}${family}${family}`, 6), `Tim ${family}…`);+ assert.equal(shortenLabel(`Kantor ${flag}${flag}${flag}`, 9), `Kantor ${flag}…`);+ assert.equal(shortenLabel('a\u0301b\u0301c\u0301d\u0301e\u0301f\u0301', 4), 'a\u0301b\u0301c\u0301…');+});++test('a label is wrapped in a first-strong isolate so its ellipsis keeps its side', () => {+ assert.equal(isolate('abc'), '\u2068abc\u2069');+});diff --git a/dashboard/src/utils/shortenLabel.ts b/dashboard/src/utils/shortenLabel.tsindex ee18fcd9e..f786e7f03 100644--- a/dashboard/src/utils/shortenLabel.ts+++ b/dashboard/src/utils/shortenLabel.ts@@ -1,15 +1,37 @@+// Graphemes, not code points: a flag, a family emoji or a letter with combining marks is several code points+// and must not be cut inside. Segmenter is in every browser the dashboard supports; code points are the fallback.+const segmenter =+ typeof Intl.Segmenter === 'function' ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null;++function graphemes(text: string): string[] {+ return segmenter ? Array.from(segmenter.segment(text), part => part.segment) : Array.from(text);+}+/**- * 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.+ * Fit a chart axis label into a fixed width. An id with no name (a run of digits, or a legacy group id, which+ * is two runs joined by a hyphen) keeps both ends, because the tail is what tells two ids apart; any other+ * label keeps its start. Visible 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);+ const chars = graphemes(label);if (chars.length <= max) return label;- if (/^\d+$/.test(label)) {+ if (/^\d+(?:-\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('')}…`;}++const FIRST_STRONG_ISOLATE = String.fromCodePoint(0x2068);+const POP_ISOLATE = String.fromCodePoint(0x2069);++/**+ * Wrap a label in a first-strong isolate. The ellipsis has no direction of its own, so beside a label written+ * the other way round from its page it would be laid out on the wrong side ("8904778…12036325" for an id).+ * The isolate gives the label its own direction from its own characters.+ */+export function isolate(label: string): string {+ return `${FIRST_STRONG_ISOLATE}${label}${POP_ISOLATE}`;+}