Let users upload videos, tag them with one or more entries, and watch them from a new Discovery Videos page. Backend: - Add Video and VideoEntry models and relations - Add video controller and /video routes - Add video thumbnail generation service (ffmpeg-static) - Add video upload storage location and config Frontend: - Add DiscoveryVideosPage with sidebar link and route - Add AddVideoDialog, VideoDropzone, EntryMultiSelect, VideoCard and VideoPlayerDialog components - Add discoveryVideos API client and useDiscoveryVideos hook - Add format helpers, video types and normalizers - Add skeleton for the page Docs: - Document the feature in docs-vault
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import { useInfiniteQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { toast } from "sonner";
|
|
import {
|
|
discoveryVideosApi,
|
|
type UpdateVideoData,
|
|
} from "@/api/discoveryVideos";
|
|
|
|
export const discoveryVideoKeys = {
|
|
all: ["discovery-videos"] as const,
|
|
};
|
|
|
|
export function useDiscoveryVideos() {
|
|
return useInfiniteQuery({
|
|
queryKey: discoveryVideoKeys.all,
|
|
queryFn: ({ pageParam }) =>
|
|
discoveryVideosApi.getAll((pageParam as number) ?? 1, 24),
|
|
initialPageParam: 1,
|
|
getNextPageParam: (lastPage) =>
|
|
lastPage.page < lastPage.totalPages ? lastPage.page + 1 : undefined,
|
|
});
|
|
}
|
|
|
|
export function useUploadVideos() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
formData,
|
|
onProgress,
|
|
}: {
|
|
formData: FormData;
|
|
onProgress?: (percent: number) => void;
|
|
}) => discoveryVideosApi.create(formData, onProgress),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: discoveryVideoKeys.all });
|
|
toast.success("Video uploaded successfully");
|
|
},
|
|
onError: () => {
|
|
toast.error("Failed to upload video");
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateVideo() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({ id, data }: { id: number; data: UpdateVideoData }) =>
|
|
discoveryVideosApi.update(id, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: discoveryVideoKeys.all });
|
|
toast.success("Video updated");
|
|
},
|
|
onError: () => {
|
|
toast.error("Failed to update video");
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteVideo() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (id: number) => discoveryVideosApi.remove(id),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: discoveryVideoKeys.all });
|
|
toast.success("Video deleted");
|
|
},
|
|
onError: () => {
|
|
toast.error("Failed to delete video");
|
|
},
|
|
});
|
|
}
|