From de81ec77d5888abcabcc116fe5be679afa377664 Mon Sep 17 00:00:00 2001 From: Whitewater Date: Tue, 4 Mar 2025 16:50:54 +0800 Subject: [PATCH] feat(mobile): add list and url support for deep link (#2958) --- apps/mobile/src/hooks/useIntentHandler.ts | 43 ++++++++++++---------- apps/mobile/src/screens/+native-intent.tsx | 3 ++ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/apps/mobile/src/hooks/useIntentHandler.ts b/apps/mobile/src/hooks/useIntentHandler.ts index a7464681a..5f6793b65 100644 --- a/apps/mobile/src/hooks/useIntentHandler.ts +++ b/apps/mobile/src/hooks/useIntentHandler.ts @@ -4,6 +4,9 @@ import { useEffect } from "react" // This needs to stay outside of react to persist between account switches let previousIntentUrl = "" +export const resetIntentUrl = () => { + previousIntentUrl = "" +} export function useIntentHandler() { const incomingUrl = Linking.useURL() @@ -13,33 +16,33 @@ export function useIntentHandler() { if (previousIntentUrl === incomingUrl) return previousIntentUrl = incomingUrl } - const id = extractFeedIdFromDeepLink(incomingUrl) - if (!id) { - console.warn("No feed id found in deep link:", incomingUrl) + + const searchParams = extractParamsFromDeepLink(incomingUrl) + if (!searchParams) { + console.warn("No valid params found in deep link:", incomingUrl) return } - router.push(`/follow?id=${id}`) - }, [incomingUrl]) + + router.push(`/follow?${searchParams.toString()}`) + }) } -// follow://add?id=1 -const extractFeedIdFromDeepLink = (incomingUrl: string | null) => { +// follow://add?id=41147805276726272 +// follow://add?type=list&id=60580187699502080 +// follow://add?type=url&url=rsshub://rsshub/routes/en +const extractParamsFromDeepLink = (incomingUrl: string | null): URLSearchParams | null => { if (!incomingUrl) return null - const url = parseUrl(incomingUrl) - if (!url) return null - if (url.protocol !== "follow:" || url.host !== "add") return null - - const feedId = url.searchParams.get("id") - - if (!feedId) return null - - return feedId -} - -const parseUrl = (url: string) => { try { - return new URL(url) + const url = new URL(incomingUrl) + if (url.protocol !== "follow:" || url.hostname !== "add") return null + + const { searchParams } = url + + // If no valid parameters were found (neither id nor url) + if (!searchParams.has("id") && !searchParams.has("url")) return null + + return searchParams } catch { return null } diff --git a/apps/mobile/src/screens/+native-intent.tsx b/apps/mobile/src/screens/+native-intent.tsx index 9bbfb8ea8..bb87dfcea 100644 --- a/apps/mobile/src/screens/+native-intent.tsx +++ b/apps/mobile/src/screens/+native-intent.tsx @@ -4,6 +4,9 @@ // // See https://docs.expo.dev/router/advanced/native-intent/#rewrite-incoming-native-deep-links +import { resetIntentUrl } from "../hooks/useIntentHandler" + export function redirectSystemPath(_options: { path: string; initial: boolean }) { + resetIntentUrl() return "/" }