aboutsummaryrefslogtreecommitdiff
path: root/apps/web/components/new/utils.ts
blob: cd0cbc39ae1a0f96e43d9501fc5c64c64c7a4754 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"use client"

import { useQuery } from "@tanstack/react-query"

export function isYouTubeUrl(url: string | undefined | null): boolean {
	if (!url) return false
	return (
		url.includes("youtube.com") ||
		url.includes("youtu.be") ||
		url.includes("m.youtube.com")
	)
}

export function extractYouTubeVideoId(
	url: string | undefined | null,
): string | null {
	if (!url) return null

	// Handle youtu.be format
	const youtuBeMatch = url.match(/(?:youtu\.be\/)([a-zA-Z0-9_-]{11})/)
	if (youtuBeMatch?.[1]) return youtuBeMatch[1]

	// Handle youtube.com/watch?v= format
	const watchMatch = url.match(/(?:youtube\.com\/watch\?v=)([a-zA-Z0-9_-]{11})/)
	if (watchMatch?.[1]) return watchMatch[1]

	// Handle youtube.com/embed/ format
	const embedMatch = url.match(/(?:youtube\.com\/embed\/)([a-zA-Z0-9_-]{11})/)
	if (embedMatch?.[1]) return embedMatch[1]

	// Handle m.youtube.com format
	const mobileMatch = url.match(
		/(?:m\.youtube\.com\/watch\?v=)([a-zA-Z0-9_-]{11})/,
	)
	if (mobileMatch?.[1]) return mobileMatch[1]

	return null
}

export function useYouTubeChannelName(url: string | undefined | null) {
	const videoId = extractYouTubeVideoId(url)
	const videoUrl = videoId
		? `https://www.youtube.com/watch?v=${videoId}`
		: url || ""

	return useQuery({
		queryKey: ["youtube-channel", videoUrl],
		queryFn: async () => {
			if (!videoUrl) return null

			try {
				const response = await fetch(
					`https://www.youtube.com/oembed?url=${encodeURIComponent(videoUrl)}&format=json`,
				)
				if (!response.ok) return null
				const data = (await response.json()) as { author_name?: string }
				return data.author_name || null
			} catch {
				return null
			}
		},
		enabled: !!videoUrl && isYouTubeUrl(url),
		staleTime: 1000 * 60 * 60 * 24,
		retry: 1,
	})
}

export function getAbsoluteUrl(url: string): string {
	try {
		const urlObj = new URL(url)
		return urlObj.host.replace(/^www\./, "")
	} catch {
		const match = url.match(/^https?:\/\/([^/]+)/)
		const host = match?.[1] ?? url.replace(/^https?:\/\//, "")
		return host.replace(/^www\./, "")
	}
}