-
Notifications
You must be signed in to change notification settings - Fork 0
Update use-profile.ts #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,77 +3,83 @@ import { supabase } from "@/integrations/supabase/client"; | |||||
| import { useToast } from "@/hooks/use-toast"; | ||||||
| import { logger } from "@/lib/logger"; | ||||||
|
|
||||||
| // Define types for better type safety | ||||||
| interface Profile { | ||||||
| id: string; | ||||||
| subscription_status: string; | ||||||
| subscription_end_date: string | null; | ||||||
| created_at: string; | ||||||
| updated_at: string; | ||||||
| company_id: string | null; | ||||||
| role: string; | ||||||
| company?: Company; | ||||||
| } | ||||||
|
|
||||||
| interface Company { | ||||||
| id: string; | ||||||
| name: string; | ||||||
| created_at: string; | ||||||
| updated_at: string; | ||||||
| } | ||||||
|
|
||||||
| export function useProfile() { | ||||||
| const { toast } = useToast(); | ||||||
|
|
||||||
| return useQuery({ | ||||||
| queryKey: ['profile'], | ||||||
| return useQuery<Profile>({ | ||||||
| queryKey: ["profile"], | ||||||
| queryFn: async () => { | ||||||
| try { | ||||||
| // First check if we have a valid session | ||||||
| // Check if the user has a valid session | ||||||
| const { data: { session }, error: sessionError } = await supabase.auth.getSession(); | ||||||
|
|
||||||
| if (sessionError) { | ||||||
| logger.error("Session error:", { error: sessionError }); | ||||||
| // Sign out if session is invalid | ||||||
| if (sessionError || !session?.user) { | ||||||
| if (sessionError) logger.error("Session error:", { error: sessionError }); | ||||||
| await supabase.auth.signOut(); | ||||||
| throw sessionError; | ||||||
| } | ||||||
|
|
||||||
| if (!session?.user) { | ||||||
| logger.info("No authenticated user found"); | ||||||
| return null; | ||||||
| throw new Error("Session expired. Please log in again."); | ||||||
| } | ||||||
|
|
||||||
| const userId = session.user.id; | ||||||
| logger.info("Fetching profile for user:", { userId }); | ||||||
| // Fetch profile with a simpler query first | ||||||
|
|
||||||
| // Fetch the profile, including company details | ||||||
| const { data: profile, error: profileError } = await supabase | ||||||
| .from('profiles') | ||||||
| .select('id, subscription_status, subscription_end_date, created_at, updated_at, company_id, role') | ||||||
| .eq('id', userId) | ||||||
| .from("profiles") | ||||||
| .select(` | ||||||
| id, | ||||||
| subscription_status, | ||||||
| subscription_end_date, | ||||||
| created_at, | ||||||
| updated_at, | ||||||
| company_id, | ||||||
| role, | ||||||
| companies(id, name, created_at, updated_at) | ||||||
| `) | ||||||
| .eq("id", userId) | ||||||
| .single(); | ||||||
|
|
||||||
| if (profileError) { | ||||||
| logger.error("Profile fetch error:", { error: profileError }); | ||||||
| throw profileError; | ||||||
| } | ||||||
|
|
||||||
| // If profile has company_id, fetch company details in a separate query | ||||||
| if (profile?.company_id) { | ||||||
| const { data: company, error: companyError } = await supabase | ||||||
| .from('companies') | ||||||
| .select('*') | ||||||
| .eq('id', profile.company_id) | ||||||
| .single(); | ||||||
|
|
||||||
| if (companyError) { | ||||||
| logger.error("Company fetch error:", { error: companyError }); | ||||||
| throw companyError; | ||||||
| } | ||||||
|
|
||||||
| return { | ||||||
| ...profile, | ||||||
| company | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| logger.info("Profile data fetched successfully"); | ||||||
| return profile; | ||||||
| } catch (error: any) { | ||||||
| logger.error("Error in profile query:", { error }); | ||||||
| toast({ | ||||||
| title: "Error", | ||||||
| description: "Failed to load profile data. Please try refreshing the page.", | ||||||
| description: "Failed to load profile data. Please refresh the page or try again later.", | ||||||
| variant: "destructive", | ||||||
| }); | ||||||
| throw error; | ||||||
| } | ||||||
| }, | ||||||
| retry: 1, | ||||||
| retry: (failureCount, error: any) => { | ||||||
| // Skip retries for authentication errors | ||||||
| if (error?.status === 401 || error?.status === 403) return false; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Use block braces for ifs, whiles, etc. (
Suggested change
ExplanationIt is recommended to always use braces and create explicit statement blocks.Using the allowed syntax to just write a single statement can lead to very confusing |
||||||
| return failureCount < 1; // Retry once for other errors | ||||||
| }, | ||||||
| retryDelay: 1000, | ||||||
| refetchOnWindowFocus: false, | ||||||
| staleTime: 300000, // 5 minutes | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Use block braces for ifs, whiles, etc. (
use-braces)Explanation
It is recommended to always use braces and create explicit statement blocks.Using the allowed syntax to just write a single statement can lead to very confusing
situations, especially where subsequently a developer might add another statement
while forgetting to add the braces (meaning that this wouldn't be included in the condition).