mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Fix #717
This commit is contained in:
41
src/utils/linking/index.ts
Normal file
41
src/utils/linking/index.ts
Normal file
@ -0,0 +1,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/@username@example.com
|
||||
|
||||
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()
|
||||
}, [])
|
||||
}
|
Reference in New Issue
Block a user