-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathuseUserProfile.tsx
More file actions
40 lines (34 loc) · 1.05 KB
/
useUserProfile.tsx
File metadata and controls
40 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use client";
import { useState, useEffect, useCallback } from "react";
import { SupabaseService } from "@/lib/supabase-service";
import { User } from "@/types";
export function useUserProfile() {
const [profile, setProfile] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const fetchProfile = useCallback(async () => {
try {
setIsLoading(true);
const data = await SupabaseService.getUserProfile();
setProfile(data);
setError(null);
} catch (err) {
setError(err as Error);
console.error("Failed to fetch user profile:", err);
} finally {
setIsLoading(false);
}
}, []);
useEffect(() => {
fetchProfile();
}, [fetchProfile]);
const refreshProfile = useCallback(async () => {
await fetchProfile();
}, [fetchProfile]);
return {
profile,
isLoading,
error,
refreshProfile,
};
}