mirror of
https://github.com/dergigi/boris.git
synced 2025-12-20 16:14:20 +01:00
refactor(video): extract buildNativeVideoUrl to reusable utility (DRY)
This commit is contained in:
@@ -31,6 +31,7 @@ import {
|
|||||||
import AuthorCard from './AuthorCard'
|
import AuthorCard from './AuthorCard'
|
||||||
import { faBooks } from '../icons/customIcons'
|
import { faBooks } from '../icons/customIcons'
|
||||||
import { classifyUrl } from '../utils/helpers'
|
import { classifyUrl } from '../utils/helpers'
|
||||||
|
import { buildNativeVideoUrl } from '../utils/videoHelpers'
|
||||||
|
|
||||||
interface ContentPanelProps {
|
interface ContentPanelProps {
|
||||||
loading: boolean
|
loading: boolean
|
||||||
@@ -204,34 +205,6 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Video actions
|
// Video actions
|
||||||
const buildNativeVideoUrl = (url: string): string | null => {
|
|
||||||
try {
|
|
||||||
const u = new URL(url)
|
|
||||||
const host = u.hostname
|
|
||||||
if (host.includes('youtube.com')) {
|
|
||||||
const id = u.searchParams.get('v')
|
|
||||||
return id ? `youtube://watch?v=${id}` : `youtube://${u.pathname}${u.search}`
|
|
||||||
}
|
|
||||||
if (host === 'youtu.be') {
|
|
||||||
const id = u.pathname.replace('/', '')
|
|
||||||
return id ? `youtube://watch?v=${id}` : 'youtube://'
|
|
||||||
}
|
|
||||||
if (host.includes('vimeo.com')) {
|
|
||||||
const id = u.pathname.split('/').filter(Boolean)[0]
|
|
||||||
return id ? `vimeo://app.vimeo.com/videos/${id}` : 'vimeo://'
|
|
||||||
}
|
|
||||||
if (host.includes('dailymotion.com') || host === 'dai.ly') {
|
|
||||||
// dailymotion.com/video/<id> or dai.ly/<id>
|
|
||||||
const parts = u.pathname.split('/').filter(Boolean)
|
|
||||||
const id = host === 'dai.ly' ? parts[0] : (parts[1] || '')
|
|
||||||
return id ? `dailymotion://video/${id}` : 'dailymotion://'
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
} catch {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleOpenVideoExternal = () => {
|
const handleOpenVideoExternal = () => {
|
||||||
if (selectedUrl) window.open(selectedUrl, '_blank', 'noopener,noreferrer')
|
if (selectedUrl) window.open(selectedUrl, '_blank', 'noopener,noreferrer')
|
||||||
setShowVideoMenu(false)
|
setShowVideoMenu(false)
|
||||||
|
|||||||
36
src/utils/videoHelpers.ts
Normal file
36
src/utils/videoHelpers.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* Build native app deep link URL for video platforms
|
||||||
|
* Returns null if the platform doesn't have a known native app URL scheme
|
||||||
|
*/
|
||||||
|
export function buildNativeVideoUrl(url: string): string | null {
|
||||||
|
try {
|
||||||
|
const u = new URL(url)
|
||||||
|
const host = u.hostname
|
||||||
|
|
||||||
|
if (host.includes('youtube.com')) {
|
||||||
|
const id = u.searchParams.get('v')
|
||||||
|
return id ? `youtube://watch?v=${id}` : `youtube://${u.pathname}${u.search}`
|
||||||
|
}
|
||||||
|
|
||||||
|
if (host === 'youtu.be') {
|
||||||
|
const id = u.pathname.replace('/', '')
|
||||||
|
return id ? `youtube://watch?v=${id}` : 'youtube://'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (host.includes('vimeo.com')) {
|
||||||
|
const id = u.pathname.split('/').filter(Boolean)[0]
|
||||||
|
return id ? `vimeo://app.vimeo.com/videos/${id}` : 'vimeo://'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (host.includes('dailymotion.com') || host === 'dai.ly') {
|
||||||
|
const parts = u.pathname.split('/').filter(Boolean)
|
||||||
|
const id = host === 'dai.ly' ? parts[0] : (parts[1] || '')
|
||||||
|
return id ? `dailymotion://video/${id}` : 'dailymotion://'
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
} catch {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user