pull/149/head
Philipp Heckel 2022-03-06 21:39:20 -05:00
parent a4fbb1b4c5
commit 3a76e4733c
3 changed files with 48 additions and 45 deletions

View File

@ -82,13 +82,21 @@ class ConnectionManager {
stateChanged(subscriptionId, state) { stateChanged(subscriptionId, state) {
if (this.stateListener) { if (this.stateListener) {
try {
this.stateListener(subscriptionId, state); this.stateListener(subscriptionId, state);
} catch (e) {
console.error(`[ConnectionManager] Error updating state of ${subscriptionId} to ${state}`, e);
}
} }
} }
notificationReceived(subscriptionId, notification) { notificationReceived(subscriptionId, notification) {
if (this.notificationListener) { if (this.notificationListener) {
try {
this.notificationListener(subscriptionId, notification); this.notificationListener(subscriptionId, notification);
} catch (e) {
console.error(`[ConnectionManager] Error handling notification for ${subscriptionId}`, e);
}
} }
} }
} }

View File

@ -42,49 +42,13 @@ const App = () => {
const Root = () => { const Root = () => {
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false); const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
const [notificationsGranted, setNotificationsGranted] = useState(notifier.granted()); const [notificationsGranted, setNotificationsGranted] = useState(notifier.granted());
const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
const users = useLiveQuery(() => userManager.all()); const users = useLiveQuery(() => userManager.all());
const subscriptions = useLiveQuery(() => subscriptionManager.all()); const subscriptions = useLiveQuery(() => subscriptionManager.all());
const selectedSubscription = findSelected(location, subscriptions); const selectedSubscription = findSelected(location, subscriptions);
console.log(window.location); useWorkers();
const handleSubscribeSubmit = async (subscription) => { useConnectionListeners();
console.log(`[App] New subscription: ${subscription.id}`, subscription);
navigate(subscriptionRoute(subscription));
handleRequestPermission();
};
const handleRequestPermission = () => {
notifier.maybeRequestPermission(granted => setNotificationsGranted(granted));
};
useEffect(() => {
poller.startWorker();
pruner.startWorker();
}, [/* initial render */]);
useEffect(() => {
const handleNotification = async (subscriptionId, notification) => {
try {
const added = await subscriptionManager.addNotification(subscriptionId, notification);
if (added) {
const defaultClickAction = (subscription) => navigate(subscriptionRoute(subscription)); // FIXME
await notifier.notify(subscriptionId, notification, defaultClickAction)
}
} catch (e) {
console.error(`[App] Error handling notification`, e);
}
};
connectionManager.registerStateListener(subscriptionManager.updateState);
connectionManager.registerNotificationListener(handleNotification);
return () => {
connectionManager.resetStateListener();
connectionManager.resetNotificationListener();
}
// This is for the use of 'navigate' // FIXME
//eslint-disable-next-line
}, [/* initial render */]);
useEffect(() => { useEffect(() => {
connectionManager.refresh(subscriptions, users); connectionManager.refresh(subscriptions, users);
@ -102,11 +66,10 @@ const Root = () => {
<Navigation <Navigation
subscriptions={subscriptions} subscriptions={subscriptions}
selectedSubscription={selectedSubscription} selectedSubscription={selectedSubscription}
mobileDrawerOpen={mobileDrawerOpen}
notificationsGranted={notificationsGranted} notificationsGranted={notificationsGranted}
requestNotificationPermission={() => notifier.maybeRequestPermission(granted => setNotificationsGranted(granted))}
mobileDrawerOpen={mobileDrawerOpen}
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)} onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
onSubscribeSubmit={handleSubscribeSubmit}
onRequestPermissionClick={handleRequestPermission}
/> />
</Box> </Box>
<Main> <Main>
@ -168,4 +131,34 @@ const findSelected = (location, subscriptions) => {
*/ */
}; };
const useWorkers = () => {
useEffect(() => {
poller.startWorker();
pruner.startWorker();
}, []);
};
const useConnectionListeners = () => {
const navigate = useNavigate();
useEffect(() => {
const handleNotification = async (subscriptionId, notification) => {
const added = await subscriptionManager.addNotification(subscriptionId, notification);
if (added) {
const defaultClickAction = (subscription) => navigate(subscriptionRoute(subscription));
await notifier.notify(subscriptionId, notification, defaultClickAction)
}
};
connectionManager.registerStateListener(subscriptionManager.updateState);
connectionManager.registerNotificationListener(handleNotification);
return () => {
connectionManager.resetStateListener();
connectionManager.resetNotificationListener();
}
},
// We have to disable dep checking for "navigate". This is fine, it never changes.
// eslint-disable-next-line
[]);
};
export default App; export default App;

View File

@ -66,8 +66,10 @@ const NavList = (props) => {
} }
const handleSubscribeSubmit = (subscription) => { const handleSubscribeSubmit = (subscription) => {
console.log(`[Navigation] New subscription: ${subscription.id}`, subscription);
handleSubscribeReset(); handleSubscribeReset();
props.onSubscribeSubmit(subscription); navigate(subscriptionRoute(subscription));
props.requestNotificationPermission();
} }
const showSubscriptionsList = props.subscriptions?.length > 0; const showSubscriptionsList = props.subscriptions?.length > 0;
@ -77,7 +79,7 @@ const NavList = (props) => {
<> <>
<Toolbar sx={{ display: { xs: 'none', sm: 'block' } }}/> <Toolbar sx={{ display: { xs: 'none', sm: 'block' } }}/>
<List component="nav" sx={{ paddingTop: (showGrantPermissionsBox) ? '0' : '' }}> <List component="nav" sx={{ paddingTop: (showGrantPermissionsBox) ? '0' : '' }}>
{showGrantPermissionsBox && <PermissionAlert onRequestPermissionClick={props.onRequestPermissionClick}/>} {showGrantPermissionsBox && <PermissionAlert onRequestPermissionClick={props.requestNotificationPermission}/>}
{showSubscriptionsList && {showSubscriptionsList &&
<> <>
<ListSubheader>Subscribed topics</ListSubheader> <ListSubheader>Subscribed topics</ListSubheader>