videofolxtv/client/src/hooks/use-playlists.ts
sebastjanartic 0465cfe7d3 Improves video previews with a darker theme and user-specific data
Updates thumbnail generation with a dark theme, shadow effects, and fetches a specific user ID.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 50814a1e-92e4-4968-856f-7bc7eedf5e8f
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/50814a1e-92e4-4968-856f-7bc7eedf5e8f/F0D1tCj
2025-08-04 20:06:47 +00:00

77 lines
2.5 KiB
TypeScript

import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@/lib/queryClient";
import { useToast } from "@/hooks/use-toast";
import type { Playlist, InsertPlaylist } from "@shared/schema";
const CURRENT_USER_ID = "5311f8eb-aba2-4f58-96bf-0ca99fe5907c"; // Test user from database
export function usePlaylists() {
const queryClient = useQueryClient();
const { toast } = useToast();
// Get user playlists
const useUserPlaylists = () => {
return useQuery({
queryKey: ["/api/playlists", CURRENT_USER_ID],
});
};
// Get single playlist
const usePlaylist = (playlistId: string) => {
return useQuery({
queryKey: ["/api/playlists", playlistId],
enabled: !!playlistId,
});
};
// Get playlist videos
const usePlaylistVideos = (playlistId: string) => {
return useQuery({
queryKey: ["/api/playlists", playlistId, "videos"],
enabled: !!playlistId,
});
};
// Add video to playlist
const addVideoToPlaylistMutation = useMutation({
mutationFn: ({ playlistId, videoId }: { playlistId: string; videoId: string }) =>
apiRequest(`/api/playlists/${playlistId}/videos`, "POST", {
videoId,
position: 0,
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["/api/playlists"] });
toast({ title: "Added to playlist!" });
},
onError: (error: any) => {
if (error.message?.includes("already in playlist")) {
toast({ title: "Video is already in this playlist", variant: "destructive" });
} else {
toast({ title: "Failed to add to playlist", variant: "destructive" });
}
},
});
// Remove video from playlist
const removeVideoFromPlaylistMutation = useMutation({
mutationFn: ({ playlistId, videoId }: { playlistId: string; videoId: string }) =>
apiRequest(`/api/playlists/${playlistId}/videos/${videoId}`, "DELETE"),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["/api/playlists"] });
toast({ title: "Removed from playlist" });
},
onError: () => {
toast({ title: "Failed to remove from playlist", variant: "destructive" });
},
});
return {
useUserPlaylists,
usePlaylist,
usePlaylistVideos,
addVideoToPlaylist: addVideoToPlaylistMutation.mutate,
removeVideoFromPlaylist: removeVideoFromPlaylistMutation.mutate,
isAddingToPlaylist: addVideoToPlaylistMutation.isPending,
isRemovingFromPlaylist: removeVideoFromPlaylistMutation.isPending,
};
}