@@ -18,6 +18,7 @@ import { useDashboardData } from './hooks/useDashboardData';
1818import { useAppUsage } from './hooks/useAppUsage' ;
1919import { useAppFilters , APPS_PER_PAGE } from './hooks/useAppFilters' ;
2020import { useRepoView } from './hooks/useRepoView' ;
21+ import { useAppState } from './hooks/useAppState' ;
2122import type { Repository } from './types' ;
2223
2324const Container = styled . div `
@@ -162,16 +163,12 @@ const ThemeButton = styled.button`
162163` ;
163164
164165function App ( ) {
165- const [ token , setToken ] = useState ( '' ) ;
166- const [ enterpriseUrl , setEnterpriseUrl ] = useState ( '' ) ;
167- const [ isConnected , setIsConnected ] = useState ( false ) ;
168- const [ selectedOrg , setSelectedOrg ] = useState ( '' ) ;
169- const [ inactiveDays , setInactiveDays ] = useState ( 90 ) ;
166+ const { state , dispatch , setToken, setEnterpriseUrl , setSelectedOrg , setInactiveDays } = useAppState ( ) ;
167+ const {
168+ token , enterpriseUrl , isConnected , selectedOrg , inactiveDays ,
169+ isFirstLoad , usageLoadingStarted , smoothedAuditProgress , usageRefreshKey ,
170+ } = state ;
170171 const [ allRepositories ] = useState < Repository [ ] > ( [ ] ) ;
171- const [ isFirstLoad , setIsFirstLoad ] = useState ( true ) ;
172- const [ usageLoadingStarted , setUsageLoadingStarted ] = useState ( false ) ;
173- const [ smoothedAuditProgress , setSmoothedAuditProgress ] = useState ( { checked : 0 , total : 0 , found : 0 } ) ;
174- const [ usageRefreshKey , setUsageRefreshKey ] = useState ( 0 ) ;
175172
176173 const {
177174 organizations,
@@ -239,35 +236,37 @@ function App() {
239236
240237 // Track when first load completes
241238 if ( isFirstLoad && ! loading && usageLoadingStarted && ! usageLoading && installations . length > 0 ) {
242- setIsFirstLoad ( false ) ;
243- setUsageLoadingStarted ( false ) ;
239+ dispatch ( { type : 'FIRST_LOAD_COMPLETE' } ) ;
244240 }
245241
246242 // Smooth the audit progress - only allow values to increase
247243 useEffect ( ( ) => {
248244 if ( usageProgress ) {
249- setSmoothedAuditProgress ( prev => ( {
250- checked : Math . max ( prev . checked , usageProgress . appsChecked ) ,
251- total : Math . max ( prev . total , usageProgress . totalApps ) ,
252- found : Math . max ( prev . found , usageProgress . appsFound ) ,
253- } ) ) ;
245+ dispatch ( {
246+ type : 'UPDATE_AUDIT_PROGRESS' ,
247+ payload : {
248+ checked : usageProgress . appsChecked ,
249+ total : usageProgress . totalApps ,
250+ found : usageProgress . appsFound ,
251+ } ,
252+ } ) ;
254253 }
255- } , [ usageProgress ] ) ;
254+ } , [ usageProgress , dispatch ] ) ;
256255
257256 // Reset smoothed progress when audit log checking is complete
258257 useEffect ( ( ) => {
259258 if ( ! isFirstLoad && smoothedAuditProgress . total > 0 && smoothedAuditProgress . checked >= smoothedAuditProgress . total ) {
260259 const timer = setTimeout ( ( ) => {
261- setSmoothedAuditProgress ( { checked : 0 , total : 0 , found : 0 } ) ;
260+ dispatch ( { type : 'RESET_AUDIT_PROGRESS' } ) ;
262261 } , 1000 ) ;
263262 return ( ) => clearTimeout ( timer ) ;
264263 }
265- } , [ isFirstLoad , smoothedAuditProgress . checked , smoothedAuditProgress . total ] ) ;
264+ } , [ isFirstLoad , smoothedAuditProgress . checked , smoothedAuditProgress . total , dispatch ] ) ;
266265
267266 // Load app usage when apps are loaded and config is ready
268267 useEffect ( ( ) => {
269268 if ( apps . size > 0 && organizations . length > 0 && configLoaded ) {
270- setUsageLoadingStarted ( true ) ;
269+ dispatch ( { type : 'USAGE_LOADING_STARTED' } ) ;
271270 const slugs = Array . from ( apps . keys ( ) ) ;
272271 const orgLogins = organizations . map ( o => o . login ) ;
273272 loadUsage ( orgLogins , slugs ) ;
@@ -278,12 +277,10 @@ function App() {
278277 const handleConnect = async ( ) => {
279278 if ( isConnected ) {
280279 refreshData ( ) ;
281- setUsageRefreshKey ( prev => prev + 1 ) ;
280+ dispatch ( { type : 'RECONNECT' } ) ;
282281 setAppsPage ( 1 ) ;
283- setIsFirstLoad ( true ) ;
284- setUsageLoadingStarted ( false ) ;
285282 } else {
286- setIsConnected ( true ) ;
283+ dispatch ( { type : 'CONNECT' } ) ;
287284 }
288285 } ;
289286
0 commit comments