11'use client'
22
33import { Construction } from "lucide-react" ;
4- import { ComponentType , useCallback , useEffect , useMemo , useState } from "react" ;
4+ import React , { ComponentType } from "react" ;
55import { Loading } from "@/app/components/Loading" ;
66import { useTranslations } from 'next-intl' ;
7- import { SystemStatus } from "@/model/system" ;
7+ import ErrorMessage from "./components/ErrorMessage" ;
8+ import { useSystem } from "@/lib/fetch/system" ;
89
9- let globalSystemStatus : SystemStatus | null = null ;
10- let lastFetchTime = 0 ;
11- const CACHE_DURATION = 30000 ; // 30 seconds
12-
13- type ExtendedSystemStatus = SystemStatus | 'checking' ;
1410const WithSystemCheck = < P extends object > (
1511 WrappedComponent : ComponentType < P >
1612) : ComponentType < P > => {
1713
1814 return function WithSystemCheckComponent ( props : P ) {
1915 const t = useTranslations ( ) ;
20- const [ systemStatus , setSystemStatus ] = useState < ExtendedSystemStatus > ( ( ) => {
21- const now = Date . now ( ) ;
22- if ( globalSystemStatus && ( now - lastFetchTime ) < CACHE_DURATION ) {
23- return globalSystemStatus ;
24- }
25- return 'checking' ;
26- } ) ;
27- const [ loading , setLoading ] = useState < boolean > ( ( ) => {
28- const now = Date . now ( ) ;
29- return ! ( globalSystemStatus && ( now - lastFetchTime ) < CACHE_DURATION ) ;
30- } ) ;
31-
32- const checkSystemStatus = useCallback ( async ( ) : Promise < void > => {
33- const now = Date . now ( ) ;
34- if ( globalSystemStatus && ( now - lastFetchTime ) < CACHE_DURATION ) {
35- setSystemStatus ( globalSystemStatus ) ;
36- setLoading ( false ) ;
37- return ;
38- }
3916
40- setLoading ( true ) ;
4117
42- try {
43- const response = await fetch ( '/api/manage/system/status' , {
44- method : 'GET' ,
45- credentials : 'include' ,
46- // Add cache control for browser caching
47- cache : 'no-store'
48- } ) ;
18+ const { data, error, isFetching } = useSystem ( )
4919
50- if ( ! response . ok ) {
51- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
52- }
53-
54- const data : { status : SystemStatus } = await response . json ( ) ;
20+ if ( isFetching ) {
21+ return < Loading message = { t ( 'withsystemcheck.check_system_status' ) } />
22+ }
5523
56- // Update global cache
57- globalSystemStatus = data . status ;
58- lastFetchTime = Date . now ( ) ;
24+ if ( error ) {
25+ return < ErrorMessage error = { error . message } /> ;
26+ }
5927
60- setSystemStatus ( data . status ) ;
61- } catch ( error ) {
62- console . error ( 'Error fetching system status:' , error ) ;
63- // Keep previous status if available, otherwise set to inactive
64- if ( ! globalSystemStatus ) {
65- setSystemStatus ( 'inactive' ) ;
66- }
67- } finally {
68- setLoading ( false ) ;
69- }
70- } , [ ] ) ;
28+ if ( ! data ) {
29+ return null ;
30+ }
7131
72- useEffect ( ( ) => {
73- checkSystemStatus ( ) ;
74- } , [ checkSystemStatus ] ) ;
32+ if ( data . status . active ) {
33+ return < WrappedComponent { ... props } /> ;
34+ }
7535
76- // Memoize the inactive system UI to prevent unnecessary re-renders
77- const inactiveSystemUI = useMemo ( ( ) => (
36+ return (
7837 < div className = "bg-white rounded-2xl p-6 shadow-sm border border-gray-100" >
7938 < div className = "flex items-center gap-3 mb-2" >
8039 < div className = "w-8 h-8 bg-red-100 rounded-full flex items-center justify-center" >
@@ -84,26 +43,11 @@ const WithSystemCheck = <P extends object>(
8443 { t ( 'withsystemcheck.system_inactive' ) }
8544 </ h1 >
8645 </ div >
87- < p className = "text-gray-500 text-sm " >
88- { t ( 'withsystemcheck.system_unavailable_message' ) }
46+ < p className = "text-gray-500 text-md " >
47+ { data . status . message ?? t ( 'withsystemcheck.system_unavailable_message' ) }
8948 </ p >
9049 </ div >
91- ) , [ t ] ) ;
92-
93- // Memoize loading component
94- const loadingUI = useMemo ( ( ) => (
95- < Loading message = { t ( 'withsystemcheck.check_system_status' ) } />
96- ) , [ t ] ) ;
97-
98- if ( loading ) {
99- return loadingUI ;
100- }
101-
102- if ( systemStatus === 'active' ) {
103- return < WrappedComponent { ...props } /> ;
104- }
105-
106- return inactiveSystemUI ;
50+ )
10751 } ;
10852} ;
10953
0 commit comments