tooot/src/components/openLink.ts

106 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-03-21 23:06:53 +01:00
import apiInstance from '@api/instance'
2022-12-04 00:35:13 +01:00
import browserPackage from '@helpers/browserPackage'
2021-05-12 15:40:55 +02:00
import navigationRef from '@helpers/navigationRef'
2022-11-12 17:52:50 +01:00
import { matchAccount, matchStatus } from '@helpers/urlMatcher'
2020-12-20 18:41:28 +01:00
import { store } from '@root/store'
2021-03-21 23:06:53 +01:00
import { SearchResult } from '@utils/queryHooks/search'
2021-01-01 16:48:16 +01:00
import { getSettingsBrowser } from '@utils/slices/settingsSlice'
2020-12-20 18:41:28 +01:00
import * as Linking from 'expo-linking'
import * as WebBrowser from 'expo-web-browser'
2021-03-21 23:06:53 +01:00
export let loadingLink = false
2021-08-29 16:08:02 +02:00
const openLink = async (url: string, navigation?: any) => {
2021-03-21 23:06:53 +01:00
if (loadingLink) {
return
}
2022-11-12 17:52:50 +01:00
const handleNavigation = (page: 'Tab-Shared-Toot' | 'Tab-Shared-Account', options: {}) => {
2021-03-21 23:06:53 +01:00
if (navigation) {
// @ts-ignore
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
}
}
// If a tooot can be found
2022-11-12 17:52:50 +01:00
const isStatus = matchStatus(url)
if (isStatus) {
if (isStatus.sameInstance) {
handleNavigation('Tab-Shared-Toot', { toot: { id: isStatus.id } })
2021-03-21 23:06:53 +01:00
return
}
loadingLink = true
let response
try {
response = await apiInstance<SearchResult>({
version: 'v2',
method: 'get',
url: 'search',
params: { type: 'statuses', q: url, limit: 1, resolve: true }
})
} catch {}
if (response && response.body && response.body.statuses.length) {
handleNavigation('Tab-Shared-Toot', {
toot: response.body.statuses[0]
})
loadingLink = false
return
}
}
// If an account can be found
2022-11-12 17:52:50 +01:00
const isAccount = matchAccount(url)
if (isAccount) {
if (isAccount.sameInstance) {
if (isAccount.style === 'default' && isAccount.id) {
handleNavigation('Tab-Shared-Account', { account: isAccount })
2021-03-21 23:06:53 +01:00
return
}
}
loadingLink = true
let response
try {
response = await apiInstance<SearchResult>({
version: 'v2',
method: 'get',
url: 'search',
2022-11-12 17:52:50 +01:00
params: {
type: 'accounts',
q: isAccount.sameInstance && isAccount.style === 'pretty' ? isAccount.username : url,
limit: 1,
resolve: true
}
2021-03-21 23:06:53 +01:00
})
} catch {}
if (response && response.body && response.body.accounts.length) {
handleNavigation('Tab-Shared-Account', {
account: response.body.accounts[0]
})
loadingLink = false
return
}
}
loadingLink = false
2020-12-20 18:41:28 +01:00
switch (getSettingsBrowser(store.getState())) {
2021-08-11 00:04:12 +02:00
// Some links might end with an empty space at the end that triggers an error
2020-12-20 18:41:28 +01:00
case 'internal':
2021-08-11 00:04:12 +02:00
await WebBrowser.openBrowserAsync(encodeURI(url), {
2021-02-28 17:41:21 +01:00
dismissButtonStyle: 'close',
2022-12-04 00:35:13 +01:00
enableBarCollapsing: true,
browserPackage: await browserPackage()
2021-02-28 17:41:21 +01:00
})
2020-12-20 18:41:28 +01:00
break
case 'external':
2021-08-11 00:04:12 +02:00
await Linking.openURL(encodeURI(url))
2020-12-20 18:41:28 +01:00
break
}
}
export default openLink