We now have an AuthContext!

This commit is contained in:
2025-05-20 19:28:31 -05:00
parent 0f92f7eb7f
commit 8169c719f6
12 changed files with 389 additions and 398 deletions

View File

@ -1,3 +0,0 @@
export * from './useAvatar';
export * from './useFileUpload';
export * from './useProfile';

View File

@ -1,49 +0,0 @@
import { useState, useEffect } from 'react';
import { getSignedUrl } from '@/lib/actions';
import type { Profile } from '@/utils/supabase';
import { toast } from 'sonner';
export const useAvatar = (profile?: Profile) => {
const [avatarUrl, setAvatarUrl] = useState<string | undefined>(undefined);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
const getAvatarUrl = async () => {
if (profile?.avatar_url) {
try {
setIsLoading(true);
const response = await getSignedUrl({
bucket: 'avatars',
url: profile.avatar_url,
transform: {
quality: 20,
},
});
if (response.success) {
setAvatarUrl(response.data);
}
} catch (error) {
console.error('Error getting signed URL:', error);
} finally {
setIsLoading(false);
}
} else {
setAvatarUrl(undefined);
}
};
getAvatarUrl().catch((error) => {
toast.error(
error instanceof Error
? error.message
: 'Failed to get signed avatar url.',
);
});
}, [profile]);
return {
avatarUrl,
isLoading,
};
};

View File

@ -1,61 +0,0 @@
import { useState, useEffect } from 'react';
import { getProfile, updateProfile } from '@/lib/actions';
import type { Profile } from '@/utils/supabase';
import { toast } from 'sonner';
export const useProfile = () => {
const [profile, setProfile] = useState<Profile | undefined>(undefined);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const fetchProfile = async () => {
try {
setIsLoading(true);
const profileResponse = await getProfile();
if (!profileResponse.success)
throw new Error('Profile response unsuccessful');
setProfile(profileResponse.data);
} catch {
setProfile(undefined);
} finally {
setIsLoading(false);
}
};
fetchProfile().catch((error) => {
toast.error(
error instanceof Error ? error.message : 'Failed to get profile',
);
});
}, []);
const updateUserProfile = async (data: {
full_name?: string;
email?: string;
avatar_url?: string;
}) => {
try {
setIsLoading(true);
const result = await updateProfile(data);
if (!result.success) {
throw new Error(result.error ?? 'Failed to update profile');
}
setProfile(result.data);
toast.success('Profile updated successfully!');
return { success: true, data: result.data };
} catch (error) {
console.error('Error updating profile: ', error);
toast.error(
error instanceof Error ? error.message : 'Failed to update profile',
);
return { success: false, error };
} finally {
setIsLoading(false);
}
};
return {
profile,
isLoading,
updateProfile: updateUserProfile,
};
};