103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { api } from "@/lib/api";
|
|
import { useAuthContext } from "@/context/auth-context";
|
|
import type { CreateChannelRequest, UpdateChannelRequest } from "@/lib/types";
|
|
|
|
export function useChannels() {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["channels"],
|
|
// Public endpoint — no token needed for TV viewing
|
|
queryFn: () => api.channels.list(token ?? ""),
|
|
});
|
|
}
|
|
|
|
export function useChannel(id: string) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["channel", id],
|
|
queryFn: () => api.channels.get(id, token!),
|
|
enabled: !!token && !!id,
|
|
});
|
|
}
|
|
|
|
export function useCreateChannel() {
|
|
const { token } = useAuthContext();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: CreateChannelRequest) =>
|
|
api.channels.create(data, token!),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["channels"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateChannel() {
|
|
const { token } = useAuthContext();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, data }: { id: string; data: UpdateChannelRequest }) =>
|
|
api.channels.update(id, data, token!),
|
|
onSuccess: (updated) => {
|
|
queryClient.invalidateQueries({ queryKey: ["channels"] });
|
|
queryClient.invalidateQueries({ queryKey: ["channel", updated.id] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteChannel() {
|
|
const { token } = useAuthContext();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => api.channels.delete(id, token!),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["channels"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useGenerateSchedule() {
|
|
const { token } = useAuthContext();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (channelId: string) =>
|
|
api.schedule.generate(channelId, token!),
|
|
onSuccess: (_, channelId) => {
|
|
queryClient.invalidateQueries({ queryKey: ["schedule", channelId] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useActiveSchedule(channelId: string) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["schedule", channelId],
|
|
queryFn: () => api.schedule.getActive(channelId, token!),
|
|
enabled: !!token && !!channelId,
|
|
retry: false,
|
|
});
|
|
}
|
|
|
|
export function useCurrentBroadcast(channelId: string) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["broadcast", channelId],
|
|
queryFn: () => api.schedule.getCurrentBroadcast(channelId, token ?? ""),
|
|
enabled: !!channelId,
|
|
refetchInterval: 30_000,
|
|
retry: false,
|
|
});
|
|
}
|
|
|
|
export function useEpg(channelId: string, from?: string, until?: string) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["epg", channelId, from, until],
|
|
queryFn: () => api.schedule.getEpg(channelId, token ?? "", from, until),
|
|
enabled: !!channelId,
|
|
});
|
|
}
|