|
| 1 | +import { UserAgent } from '@/shared/components/provider/UserAgentProvider'; |
| 2 | + |
| 3 | +const FALL_BACK_DELAY_MS = 1500; |
| 4 | + |
| 5 | +const ANDROID_STORE_URL = 'https://play.google.com/store/apps/details?id=com.plottwist.tuk'; |
| 6 | +const IOS_STORE_URL_WEB = |
| 7 | + 'https://apps.apple.com/kr/app/%ED%88%AD-%EB%A7%8C%EB%82%8C%EC%9D%84-%EB%84%8C%EC%A7%80%EC%8B%9C/id6749781762'; |
| 8 | +const IOS_STORE_URL_APPS = 'itms-apps://itunes.apple.com/app/id6749781762'; |
| 9 | + |
| 10 | +const GATHERING_DEEP_LINK = (gatheringId: number) => |
| 11 | + `tuk-app://tuk/join-gathering?gatheringId=${gatheringId}`; |
| 12 | + |
| 13 | +const PROPOSAL_DEEP_LINK = (proposalId: number) => `tuk-app://tuk/proposal-detail/${proposalId}`; |
| 14 | + |
| 15 | +export const joinProposalInAPP = (proposalId: number, userAgent: UserAgent) => { |
| 16 | + if (userAgent.isAndroid) { |
| 17 | + window.location.href = generateAndroidIntentURL( |
| 18 | + `tuk/proposal-detail/${proposalId}`, |
| 19 | + ANDROID_STORE_URL |
| 20 | + ); |
| 21 | + |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + if (userAgent.isIOS) { |
| 26 | + appendIOSIframe(PROPOSAL_DEEP_LINK(proposalId)); |
| 27 | + |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + window.location.href = IOS_STORE_URL_WEB; |
| 32 | +}; |
| 33 | + |
| 34 | +export const joinGatheringInAPP = (gatheringId: number, userAgent: UserAgent) => { |
| 35 | + if (userAgent.isAndroid) { |
| 36 | + window.location.href = generateAndroidIntentURL( |
| 37 | + `tuk/join-gathering?gatheringId=${gatheringId}`, |
| 38 | + ANDROID_STORE_URL |
| 39 | + ); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (userAgent.isIOS) { |
| 44 | + appendIOSIframe(GATHERING_DEEP_LINK(gatheringId)); |
| 45 | + |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + window.location.href = IOS_STORE_URL_WEB; |
| 50 | +}; |
| 51 | + |
| 52 | +export const joinHomeInAPP = (userAgent: UserAgent) => { |
| 53 | + if (userAgent.isAndroid) { |
| 54 | + window.location.href = generateAndroidIntentURL('tuk', ANDROID_STORE_URL); |
| 55 | + |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (userAgent.isIOS) { |
| 60 | + appendIOSIframe('tuk-app://tuk'); |
| 61 | + |
| 62 | + return; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +const generateAndroidIntentURL = (deepLinkPath: string, playStoreURL: string) => { |
| 67 | + return ( |
| 68 | + `intent://${deepLinkPath}` + |
| 69 | + '#Intent;' + |
| 70 | + 'scheme=tuk-app;' + |
| 71 | + 'package=com.plottwist.tuk;' + |
| 72 | + `S.browser_fallback_url=${encodeURIComponent(playStoreURL)};` + |
| 73 | + 'end' |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +const appendIOSIframe = (targetDeepLink: string) => { |
| 78 | + let timer: number | null = null; |
| 79 | + |
| 80 | + const cleanup = () => { |
| 81 | + if (timer) { |
| 82 | + window.clearTimeout(timer); |
| 83 | + timer = null; |
| 84 | + } |
| 85 | + |
| 86 | + document.removeEventListener('visibilitychange', onHide, true); |
| 87 | + window.removeEventListener('pagehide', onHide, true); |
| 88 | + window.removeEventListener('blur', onHide, true); |
| 89 | + const iframe = document.getElementById('__dl_iframe__'); |
| 90 | + if (iframe && iframe.parentNode) iframe.parentNode.removeChild(iframe); |
| 91 | + }; |
| 92 | + |
| 93 | + const onHide = () => cleanup(); |
| 94 | + |
| 95 | + document.addEventListener('visibilitychange', onHide, true); |
| 96 | + window.addEventListener('pagehide', onHide, true); |
| 97 | + window.addEventListener('blur', onHide, true); |
| 98 | + |
| 99 | + const iframe = document.createElement('iframe'); |
| 100 | + iframe.style.display = 'none'; |
| 101 | + iframe.id = '__dl_iframe__'; |
| 102 | + iframe.src = targetDeepLink; |
| 103 | + document.body.appendChild(iframe); |
| 104 | + |
| 105 | + timer = window.setTimeout(() => { |
| 106 | + try { |
| 107 | + window.location.replace(IOS_STORE_URL_APPS); |
| 108 | + } catch { |
| 109 | + window.location.replace(IOS_STORE_URL_WEB); |
| 110 | + } finally { |
| 111 | + cleanup(); |
| 112 | + } |
| 113 | + }, FALL_BACK_DELAY_MS); |
| 114 | + |
| 115 | + return; |
| 116 | +}; |
0 commit comments