Move things around a bit

This commit is contained in:
Philipp Heckel 2022-03-08 11:33:17 -05:00
parent 30b13cbdbc
commit 4aad98256a
5 changed files with 26 additions and 28 deletions

View file

@ -1,7 +1,7 @@
import api from "./Api"; import api from "./Api";
import subscriptionManager from "./SubscriptionManager"; import subscriptionManager from "./SubscriptionManager";
const delayMillis = 3000; // 3 seconds const delayMillis = 8000; // 8 seconds
const intervalMillis = 300000; // 5 minutes const intervalMillis = 300000; // 5 minutes
class Poller { class Poller {
@ -13,6 +13,7 @@ class Poller {
if (this.timer !== null) { if (this.timer !== null) {
return; return;
} }
console.log(`[Poller] Starting worker`);
this.timer = setInterval(() => this.pollAll(), intervalMillis); this.timer = setInterval(() => this.pollAll(), intervalMillis);
setTimeout(() => this.pollAll(), delayMillis); setTimeout(() => this.pollAll(), delayMillis);
} }
@ -55,4 +56,6 @@ class Poller {
} }
const poller = new Poller(); const poller = new Poller();
poller.startWorker();
export default poller; export default poller;

View file

@ -13,6 +13,7 @@ class Pruner {
if (this.timer !== null) { if (this.timer !== null) {
return; return;
} }
console.log(`[Pruner] Starting worker`);
this.timer = setInterval(() => this.prune(), intervalMillis); this.timer = setInterval(() => this.prune(), intervalMillis);
setTimeout(() => this.prune(), delayMillis); setTimeout(() => this.prune(), delayMillis);
} }
@ -34,4 +35,6 @@ class Pruner {
} }
const pruner = new Pruner(); const pruner = new Pruner();
pruner.startWorker();
export default pruner; export default pruner;

View file

@ -47,7 +47,6 @@ const Root = () => {
const selectedSubscription = findSelected(location, subscriptions); const selectedSubscription = findSelected(location, subscriptions);
const newNotificationsCount = subscriptions?.reduce((prev, cur) => prev + cur.new, 0) || 0; const newNotificationsCount = subscriptions?.reduce((prev, cur) => prev + cur.new, 0) || 0;
useWorkers();
useConnectionListeners(); useConnectionListeners();
useEffect(() => { useEffect(() => {
@ -66,16 +65,14 @@ const Root = () => {
selectedSubscription={selectedSubscription} selectedSubscription={selectedSubscription}
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)} onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
/> />
<Box component="nav" sx={{width: {sm: Navigation.width}, flexShrink: {sm: 0}}}> <Navigation
<Navigation subscriptions={subscriptions}
subscriptions={subscriptions} selectedSubscription={selectedSubscription}
selectedSubscription={selectedSubscription} notificationsGranted={notificationsGranted}
notificationsGranted={notificationsGranted} mobileDrawerOpen={mobileDrawerOpen}
requestNotificationPermission={() => notifier.maybeRequestPermission(granted => setNotificationsGranted(granted))} onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
mobileDrawerOpen={mobileDrawerOpen} onNotificationGranted={setNotificationsGranted}
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)} />
/>
</Box>
<Main> <Main>
<Toolbar/> <Toolbar/>
<Routes> <Routes>
@ -136,14 +133,6 @@ const findSelected = (location, subscriptions) => {
*/ */
}; };
const useWorkers = () => {
useEffect(() => {
poller.startWorker();
pruner.startWorker();
}, []);
};
const useConnectionListeners = () => { const useConnectionListeners = () => {
const navigate = useNavigate(); const navigate = useNavigate();
useEffect(() => { useEffect(() => {

View file

@ -19,13 +19,15 @@ import {ConnectionState} from "../app/Connection";
import {useLocation, useNavigate} from "react-router-dom"; import {useLocation, useNavigate} from "react-router-dom";
import subscriptionManager from "../app/SubscriptionManager"; import subscriptionManager from "../app/SubscriptionManager";
import {ChatBubble} from "@mui/icons-material"; import {ChatBubble} from "@mui/icons-material";
import Box from "@mui/material/Box";
import notifier from "../app/Notifier";
const navWidth = 240; const navWidth = 240;
const Navigation = (props) => { const Navigation = (props) => {
const navigationList = <NavList {...props}/>; const navigationList = <NavList {...props}/>;
return ( return (
<> <Box component="nav" sx={{width: {sm: Navigation.width}, flexShrink: {sm: 0}}}>
{/* Mobile drawer; only shown if menu icon clicked (mobile open) and display is small */} {/* Mobile drawer; only shown if menu icon clicked (mobile open) and display is small */}
<Drawer <Drawer
variant="temporary" variant="temporary"
@ -50,7 +52,7 @@ const Navigation = (props) => {
> >
{navigationList} {navigationList}
</Drawer> </Drawer>
</> </Box>
); );
}; };
Navigation.width = navWidth; Navigation.width = navWidth;
@ -70,9 +72,13 @@ const NavList = (props) => {
console.log(`[Navigation] New subscription: ${subscription.id}`, subscription); console.log(`[Navigation] New subscription: ${subscription.id}`, subscription);
handleSubscribeReset(); handleSubscribeReset();
navigate(subscriptionRoute(subscription)); navigate(subscriptionRoute(subscription));
props.requestNotificationPermission(); handleRequestNotificationPermission();
} }
const handleRequestNotificationPermission = () => {
notifier.maybeRequestPermission(granted => props.onNotificationGranted(granted))
};
const showSubscriptionsList = props.subscriptions?.length > 0; const showSubscriptionsList = props.subscriptions?.length > 0;
const showGrantPermissionsBox = props.subscriptions?.length > 0 && !props.notificationsGranted; const showGrantPermissionsBox = props.subscriptions?.length > 0 && !props.notificationsGranted;
@ -80,7 +86,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.requestNotificationPermission}/>} {showGrantPermissionsBox && <PermissionAlert onRequestPermissionClick={handleRequestNotificationPermission}/>}
{!showSubscriptionsList && {!showSubscriptionsList &&
<ListItemButton onClick={() => navigate("/")} selected={location.pathname === "/"}> <ListItemButton onClick={() => navigate("/")} selected={location.pathname === "/"}>
<ListItemIcon><ChatBubble/></ListItemIcon> <ListItemIcon><ChatBubble/></ListItemIcon>

View file

@ -54,8 +54,8 @@ const NotificationList = (props) => {
const pageSize = 20; const pageSize = 20;
const notifications = props.notifications; const notifications = props.notifications;
const [maxCount, setMaxCount] = useState(pageSize); const [maxCount, setMaxCount] = useState(pageSize);
const count = Math.min(notifications.length, maxCount);
// Reset state when the list identifier changes, i.e when we switch between subscriptions
useEffect(() => { useEffect(() => {
return () => { return () => {
setMaxCount(pageSize); setMaxCount(pageSize);
@ -63,9 +63,6 @@ const NotificationList = (props) => {
} }
}, [props.id]); }, [props.id]);
const count = Math.min(notifications.length, maxCount);
console.log(`xxx id=${props.id} scrollMax=${maxCount} count=${count} len=${notifications.length}`)
return ( return (
<InfiniteScroll <InfiniteScroll
dataLength={count} dataLength={count}