forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import React from 'react'
2
3import {
4 createStarterPackLinkFromAndroidReferrer,
5 httpStarterPackUriToAtUri,
6} from '#/lib/strings/starter-pack'
7import {useHasCheckedForStarterPack} from '#/state/preferences/used-starter-packs'
8import {useSetActiveStarterPack} from '#/state/shell/starter-pack'
9import {IS_ANDROID} from '#/env'
10import {Referrer, SharedPrefs} from '../../../modules/expo-bluesky-swiss-army'
11
12export function useStarterPackEntry() {
13 const [ready, setReady] = React.useState(false)
14 const setActiveStarterPack = useSetActiveStarterPack()
15 const hasCheckedForStarterPack = useHasCheckedForStarterPack()
16
17 React.useEffect(() => {
18 if (ready) return
19
20 // On Android, we cannot clear the referral link. It gets stored for 90 days and all we can do is query for it. So,
21 // let's just ensure we never check again after the first time.
22 if (hasCheckedForStarterPack) {
23 setReady(true)
24 return
25 }
26
27 // Safety for Android. Very unlike this could happen, but just in case. The response should be nearly immediate
28 const timeout = setTimeout(() => {
29 setReady(true)
30 }, 500)
31
32 ;(async () => {
33 let uri: string | null | undefined
34
35 if (IS_ANDROID) {
36 const res = await Referrer.getGooglePlayReferrerInfoAsync()
37
38 if (res && res.installReferrer) {
39 uri = createStarterPackLinkFromAndroidReferrer(res.installReferrer)
40 }
41 } else {
42 const starterPackUri = SharedPrefs.getString('starterPackUri')
43 if (starterPackUri) {
44 uri = httpStarterPackUriToAtUri(starterPackUri)
45 SharedPrefs.setValue('starterPackUri', null)
46 }
47 }
48
49 if (uri) {
50 setActiveStarterPack({
51 uri,
52 })
53 }
54
55 setReady(true)
56 })()
57
58 return () => {
59 clearTimeout(timeout)
60 }
61 }, [ready, setActiveStarterPack, hasCheckedForStarterPack])
62
63 return ready
64}