mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at thread-bug 83 lines 2.8 kB view raw
1import {useCallback} from 'react' 2import {Linking} from 'react-native' 3import * as WebBrowser from 'expo-web-browser' 4 5import {logEvent} from '#/lib/statsig/statsig' 6import { 7 createBskyAppAbsoluteUrl, 8 createProxiedUrl, 9 isBskyAppUrl, 10 isBskyRSSUrl, 11 isRelativeUrl, 12 toNiceDomain, 13} from '#/lib/strings/url-helpers' 14import {logger} from '#/logger' 15import {isNative} from '#/platform/detection' 16import {useInAppBrowser} from '#/state/preferences/in-app-browser' 17import {useTheme} from '#/alf' 18import {useDialogContext} from '#/components/Dialog' 19import {useSheetWrapper} from '#/components/Dialog/sheet-wrapper' 20import {useGlobalDialogsControlContext} from '#/components/dialogs/Context' 21 22export function useOpenLink() { 23 const enabled = useInAppBrowser() 24 const t = useTheme() 25 const sheetWrapper = useSheetWrapper() 26 const dialogContext = useDialogContext() 27 const {inAppBrowserConsentControl} = useGlobalDialogsControlContext() 28 29 const openLink = useCallback( 30 async (url: string, override?: boolean, shouldProxy?: boolean) => { 31 if (isBskyRSSUrl(url) && isRelativeUrl(url)) { 32 url = createBskyAppAbsoluteUrl(url) 33 } 34 35 if (!isBskyAppUrl(url)) { 36 logEvent('link:clicked', { 37 domain: toNiceDomain(url), 38 url, 39 }) 40 41 if (shouldProxy) { 42 url = createProxiedUrl(url) 43 } 44 } 45 46 if (isNative && !url.startsWith('mailto:')) { 47 if (override === undefined && enabled === undefined) { 48 // consent dialog is a global dialog, and while it's possible to nest dialogs, 49 // the actual components need to be nested. sibling dialogs on iOS are not supported. 50 // thus, check if we're in a dialog, and if so, close the existing dialog before opening the 51 // consent dialog -sfn 52 if (dialogContext.isWithinDialog) { 53 dialogContext.close(() => { 54 inAppBrowserConsentControl.open(url) 55 }) 56 } else { 57 inAppBrowserConsentControl.open(url) 58 } 59 return 60 } else if (override ?? enabled) { 61 await sheetWrapper( 62 WebBrowser.openBrowserAsync(url, { 63 presentationStyle: 64 WebBrowser.WebBrowserPresentationStyle.PAGE_SHEET, 65 toolbarColor: t.atoms.bg.backgroundColor, 66 controlsColor: t.palette.primary_500, 67 createTask: false, 68 }).catch(err => { 69 if (__DEV__) 70 logger.error('Could not open web browser', {message: err}) 71 Linking.openURL(url) 72 }), 73 ) 74 return 75 } 76 } 77 Linking.openURL(url) 78 }, 79 [enabled, inAppBrowserConsentControl, t, sheetWrapper, dialogContext], 80 ) 81 82 return openLink 83}