Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): title the contact dialog by the address book

Yudhi Armyndharis8045f1829553
diff --git a/dashboard/src/components/chats/ChatDialogs.tsx b/dashboard/src/components/chats/ChatDialogs.tsx
index e08da14d1..01d4be9ab 100644
--- a/dashboard/src/components/chats/ChatDialogs.tsx
+++ b/dashboard/src/components/chats/ChatDialogs.tsx
@@ -62,22 +62,28 @@ export function MuteChatDialog({
// The gateway's cap on an address-book name (UpsertContactDto).
const NAME_MAX_LENGTH = 100;
-/** Save a 1:1 chat's contact to the account's address book, or rename the entry it has. */
+/**
+ * Save a 1:1 chat's contact to the account's address book, or rename the entry it has. `saved` is the address
+ * book's word on whether it holds the contact, the one the chat's menu went by; the chat's name says nothing
+ * of it, since an unsaved contact is named by its push name.
+ */
export function SaveContactDialog({
chat,
+ saved,
busy,
onSave,
onClose,
}: {
chat: Chat;
+ saved: boolean;
busy: boolean;
onSave: (firstName: string, lastName: string) => void;
onClose: () => void;
}) {
const { t } = useTranslation();
- // A push name (`~Max`) or the number itself is no name to start from.
- const known = chat.name && !chat.name.startsWith('~') && /\D/.test(chat.name.replace(/[\s+()-]/g, ''));
- const [first, ...rest] = known ? chat.name.trim().split(/\s+/) : [''];
+ // A saved contact starts from its name, unless that is a push name (`~Max`) or the number itself.
+ const named = chat.name && !chat.name.startsWith('~') && /\D/.test(chat.name.replace(/[\s+()-]/g, ''));
+ const [first, ...rest] = saved && named ? chat.name.trim().split(/\s+/) : [''];
const [firstName, setFirstName] = useState(first ?? '');
const [lastName, setLastName] = useState(rest.join(' '));
const canSave = firstName.trim().length > 0 && !busy;
@@ -85,7 +91,7 @@ export function SaveContactDialog({
<Modal
open
onClose={onClose}
- title={known ? t('chats.rowActions.renameContact') : t('chats.rowActions.saveContact')}
+ title={saved ? t('chats.rowActions.renameContact') : t('chats.rowActions.saveContact')}
className="confirm-modal"
closeLabel={t('common.close')}
footer={
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 48651ba72..62907cb0f 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -90,6 +90,15 @@ const PUSH_ONLY_CONTACT = {
isBlocked: false,
};
const LID_CHAT: Chat = { ...CHAT, id: '262813461250071@lid', name: 'Lia', lastMessage: 'from a lid' };
+// A contact the address book holds as saved under a name that reads like a number.
+const NUMBER_NAMED_CHAT: Chat = { ...CHAT, id: '[email protected]', name: '+1 555 000 3333', lastMessage: 'hey' };
+const NUMBER_NAMED_CONTACT = {
+ id: NUMBER_NAMED_CHAT.id,
+ name: NUMBER_NAMED_CHAT.name,
+ number: '15550003333',
+ isMyContact: true,
+ isBlocked: false,
+};
let listExtra = false;
// Served when a test turns `listDuplicateContact` on: Bob listed twice under one id, as whatsapp-web.js lists a
// contact it holds as both a LID and a phone record. The first copy carries another number than the id's.
@@ -129,7 +138,11 @@ function installFetchStub(): void {
}
if (method === 'GET' && path.startsWith(`${base}/chats?`)) {
return Promise.resolve(
- jsonResponse([CHAT, ...(listGroup ? [GROUP] : []), ...(listExtra ? [PUSH_ONLY_CHAT, LID_CHAT] : [])]),
+ jsonResponse([
+ CHAT,
+ ...(listGroup ? [GROUP] : []),
+ ...(listExtra ? [PUSH_ONLY_CHAT, LID_CHAT, NUMBER_NAMED_CHAT] : []),
+ ]),
);
}
const groupPath = `${base}/groups/${encodeURIComponent(GROUP.id)}`;
@@ -202,7 +215,7 @@ function installFetchStub(): void {
ALICE_CONTACT,
...(listDuplicateContact ? [DUPLICATE_CONTACT] : []),
CONTACT,
- ...(listExtra ? [PUSH_ONLY_CONTACT] : []),
+ ...(listExtra ? [PUSH_ONLY_CONTACT, NUMBER_NAMED_CONTACT] : []),
]),
);
}
@@ -995,6 +1008,26 @@ test('a contact Baileys knows by push name only is unsaved: Unknown lists it, an
assert.ok(!screen.queryByRole('button', { name: /^Rename contact/ }), 'an unsaved contact is offered Rename');
});
+test("the contact dialog follows the address book the menu read, not the chat's name", async () => {
+ const { screen, fireEvent, within } = rtl;
+ listExtra = true;
+ renderPage();
+ await screen.findByText('Bobby');
+
+ // Known to Baileys by a bare push name: unsaved, so Save, and the push name is no name to start from.
+ pickRowAction('Bobby', 'Save contact');
+ let dialog = await screen.findByRole('dialog');
+ assert.equal(dialog.querySelector('h2')?.textContent, 'Save contact');
+ assert.equal((within(dialog).getByLabelText('First name') as HTMLInputElement).value, '');
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Cancel' }));
+
+ // Saved under a name that reads like a number: Rename, with nothing to start from.
+ pickRowAction('+1 555 000 3333', 'Rename contact');
+ dialog = await screen.findByRole('dialog');
+ assert.equal(dialog.querySelector('h2')?.textContent, 'Rename contact');
+ assert.equal((within(dialog).getByLabelText('First name') as HTMLInputElement).value, '');
+});
+
test('a chat keyed by a privacy id offers Save contact disabled, with the reason, and sends nothing', async () => {
const { screen, fireEvent } = rtl;
listExtra = true;
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index 27e1113ba..ab48c2d2b 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -1318,11 +1318,13 @@ export function Chats() {
.catch(failWith('chats.rowActions.blockFailed'));
};
+ const isSavedContact = (chat: Chat) => savedContactKeys?.has(contactKey(chat.id)) ?? false;
+
const rowActions: ChatRowActions = {
onGroupInfo: setGroupDialogChat,
onSaveContact: canWrite ? setContactDialogChat : undefined,
canSaveContact: chat => addressbookIdOf(chat) !== null,
- isSavedContact: chat => savedContactKeys?.has(contactKey(chat.id)) ?? false,
+ isSavedContact,
onTogglePin: canWrite ? chat => void handleTogglePin(chat) : undefined,
onMute: canWrite ? chat => (chat.muted ? void handleMute(chat, null) : setMuteDialogChat(chat)) : undefined,
onToggleArchive: canWrite ? chat => void handleToggleArchive(chat) : undefined,
@@ -1850,6 +1852,7 @@ export function Chats() {
{contactDialogChat && (
<SaveContactDialog
chat={contactDialogChat}
+ saved={isSavedContact(contactDialogChat)}
busy={dialogRunning === contactDialogChat}
onSave={(first, last) => void handleSaveContact(contactDialogChat, first, last)}
onClose={() => setContactDialogChat(null)}