From 19a7b6ea807dc55619878c1aae7ac4967bdc9433 Mon Sep 17 00:00:00 2001 From: sujal-into Date: Fri, 22 Nov 2024 18:01:50 +0545 Subject: [PATCH] fix: hooks to fetch duration --- packages/hooks/feed/useVideoAudioDuration.tsx | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 packages/hooks/feed/useVideoAudioDuration.tsx diff --git a/packages/hooks/feed/useVideoAudioDuration.tsx b/packages/hooks/feed/useVideoAudioDuration.tsx new file mode 100644 index 000000000..97033bcf7 --- /dev/null +++ b/packages/hooks/feed/useVideoAudioDuration.tsx @@ -0,0 +1,24 @@ +import { useQuery } from "@tanstack/react-query"; + +import { getVideoDurationFromURL } from "@/utils/video"; + +export const useVideoAudioDuration = (url: string, duration: number) => { + const { data, isLoading } = useQuery( + ["getVideoDuration", url], + async () => { + if (duration) return duration; + + try { + const duration = await getVideoDurationFromURL(url); + console.log(duration); + return duration; + } catch (error) { + console.log(error); + return 0; + } + }, + { staleTime: Infinity }, + ); + + return { duration: data || 0, isLoading }; +};