aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/static/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/static/app.js')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/static/app.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/app.js b/packages/meshbay-hub/src/meshbay_hub/static/app.js
index 2254a11..50bd8ac 100644
--- a/packages/meshbay-hub/src/meshbay_hub/static/app.js
+++ b/packages/meshbay-hub/src/meshbay_hub/static/app.js
@@ -43,6 +43,9 @@ import { InvitePage, JoinByLink } from './invite-page.js';
// How often to look. Cheap — it reads a timestamp out of the token and almost
// always does nothing.
const TOKEN_CHECK_MS = 60000;
+// The least time between two askings for the notification list on somebody's
+// return: a gesture, never a timer, and two in a row are one question.
+const NOTIF_REFRESH_MS = 30000;
const THEME_KEY = 'mb_theme';
// ── Theme ────────────────────────────────────────────────────────────────────
@@ -980,10 +983,12 @@ function App() {
hubFetch('/v1/hub/info').then(setHubInfo).catch(() => {});
}, []);
+ const notifAskedAtRef = useRef(0);
const fetchNotifications = useCallback(() => {
if (!user || notifDisabled) {
setNotifications([]); setUnreadCount(0); return;
}
+ notifAskedAtRef.current = Date.now();
// `unread_only`: clicking one is what dismisses it (see markRead), so a
// read notification is a dismissed notification and must not come back on
// the next launch. Without this the two halves disagreed — the click
@@ -1046,6 +1051,30 @@ function App() {
syncPush(user).catch(() => {});
}, [user]);
+ // The list used to be asked for at sign-in and then never, so an application
+ // left open showed nothing created after it started: a chat line the hub
+ // wrote within 20 ms of the message stayed unseen. The hub has no channel to
+ // the page, and is not polled on a timer, so the list is asked for again
+ // when somebody comes back to look: the application returning to the
+ // foreground (a phone included), and the home page, where it is shown.
+ const refreshNotifications = useCallback(() => {
+ if (Date.now() - notifAskedAtRef.current < NOTIF_REFRESH_MS) return;
+ fetchNotifications();
+ }, [fetchNotifications]);
+
+ useEffect(() => {
+ if (!user) return;
+ const onVisible = () => {
+ if (document.visibilityState === 'visible') refreshNotifications();
+ };
+ document.addEventListener('visibilitychange', onVisible);
+ return () => document.removeEventListener('visibilitychange', onVisible);
+ }, [user, refreshNotifications]);
+
+ useEffect(() => {
+ if (user && route === '/') refreshNotifications();
+ }, [user, route, refreshNotifications]);
+
// The node this application ships, set up, started and linked for whoever
// signs in (main.js `ensureNode`): installing it is all it should take.
// Keyed on the account, not the session object -- the token rotates, and a