Add initialization status to push notification hook. Can be used to better control render strategy in cmpnts utilizing it. (#284)

This commit is contained in:
Dan Peterson 2021-11-12 11:06:07 -06:00 committed by GitHub
parent 6d217dbc50
commit c242c37869
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View file

@ -8,10 +8,19 @@ import Button from 'component/button';
import usePersistedState from 'effects/use-persisted-state'; import usePersistedState from 'effects/use-persisted-state';
export const BrowserNotificationBanner = () => { export const BrowserNotificationBanner = () => {
const { pushSupported, pushEnabled, pushPermission, pushToggle, pushErrorModal } = useBrowserNotifications(); const {
pushInitialized,
pushSupported,
pushEnabled,
pushPermission,
pushToggle,
pushErrorModal,
} = useBrowserNotifications();
const [hasAcknowledgedPush, setHasAcknowledgedPush] = usePersistedState('push-nag', false); const [hasAcknowledgedPush, setHasAcknowledgedPush] = usePersistedState('push-nag', false);
if (!pushSupported || pushEnabled || pushPermission === 'denied' || hasAcknowledgedPush) return null; if (!pushInitialized || !pushSupported || pushEnabled || pushPermission === 'denied' || hasAcknowledgedPush) {
return null;
}
const handleClose = () => setHasAcknowledgedPush(true); const handleClose = () => setHasAcknowledgedPush(true);

View file

@ -13,13 +13,17 @@ export default () => {
const [pushEnabled, setPushEnabled] = useState(false); const [pushEnabled, setPushEnabled] = useState(false);
const [pushSupported, setPushSupported] = useState(true); const [pushSupported, setPushSupported] = useState(true);
const [encounteredError, setEncounteredError] = useState(false); const [encounteredError, setEncounteredError] = useState(false);
const [pushInitialized, setPushInitialized] = useState(false);
const [user] = useState(selectUser(store.getState())); const [user] = useState(selectUser(store.getState()));
useEffect(() => { useEffect(() => {
setPushSupported(pushNotifications.supported); setPushSupported(pushNotifications.supported);
if (pushNotifications.supported) { if (pushNotifications.supported) {
pushNotifications.subscribed(user.id).then((isSubscribed: boolean) => setSubscribed(isSubscribed)); pushNotifications.subscribed(user.id).then((isSubscribed: boolean) => {
setSubscribed(isSubscribed);
setPushInitialized(true);
});
} }
}, [user]); }, [user]);
@ -59,6 +63,7 @@ export default () => {
}; };
return { return {
pushInitialized,
pushSupported, pushSupported,
pushEnabled, pushEnabled,
pushPermission, pushPermission,