Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep the webhook filter popover on screen
Yudhi Armyndharis975131ce0d6f
diff --git a/dashboard/src/pages/Webhooks.css b/dashboard/src/pages/Webhooks.cssindex fb22b5674..3af4ddc84 100644--- a/dashboard/src/pages/Webhooks.css+++ b/dashboard/src/pages/Webhooks.css@@ -324,7 +324,8 @@position: fixed;z-index: 100;min-width: 200px;- max-width: 340px;+ /* 16px of the viewport stay free: FilterBadge pulls the popover back from the screen edges by 8px each. */+ max-width: min(340px, calc(100vw - 16px));padding: 0.5rem 0.625rem;background: var(--bg-white);border: 1px solid var(--border);diff --git a/dashboard/src/pages/Webhooks.responsive.test.ts b/dashboard/src/pages/Webhooks.responsive.test.tsnew file mode 100644index 000000000..883d9e7f4--- /dev/null+++ b/dashboard/src/pages/Webhooks.responsive.test.ts@@ -0,0 +1,13 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { decl, readRules } from '../test-helpers/css-rules.ts';++// FilterBadge keeps the popover 8px off each screen edge, so on a phone narrower than its 340px cap the popover has+// to be narrower than the screen by 16px, or it ends up touching both edges.+const rules = readRules(new URL('./Webhooks.css', import.meta.url));++test('the filter popover is never wider than the screen less its margins', () => {+ const popover = rules.find(r => !r.media && r.selectors.includes('.webhooks-page .filter-popover'));+ assert.ok(popover, 'no .filter-popover rule');+ assert.equal(decl(popover.body, 'max-width'), 'min(340px, calc(100vw - 16px))');+});diff --git a/dashboard/src/pages/Webhooks.test.ts b/dashboard/src/pages/Webhooks.test.tsindex 33bd0df30..7c90b4948 100644--- a/dashboard/src/pages/Webhooks.test.ts+++ b/dashboard/src/pages/Webhooks.test.ts@@ -197,6 +197,65 @@ test('the filter badge popover names enum values in words, like the filter build]);});+// The popover is fixed against the viewport and was placed at the badge's left edge and below its bottom edge, so a+// badge near the right edge of a phone, or near the bottom of a short window, pushed it past the screen.+test('the filter badge popover stays inside the viewport', async () => {+ webhooksStatus = 200;+ webhookList = [+ {+ id: 'w1',+ sessionId: 'sess-1',+ url: 'https://example.test/hook',+ events: ['message.received'],+ active: true,+ filters: { conditions: [{ field: 'sender', operator: 'is', value: ['[email protected]'] }] },+ },+ ];+ const sizes = Object.getOwnPropertyDescriptors(window.HTMLElement.prototype);+ const innerWidth = window.innerWidth;+ const innerHeight = window.innerHeight;+ // jsdom lays nothing out: the popover is 240 by 80 px, the window 390 by 700.+ const size = (name: 'offsetWidth' | 'offsetHeight', value: number) =>+ Object.defineProperty(window.HTMLElement.prototype, name, {+ configurable: true,+ get(this: HTMLElement) {+ return this.classList.contains('filter-popover') ? value : 0;+ },+ });+ size('offsetWidth', 240);+ size('offsetHeight', 80);+ window.innerWidth = 390;+ window.innerHeight = 700;+ try {+ renderWebhooks();+ const badge = (await rtl.screen.findByText('1 filter')).closest('.filter-badge') as HTMLElement;+ const placeBadge = (left: number, top: number) => {+ badge.getBoundingClientRect = () =>+ ({ left, right: left + 70, top, bottom: top + 20, width: 70, height: 20, x: left, y: top }) as DOMRect;+ rtl.fireEvent.blur(badge);+ rtl.fireEvent.focus(badge);+ const popover = document.querySelector<HTMLElement>('.filter-popover')!;+ return { left: popover.style.left, top: popover.style.top };+ };+ // Room on both sides: below the badge, at its left edge.+ assert.deepEqual(placeBadge(100, 200), { left: '100px', top: '226px' });+ // Near the right edge: moved left until it ends 8px short of the edge.+ assert.deepEqual(placeBadge(350, 200), { left: '142px', top: '226px' });+ // Near the left edge: kept 8px off it.+ assert.deepEqual(placeBadge(2, 200), { left: '8px', top: '226px' });+ // Near the bottom: above the badge, whose top is at 650.+ assert.deepEqual(placeBadge(100, 650), { left: '100px', top: '564px' });+ } finally {+ window.innerWidth = innerWidth;+ window.innerHeight = innerHeight;+ for (const name of ['offsetWidth', 'offsetHeight'] as const) {+ const original = sizes[name];+ if (original) Object.defineProperty(window.HTMLElement.prototype, name, original);+ else delete (window.HTMLElement.prototype as unknown as Record<string, unknown>)[name];+ }+ }+});+test('a second click on Create while the first create is in flight sends nothing', async () => {const { screen, fireEvent, waitFor } = rtl;webhooksStatus = 200;diff --git a/dashboard/src/pages/Webhooks.tsx b/dashboard/src/pages/Webhooks.tsxindex fde837f7f..e7eabd526 100644--- a/dashboard/src/pages/Webhooks.tsx+++ b/dashboard/src/pages/Webhooks.tsx@@ -1,4 +1,4 @@-import { useState } from 'react';+import { useLayoutEffect, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';import {Plus,@@ -65,12 +65,26 @@ function conditionSummary(c: WebhookFilterCondition, t: TFn): string {// fixed-positioned from the badge's rect so the card's `overflow: hidden` doesn't clip it.function FilterBadge({ filters }: { filters: WebhookFilters }) {const { t } = useTranslation();- const [coords, setCoords] = useState<{ top: number; left: number } | null>(null);- const openAt = (el: HTMLElement) => {- const r = el.getBoundingClientRect();- setCoords({ top: r.bottom + 6, left: r.left });- };- const close = () => setCoords(null);+ const [anchor, setAnchor] = useState<DOMRect | null>(null);+ const popoverRef = useRef<HTMLDivElement | null>(null);+ const openAt = (el: HTMLElement) => setAnchor(el.getBoundingClientRect());+ const close = () => setAnchor(null);++ // Below the badge at its left edge when that fits; otherwise above it, and pulled back from the screen edges.+ useLayoutEffect(() => {+ const popover = popoverRef.current;+ if (!anchor || !popover) return;+ const gap = 6;+ const margin = 8;+ // Measured with all the room the viewport allows, because the box shrinks to the space left of its edge.+ popover.style.left = `${margin}px`;+ const { offsetWidth: width, offsetHeight: height } = popover;+ const fitsBelow = anchor.bottom + gap + height <= window.innerHeight - margin;+ Object.assign(popover.style, {+ top: `${fitsBelow ? anchor.bottom + gap : Math.max(margin, anchor.top - gap - height)}px`,+ left: `${Math.min(Math.max(margin, anchor.left), window.innerWidth - width - margin)}px`,+ });+ }, [anchor]);return (<span@@ -83,8 +97,13 @@ function FilterBadge({ filters }: { filters: WebhookFilters }) {><Filter size={12} />{t('webhooks.filters.badge', { count: filters.conditions.length })}- {coords && (- <div className="filter-popover" style={{ top: coords.top, left: coords.left }} role="tooltip">+ {anchor && (+ <div+ className="filter-popover"+ ref={popoverRef}+ style={{ top: anchor.bottom + 6, left: anchor.left }}+ role="tooltip"+ ><div className="filter-popover-title">{t('webhooks.filters.title')}</div>{filters.conditions.map((condition, i) => (<div key={i} className="filter-popover-row">