186 lines
6.1 KiB
TypeScript
186 lines
6.1 KiB
TypeScript
"use client";
|
|
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { toast } from "sonner";
|
|
import { api } from "@/lib/api";
|
|
import { useAuthContext } from "@/context/auth-context";
|
|
import type { CreateChannelRequest, UpdateChannelRequest } from "@/lib/types";
|
|
|
|
export function useConfig() {
|
|
return useQuery({
|
|
queryKey: ["config"],
|
|
queryFn: () => api.config.get(),
|
|
staleTime: Infinity, // config doesn't change at runtime
|
|
});
|
|
}
|
|
|
|
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: (channel) => {
|
|
queryClient.invalidateQueries({ queryKey: ["channels"] });
|
|
toast.success(`Channel "${channel.name}" created`);
|
|
},
|
|
onError: (e: Error) => toast.error(e.message),
|
|
});
|
|
}
|
|
|
|
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] });
|
|
toast.success(`Channel "${updated.name}" saved`);
|
|
},
|
|
onError: (e: Error) => toast.error(e.message),
|
|
});
|
|
}
|
|
|
|
export function useDeleteChannel() {
|
|
const { token } = useAuthContext();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => api.channels.delete(id, token!),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["channels"] });
|
|
toast.success("Channel deleted");
|
|
},
|
|
onError: (e: Error) => toast.error(e.message),
|
|
});
|
|
}
|
|
|
|
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] });
|
|
toast.success("Schedule generated");
|
|
},
|
|
onError: (e: Error) => toast.error(`Schedule failed: ${e.message}`),
|
|
});
|
|
}
|
|
|
|
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, channelPassword?: string) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["broadcast", channelId, channelPassword],
|
|
queryFn: () => api.schedule.getCurrentBroadcast(channelId, token ?? "", channelPassword),
|
|
enabled: !!channelId,
|
|
refetchInterval: 30_000,
|
|
retry: false,
|
|
});
|
|
}
|
|
|
|
export function useEpg(channelId: string, from?: string, until?: string, channelPassword?: string) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["epg", channelId, from, until, channelPassword],
|
|
queryFn: () => api.schedule.getEpg(channelId, token ?? "", from, until, channelPassword),
|
|
enabled: !!channelId,
|
|
});
|
|
}
|
|
|
|
export function useConfigHistory(channelId: string) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["config-history", channelId],
|
|
queryFn: () => api.channels.listConfigHistory(channelId, token!),
|
|
enabled: !!token && !!channelId,
|
|
});
|
|
}
|
|
|
|
export function usePinSnapshot() {
|
|
const { token } = useAuthContext();
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ channelId, snapId, label }: { channelId: string; snapId: string; label: string | null }) =>
|
|
api.channels.patchConfigSnapshot(channelId, snapId, label, token!),
|
|
onSuccess: (_, { channelId }) => qc.invalidateQueries({ queryKey: ["config-history", channelId] }),
|
|
onError: (e: Error) => toast.error(e.message),
|
|
});
|
|
}
|
|
|
|
export function useRestoreConfig() {
|
|
const { token } = useAuthContext();
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ channelId, snapId }: { channelId: string; snapId: string }) =>
|
|
api.channels.restoreConfigSnapshot(channelId, snapId, token!),
|
|
onSuccess: (_, { channelId }) => {
|
|
qc.invalidateQueries({ queryKey: ["channels"] });
|
|
qc.invalidateQueries({ queryKey: ["config-history", channelId] });
|
|
},
|
|
onError: (e: Error) => toast.error(e.message),
|
|
});
|
|
}
|
|
|
|
export function useScheduleHistory(channelId: string) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["schedule-history", channelId],
|
|
queryFn: () => api.channels.listScheduleHistory(channelId, token!),
|
|
enabled: !!token && !!channelId,
|
|
});
|
|
}
|
|
|
|
export function useScheduleGeneration(channelId: string, genId: string | null) {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["schedule-generation", channelId, genId],
|
|
queryFn: () => api.channels.getScheduleGeneration(channelId, genId!, token!),
|
|
enabled: !!token && !!channelId && genId !== null,
|
|
});
|
|
}
|
|
|
|
export function useRollbackSchedule() {
|
|
const { token } = useAuthContext();
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ channelId, genId }: { channelId: string; genId: string }) =>
|
|
api.channels.rollbackSchedule(channelId, genId, token!),
|
|
onSuccess: (_, { channelId }) => {
|
|
qc.invalidateQueries({ queryKey: ["schedule-history", channelId] });
|
|
qc.invalidateQueries({ queryKey: ["schedule", channelId] });
|
|
},
|
|
onError: (e: Error) => toast.error(e.message),
|
|
});
|
|
}
|