feat: add image preview for large view cards

- Extract YouTube video thumbnails from URLs
- Display thumbnail images as background in large preview cards
- Add gradient overlay for better text contrast
- Fallback to icon placeholder for non-YouTube URLs
- Handle multiple YouTube URL formats (watch, youtu.be, shorts)
- Gracefully handle missing images with icon fallback
This commit is contained in:
Gigi
2025-10-03 10:16:22 +02:00
parent bd3193957c
commit 57c5be9907
3 changed files with 67 additions and 7 deletions

39
src/utils/imagePreview.ts Normal file
View File

@@ -0,0 +1,39 @@
// Utility to extract preview images from URLs
export const extractYouTubeVideoId = (url: string): string | null => {
// Handle various YouTube URL formats
const patterns = [
/(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([^&\n?#]+)/,
/youtube\.com\/shorts\/([^&\n?#]+)/,
]
for (const pattern of patterns) {
const match = url.match(pattern)
if (match && match[1]) {
return match[1]
}
}
return null
}
export const getYouTubeThumbnail = (url: string): string | null => {
const videoId = extractYouTubeVideoId(url)
if (!videoId) return null
// Use maxresdefault for best quality, falls back to hqdefault if not available
return `https://img.youtube.com/vi/${videoId}/hqdefault.jpg`
}
export const getPreviewImage = (url: string, type: string): string | null => {
// YouTube videos
if (type === 'youtube') {
return getYouTubeThumbnail(url)
}
// For other URLs, we would need to fetch OG tags
// but CORS will block us on localhost
// Return null for now and show placeholder
return null
}