Spaces:
Sleeping
Sleeping
| import { useEffect, useState, useCallback } from "react"; | |
| import { supabase } from "@/integrations/supabase/client"; | |
| import { useAuth } from "@/contexts/AuthContext"; | |
| export interface Profile { | |
| id: string; | |
| username: string; | |
| total_xp: number; | |
| level: number; | |
| best_productivity: number; | |
| best_focus_score: number; | |
| sessions_count: number; | |
| } | |
| export function useProfile() { | |
| const { user } = useAuth(); | |
| const [profile, setProfile] = useState<Profile | null>(null); | |
| const [loading, setLoading] = useState(true); | |
| const fetchProfile = useCallback(async () => { | |
| if (!user) { | |
| setProfile(null); | |
| setLoading(false); | |
| return; | |
| } | |
| const { data } = await supabase | |
| .from("profiles") | |
| .select("*") | |
| .eq("id", user.id) | |
| .maybeSingle(); | |
| setProfile(data as Profile | null); | |
| setLoading(false); | |
| }, [user]); | |
| useEffect(() => { | |
| fetchProfile(); | |
| }, [fetchProfile]); | |
| return { profile, loading, refetch: fetchProfile }; | |
| } | |