Skip to content

Commit 8cc7a38

Browse files
committed
fix(whatsapp): use api.whatsapp.com for share links
PR #459 and follow-up issue #575 both point to the same platform change: WhatsApp now handles desktop and mobile share handoff through api.whatsapp.com. Stop branching between web and api hosts and cover the generated URL in a regression test.
1 parent af3a338 commit 8cc7a38

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

.changeset/dirty-planes-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-share': patch
3+
---
4+
5+
Use `api.whatsapp.com` for generated WhatsApp share links, following the behavior proposed in PR #459 and requested again in issue #575.

src/WhatsappShareButton.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@ import assert from './utils/assert';
22
import objectToGetParams from './utils/objectToGetParams';
33
import createShareButton from './hocs/createShareButton';
44

5-
function isMobileOrTablet() {
6-
return /(android|iphone|ipad|mobile)/i.test(navigator.userAgent);
7-
}
8-
95
function whatsappLink(url: string, { title, separator }: { title?: string; separator?: string }) {
106
assert(url, 'whatsapp.url');
117
return (
12-
'https://' +
13-
(isMobileOrTablet() ? 'api' : 'web') +
14-
'.whatsapp.com/send' +
8+
'https://api.whatsapp.com/send' +
159
objectToGetParams({
1610
text: title ? title + separator + url : url,
1711
})

test/WhatsappShareButton.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import { fireEvent, render, screen } from '@testing-library/react';
3+
import { describe, expect, it, vi } from 'vitest';
4+
5+
import WhatsappShareButton from '../src/WhatsappShareButton';
6+
7+
describe('WhatsappShareButton', () => {
8+
it('uses the api host for generated share links', () => {
9+
const openSpy = vi.spyOn(window, 'open').mockReturnValue(null);
10+
11+
render(
12+
<WhatsappShareButton title="Example" url="https://example.com">
13+
Share to WhatsApp
14+
</WhatsappShareButton>,
15+
);
16+
17+
fireEvent.click(screen.getByRole('button', { name: 'Share to WhatsApp' }));
18+
19+
expect(openSpy).toHaveBeenCalledWith(
20+
'https://api.whatsapp.com/send?text=Example%20https%3A%2F%2Fexample.com',
21+
'',
22+
expect.any(String),
23+
);
24+
});
25+
});

0 commit comments

Comments
 (0)