|
| 1 | +import { useQuery } from '@tanstack/react-query' |
| 2 | +import { useSession } from 'next-auth/react' |
| 3 | +import { useEffect } from 'react' |
| 4 | + |
| 5 | +import type { UserProfile } from '@/types/user' |
| 6 | + |
| 7 | +const USER_PROFILE_STORAGE_KEY = 'codebuff-user-profile' |
| 8 | + |
| 9 | +// Helper functions for local storage |
| 10 | +const getUserProfileFromStorage = (): UserProfile | null => { |
| 11 | + if (typeof window === 'undefined') return null |
| 12 | + |
| 13 | + try { |
| 14 | + const stored = localStorage.getItem(USER_PROFILE_STORAGE_KEY) |
| 15 | + if (!stored) return null |
| 16 | + |
| 17 | + const parsed = JSON.parse(stored) |
| 18 | + // Convert created_at string back to Date if it exists |
| 19 | + if (parsed.created_at) { |
| 20 | + parsed.created_at = new Date(parsed.created_at) |
| 21 | + } |
| 22 | + return parsed |
| 23 | + } catch { |
| 24 | + return null |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const setUserProfileToStorage = (profile: UserProfile) => { |
| 29 | + if (typeof window === 'undefined') return |
| 30 | + |
| 31 | + try { |
| 32 | + localStorage.setItem(USER_PROFILE_STORAGE_KEY, JSON.stringify(profile)) |
| 33 | + } catch { |
| 34 | + // Silently fail if localStorage is not available |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +const clearUserProfileFromStorage = () => { |
| 39 | + if (typeof window === 'undefined') return |
| 40 | + |
| 41 | + try { |
| 42 | + localStorage.removeItem(USER_PROFILE_STORAGE_KEY) |
| 43 | + } catch { |
| 44 | + // Silently fail if localStorage is not available |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export const useUserProfile = () => { |
| 49 | + const { data: session } = useSession() |
| 50 | + |
| 51 | + const query = useQuery<UserProfile>({ |
| 52 | + queryKey: ['user-profile'], |
| 53 | + queryFn: async () => { |
| 54 | + const response = await fetch('/api/user/profile') |
| 55 | + if (!response.ok) { |
| 56 | + throw new Error('Failed to fetch user profile') |
| 57 | + } |
| 58 | + const data = await response.json() |
| 59 | + |
| 60 | + // Convert created_at string to Date if it exists |
| 61 | + if (data.created_at) { |
| 62 | + data.created_at = new Date(data.created_at) |
| 63 | + } |
| 64 | + |
| 65 | + return data |
| 66 | + }, |
| 67 | + enabled: !!session?.user, |
| 68 | + staleTime: 5 * 60 * 1000, // 5 minutes |
| 69 | + initialData: () => { |
| 70 | + // Return undefined if no data, which is compatible with useQuery |
| 71 | + return getUserProfileFromStorage() ?? undefined |
| 72 | + }, |
| 73 | + }) |
| 74 | + |
| 75 | + // Persist to localStorage whenever data changes |
| 76 | + useEffect(() => { |
| 77 | + if (query.data) { |
| 78 | + setUserProfileToStorage(query.data) |
| 79 | + } |
| 80 | + }, [query.data]) |
| 81 | + |
| 82 | + // Clear localStorage when user logs out |
| 83 | + useEffect(() => { |
| 84 | + if (!session?.user) { |
| 85 | + clearUserProfileFromStorage() |
| 86 | + } |
| 87 | + }, [session?.user]) |
| 88 | + |
| 89 | + return { |
| 90 | + ...query, |
| 91 | + clearCache: clearUserProfileFromStorage, |
| 92 | + } |
| 93 | +} |
0 commit comments