pinafore/routes/_utils/offlineNotification.js

25 lines
626 B
JavaScript
Raw Normal View History

2018-01-19 05:25:12 +00:00
import debounce from 'lodash/debounce'
import { toast } from './toast'
const OFFLINE_DELAY = 1000
const notifyOffline = debounce(() => {
2018-01-19 08:51:51 +00:00
toast.say('You seem to be offline. You can still read toots while offline.')
2018-01-19 05:25:12 +00:00
}, OFFLINE_DELAY)
const observe = online => {
if (!localStorage.store_currentInstance) {
return // only show notification for logged-in users
}
document.body.classList.toggle('offline', !online)
if (!online) {
notifyOffline()
}
}
2018-01-19 07:37:43 +00:00
if (!navigator.onLine) {
observe(false)
}
2018-01-19 05:25:12 +00:00
window.addEventListener('offline', () => observe(false));
window.addEventListener('online', () => observe(true));