|
1 | 1 | import { useSyncExternalStore } from "react"; |
2 | 2 |
|
3 | 3 | /* |
4 | | - * Which Bot the person put at the centre of OpenMuse. A per-browser preference, like the skin itself: |
5 | | - * it lives in localStorage and nowhere else, so choosing a Bot here changes nothing on the server. |
| 4 | + * Which Bot each person put at the centre of the frame. Kept in this browser, and kept per signed-in |
| 5 | + * person: the key carries their user id, so the next person on a shared machine starts from OpenBot's |
| 6 | + * own default Bot rather than the last person's choice. Choosing a Bot changes nothing on the server. |
6 | 7 | */ |
7 | | -export const CHOSEN_BOT_KEY = "openmuse-bot"; |
8 | | - |
9 | | -function read(): string | null { |
10 | | - try { |
11 | | - return window.localStorage.getItem(CHOSEN_BOT_KEY); |
12 | | - } catch { |
13 | | - return null; |
14 | | - } |
| 8 | +export function chosenBotKey(userId: string): string { |
| 9 | + return `openmuse-bot:${userId}`; |
15 | 10 | } |
16 | 11 |
|
17 | | -let current: string | null = typeof window === "undefined" ? null : read(); |
| 12 | +const choices = new Map<string, string | null>(); |
18 | 13 | const listeners = new Set<() => void>(); |
19 | 14 |
|
20 | | -export function chooseBot(id: string) { |
21 | | - if (current === id) return; |
22 | | - current = id; |
23 | | - // localStorage throws in some privacy modes; the choice still holds for this visit. |
| 15 | +function read(userId: string): string | null { |
| 16 | + if (!choices.has(userId)) { |
| 17 | + let stored: string | null = null; |
| 18 | + // localStorage throws in some privacy modes; the frame then starts from the default Bot. |
| 19 | + try { |
| 20 | + stored = window.localStorage.getItem(chosenBotKey(userId)); |
| 21 | + } catch {} |
| 22 | + choices.set(userId, stored); |
| 23 | + } |
| 24 | + return choices.get(userId) ?? null; |
| 25 | +} |
| 26 | + |
| 27 | +export function chooseBot(userId: string, agentId: string) { |
| 28 | + if (read(userId) === agentId) return; |
| 29 | + choices.set(userId, agentId); |
| 30 | + // The choice still holds for this visit when the browser refuses to store it. |
24 | 31 | try { |
25 | | - window.localStorage.setItem(CHOSEN_BOT_KEY, id); |
| 32 | + window.localStorage.setItem(chosenBotKey(userId), agentId); |
26 | 33 | } catch {} |
27 | 34 | for (const listener of listeners) listener(); |
28 | 35 | } |
29 | 36 |
|
| 37 | +/** Forgets every choice read into memory, as a new page load would. */ |
| 38 | +export function forgetChosenBots() { |
| 39 | + choices.clear(); |
| 40 | + for (const listener of listeners) listener(); |
| 41 | +} |
| 42 | + |
30 | 43 | function subscribe(listener: () => void) { |
31 | 44 | listeners.add(listener); |
32 | 45 | return () => { |
33 | 46 | listeners.delete(listener); |
34 | 47 | }; |
35 | 48 | } |
36 | 49 |
|
37 | | -export function useChosenBotId(): string | null { |
| 50 | +/** The Bot this person chose, or null before they choose one or while nobody is known yet. */ |
| 51 | +export function useChosenBotId(userId: string | undefined): string | null { |
38 | 52 | return useSyncExternalStore( |
39 | 53 | subscribe, |
40 | | - () => current, |
| 54 | + () => (userId ? read(userId) : null), |
41 | 55 | () => null, |
42 | 56 | ); |
43 | 57 | } |
0 commit comments