Skip to content

Commit 36e1eba

Browse files
author
Doong
committed
update use-open-dynamic-link queryKey
1 parent 95ff71b commit 36e1eba

3 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export {
88
useOpenDynamicLink
99
}
1010

11-
interface DynamicLinkProps extends IProps {
11+
export interface DynamicLinkProps extends IProps {
1212
children: ReactNode
1313
className?: string
1414
htmlProps?: AnchorHTMLAttributes<HTMLAnchorElement>

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ export interface IProps {
99
appStoreUrl: string
1010
googlePlayUrl: string
1111
fallbackUrl: string
12-
domainUrl?: string
12+
originUrl?: string
1313
userAgent?: string
1414
platform?: IPlatform
1515
timeout?: number
16+
queryKey?: string
1617
onOpenStore?: (params: { link: string }) => void
1718
}

src/use-open-dynamic-link.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { IProps } from './types';
55
interface IParams {
66
targetUrl?: string
77
isMainDomain?: boolean
8+
queryKey?: string
89
}
910

1011
function getQueryParam(key: string, url?: string) {
@@ -20,11 +21,22 @@ function getQueryParam(key: string, url?: string) {
2021
}
2122

2223
export function getLink(domainUrl: string, params?: IParams) {
24+
const queryKey = params?.queryKey!;
25+
2326
if (params?.isMainDomain) {
27+
if (!domainUrl) {
28+
return ''
29+
}
30+
2431
const url = `${domainUrl.replace(/\/+$/, '')}${window.location.pathname}${window.location.search}`;
2532

33+
if (!queryKey) {
34+
return url
35+
}
36+
2637
const urlObj = new URL(url);
27-
urlObj.searchParams.delete('isOpenStore');
38+
urlObj.searchParams.delete(queryKey);
39+
urlObj.searchParams.delete('origin');
2840

2941
return urlObj.toString();
3042
}
@@ -42,50 +54,52 @@ export function getLink(domainUrl: string, params?: IParams) {
4254
}
4355
}
4456

45-
if (realLink.includes('isOpenStore=true')) {
57+
if (realLink.includes(`${queryKey}=1`)) {
4658
return realLink
4759
}
4860

49-
return `${realLink}${realLink.includes('?') ? '&' : '?'}isOpenStore=true`
61+
return `${realLink}${realLink.includes('?') ? '&' : '?'}${queryKey}=1&origin=${window.location.origin}`
5062
}
5163

5264
export const useOpenDynamicLink = (props: IProps) => {
5365
const {
5466
subdomainUrl,
5567
appStoreUrl,
5668
googlePlayUrl,
57-
domainUrl,
5869
fallbackUrl,
5970
platform = {},
6071
timeout = 100,
72+
queryKey = 'openStore',
6173
onOpenStore
6274
} = props;
6375

76+
let originUrl = props?.originUrl;
77+
6478
const handleOpenDynamicLink = useCallback((params?: IParams) => {
65-
const fullLink = getLink(subdomainUrl, params);
79+
const fullLink = getLink(subdomainUrl, { ...params, queryKey });
6680

6781
window.open(fullLink);
68-
}, [subdomainUrl]);
82+
}, [subdomainUrl, queryKey]);
6983

7084
const handleOpenStore = useCallback(() => {
7185
const userAgent = props?.userAgent || window.navigator.userAgent;
7286
let storeLink: string = fallbackUrl;
7387
let redirectToUrl: string = '';
7488

89+
if (!originUrl) {
90+
originUrl = getQueryParam('origin') || '';
91+
}
92+
7593
if (/iPhone|iPad|iPod/i.test(userAgent) || platform?.isIOS || platform?.isIpad) {
7694
storeLink = appStoreUrl;
7795

78-
if (domainUrl) {
79-
redirectToUrl = getLink(domainUrl, { isMainDomain: true });
80-
}
96+
redirectToUrl = getLink(originUrl, { isMainDomain: true, queryKey });
8197
}
8298

8399
if (/Android/i.test(userAgent) || platform?.isAndroid) {
84100
storeLink = googlePlayUrl;
85101

86-
if (domainUrl) {
87-
redirectToUrl = getLink(domainUrl, { isMainDomain: true });
88-
}
102+
redirectToUrl = getLink(originUrl, { isMainDomain: true, queryKey });
89103
}
90104

91105
if (redirectToUrl) {
@@ -101,11 +115,11 @@ export const useOpenDynamicLink = (props: IProps) => {
101115
window.location.href = redirectToUrl
102116
}, timeout)
103117
}
104-
}, []);
118+
}, [queryKey]);
105119

106120
const isOpenStore = useMemo(() => {
107-
return Boolean(getQueryParam('isOpenStore'))
108-
}, []);
121+
return Boolean(getQueryParam(queryKey))
122+
}, [queryKey]);
109123

110124
useEffect(() => {
111125
if (isOpenStore) {
@@ -114,7 +128,7 @@ export const useOpenDynamicLink = (props: IProps) => {
114128
}, [isOpenStore]);
115129

116130
return {
117-
link: getLink(subdomainUrl),
131+
link: getLink(subdomainUrl, { queryKey }),
118132
openDynamicLink: handleOpenDynamicLink,
119133
openStore: handleOpenStore
120134
}

0 commit comments

Comments
 (0)