diff --git a/apps/mobile/src/hooks/useWebViewNavigation.tsx b/apps/mobile/src/hooks/useWebViewNavigation.tsx index b6d034896..d290aa1b0 100644 --- a/apps/mobile/src/hooks/useWebViewNavigation.tsx +++ b/apps/mobile/src/hooks/useWebViewNavigation.tsx @@ -1,10 +1,11 @@ -import { parseSafeUrl, transformVideoUrl } from "@follow/utils" +import { parseSafeUrl } from "@follow/utils" import type { RefObject } from "react" import { useCallback } from "react" import type WebView from "react-native-webview" import type { WebViewNavigation } from "react-native-webview" import { openLink } from "../lib/native" +import { resolveVideoUrlForMobileOpen } from "../lib/video-url" const allowHosts = new Set(["app.follow.is", "app.folo.is"]) export function useWebViewNavigation({ webViewRef }: { webViewRef: RefObject }) { @@ -18,7 +19,7 @@ export function useWebViewNavigation({ webViewRef }: { webViewRef: RefObject { + vi.stubGlobal("ELECTRON", false) + ;({ resolveVideoUrlForMobileOpen } = await import("./video-url")) +}) + +describe("resolveVideoUrlForMobileOpen", () => { + it("keeps YouTube watch URLs unchanged for mobile browser opens", () => { + const url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" + + expect(resolveVideoUrlForMobileOpen(url)).toBe(url) + }) + + it("keeps YouTube shorts URLs unchanged for mobile browser opens", () => { + const url = "https://www.youtube.com/shorts/dQw4w9WgXcQ" + + expect(resolveVideoUrlForMobileOpen(url)).toBe(url) + }) + + it("still transforms Bilibili links into the player URL", () => { + const url = "https://www.bilibili.com/video/BV1xx411c7mD" + const resolved = resolveVideoUrlForMobileOpen(url) + + expect(resolved).toContain("https://www.bilibili.com/blackboard/newplayer.html?") + expect(resolved).toContain("bvid=BV1xx411c7mD") + }) + + it("falls back to the original URL when no transform is available", () => { + const url = "https://example.com/post/1" + + expect(resolveVideoUrlForMobileOpen(url)).toBe(url) + }) +}) diff --git a/apps/mobile/src/lib/video-url.ts b/apps/mobile/src/lib/video-url.ts new file mode 100644 index 000000000..e3ef09999 --- /dev/null +++ b/apps/mobile/src/lib/video-url.ts @@ -0,0 +1,15 @@ +import { parseSafeUrl, transformVideoUrl } from "@follow/utils" + +const isYoutubeWatchUrl = (url: URL) => + url.hostname === "www.youtube.com" && + ((url.pathname === "/watch" && url.searchParams.has("v")) || url.pathname.startsWith("/shorts/")) + +export const resolveVideoUrlForMobileOpen = (url: string) => { + const parsedUrl = parseSafeUrl(url) + + if (parsedUrl && isYoutubeWatchUrl(parsedUrl)) { + return url + } + + return transformVideoUrl({ url }) || url +} diff --git a/apps/mobile/src/modules/entry-list/templates/EntryVideoItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntryVideoItem.tsx index fa792a128..1ae3a3064 100644 --- a/apps/mobile/src/modules/entry-list/templates/EntryVideoItem.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntryVideoItem.tsx @@ -3,7 +3,7 @@ import { useEntry } from "@follow/store/entry/hooks" import { unreadSyncService } from "@follow/store/unread/store" import { useIsLoggedIn } from "@follow/store/user/hooks" import { tracker } from "@follow/tracker" -import { formatDuration, transformVideoUrl } from "@follow/utils" +import { formatDuration } from "@follow/utils" import { memo, useMemo } from "react" import { useTranslation } from "react-i18next" import { Linking, View } from "react-native" @@ -15,6 +15,7 @@ import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable" import { Text } from "@/src/components/ui/typography/Text" import { openLink } from "@/src/lib/native" import { toast } from "@/src/lib/toast" +import { resolveVideoUrlForMobileOpen } from "@/src/lib/video-url" import { VideoContextMenu } from "../../context-menu/video" import { EntryGridFooter } from "../../entry-content/EntryGridFooter" @@ -141,10 +142,6 @@ const openVideo = async (url: string) => { } // Fallback to opening in in-app browser - const formattedUrl = openLinksInExternalApp - ? url - : transformVideoUrl({ - url, - }) || url + const formattedUrl = openLinksInExternalApp ? url : resolveVideoUrlForMobileOpen(url) openLink(formattedUrl) }