-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: headerbar PWA update notifications [LIBS-344] (#748)
- Loading branch information
Showing
22 changed files
with
924 additions
and
739 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,43 @@ | ||
import { | ||
Button, | ||
ButtonStrip, | ||
Modal, | ||
ModalActions, | ||
ModalContent, | ||
ModalTitle, | ||
} from '@dhis2/ui' | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
import i18n from '../locales' | ||
|
||
export function ConfirmUpdateModal({ clientsCount, onCancel, onConfirm }) { | ||
return ( | ||
<Modal position="middle"> | ||
<ModalTitle>{i18n.t('Save your data')}</ModalTitle> | ||
<ModalContent> | ||
{clientsCount | ||
? i18n.t( | ||
"Updating will reload all {{n}} open instances of this app, and any unsaved data will be lost. Save any data you need to, then click 'Reload' when ready.", | ||
{ n: clientsCount } | ||
) | ||
: // Fallback if clientsCount is unavailable: | ||
i18n.t( | ||
"Updating will reload all open instances of this app, and any unsaved data will be lost. Save any data you need to, then click 'Reload' when ready." | ||
)} | ||
</ModalContent> | ||
<ModalActions> | ||
<ButtonStrip end> | ||
<Button onClick={onCancel}>{i18n.t('Cancel')}</Button> | ||
<Button destructive onClick={onConfirm}> | ||
{i18n.t('Reload')} | ||
</Button> | ||
</ButtonStrip> | ||
</ModalActions> | ||
</Modal> | ||
) | ||
} | ||
ConfirmUpdateModal.propTypes = { | ||
clientsCount: PropTypes.number, | ||
onCancel: PropTypes.func, | ||
onConfirm: PropTypes.func, | ||
} |
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,42 @@ | ||
import { useConfig } from '@dhis2/app-runtime' | ||
import { HeaderBar } from '@dhis2/ui' | ||
import React from 'react' | ||
import { usePWAUpdateState } from '../utils/usePWAUpdateState' | ||
import { ConfirmUpdateModal } from './ConfirmUpdateModal' | ||
|
||
/** | ||
* Check for SW updates or a first activation, displaying an update notification | ||
* message in the HeaderBar profile menu. When an update is applied, if there are | ||
* multiple tabs of this app open, there's anadditional warning step because all | ||
* clients of the service worker will reload when there's an update, which may | ||
* cause data loss. | ||
*/ | ||
|
||
export function ConnectedHeaderBar() { | ||
const { appName } = useConfig() | ||
const { | ||
updateAvailable, | ||
confirmReload, | ||
confirmationRequired, | ||
clientsCount, | ||
onConfirmUpdate, | ||
onCancelUpdate, | ||
} = usePWAUpdateState() | ||
|
||
return ( | ||
<> | ||
<HeaderBar | ||
appName={appName} | ||
updateAvailable={updateAvailable} | ||
onApplyAvailableUpdate={confirmReload} | ||
/> | ||
{confirmationRequired ? ( | ||
<ConfirmUpdateModal | ||
clientsCount={clientsCount} | ||
onConfirm={onConfirmUpdate} | ||
onCancel={onCancelUpdate} | ||
/> | ||
) : null} | ||
</> | ||
) | ||
} |
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,18 @@ | ||
import { OfflineInterface } from '@dhis2/pwa' | ||
import PropTypes from 'prop-types' | ||
import React, { createContext, useContext } from 'react' | ||
|
||
const theOfflineInterface = new OfflineInterface() | ||
const OfflineInterfaceContext = createContext(theOfflineInterface) | ||
|
||
export const OfflineInterfaceProvider = ({ children }) => ( | ||
<OfflineInterfaceContext.Provider value={theOfflineInterface}> | ||
{children} | ||
</OfflineInterfaceContext.Provider> | ||
) | ||
|
||
OfflineInterfaceProvider.propTypes = { | ||
children: PropTypes.node, | ||
} | ||
|
||
export const useOfflineInterface = () => useContext(OfflineInterfaceContext) |
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,40 @@ | ||
import { | ||
REGISTRATION_STATE_WAITING, | ||
REGISTRATION_STATE_FIRST_ACTIVATION, | ||
} from '@dhis2/pwa' | ||
import PropTypes from 'prop-types' | ||
import { useEffect, useState } from 'react' | ||
import { useOfflineInterface } from './OfflineInterfaceContext' | ||
|
||
export const PWALoadingBoundary = ({ children }) => { | ||
const [pwaReady, setPWAReady] = useState(false) | ||
const offlineInterface = useOfflineInterface() | ||
|
||
useEffect(() => { | ||
const checkRegistration = async () => { | ||
const registrationState = | ||
await offlineInterface.getRegistrationState() | ||
const clientsInfo = await offlineInterface.getClientsInfo() | ||
if ( | ||
(registrationState === REGISTRATION_STATE_WAITING || | ||
registrationState === | ||
REGISTRATION_STATE_FIRST_ACTIVATION) && | ||
clientsInfo.clientsCount === 1 | ||
) { | ||
console.log( | ||
'Reloading on startup to activate waiting service worker' | ||
) | ||
offlineInterface.useNewSW() | ||
} else { | ||
setPWAReady(true) | ||
} | ||
} | ||
checkRegistration() | ||
}, [offlineInterface]) | ||
|
||
return pwaReady ? children : null | ||
} | ||
|
||
PWALoadingBoundary.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.