@@ -8,9 +8,10 @@ import {
88 SpinnerGapIcon ,
99 UserIcon ,
1010} from "@phosphor-icons/react" ;
11- import { useState } from "react" ;
11+ import { useEffect , useRef , useState } from "react" ;
1212import { toast } from "sonner" ;
1313import { CreateOrganizationDialog } from "@/components/organizations/create-organization-dialog" ;
14+ import { useOrganizationsContext } from "@/components/providers/organizations-provider" ;
1415import { Avatar , AvatarFallback , AvatarImage } from "@/components/ui/avatar" ;
1516import { Button } from "@/components/ui/button" ;
1617import {
@@ -125,16 +126,40 @@ function OrganizationSelectorTrigger({
125126}
126127
127128export function OrganizationSelector ( ) {
128- const { data : organizations , isPending : isLoadingOrgs } =
129- authClient . useListOrganizations ( ) ;
130- const { data : activeOrganization , isPending : isLoadingActive } =
131- authClient . useActiveOrganization ( ) ;
129+ const { organizations, activeOrganization, isLoading } =
130+ useOrganizationsContext ( ) ;
132131 const [ isOpen , setIsOpen ] = useState ( false ) ;
133132 const [ showCreateDialog , setShowCreateDialog ] = useState ( false ) ;
134133 const [ query , setQuery ] = useState ( "" ) ;
135134 const [ isSwitching , setIsSwitching ] = useState ( false ) ;
136135
137- const isLoading = isLoadingOrgs || isLoadingActive ;
136+ const prevStateRef = useRef < {
137+ isLoading : boolean ;
138+ organizationsCount : number ;
139+ hasActiveOrg : boolean ;
140+ activeOrgName ?: string ;
141+ } | null > ( null ) ;
142+
143+ useEffect ( ( ) => {
144+ const currentState = {
145+ isLoading,
146+ organizationsCount : organizations . length ,
147+ hasActiveOrg : ! ! activeOrganization ,
148+ activeOrgName : activeOrganization ?. name ,
149+ } ;
150+
151+ const prevState = prevStateRef . current ;
152+ if (
153+ ! prevState ||
154+ prevState . isLoading !== currentState . isLoading ||
155+ prevState . organizationsCount !== currentState . organizationsCount ||
156+ prevState . hasActiveOrg !== currentState . hasActiveOrg ||
157+ prevState . activeOrgName !== currentState . activeOrgName
158+ ) {
159+ console . log ( "[OrganizationSelector] State changed:" , currentState ) ;
160+ prevStateRef . current = currentState ;
161+ }
162+ } , [ isLoading , organizations . length , activeOrganization ] ) ;
138163
139164 const handleSelectOrganization = async ( organizationId : string | null ) => {
140165 if ( organizationId === activeOrganization ?. id ) {
0 commit comments