/
index.ts
41 lines (36 loc) · 1.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import openLink from '@components/openLink'
import navigationRef from '@utils/navigation/navigationRef'
import { getReadableAccounts, setAccount } from '@utils/storage/actions'
import * as Linking from 'expo-linking'
import { useEffect } from 'react'
// /compose OR /compose/@[email protected]
export const useLinking = () => {
const parseLink = async (link: string) => {
const parsed = Linking.parse(link)
switch (parsed.scheme) {
case 'tooot':
if (parsed.hostname === 'compose') {
if (parsed.path?.length) {
const accounts = getReadableAccounts()
const foundNotActiveAccount = accounts.find(
account => account.acct === parsed.path && !account.active
)
if (foundNotActiveAccount) {
await setAccount(foundNotActiveAccount.key)
}
}
navigationRef.navigate('Screen-Compose')
}
break
case 'https':
case 'http':
await openLink(link)
break
}
}
useEffect(() => {
Linking.getInitialURL().then(parseLink)
const listener = Linking.addEventListener('url', ({ url }) => parseLink(url))
return () => listener.remove()
}, [])
}