@@ -2,43 +2,63 @@ import React from "react";
22
33export type Props = {
44 tag : string ;
5- label : string ;
65 today : string ;
76 total : string ;
7+ ok : string ;
8+ error : string ;
9+ loading : string ;
810} ;
911
12+ type State = "ok" | "error" | "loading" ;
13+
1014type Data = {
1115 today : string ;
1216 total : string ;
1317} ;
1418
15- const HitsInfo : React . FC < Props > = ( { tag, label, today : todayLabel , total : totalLabel } ) => {
19+ const HitsInfo : React . FC < Props > = ( {
20+ tag,
21+ today : todayLabel ,
22+ total : totalLabel ,
23+ ok,
24+ error,
25+ loading,
26+ } ) => {
27+ const [ state , setState ] = React . useState < State > ( "loading" ) ;
1628 const [ data , setData ] = React . useState < Data | null > ( null ) ;
1729 React . useEffect ( ( ) => {
1830 const hitsUrl = new URL ( "https://hits.zkitefly.eu.org" ) ;
1931 hitsUrl . searchParams . set ( "tag" , tag ) ;
2032
2133 fetch ( hitsUrl , { method : "HEAD" } )
22- . then ( async ( response ) => {
23- if ( response . status !== 200 ) return ;
24- const { headers } = response ;
25- const total = headers . get ( "X-Total-Hits" ) ;
26- const today = headers . get ( "X-Today-Hits" ) ;
27- if ( total !== null && today !== null ) {
28- setData ( { total, today } ) ;
34+ . then ( ( response ) => {
35+ if ( response . status === 200 ) {
36+ const { headers } = response ;
37+ const total = headers . get ( "X-Total-Hits" ) ;
38+ const today = headers . get ( "X-Today-Hits" ) ;
39+ if ( total !== null && today !== null ) {
40+ setData ( { total, today } ) ;
41+ setState ( "ok" ) ;
42+ return ;
43+ }
2944 }
45+ setState ( "error" ) ;
3046 } )
31- . catch ( ( ) => { } ) ;
47+ . catch ( ( ) => {
48+ setState ( "error" ) ;
49+ } ) ;
3250 } , [ ] ) ;
3351
34- if ( ! data ) return < > </ > ;
52+ if ( data === null ) {
53+ return < > { state === "ok" ? ok : state === "error" ? error : loading } </ > ;
54+ }
3555
3656 return (
37- < div className = "hits" >
38- < strong > { label } </ strong > < span title = { todayLabel } > { data . today } </ span >
57+ < >
58+ < span title = { todayLabel } > { data . today } </ span >
3959 { " / " }
4060 < span title = { totalLabel } > { data . total } </ span >
41- </ div >
61+ </ >
4262 ) ;
4363} ;
4464
0 commit comments