Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Niloofar/ Added network status logic #81

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@babel/preset-env": "^7.24.5",
"@deriv-com/api-hooks": "^0.1.24",
"@deriv-com/translations": "^1.2.4",
"@deriv-com/ui": "^1.26.0",
"@deriv-com/ui": "^1.27.5",
"@deriv-com/utils": "latest",
"@deriv/deriv-api": "^1.0.15",
"@deriv/quill-design": "^1.2.24",
Expand Down
22 changes: 21 additions & 1 deletion src/components/AppFooter/AppFooter.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,32 @@
width: 0.8rem;
height: 0.8rem;
border-radius: 100%;
background-color: #4bb4b3;
margin-left: 2rem;

&-online {
background-color: #4bb4b3;
}

&-offline {
background-color: #ff444f;
}

&-blinking {
animation: blink 1s infinite alternate;
}
}

&__endpoint {
color: #377cfc;
text-decoration: underline;
}
}

@keyframes blink {
0% {
opacity: 1;
}
100% {
opacity: 0.2;
}
}
2 changes: 1 addition & 1 deletion src/components/AppFooter/AppFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import FullScreen from './FullScreen';
import HelpCentre from './HelpCentre';
import LanguageSettings from './LanguageSettings';
import Livechat from './Livechat';
import { NetworkStatus } from './NetworkStatus';
import NetworkStatus from './NetworkStatus';
import ResponsibleTrading from './ResponsibleTrading';
import { ServerTime } from './ServerTime';
import WhatsApp from './WhatsApp';
Expand Down
22 changes: 19 additions & 3 deletions src/components/AppFooter/NetworkStatus.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { useMemo } from 'react';
import clsx from 'clsx';
import { useNetworkStatus } from '@/hooks';
import { useTranslations } from '@deriv-com/translations';
import { TooltipMenuIcon } from '@deriv-com/ui';

export const NetworkStatus = () => {
const statusConfigs = {
blinking: {
className: 'app-footer__network-status-online app-footer__network-status-blinking',
tooltip: 'Connecting to server',
},
offline: { className: 'app-footer__network-status-offline', tooltip: 'Offline' },
online: { className: 'app-footer__network-status-online', tooltip: 'Online' },
};

const NetworkStatus = () => {
matin-deriv marked this conversation as resolved.
Show resolved Hide resolved
const status = useNetworkStatus();
const { localize } = useTranslations();
const { className, tooltip } = useMemo(() => statusConfigs[status], [status]);

return (
<TooltipMenuIcon
as='div'
className='app-footer__icon'
disableHover
tooltipContent={localize('Network status: Online')}
tooltipContent={localize('Network status: {{status}}', { status: tooltip })}
>
<div className='app-footer__network-status' />
<div className={clsx('app-footer__network-status', className)} />
</TooltipMenuIcon>
);
};

export default NetworkStatus;
2 changes: 2 additions & 0 deletions src/hooks/custom-hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export { default as useFullScreen } from './useFullScreen';
export { default as useIsAdvertiser } from './useIsAdvertiser';
export { default as useIsAdvertiserBarred } from './useIsAdvertiserBarred';
export { default as useModalManager } from './useModalManager';
export { default as useNavigatorOnline } from './useNavigatorOnline';
export { default as useNetworkStatus } from './useNetworkStatus';
export { default as usePoiPoaStatus } from './usePoiPoaStatus';
export { default as useQueryString } from './useQueryString';
export { default as useSendbird } from './useSendbird';
33 changes: 33 additions & 0 deletions src/hooks/custom-hooks/useNavigatorOnline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect, useState } from 'react';
matin-deriv marked this conversation as resolved.
Show resolved Hide resolved

/**
* Retrieves the current online status of the browser.
* @returns {boolean} The online status of the browser.
*/
const getOnlineStatus = () =>
typeof navigator !== 'undefined' && typeof navigator.onLine === 'boolean' ? navigator.onLine : true;

/**
* A custom React hook that tracks the online status of the browser.
* @returns {boolean} The current online status of the browser.
*/
const useNavigatorOnline = () => {
const [status, setStatus] = useState(getOnlineStatus());

const setOnline = () => setStatus(true);
const setOffline = () => setStatus(false);

useEffect(() => {
window.addEventListener('online', setOnline);
window.addEventListener('offline', setOffline);

return () => {
window.removeEventListener('online', setOnline);
window.removeEventListener('offline', setOffline);
};
}, []);
matin-deriv marked this conversation as resolved.
Show resolved Hide resolved

return status;
};

export default useNavigatorOnline;
22 changes: 22 additions & 0 deletions src/hooks/custom-hooks/useNetworkStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect, useState } from 'react';
import useNavigatorOnline from './useNavigatorOnline';

type TStatus = 'blinking' | 'offline' | 'online';

const useNetworkStatus = () => {
const [status, setStatus] = useState<TStatus>('online');
const networkStatus = useNavigatorOnline();

// TODO we need socket connection state from api-hooks whenever it finished we can update this part and check
matin-deriv marked this conversation as resolved.
Show resolved Hide resolved
// both navigatorStatus and socket Status
// for now we just check the user network status.

useEffect(() => {
if (networkStatus) setStatus('online');
else setStatus('offline');
}, [networkStatus]);

return status;
};

export default useNetworkStatus;
Loading