forked from DiceDB/alloy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue- DiceDB#26 Add Feature to Show DiceDB Server Connection Status …
…On Playground.
- Loading branch information
1 parent
572ca60
commit 66cdaf7
Showing
7 changed files
with
132 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { useEffect } from 'react'; | ||
import { Wifi, WifiOff, Loader } from 'lucide-react'; | ||
|
||
import { RESPONSE_CODES } from '@/shared/constants/ResponseCode'; | ||
import { useServerStatus } from './context/ServerContext'; | ||
|
||
const TerminalStatus = () => { | ||
const { serverStatus, responseStatusCode, checkServerStatus } = | ||
useServerStatus(); | ||
|
||
useEffect(() => { | ||
checkServerStatus(); | ||
}, []); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
return ( | ||
<div className="flex items-center gap-2"> | ||
{responseStatusCode === RESPONSE_CODES.OK ? ( | ||
<Wifi className="text-green-500" /> | ||
) : ( | ||
<WifiOff className="text-red-500" /> | ||
)} | ||
<span | ||
className={`text-sm ${ | ||
responseStatusCode === RESPONSE_CODES.OK | ||
? 'text-green-400' | ||
: 'text-red-400' | ||
}`} | ||
> | ||
{serverStatus === 'Connected' ? ( | ||
serverStatus | ||
) : responseStatusCode === RESPONSE_CODES.SERVICE_UNAVAILABLE ? ( | ||
'Failed to connect' | ||
) : ( | ||
<Loader className="animate-spin" /> | ||
)} | ||
</span> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TerminalStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use client'; | ||
|
||
import { SERVER_STATUS_URL } from '@/shared/constants/apiEndpoints'; | ||
import { RESPONSE_CODES } from '@/shared/constants/ResponseCode'; | ||
import React,{ createContext, useContext, useState } from 'react'; | ||
|
||
interface ServerStatusContextType { | ||
serverStatus: string; | ||
responseStatusCode: number; | ||
checkServerStatus: () => Promise<boolean>; | ||
} | ||
|
||
export const ServerStatusContext = createContext< | ||
ServerStatusContextType | undefined | ||
>(undefined); | ||
|
||
export const ServerStatusProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [serverStatus, setServerStatus] = useState<string>('Failed to connect'); | ||
const [responseStatusCode, setResponseStatusCode] = useState<number>(0); | ||
|
||
|
||
const checkServerStatus = async () => { | ||
try { | ||
const response = await fetch(SERVER_STATUS_URL, { | ||
method: 'GET', | ||
}); | ||
setResponseStatusCode(response.status); | ||
setServerStatus( | ||
response.status === RESPONSE_CODES.OK | ||
? 'Connected' | ||
: 'Failed to connect', | ||
); | ||
return response.status === RESPONSE_CODES.OK; | ||
} catch (error) { | ||
console.error(`Error occurred at useServerStatus: ${error}`); | ||
setServerStatus('Failed to connect'); | ||
setResponseStatusCode(RESPONSE_CODES.SERVICE_UNAVAILABLE); | ||
return false; | ||
} | ||
}; | ||
|
||
return ( | ||
<ServerStatusContext.Provider | ||
value={{ serverStatus, responseStatusCode, checkServerStatus }} | ||
> | ||
{children} | ||
</ServerStatusContext.Provider> | ||
); | ||
}; | ||
|
||
export const useServerStatus = () => { | ||
const context = useContext(ServerStatusContext); | ||
if (!context) { | ||
throw new Error('useServerStatus must be used within a ServerStatusProvider'); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const RESPONSE_CODES = { | ||
OK: 200, | ||
BAD_REQUEST: 400, | ||
UNAUTHORIZED: 401, | ||
FORBIDDEN: 403, | ||
NOT_FOUND: 404, | ||
INTERNAL_SERVER_ERROR: 500, | ||
SERVICE_UNAVAILABLE: 503, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// This folder will contain or hold constants. For example in this particular file we can store the api endpoints as constants | ||
export const CLI_COMMAND_URL = 'http://localhost:8080/cli'; | ||
export const SERVER_STATUS_URL = 'http://localhost:8080/health'; |