Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
use tanstack query in VideoThumbnail
Brendan Allan694412d54d26
diff --git a/apps/web/components/VideoThumbnail.tsx b/apps/web/components/VideoThumbnail.tsxindex 9be59339e..6de484dad 100644--- a/apps/web/components/VideoThumbnail.tsx+++ b/apps/web/components/VideoThumbnail.tsx@@ -1,5 +1,6 @@import { useUploadingContext } from "@/app/(org)/dashboard/caps/UploadingContext";import { LogoSpinner } from "@cap/ui";+import { useQuery } from "@tanstack/react-query";import clsx from "clsx";import Image from "next/image";import { memo, useEffect, useState } from "react";@@ -31,47 +32,37 @@ export const VideoThumbnail: React.FC<VideoThumbnailProps> = memo(objectFit = "cover",containerClass,}) => {- const [imageUrls, setImageUrls] = useState({ screen: "" });- const [loading, setLoading] = useState(true);- const [failed, setFailed] = useState(false);- const { uploadingCapId } = useUploadingContext()-- useEffect(() => {- // Reset states when videoId changes- setLoading(true);- setFailed(false);-- const fetchPreSignedUrls = async () => {- try {- // Add cache busting parameter to ensure we get fresh thumbnails- const cacheBuster = new Date().getTime();- const response = await fetch(- `/api/thumbnail?userId=${userId}&videoId=${videoId}&t=${cacheBuster}`- );- if (response.ok) {- const data = await response.json();- // Add cache busting to the thumbnail URL as well- setImageUrls({ screen: `${data.screen}${data.screen.includes('?') ? '&' : '?'}t=${cacheBuster}` });- } else {- console.error("Failed to fetch pre-signed URLs");- setFailed(true);- }- } catch (error) {- console.error("Error fetching pre-signed URLs:", error);- setFailed(true);- } finally {- // If we couldn't fetch the URL, we should stop showing the spinner- if (!imageUrls.screen) {- setLoading(false);- }+ const imageUrl = useQuery({+ queryKey: ["thumbnail", userId, videoId],+ queryFn: async () => {+ const cacheBuster = new Date().getTime();+ const response = await fetch(+ `/api/thumbnail?userId=${userId}&videoId=${videoId}&t=${cacheBuster}`+ );+ if (response.ok) {+ const data = await response.json();+ // Add cache busting to the thumbnail URL as well+ return `${data.screen}${+ data.screen.includes("?") ? "&" : "?"+ }t=${cacheBuster}`;+ } else {+ throw new Error("Failed to fetch pre-signed URLs");}- };+ },+ });- fetchPreSignedUrls();- }, [userId, videoId, uploadingCapId]);+ const { uploadingCapId } = useUploadingContext();++ useEffect(() => {+ imageUrl.refetch();+ }, [imageUrl, uploadingCapId]);const randomGradient = `linear-gradient(to right, ${generateRandomGrayScaleColor()}, ${generateRandomGrayScaleColor()})`;+ const [imageStatus, setImageStatus] = useState<+ "loading" | "error" | "success"+ >("loading");+return (<divclassName={clsx(@@ -79,32 +70,29 @@ export const VideoThumbnail: React.FC<VideoThumbnailProps> = memo(containerClass)}>- <div className="flex absolute top-0 left-0 z-10 justify-center items-center w-full h-full">- {failed ? (+ <div className="flex absolute inset-0 z-10 justify-center items-center">+ {imageUrl.isError || imageStatus === "error" ? (<divclassName="w-full h-full"style={{ backgroundImage: randomGradient }}- ></div>+ />) : (- loading === true && (+ (imageUrl.isPending || imageStatus === "loading") && (<LogoSpinner className="w-5 h-auto animate-spin md:w-8" />))}</div>- {imageUrls.screen && (+ {imageUrl.data && (<Image- src={imageUrls.screen}+ src={imageUrl.data}fill={true}sizes="(max-width: 768px) 100vw, 33vw"alt={alt}key={videoId}style={{ objectFit: objectFit as any }}className={clsx("w-full h-full", imageClass)}- onLoad={() => setLoading(false)}- onError={() => {- setFailed(true);- setLoading(false);- }}+ onLoad={() => setImageStatus("success")}+ onError={() => setImageStatus("error")}/>)}</div>