-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from rossbulat/rb-offline-monitor
feat: Offline monitor
- Loading branch information
Showing
7 changed files
with
106 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2024 @rossbulat/console authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
export class OnlineStatusController { | ||
// Set up online / offline event listeners. Relays information to `document` for the UI to handle. | ||
static initOnlineEvents() { | ||
window.addEventListener('offline', async () => { | ||
this.dispatchEvent(false); | ||
}); | ||
window.addEventListener('online', () => { | ||
this.dispatchEvent(true); | ||
}); | ||
} | ||
|
||
// Dispatch an `online-status` event. | ||
static dispatchEvent(online: boolean) { | ||
document.dispatchEvent( | ||
new CustomEvent('online-status', { | ||
detail: { | ||
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,6 @@ | ||
// Copyright 2024 @rossbulat/console authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
export interface OnlineStatusEvent { | ||
online: 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,29 @@ | ||
// Copyright 2024 @rossbulat/console authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import styled from 'styled-components'; | ||
|
||
export const Wrapper = styled.div` | ||
background: var(--accent-color-primary); | ||
border: 0.5px solid var(--border-primary-color); | ||
/* TODO: make theme variable + dark mode support */ | ||
box-shadow: | ||
0 2px 3px -1px rgba(0, 0, 0, 0.05), | ||
0 1px 4px -2px rgba(0, 0, 0, 0.05); | ||
position: fixed; | ||
bottom: 0.5rem; | ||
right: 0.5rem; | ||
z-index: 999; | ||
padding: 0.6rem 0.9rem; | ||
border-radius: 0.6rem; | ||
display: flex; | ||
> h3, | ||
svg { | ||
color: var(--text-color-invert); | ||
} | ||
> svg { | ||
margin-right: 0.5rem; | ||
} | ||
`; |
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,36 @@ | ||
// Copyright 2024 @rossbulat/console authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import { Wrapper } from './Wrapper'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { faWarning } from '@fortawesome/free-solid-svg-icons'; | ||
import { useRef, useState } from 'react'; | ||
import { useEventListener } from 'usehooks-ts'; | ||
import { isCustomEvent } from 'Utils'; | ||
|
||
export const Offline = () => { | ||
// Whether the app is offline. | ||
const [offline, setOffline] = useState<boolean>(false); | ||
|
||
// Handle incoming online status updates. | ||
const handleOnlineStatus = (e: Event): void => { | ||
if (isCustomEvent(e)) { | ||
const { online } = e.detail; | ||
setOffline(!online); | ||
} | ||
}; | ||
|
||
// Listen for online status updates. | ||
const documentRef = useRef<Document>(document); | ||
useEventListener('online-status', handleOnlineStatus, documentRef); | ||
|
||
if (!offline) { | ||
return null; | ||
} | ||
return ( | ||
<Wrapper> | ||
<FontAwesomeIcon icon={faWarning} /> | ||
<h3>Connection appears to be offline</h3> | ||
</Wrapper> | ||
); | ||
}; |
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