Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

Make the subscription flag reactive and share the local-flag primitive

Gabriel Stein16b5f9c569e5
diff --git a/src/components/notes/NoteEditor.tsx b/src/components/notes/NoteEditor.tsx
index 356e8db9a..c9b0ea072 100644
--- a/src/components/notes/NoteEditor.tsx
+++ b/src/components/notes/NoteEditor.tsx
@@ -1,4 +1,12 @@
-import { useState, useRef, useEffect, useMemo, useCallback, type ComponentProps } from "react";
+import {
+ useState,
+ useRef,
+ useEffect,
+ useMemo,
+ useCallback,
+ useSyncExternalStore,
+ type ComponentProps,
+} from "react";
import { useTranslation } from "react-i18next";
import {
Download,
@@ -27,6 +35,7 @@ import {
updateNoteInStore,
} from "../../stores/noteStore";
import { TeamsService } from "../../services/TeamsService";
+import { readIsSubscribed, subscribeIsSubscribed } from "../../lib/subscriptionFlag";
import { useAuth } from "../../hooks/useAuth";
import { RichTextEditor } from "../ui/RichTextEditor";
import type { Editor } from "@tiptap/react";
@@ -255,10 +264,8 @@ export default function NoteEditor({
// Same gate as SyncService.canSyncSharedNotes: sharing needs a subscription.
// An already-shared note stays manageable (unshare/revoke) after a lapse.
// Team notes never get a public web link — their audience is the space (D8).
- const canShare =
- isSignedIn &&
- !isTeamNote &&
- (localStorage.getItem("isSubscribed") === "true" || Boolean(note.is_shared));
+ const isSubscribed = useSyncExternalStore(subscribeIsSubscribed, readIsSubscribed);
+ const canShare = isSignedIn && !isTeamNote && (isSubscribed || Boolean(note.is_shared));
// A newer cloud copy arrived while this note had unpushed edits (plan §7.3).
const conflict = useNoteConflict(note.client_note_id);
const [conflictEditorName, setConflictEditorName] = useState<string | null>(null);
diff --git a/src/hooks/useUsage.ts b/src/hooks/useUsage.ts
index c947b8c3c..d15273750 100644
--- a/src/hooks/useUsage.ts
+++ b/src/hooks/useUsage.ts
@@ -2,6 +2,7 @@ import { useState, useEffect, useCallback, useRef } from "react";
import { useAuth } from "./useAuth";
import { CACHE_CONFIG } from "../config/constants";
import { withSessionRefresh } from "../lib/auth";
+import { notifyIsSubscribedChanged, readIsSubscribed } from "../lib/subscriptionFlag";
interface UsageData {
wordsUsed: number;
@@ -96,7 +97,10 @@ export function useUsage(): UseUsageResult | null {
resetAt: result.resetAt ?? "rolling",
});
lastFetchRef.current = Date.now();
- localStorage.setItem("isSubscribed", String(result.isSubscribed ?? false));
+ const isSubscribed = result.isSubscribed ?? false;
+ const flagChanged = readIsSubscribed() !== isSubscribed;
+ localStorage.setItem("isSubscribed", String(isSubscribed));
+ if (flagChanged) notifyIsSubscribedChanged();
} else {
const error: any = new Error(result.error || "Failed to fetch usage");
error.code = result.code;
diff --git a/src/lib/reactiveLocalFlag.ts b/src/lib/reactiveLocalFlag.ts
new file mode 100644
index 000000000..55cb66bec
--- /dev/null
+++ b/src/lib/reactiveLocalFlag.ts
@@ -0,0 +1,26 @@
+// Reactive view over a boolean localStorage flag: same-window writers call
+// notify(); cross-window flips ride the storage event.
+export interface ReactiveLocalFlag {
+ read: () => boolean;
+ notify: () => void;
+ subscribe: (onChange: () => void) => () => void;
+}
+
+export function createReactiveLocalFlag(key: string): ReactiveLocalFlag {
+ const subscribers = new Set<() => void>();
+ return {
+ read: () => localStorage.getItem(key) === "true",
+ notify: () => subscribers.forEach((n) => n()),
+ subscribe: (onChange) => {
+ subscribers.add(onChange);
+ const onStorage = (e: StorageEvent) => {
+ if (e.key === key) onChange();
+ };
+ window.addEventListener("storage", onStorage);
+ return () => {
+ subscribers.delete(onChange);
+ window.removeEventListener("storage", onStorage);
+ };
+ },
+ };
+}
diff --git a/src/lib/subscriptionFlag.ts b/src/lib/subscriptionFlag.ts
new file mode 100644
index 000000000..302dd2e3a
--- /dev/null
+++ b/src/lib/subscriptionFlag.ts
@@ -0,0 +1,9 @@
+import { createReactiveLocalFlag } from "./reactiveLocalFlag";
+
+// Written by useUsage after each usage fetch; read by surfaces that gate on
+// subscription without paying a usage fetch (e.g. the note share affordance).
+const flag = createReactiveLocalFlag("isSubscribed");
+
+export const readIsSubscribed = flag.read;
+export const notifyIsSubscribedChanged = flag.notify;
+export const subscribeIsSubscribed = flag.subscribe;
diff --git a/src/lib/teamSpacesCapability.ts b/src/lib/teamSpacesCapability.ts
index d9be1b43b..910d6499c 100644
--- a/src/lib/teamSpacesCapability.ts
+++ b/src/lib/teamSpacesCapability.ts
@@ -1,25 +1,8 @@
-// Reactive view over the localStorage capability flag the sync probe writes:
-// same-window flips notify subscribers; cross-window ones ride the storage event.
-const CAPABILITY_KEY = "teamSpacesCapability";
+import { createReactiveLocalFlag } from "./reactiveLocalFlag";
-const subscribers = new Set<() => void>();
+// Written by the sync probe (me/teams 404 check); read by the TEAM SPACES gate.
+const flag = createReactiveLocalFlag("teamSpacesCapability");
-export function readTeamSpacesCapability(): boolean {
- return localStorage.getItem(CAPABILITY_KEY) === "true";
-}
-
-export function notifyTeamSpacesCapabilityChanged(): void {
- subscribers.forEach((notify) => notify());
-}
-
-export function subscribeTeamSpacesCapability(onChange: () => void): () => void {
- subscribers.add(onChange);
- const onStorage = (e: StorageEvent) => {
- if (e.key === CAPABILITY_KEY) onChange();
- };
- window.addEventListener("storage", onStorage);
- return () => {
- subscribers.delete(onChange);
- window.removeEventListener("storage", onStorage);
- };
-}
+export const readTeamSpacesCapability = flag.read;
+export const notifyTeamSpacesCapabilityChanged = flag.notify;
+export const subscribeTeamSpacesCapability = flag.subscribe;