fix(video): prevent duplicate thumbnail requests by adding isRetrieving check

Add isRetrieving function to check if a thumbnail request is already pending before making a new one. This prevents duplicate requests and potential errors when multiple thumbnail fetches are attempted for the same video.
This commit is contained in:
icarus 2025-10-14 01:01:35 +08:00
parent 2015b2e58c
commit 3c6f32dbac
2 changed files with 12 additions and 3 deletions

View File

@ -92,7 +92,7 @@ export const useProviderVideos = (providerId: string) => {
}
}
const { data, error } = useSWR('video/openai/videos', fetcher, { refreshInterval: 3000 })
const { retrieveThumbnail } = useVideoThumbnail()
const { retrieveThumbnail, isRetrieving } = useVideoThumbnail()
useEffect(() => {
if (error) {
logger.error('Failed to fetch video status updates', error)
@ -128,6 +128,7 @@ export const useProviderVideos = (providerId: string) => {
const newVideo = { ...storeVideo, status: 'completed', thumbnail: null, metadata: retrievedVideo } as const
setVideo(newVideo)
// Try to get thumbnail
if (isRetrieving(storeVideo.id)) return
retrieveThumbnail(newVideo)
.then((thumbnail) => {
const latestVideo = videosRef.current?.find((v) => v.id === newVideo.id)

View File

@ -14,6 +14,14 @@ export const useVideoThumbnail = () => {
return `video-thumbnail-${id}`
}, [])
const isRetrieving = useCallback(
(id: string) => {
const key = getThumbnailKey(id)
return pendingSet.has(key)
},
[getThumbnailKey]
)
const retrieveThumbnail = useCallback(
async (video: Video): Promise<string> => {
const provider = getProviderById(video.providerId)
@ -21,7 +29,7 @@ export const useVideoThumbnail = () => {
throw new Error(`Provider not found for id ${video.providerId}`)
}
const thumbnailKey = getThumbnailKey(video.id)
if (pendingSet.has(thumbnailKey)) {
if (isRetrieving(video.id)) {
throw new Error('Thumbnail retrieval already pending')
}
@ -82,5 +90,5 @@ export const useVideoThumbnail = () => {
[getThumbnailKey]
)
return { getThumbnailKey, retrieveThumbnail, removeThumbnail }
return { getThumbnailKey, retrieveThumbnail, removeThumbnail, isRetrieving }
}