Update useExternalLink to match webapp (#8675)

This commit is contained in:
Daniel Espino García 2025-03-13 12:28:00 +01:00 committed by GitHub
parent 49c6079af3
commit 4f9323ee25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View file

@ -25,6 +25,13 @@ describe('useExternalLink', () => {
expect(queryParams).toEqual({});
});
it('mailto links are untouched even if to mattermost emails', () => {
const mailtoURL = 'mailto:example@mattermost.com?subject=123&body=456';
const {result: {current: [mailtoHref, mailtoQueryParams]}} = renderHook(() => useExternalLink(getBaseProps(), mailtoURL));
expect(mailtoHref).toEqual(mailtoURL);
expect(mailtoQueryParams).toEqual({});
});
it('all base queries are set correctly', () => {
const url = 'https://www.mattermost.com/some/url';
const {result: {current: [href, queryParams]}} = renderHook(() => useExternalLink(getBaseProps(), url));
@ -116,4 +123,10 @@ describe('useExternalLink', () => {
expect(firstHref).toBe(secondHref);
expect(firstParams).toBe(secondParams);
});
it('do not substitute %20 on query params', () => {
const url = 'https://www.mattermost.com/some/url?subject=hello%20world';
const {result: {current: [href]}} = renderHook(() => useExternalLink(getBaseProps(), url));
expect(href).toContain('subject=hello%20world');
});
});

View file

@ -2,7 +2,7 @@
// See LICENSE.txt for license information.
import {useMemo} from 'react';
import {URL, URLSearchParams} from 'react-native-url-polyfill';
import {URL} from 'react-native-url-polyfill';
export type ExternalLinkQueryParams = {
utm_source?: string;
@ -30,7 +30,7 @@ export function useExternalLink(
overwriteQueryParams: ExternalLinkQueryParams = {},
): [string, Record<string, string>] {
return useMemo(() => {
if (!href?.includes('mattermost.com')) {
if (!href?.includes('mattermost.com') || href.startsWith('mailto:')) {
return [href, {}];
}
@ -47,7 +47,7 @@ export function useExternalLink(
...overwriteQueryParams,
...existingQueryParamsObj,
};
parsedUrl.search = new URLSearchParams(queryParams).toString();
parsedUrl.search = Object.entries(queryParams).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&');
return [parsedUrl.toString(), queryParams];
}, [href, isCloud, location, overwriteQueryParams, telemetryId, userId]);