about summary refs log tree commit diff
path: root/src/lib/hooks/useOpenLink.ts
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2024-10-12 02:42:58 +0300
committerGitHub <noreply@github.com>2024-10-11 16:42:58 -0700
commitd6322477fea0a55f1dd0c49865a248cb2f578f75 (patch)
tree1453d76eca95a8f6c86c4954d4180920dea0f830 /src/lib/hooks/useOpenLink.ts
parent8e16427497dc1d0b288e889c4a4a72abbfdf5a7e (diff)
downloadvoidsky-d6322477fea0a55f1dd0c49865a248cb2f578f75.tar.zst
Present in-app browser as sheet (#5718)
* use page sheet presentation

* move to its own file rather than sitting in prefs

* whoops, missed one
Diffstat (limited to 'src/lib/hooks/useOpenLink.ts')
-rw-r--r--src/lib/hooks/useOpenLink.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/lib/hooks/useOpenLink.ts b/src/lib/hooks/useOpenLink.ts
new file mode 100644
index 000000000..5b75695b8
--- /dev/null
+++ b/src/lib/hooks/useOpenLink.ts
@@ -0,0 +1,54 @@
+import {useCallback} from 'react'
+import {Linking} from 'react-native'
+import * as WebBrowser from 'expo-web-browser'
+
+import {
+  createBskyAppAbsoluteUrl,
+  isBskyRSSUrl,
+  isRelativeUrl,
+} from '#/lib/strings/url-helpers'
+import {isNative} from '#/platform/detection'
+import {useModalControls} from '#/state/modals'
+import {useInAppBrowser} from '#/state/preferences/in-app-browser'
+import {useTheme} from '#/alf'
+import {useSheetWrapper} from '#/components/Dialog/sheet-wrapper'
+
+export function useOpenLink() {
+  const {openModal} = useModalControls()
+  const enabled = useInAppBrowser()
+  const t = useTheme()
+  const sheetWrapper = useSheetWrapper()
+
+  const openLink = useCallback(
+    async (url: string, override?: boolean) => {
+      if (isBskyRSSUrl(url) && isRelativeUrl(url)) {
+        url = createBskyAppAbsoluteUrl(url)
+      }
+
+      if (isNative && !url.startsWith('mailto:')) {
+        if (override === undefined && enabled === undefined) {
+          openModal({
+            name: 'in-app-browser-consent',
+            href: url,
+          })
+          return
+        } else if (override ?? enabled) {
+          await sheetWrapper(
+            WebBrowser.openBrowserAsync(url, {
+              presentationStyle:
+                WebBrowser.WebBrowserPresentationStyle.PAGE_SHEET,
+              toolbarColor: t.atoms.bg.backgroundColor,
+              controlsColor: t.palette.primary_500,
+              createTask: false,
+            }),
+          )
+          return
+        }
+      }
+      Linking.openURL(url)
+    },
+    [enabled, openModal, t, sheetWrapper],
+  )
+
+  return openLink
+}