code cleanup, comments, scaffold prefs db

This commit is contained in:
Liam 2022-11-14 15:49:52 -05:00
parent 12f0785e1c
commit 563c4860c9

View file

@ -175,12 +175,30 @@ const DeleteAfter = () => {
</Pref> </Pref>
) )
}; };
/**
* WIP. No logic for delivering background notifications yet, just scaffolding for the view and database entry.
* @returns {[JSX.Element | null]} Checkbox to toggle background notifications. Returns null if app is not installed.
*/
const BackgroundPush = () => { const BackgroundPush = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const handleChange = () => { const [ backgroundPush, setBackgroundPush ] = useState(false);
console.log('The checkbox was toggled.'); const [ canBeToggled, setCanBeToggled ] = useState(false);
const handleChange = async () => {
setBackgroundPush(!backgroundPush);
await prefs.setBackgroundPush(backgroundPush);
}
// https://developer.mozilla.org/en-US/docs/Web/API/Window/appinstalled_event
// This event is only fired upon successful PWA installation.
useEffect(() => {
window.addEventListener('appinstalled', () => {
setCanBeToggled(true);
})
},[])
// Hide the option if we are not in a PWA.
if (!canBeToggled) {
return null;
} }
return ( return (
@ -502,76 +520,58 @@ const Language = () => {
</Pref> </Pref>
) )
}; };
/**
* Installation menu, with button that triggers PWA installation.
* @returns {[ JSX.Element | null ]} Installation card and button if the browser environment supports installing PWAs, null if not.
*/
const Installation = () => { const Installation = () => {
// https://web.dev/learn/pwa/installation-prompt/
const { t } = useTranslation(); // Need help setting up the locale const { t } = useTranslation(); // Need help setting up the locale
// https://web.dev/codelab-make-installable/
const [ isInstallableChrome, setIsInstallableChrome ] = useState(false);
const [ deferredPromptEvent, setDeferredPromptEvent ] = useState(null);
// Determine if browser environment passes installation criteria.
useEffect(() => {
if ('BeforeInstallPromptEvent' in window) {
setIsInstallableChrome(true);
}
});
// Save the deferred prompt event, pass it as a prop to the button.
useEffect(() => {
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
setDeferredPromptEvent(e);
});
});
// If the web app is in Google Chrome, is installable, and we are NOT in a PWA environment show this option.
// Otherwise, return an empty component.
if (isInstallableChrome && deferredPromptEvent) {
return ( return (
<Card sx={{p: 3}} aria-label={"Install ntfy"}> <Card sx={{p: 3}} aria-label={"Install ntfy"}>
<Typography variant="h5" sx={{marginBottom: 2}}> <Typography variant="h5" sx={{marginBottom: 2}}>
{"Install NTFY"} {"Install NTFY"}
</Typography> </Typography>
<Install/> <InstallChrome
deferredPrompt={deferredPromptEvent}
/>
</Card> </Card>
) )
} }
const Install = () => { // Return nothing if installation criteria are not met.
return null;
}
const InstallChrome = ({ deferredPrompt }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const [installationSupported, setInstallationSuported] = useState(false);
const [promptInstall, setPromptInstall] = useState(null);
const [isInstalled, setIsInstalled] = useState(false);
const [deferredPrompt, setDeferredPrompt] = useState();
useEffect(() => {
if ('BeforeInstallPromptEvent' in window) {
console.log('BeforeInstallPromptEvent supported but not fired.');
setInstallationSuported(true);
} else {
console.log('BeforeInstallPromptEvent not supported.');
return;
}
}, [])
const onClick = async () => { const onClick = async () => {
if (deferredPrompt) {
deferredPrompt.prompt(); deferredPrompt.prompt();
const { outcome } = await e.userChoice;
if (outcome === 'accepted') {
setIsInstalled(true);
} else if (outcome === 'dismissed') {
console.log('App installer was dismissed.')
}
}
}
useEffect(() => {
window.addEventListener("beforeinstallprompt", (e) => {
console.log('beforeinstallprompt fired.')
e.preventDefault();
setDeferredPrompt(e);
});
}, [])
if (!installationSupported) {
return (
<FormControl
fullWidth
variant="standard"
sx={{ m: 1 }}
>
<Button
variant="contained"
disabled={true}
>
Browser not supported.
</Button>
</FormControl>
)
} }
return ( return (
@ -582,7 +582,7 @@ const Install = () => {
> >
<Button <Button
variant="contained" variant="contained"
onClick={async () => await onClick()} onClick={async (e) => await onClick(e)}
> >
Install Install
</Button> </Button>