mattermost-mobile/app/hooks/use_external_link.ts
Daniel Espino García e0992c0bf6
Add test notification tool (#8271)
* Add test notification menu

* Reduce the minimum version for testing purposes

* Address feedback

* Add missing strings

* Address feedback

* Fix snapshots

* Bump version limit and use correct link

* Fix tests

* Fix URL issues
2024-11-11 10:22:57 +01:00

54 lines
1.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {useMemo} from 'react';
import {URL, URLSearchParams} from 'react-native-url-polyfill';
export type ExternalLinkQueryParams = {
utm_source?: string;
utm_medium?: string;
utm_campaign?: string;
utm_content?: string;
userId?: string;
}
type StateProps = {
userId: string;
telemetryId: string;
isCloud: boolean;
}
// This mimics the behavior of webapp/channels/src/components/common/hooks/use_external_link.ts
export function useExternalLink(
{
userId,
telemetryId,
isCloud,
}: StateProps,
href: string,
location: string = '',
overwriteQueryParams: ExternalLinkQueryParams = {},
): [string, Record<string, string>] {
return useMemo(() => {
if (!href?.includes('mattermost.com')) {
return [href, {}];
}
const parsedUrl = new URL(href);
const existingURLSearchParams = parsedUrl.searchParams;
const existingQueryParamsObj = Object.fromEntries(existingURLSearchParams.entries());
const queryParams = {
utm_source: 'mattermost',
utm_medium: isCloud ? 'in-product-cloud' : 'in-product',
utm_content: location,
uid: userId,
sid: telemetryId,
...overwriteQueryParams,
...existingQueryParamsObj,
};
parsedUrl.search = new URLSearchParams(queryParams).toString();
return [parsedUrl.toString(), queryParams];
}, [href, isCloud, location, overwriteQueryParams, telemetryId, userId]);
}