From 259aa46ef82697e5a6128208c24a9721ede528ec Mon Sep 17 00:00:00 2001 From: Gib Date: Tue, 20 May 2025 16:11:41 -0500 Subject: [PATCH] Updated Nav bar avatar to use hooks --- src/app/(auth-pages)/profile/page.tsx | 2 +- .../navigation/auth/AvatarDropdown.tsx | 68 +++++++------------ .../default/profile/AvatarUpload.tsx | 18 ++++- .../default/profile/ProfileForm.tsx | 2 +- src/lib/hooks/index.ts | 3 + src/lib/hooks/useProfile.ts | 4 +- 6 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 src/lib/hooks/index.ts diff --git a/src/app/(auth-pages)/profile/page.tsx b/src/app/(auth-pages)/profile/page.tsx index ae2b419..5f022b3 100644 --- a/src/app/(auth-pages)/profile/page.tsx +++ b/src/app/(auth-pages)/profile/page.tsx @@ -1,5 +1,5 @@ 'use client'; -import { useProfile } from '@/lib/hooks/useProfile'; +import { useProfile } from '@/lib/hooks'; import { AvatarUpload, ProfileForm } from '@/components/default/profile'; import { Card, diff --git a/src/components/default/navigation/auth/AvatarDropdown.tsx b/src/components/default/navigation/auth/AvatarDropdown.tsx index 5efcc41..d55c3c2 100644 --- a/src/components/default/navigation/auth/AvatarDropdown.tsx +++ b/src/components/default/navigation/auth/AvatarDropdown.tsx @@ -1,5 +1,6 @@ 'use client'; +import Link from 'next/link'; import { useState, useEffect } from 'react'; import { Avatar, @@ -12,60 +13,41 @@ import { DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui'; -import { getSignedUrl, getProfile, signOut } from '@/lib/actions'; - -import type { Profile } from '@/utils/supabase'; -import Link from 'next/link'; +import { useProfile, useAvatar } from '@/lib/hooks' +import { signOut } from '@/lib/actions'; +import { User } from 'lucide-react'; const AvatarDropdown = () => { - const [profile, setProfile] = useState(undefined); - const [signedUrl, setSignedUrl] = useState(undefined); + const { profile } = useProfile(); + const { avatarUrl, isLoading } = useAvatar(profile); const handleSignOut = async () => { await signOut(); }; - useEffect(() => { - const handleGetProfile = async () => { - try { - const profileResponse = await getProfile(); - if (!profileResponse.success) - throw new Error('Profile response unsuccessful'); - setProfile(profileResponse.data); - } catch (error) { - console.error('Error getting profile:', error); - } - }; - handleGetProfile().catch((error) => { - console.error('Error getting profile:', error); - }) - }, []); - - useEffect(() => { - const handleGetSignedUrl = async () => { - try { - const response = await getSignedUrl({ - bucket: 'avatars', - url: profile?.avatar_url ?? '', - }); - if (response.success) { - setSignedUrl(response.data); - } - } catch (error) { - console.error('Error getting signed URL:', error); - } - }; - handleGetSignedUrl().catch((error) => { - console.error('Error getting signed URL:', error); - }); - }, [profile]); + if (isLoading) { + return ( + + + + + + ); + } return ( - - - AN + + {avatarUrl ? ( + + ) : ( + + {profile?.full_name + ? profile.full_name.split(' ').map(n => n[0]).join('').toUpperCase() + : } + + )} diff --git a/src/components/default/profile/AvatarUpload.tsx b/src/components/default/profile/AvatarUpload.tsx index fc4d9d1..60e8a5c 100644 --- a/src/components/default/profile/AvatarUpload.tsx +++ b/src/components/default/profile/AvatarUpload.tsx @@ -10,7 +10,7 @@ type AvatarUploadProps = { }; export const AvatarUpload = ({ profile, onAvatarUploaded }: AvatarUploadProps) => { - const { avatarUrl } = useAvatar(profile); + const { avatarUrl, isLoading } = useAvatar(profile); const { isUploading, fileInputRef, uploadToStorage } = useFileUpload(); const handleAvatarClick = () => { @@ -27,6 +27,22 @@ export const AvatarUpload = ({ profile, onAvatarUploaded }: AvatarUploadProps) = } }; + if (isLoading) { + return ( +
+
+ + + {profile?.full_name + ? profile.full_name.split(' ').map(n => n[0]).join('').toUpperCase() + : } + + +
+
+ ); + } + return (
diff --git a/src/components/default/profile/ProfileForm.tsx b/src/components/default/profile/ProfileForm.tsx index ea387b9..73fb641 100644 --- a/src/components/default/profile/ProfileForm.tsx +++ b/src/components/default/profile/ProfileForm.tsx @@ -92,7 +92,7 @@ export function ProfileForm({ profile, isLoading, onSubmit }: ProfileFormProps) )} /> -
+