2022-12-28 23:41:36 +01:00
|
|
|
import browserPackage from '@utils/helpers/browserPackage'
|
2023-01-03 23:57:23 +01:00
|
|
|
import { urlMatcher } from '@utils/helpers/urlMatcher'
|
2022-12-28 23:41:36 +01:00
|
|
|
import navigationRef from '@utils/navigation/navigationRef'
|
2023-01-03 23:57:23 +01:00
|
|
|
import { queryClient } from '@utils/queryHooks'
|
|
|
|
import { QueryKeyAccount } from '@utils/queryHooks/account'
|
|
|
|
import { searchLocalAccount, searchLocalStatus } from '@utils/queryHooks/search'
|
|
|
|
import { QueryKeyStatus } from '@utils/queryHooks/status'
|
2022-12-28 23:41:36 +01:00
|
|
|
import { getGlobalStorage } from '@utils/storage/actions'
|
2020-12-20 18:41:28 +01:00
|
|
|
import * as Linking from 'expo-linking'
|
|
|
|
import * as WebBrowser from 'expo-web-browser'
|
|
|
|
|
2021-08-29 16:08:02 +02:00
|
|
|
const openLink = async (url: string, navigation?: any) => {
|
2023-01-03 23:57:23 +01:00
|
|
|
const handleNavigation = (page: 'Tab-Shared-Toot' | 'Tab-Shared-Account', options: any) => {
|
2021-03-21 23:06:53 +01:00
|
|
|
if (navigation) {
|
|
|
|
navigation.push(page, options)
|
|
|
|
} else {
|
2022-05-08 23:40:42 +02:00
|
|
|
// @ts-ignore
|
2021-08-29 15:25:38 +02:00
|
|
|
navigationRef.navigate(page, options)
|
2021-03-21 23:06:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 23:57:23 +01:00
|
|
|
const match = urlMatcher(url)
|
2021-03-21 23:06:53 +01:00
|
|
|
// If a tooot can be found
|
2023-01-03 23:57:23 +01:00
|
|
|
if (match?.status?.id) {
|
|
|
|
let response: Mastodon.Status | undefined = undefined
|
|
|
|
|
|
|
|
const queryKey: QueryKeyStatus = [
|
|
|
|
'Status',
|
|
|
|
{ id: match.status.id, uri: url, _remote: match.status._remote }
|
|
|
|
]
|
|
|
|
const cache = queryClient.getQueryData<Mastodon.Status>(queryKey)
|
|
|
|
|
|
|
|
if (cache) {
|
|
|
|
handleNavigation('Tab-Shared-Toot', { toot: cache })
|
2021-03-21 23:06:53 +01:00
|
|
|
return
|
2023-01-03 23:57:23 +01:00
|
|
|
} else {
|
|
|
|
try {
|
2023-01-15 18:00:58 +01:00
|
|
|
response = await searchLocalStatus(url, true)
|
2023-01-03 23:57:23 +01:00
|
|
|
} catch {}
|
|
|
|
if (response) {
|
|
|
|
handleNavigation('Tab-Shared-Toot', { toot: response })
|
|
|
|
return
|
|
|
|
}
|
2021-03-21 23:06:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an account can be found
|
2023-01-03 23:57:23 +01:00
|
|
|
if (match?.account) {
|
|
|
|
if (!match.account._remote && match.account.id) {
|
|
|
|
handleNavigation('Tab-Shared-Account', { account: match.account.id })
|
|
|
|
return
|
2021-03-21 23:06:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-03 23:57:23 +01:00
|
|
|
let response: Mastodon.Account | undefined = undefined
|
|
|
|
|
|
|
|
const queryKey: QueryKeyAccount = [
|
|
|
|
'Account',
|
|
|
|
{ id: match.account.id, url: url, _remote: match.account._remote }
|
|
|
|
]
|
|
|
|
const cache = queryClient.getQueryData<Mastodon.Status>(queryKey)
|
|
|
|
|
|
|
|
if (cache) {
|
|
|
|
handleNavigation('Tab-Shared-Account', { account: cache })
|
2021-03-21 23:06:53 +01:00
|
|
|
return
|
2023-01-03 23:57:23 +01:00
|
|
|
} else {
|
|
|
|
try {
|
2023-01-15 18:00:58 +01:00
|
|
|
response = await searchLocalAccount(url, true)
|
2023-01-03 23:57:23 +01:00
|
|
|
} catch {}
|
|
|
|
if (response) {
|
|
|
|
handleNavigation('Tab-Shared-Account', { account: response })
|
|
|
|
return
|
|
|
|
}
|
2021-03-21 23:06:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 18:17:57 +01:00
|
|
|
// Some links might end with an empty space at the end that triggers an error
|
2023-01-03 23:57:23 +01:00
|
|
|
switch (getGlobalStorage.string('app.browser')) {
|
2023-01-22 18:17:57 +01:00
|
|
|
case 'external':
|
|
|
|
await Linking.openURL(url.trim())
|
|
|
|
break
|
2023-01-03 23:57:23 +01:00
|
|
|
case 'internal':
|
2023-01-22 18:17:57 +01:00
|
|
|
default:
|
2023-01-03 23:57:23 +01:00
|
|
|
await WebBrowser.openBrowserAsync(url.trim(), {
|
|
|
|
dismissButtonStyle: 'close',
|
|
|
|
enableBarCollapsing: true,
|
|
|
|
...(await browserPackage())
|
|
|
|
})
|
|
|
|
break
|
2020-12-20 18:41:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default openLink
|