- 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.
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { useEffect } from "react";
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { supabaseBrowser } from "../supabase/client";
|
|
import { qk } from "../query/keys";
|
|
import { throwIfError } from "../api/_helpers";
|
|
|
|
export function useSession() {
|
|
const supabase = supabaseBrowser();
|
|
const qc = useQueryClient();
|
|
|
|
const query = useQuery({
|
|
queryKey: qk.session(),
|
|
queryFn: async () => {
|
|
const { data, error } = await supabase.auth.getSession();
|
|
throwIfError(error);
|
|
return data.session ?? null;
|
|
},
|
|
staleTime: Infinity,
|
|
});
|
|
|
|
useEffect(() => {
|
|
const { data } = supabase.auth.onAuthStateChange(() => {
|
|
qc.invalidateQueries({ queryKey: qk.session() });
|
|
qc.invalidateQueries({ queryKey: qk.meProfile() });
|
|
qc.invalidateQueries({ queryKey: qk.auth });
|
|
});
|
|
|
|
return () => data.subscription.unsubscribe();
|
|
}, [supabase, qc]);
|
|
|
|
return query;
|
|
}
|
|
|
|
export function useMeProfile() {
|
|
const supabase = supabaseBrowser();
|
|
|
|
return useQuery({
|
|
queryKey: qk.meProfile(),
|
|
queryFn: async () => {
|
|
const { data: u, error: uErr } = await supabase.auth.getUser();
|
|
throwIfError(uErr);
|
|
const userId = u.user?.id;
|
|
if (!userId) return null;
|
|
|
|
const { data, error } = await supabase
|
|
.from("usuarios_app")
|
|
.select("id,nombre_completo,email,externo,creado_en,actualizado_en")
|
|
.eq("id", userId)
|
|
.single();
|
|
|
|
// si aún no existe perfil en usuarios_app, permite null (tu seed/trigger puede crearlo)
|
|
if (error && (error as any).code === "PGRST116") return null;
|
|
|
|
throwIfError(error);
|
|
return data ?? null;
|
|
},
|
|
staleTime: 60_000,
|
|
});
|
|
}
|