返回 AiToEarn
useVideoThumbnail.ts
根目录 / project / aitoearn-web / src / hooks / useVideoThumbnail.ts
1 /**
2 * useVideoThumbnail - 自动获取视频封面的 Hook
3 * 从后端 API 获取视频封面,并通过 IndexedDB 持久化缓存
4 */
5
6 import { useEffect } from 'react'
7 import { useThumbnailCache } from '@/store/thumbnailCache'
8
9 export function useVideoThumbnail(videoUrl: string | null | undefined): string {
10 const thumbnailUrl = useThumbnailCache(state =>
11 videoUrl ? (state.cache[videoUrl] || '') : '',
12 )
13
14 useEffect(() => {
15 if (!videoUrl)
16 return
17 useThumbnailCache.getState().fetchThumbnail(videoUrl)
18 }, [videoUrl])
19
20 return thumbnailUrl
21 }
22
22 lines TYPESCRIPT