This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network-status): Notify users when they're working offline (#2109)
- Loading branch information
1 parent
b0ba451
commit fa5bcb6
Showing
8 changed files
with
152 additions
and
0 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
51 changes: 51 additions & 0 deletions
51
src/__tests__/components/network-status/NetworkStatusMessage.test.tsx
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,51 @@ | ||
import { render, shallow } from 'enzyme' | ||
import React from 'react' | ||
|
||
import { useTranslation } from '../../../__mocks__/react-i18next' | ||
import { NetworkStatusMessage } from '../../../components/network-status' | ||
import { useNetworkStatus } from '../../../components/network-status/useNetworkStatus' | ||
|
||
jest.mock('../../../components/network-status/useNetworkStatus') | ||
const useNetworkStatusMock = (useNetworkStatus as unknown) as jest.MockInstance< | ||
ReturnType<typeof useNetworkStatus>, | ||
any | ||
> | ||
|
||
const englishTranslationsMock = { | ||
'networkStatus.offline': 'you are working in offline mode', | ||
'networkStatus.online': 'you are back online', | ||
} | ||
|
||
const useTranslationReturnValue = useTranslation() as any | ||
useTranslationReturnValue.t = (key: keyof typeof englishTranslationsMock) => | ||
englishTranslationsMock[key] | ||
const { t } = useTranslationReturnValue | ||
|
||
describe('NetworkStatusMessage', () => { | ||
it('returns null if the app has always been online', () => { | ||
useNetworkStatusMock.mockReturnValue({ | ||
isOnline: true, | ||
wasOffline: false, | ||
}) | ||
const wrapper = shallow(<NetworkStatusMessage />) | ||
expect(wrapper.equals(null as any)).toBe(true) | ||
}) | ||
it(`shows the message "${t('networkStatus.offline')}" if the app goes offline`, () => { | ||
useNetworkStatusMock.mockReturnValue({ | ||
isOnline: false, | ||
wasOffline: false, | ||
}) | ||
const wrapper = render(<NetworkStatusMessage />) | ||
expect(wrapper.text()).toContain(t('networkStatus.offline')) | ||
}) | ||
it(`shows the message "${t( | ||
'networkStatus.online', | ||
)}" if the app goes back online after it was offline`, () => { | ||
useNetworkStatusMock.mockReturnValue({ | ||
isOnline: true, | ||
wasOffline: true, | ||
}) | ||
const wrapper = render(<NetworkStatusMessage />) | ||
expect(wrapper.text()).toContain(t('networkStatus.online')) | ||
}) | ||
}) |
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,57 @@ | ||
import React, { useState } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { useNetworkStatus } from './useNetworkStatus' | ||
|
||
const ONLINE_COLOR = 'rgba(0, 255, 0, 0.55)' | ||
const OFFLINE_COLOR = 'rgba(255, 0, 0, 0.65)' | ||
const OPACITY_TRANSITION_TIME = 4000 | ||
const BASE_STYLE = { | ||
height: '50px', | ||
pointerEvents: 'none' as 'none', | ||
transition: `opacity ${OPACITY_TRANSITION_TIME}ms ease-in`, | ||
} | ||
|
||
export const NetworkStatusMessage = () => { | ||
const { t } = useTranslation() | ||
const { isOnline, wasOffline } = useNetworkStatus() | ||
const [shouldRender, setShouldRender] = useState(true) | ||
const [opacity, setOpacity] = useState(1) | ||
|
||
if (isOnline && !wasOffline) { | ||
return null | ||
} | ||
|
||
if (!isOnline && opacity !== 1) { | ||
setShouldRender(true) | ||
setOpacity(1) | ||
} | ||
|
||
if (isOnline && wasOffline && opacity !== 0) { | ||
setOpacity(0) | ||
setTimeout(() => { | ||
setShouldRender(false) | ||
}, OPACITY_TRANSITION_TIME) | ||
} | ||
|
||
if (!shouldRender) { | ||
return null | ||
} | ||
|
||
const style = { | ||
...BASE_STYLE, | ||
backgroundColor: isOnline ? ONLINE_COLOR : OFFLINE_COLOR, | ||
opacity, | ||
} | ||
|
||
return ( | ||
<div | ||
className={`fixed-bottom d-flex justify-content-center align-items-center ${ | ||
isOnline ? 'online' : 'offline' | ||
}`} | ||
style={style} | ||
> | ||
{isOnline ? t('networkStatus.online') : t('networkStatus.offline')} | ||
</div> | ||
) | ||
} |
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,2 @@ | ||
export { NetworkStatusMessage } from './NetworkStatusMessage' | ||
export { useNetworkStatus } from './useNetworkStatus' |
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,4 @@ | ||
export interface NetworkStatus { | ||
isOnline: boolean | ||
wasOffline: boolean | ||
} |
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,28 @@ | ||
import { useState, useEffect } from 'react' | ||
|
||
import { NetworkStatus } from './types' | ||
|
||
export const useNetworkStatus = (): NetworkStatus => { | ||
const isOnline = navigator.onLine | ||
const [networkStatus, setNetworkStatus] = useState({ | ||
isOnline, | ||
wasOffline: !isOnline, | ||
}) | ||
const handleOnline = () => { | ||
setNetworkStatus((prevState) => ({ ...prevState, isOnline: true })) | ||
} | ||
const handleOffline = () => { | ||
setNetworkStatus((prevState) => ({ ...prevState, isOnline: false, wasOffline: true })) | ||
} | ||
useEffect(() => { | ||
window.addEventListener('online', handleOnline) | ||
window.addEventListener('offline', handleOffline) | ||
|
||
return () => { | ||
window.removeEventListener('online', handleOnline) | ||
window.removeEventListener('offline', handleOffline) | ||
} | ||
}, []) | ||
|
||
return networkStatus | ||
} |
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,6 @@ | ||
export default { | ||
networkStatus: { | ||
offline: 'you are working in offline mode', | ||
online: 'you are back online', | ||
}, | ||
} |
fa5bcb6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: