@@ -12,25 +12,21 @@ import Loader from 'react-loader-spinner'
1212export interface MenuAppBarProps extends StoreProps {
1313 menuClicked : ( ) => void
1414}
15+
16+ interface NetworkState {
17+ since : string
18+ online : boolean
19+ rtt : number
20+ type : string
21+ saveData : boolean
22+ downLink : number
23+ downLinkMax : number
24+ effectiveType : string
25+ }
26+
1527// https://medium.com/@vivekjoy /usenetwork-create-a-custom-react-hook-to-detect-online-and-offline-network-status-and-get-network-4a2e12c7e58b
1628// https://v1.mui.com/demos/app-bar/
1729class MenuAppBar extends React . Component < MenuAppBarProps > {
18-
19- /*
20- constructor(props: MenuAppBarProps) {
21- super(props)
22- /*const [state, setState] = useState(() => {
23- return {
24- since: undefined,
25- online: navigator.onLine,
26- ...this.getNetworkConnectionInfo(),
27- }
28- })
29- // const info = this.getNetworkConnectionInfo()
30- //
31- }
32- */
33-
3430
3531 render ( ) : JSX . Element {
3632 // TODO: Info Button explain all relevant aspects to consider the salary which are not matched by the survey..
@@ -53,27 +49,42 @@ class MenuAppBar extends React.Component<MenuAppBarProps> {
5349 }
5450
5551 get loader ( ) : JSX . Element {
56- const maxChunks = Object . values ( CHUNK_COUNT_PER_YEAR )
57- . reduce ( ( previousValue : number , currentValue : number ) => {
58- return 0 + previousValue + currentValue
59- } )
60- //
61- const chunksDownloaded = Object . values ( this . props . entryStore ! . parsedDataByYear )
62- . map ( ( resultSetForYear ) => resultSetForYear . chunksParsed )
63- . reduce ( ( previousValue : number , currentValue : number ) => {
64- return 0 + previousValue + currentValue
65- } , 0 )
66- const loadingPercentage = Math . round ( chunksDownloaded / maxChunks * 100 )
67- if ( loadingPercentage > 99 ) {
52+ // If entryStore or controlStore is not available, show no loader
53+ if ( ! this . props . entryStore || ! this . props . controlStore ) {
54+ return < div > </ div >
55+ }
56+
57+ // Get the currently selected year
58+ const selectedYearStr = this . props . controlStore . controlState . selectedYear ;
59+ if ( ! selectedYearStr ) {
60+ return < div > </ div >
61+ }
62+
63+ // Get max chunks for the selected year
64+ const maxChunks = CHUNK_COUNT_PER_YEAR [ selectedYearStr ] || 0 ;
65+ if ( maxChunks === 0 ) {
66+ return < div > </ div >
67+ }
68+
69+ // Get chunks parsed for the selected year
70+ const yearData = this . props . entryStore . parsedDataByYear [ parseInt ( selectedYearStr , 10 ) ] ;
71+ const chunksDownloaded = yearData ? yearData . chunksParsed : 0 ;
72+
73+ // Calculate loading percentage
74+ const loadingPercentage = Math . round ( ( chunksDownloaded / maxChunks ) * 100 ) ;
75+
76+ // Hide loader when loading is complete
77+ if ( loadingPercentage >= 100 ) {
6878 return < div > </ div >
6979 }
80+
7081 return (
7182 < div style = { { padding : 'auto' , position : 'absolute' , right : '25px' } } >
72- < Typography variant = 'body1' >
73- < div style = { { } } >
83+ < div style = { { } } >
84+ < Typography variant = 'body1' >
7485 { loadingPercentage } %
75- </ div >
76- </ Typography >
86+ </ Typography >
87+ </ div >
7788 < Loader
7889 type = "Audio"
7990 color = "#F48024"
@@ -84,13 +95,21 @@ class MenuAppBar extends React.Component<MenuAppBarProps> {
8495 )
8596 }
8697
87- getNetworkConnectionInfo ( ) : any {
88- const connection = this . getNetworkConnection ( )
98+ getNetworkConnectionInfo ( ) : NetworkState {
99+ const defaults : NetworkState = {
100+ since : new Date ( ) . toString ( ) ,
101+ online : false ,
102+ rtt : 0 , type : '' , saveData : false ,
103+ downLink : 0 , downLinkMax : 0 , effectiveType : '' ,
104+ }
105+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
106+ const connection : any = this . getNetworkConnection ( )
89107 if ( ! connection ) {
90- return { }
108+ return defaults
91109 }
92- //
93110 return {
111+ since : new Date ( ) . toString ( ) ,
112+ online : navigator . onLine ,
94113 rtt : connection . rtt ,
95114 type : connection . type ,
96115 saveData : connection . saveData ,
@@ -99,38 +118,27 @@ class MenuAppBar extends React.Component<MenuAppBarProps> {
99118 effectiveType : connection . effectiveType ,
100119 }
101120 }
102-
103- useNetwork ( ) : any {
104- const [ state , setState ] = useState ( ( ) => {
105- return {
106- since : undefined ,
107- online : navigator . onLine ,
108- ...this . getNetworkConnectionInfo ( ) ,
109- }
110- } )
121+
122+ useNetwork ( ) : NetworkState {
123+ const [ state , setState ] = useState ( this . getNetworkConnectionInfo ( ) )
111124 useEffect ( ( ) => {
112125 const handleOnline = ( ) : void => {
113- setState (
114- ( prevState : any ) : any => ( {
115- ...prevState ,
116- online : true ,
117- since : new Date ( ) . toString ( ) ,
118- } ) as any )
126+ setState ( ( prevState : NetworkState ) : NetworkState => ( {
127+ ...prevState ,
128+ online : true ,
129+ } ) )
119130 }
120- const handleOffline = ( ) : any => {
121- setState (
122- ( prevState : any ) : any => (
123- {
124- ...prevState ,
125- online : false ,
126- since : new Date ( ) . toString ( ) ,
127- } )
128- )
131+ const handleOffline = ( ) : void => {
132+ setState ( ( prevState : NetworkState ) : NetworkState => ( {
133+ ...prevState ,
134+ online : false ,
135+ } ) )
129136 }
130- const handleConnectionChange = ( ) : any => {
131- setState ( ( prevState : any ) => ( {
137+ const handleConnectionChange = ( _event : Event ) : void => {
138+ const networkInfo = this . getNetworkConnectionInfo ( )
139+ setState ( ( prevState : NetworkState ) => ( {
132140 ...prevState ,
133- ...this . getNetworkConnectionInfo ( ) ,
141+ ...networkInfo ,
134142 } ) )
135143 }
136144 window . addEventListener ( 'online' , handleOnline )
@@ -146,16 +154,10 @@ class MenuAppBar extends React.Component<MenuAppBarProps> {
146154 return state
147155 }
148156
149- getNetworkConnection ( ) : NetworkInformation & any {
150- return (
151- navigator . connection
152- // ||
153- //navigator.mozConnection ||
154- // navigator.webkitConnection ||
155- // null
156- )
157+ getNetworkConnection ( ) : EventTarget {
158+ return null as unknown as EventTarget // navigator.connection!
157159 }
158160
159161}
160162
161- export default inject ( ...injectClause ) ( observer ( MenuAppBar ) )
163+ export default inject ( ...injectClause ) ( observer ( MenuAppBar ) )
0 commit comments