11import { useReducer , useEffect , useCallback , useMemo } from 'react' ;
2- import { useSelector } from 'react-redux' ;
2+ import { useDispatch , useSelector } from 'react-redux' ;
33import { debounce } from 'lodash' ;
44import { Org } from 'constants/orgConstants' ;
55import { getWorkspaces } from 'redux/selectors/orgSelectors' ;
6+ import { fetchWorkspacesAction } from 'redux/reduxActions/orgActions' ;
67import UserApi from 'api/userApi' ;
78
89// State interface for the workspace manager
@@ -73,6 +74,7 @@ export function useWorkspaceManager({
7374} : UseWorkspaceManagerOptions ) {
7475 // Get workspaces from Redux
7576 const workspaces = useSelector ( getWorkspaces ) ;
77+ const reduxDispatch = useDispatch ( ) ;
7678
7779 // Initialize reducer with Redux total count
7880 const [ state , dispatch ] = useReducer ( workspaceReducer , {
@@ -81,6 +83,14 @@ export function useWorkspaceManager({
8183 } ) ;
8284
8385
86+
87+ /* ----- first-time fetch ------------------------------------------------ */
88+ useEffect ( ( ) => {
89+ if ( ! workspaces . isFetched && ! workspaces . loading ) {
90+ reduxDispatch ( fetchWorkspacesAction ( 1 , pageSize ) ) ;
91+ }
92+ } , [ workspaces . isFetched , workspaces . loading , pageSize , reduxDispatch ] ) ;
93+
8494 // API call to fetch workspaces (memoized for stable reference)
8595 const fetchWorkspacesPage = useCallback (
8696 async ( page : number , search ?: string ) => {
@@ -177,7 +187,7 @@ export function useWorkspaceManager({
177187 // State
178188 searchTerm : state . searchTerm ,
179189 currentPage : state . currentPage ,
180- isLoading : state . isLoading ,
190+ isLoading : state . isLoading || workspaces . loading ,
181191 displayWorkspaces,
182192 totalCount : currentTotalCount ,
183193
0 commit comments