Files
acad-ia-2/src/data/hooks/useNotifications.ts
Alejandro Rosales 65a73ca99f feat: add subjects and tasks API, hooks, and related types
- Implemented subjects API with functions for creating, updating, and retrieving subjects, including history and bibliography.
- Added tasks API for managing user tasks, including listing and marking tasks as completed.
- Created hooks for managing AI interactions, authentication, subjects, tasks, and metadata queries.
- Established query keys for caching and managing query states.
- Introduced Supabase client and environment variable management for better configuration.
- Defined types for database and domain models to ensure type safety across the application.
2026-01-09 09:00:33 -06:00

50 lines
1.3 KiB
TypeScript

import { useEffect } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { qk } from "../query/keys";
import { notificaciones_marcar_leida, notificaciones_mias_list } from "../api/notifications.api";
import { supabaseBrowser } from "../supabase/client";
export function useMisNotificaciones() {
return useQuery({
queryKey: qk.notificaciones(),
queryFn: notificaciones_mias_list,
staleTime: 10_000,
});
}
/** 🔥 Opcional: realtime (si tienes Realtime habilitado) */
export function useRealtimeNotificaciones(enable = true) {
const supabase = supabaseBrowser();
const qc = useQueryClient();
useEffect(() => {
if (!enable) return;
const channel = supabase
.channel("rt-notificaciones")
.on(
"postgres_changes",
{ event: "*", schema: "public", table: "notificaciones" },
() => {
qc.invalidateQueries({ queryKey: qk.notificaciones() });
}
)
.subscribe();
return () => {
supabase.removeChannel(channel);
};
}, [enable, supabase, qc]);
}
export function useMarcarNotificacionLeida() {
const qc = useQueryClient();
return useMutation({
mutationFn: notificaciones_marcar_leida,
onSuccess: () => {
qc.invalidateQueries({ queryKey: qk.notificaciones() });
},
});
}