@unisim/sdk 0.231.2 → 0.233.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -2
- package/dist/PreferencesDialog.d.ts.map +1 -1
- package/dist/PreferencesDialog.js +12 -2
- package/dist/PreferencesDialog.js.map +1 -1
- package/dist/SyncedSettingsSync.d.ts +2 -0
- package/dist/SyncedSettingsSync.d.ts.map +1 -0
- package/dist/SyncedSettingsSync.js +34 -0
- package/dist/SyncedSettingsSync.js.map +1 -0
- package/dist/TuneAccountSection.d.ts +8 -1
- package/dist/TuneAccountSection.d.ts.map +1 -1
- package/dist/TuneAccountSection.js +2 -2
- package/dist/TuneAccountSection.js.map +1 -1
- package/dist/TuneSync.d.ts.map +1 -1
- package/dist/TuneSync.js +49 -158
- package/dist/TuneSync.js.map +1 -1
- package/dist/defaultView.d.ts +4 -2
- package/dist/defaultView.d.ts.map +1 -1
- package/dist/defaultView.js +8 -2
- package/dist/defaultView.js.map +1 -1
- package/dist/desktop.d.ts +33 -0
- package/dist/desktop.d.ts.map +1 -1
- package/dist/desktop.js.map +1 -1
- package/dist/i18n.d.ts.map +1 -1
- package/dist/i18n.js +24 -0
- package/dist/i18n.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +15 -1
- package/dist/provider.js.map +1 -1
- package/dist/rowSync.d.ts +37 -0
- package/dist/rowSync.d.ts.map +1 -0
- package/dist/rowSync.js +172 -0
- package/dist/rowSync.js.map +1 -0
- package/dist/suiteDesktopSession.d.ts +37 -0
- package/dist/suiteDesktopSession.d.ts.map +1 -0
- package/dist/suiteDesktopSession.js +143 -0
- package/dist/suiteDesktopSession.js.map +1 -0
- package/dist/syncedSettings.d.ts +100 -0
- package/dist/syncedSettings.d.ts.map +1 -0
- package/dist/syncedSettings.js +272 -0
- package/dist/syncedSettings.js.map +1 -0
- package/electron/handoff.cjs +11 -1
- package/electron/signin.cjs +51 -17
- package/electron/suite-session.cjs +561 -0
- package/package.json +3 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/** The `user_app_prefs.app` value holding one app's own synced Tune rows. */
|
|
2
|
+
export declare function settingsRowApp(app: string): string;
|
|
3
|
+
/** The name the device stamp is kept under (`unisim:tune-stamp:app:<app>`). */
|
|
4
|
+
export declare function settingsStampName(app: string): string;
|
|
5
|
+
/** A store's row: the values, as the device that last changed them had them. */
|
|
6
|
+
export interface SettingsRow {
|
|
7
|
+
at: number;
|
|
8
|
+
values: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
/** A row's `data`, or `null` when it is not one (absent, corrupt, unstamped). */
|
|
11
|
+
export declare function parseSettingsRow(raw: unknown): SettingsRow | null;
|
|
12
|
+
export interface SyncedSettingsOptions<T> {
|
|
13
|
+
/**
|
|
14
|
+
* Where the values live on the device. Default `unisim:settings:<app>`.
|
|
15
|
+
* Pass an app's EXISTING key to keep reading it: `normalise` receives
|
|
16
|
+
* whatever is stored there, in whatever shape the old code wrote, and the
|
|
17
|
+
* first change rewrites it as this store's flat object. Only do that when
|
|
18
|
+
* nothing else reads the key (a pre-paint script, another module).
|
|
19
|
+
*/
|
|
20
|
+
storageKey?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Existing values from before this store, read once when `storageKey` holds
|
|
23
|
+
* nothing — e.g. one localStorage key per switch. The result goes through
|
|
24
|
+
* `normalise` and is saved under `storageKey` straight away, so nobody loses
|
|
25
|
+
* a setting. It is NOT a change: unstamped, it never outranks the account.
|
|
26
|
+
* Return `undefined` for "nothing to migrate".
|
|
27
|
+
*/
|
|
28
|
+
legacy?: () => unknown;
|
|
29
|
+
/**
|
|
30
|
+
* Keys that stay on this device and never go to the account (a phone
|
|
31
|
+
* gesture, a choice that needs this device's own permission). A change to
|
|
32
|
+
* one is saved locally and not stamped.
|
|
33
|
+
*/
|
|
34
|
+
deviceOnly?: readonly (keyof T & string)[];
|
|
35
|
+
}
|
|
36
|
+
export interface SyncedSettings<T extends object> {
|
|
37
|
+
/** Subscribe a component to the whole set. */
|
|
38
|
+
(): T;
|
|
39
|
+
/** Subscribe a component to a slice — `useSettings((s) => s.pageSize)`. */
|
|
40
|
+
<U>(selector: (state: T) => U): U;
|
|
41
|
+
/** Read outside React. */
|
|
42
|
+
getState(): T;
|
|
43
|
+
/**
|
|
44
|
+
* Change some values. Saved on the device at once; a real change to a
|
|
45
|
+
* synced key is stamped and, signed in, goes to the account. Values go
|
|
46
|
+
* through `normalise`, so a bad one falls back to what it would read as.
|
|
47
|
+
*/
|
|
48
|
+
set(patch: Partial<T> | ((state: T) => Partial<T>)): void;
|
|
49
|
+
/** Zustand's name for `set`. */
|
|
50
|
+
setState(patch: Partial<T> | ((state: T) => Partial<T>)): void;
|
|
51
|
+
/**
|
|
52
|
+
* Every value back to its default, saved, and — when that changed anything —
|
|
53
|
+
* stamped and synced, so the account's other devices follow. The SDK's
|
|
54
|
+
* `useResetAppDefaults` (every Tune this app's Reset to defaults) calls it.
|
|
55
|
+
*/
|
|
56
|
+
reset(): void;
|
|
57
|
+
/** Low-level change listener; returns the unsubscribe. */
|
|
58
|
+
subscribe(listener: () => void): () => void;
|
|
59
|
+
readonly app: string;
|
|
60
|
+
readonly storageKey: string;
|
|
61
|
+
/** A fresh copy of the defaults, through `normalise`. */
|
|
62
|
+
readonly defaults: T;
|
|
63
|
+
}
|
|
64
|
+
/** What SyncedSettingsSync needs from one store. */
|
|
65
|
+
export interface SyncedSettingsEntry {
|
|
66
|
+
app: string;
|
|
67
|
+
/** The synced values (no `deviceOnly` keys). */
|
|
68
|
+
snapshot(): Record<string, unknown>;
|
|
69
|
+
/** Put the account's values on this device, quietly. */
|
|
70
|
+
applyRemote(values: Record<string, unknown>): void;
|
|
71
|
+
/** This device's own real changes to synced keys. */
|
|
72
|
+
onChange(listener: () => void): () => void;
|
|
73
|
+
reset(): void;
|
|
74
|
+
}
|
|
75
|
+
/** Every synced store made on this page (one per `app`; a re-made one replaces it). */
|
|
76
|
+
export declare function getSyncedSettingsEntries(): SyncedSettingsEntry[];
|
|
77
|
+
export declare function subscribeSyncedSettingsEntries(listener: () => void): () => void;
|
|
78
|
+
/** Reset every synced store on this page (Reset to defaults, via useResetAppDefaults). */
|
|
79
|
+
export declare function resetAllSyncedSettings(): void;
|
|
80
|
+
/**
|
|
81
|
+
* Create an app's synced Tune settings. Call it ONCE, at module level, and
|
|
82
|
+
* export the result.
|
|
83
|
+
*
|
|
84
|
+
* - `app`: the app's product code (`config.product`), ≤ 31 characters. The
|
|
85
|
+
* row is `tune-app:<app>`; the device stamp `unisim:tune-stamp:app:<app>`.
|
|
86
|
+
* - `defaults`: every setting's default. JSON values only. ⚠️ Use `null`, not
|
|
87
|
+
* `undefined`, for "none": an absent key in the account's row leaves this
|
|
88
|
+
* device's value as it is (so an older build that doesn't know a newer
|
|
89
|
+
* setting can't reset it), and `undefined` doesn't survive JSON.
|
|
90
|
+
* - `normalise(raw, defaults)`: turn anything — the stored object (in an old
|
|
91
|
+
* shape too), the account's values, a patched set — into a complete, valid
|
|
92
|
+
* set. It must not trust `raw` (it can be `undefined`, `null`, junk) and
|
|
93
|
+
* must fill every key; a throw reads as the defaults.
|
|
94
|
+
*
|
|
95
|
+
* ⚠️ Never change a setting's default by accident: a device that never
|
|
96
|
+
* changed anything shows the defaults, and so does a Reset. Tick boxes start
|
|
97
|
+
* unticked (BRANDING.md); word a default-on feature as an opt-out.
|
|
98
|
+
*/
|
|
99
|
+
export declare function createSyncedSettings<T extends object>(app: string, defaults: T, normalise: (raw: unknown, defaults: T) => T, options?: SyncedSettingsOptions<T>): SyncedSettings<T>;
|
|
100
|
+
//# sourceMappingURL=syncedSettings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncedSettings.d.ts","sourceRoot":"","sources":["../src/syncedSettings.ts"],"names":[],"mappings":"AA+CA,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ACb,0DAA0D;IAC1D,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAA;IAC3C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,yDAAyD;IACzD,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAA;CACrB;AAID,oDAAoD;AACpD,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,MAAM,CAAA;IACX,gDAAgD;IAChD,QAAQ,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACnC,wDAAwD;IACxD,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAA;IAClD,qDAAqD;IACrD,QAAQ,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAA;IAC1C,KAAK,IAAI,IAAI,CAAA;CACd;AAKD,uFAAuF;AACvF,wBAAgB,wBAAwB,IAAI,mBAAmB,EAAE,CAEhE;AAED,wBAAgB,8BAA8B,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAG/E;AAED,0FAA0F;AAC1F,wBAAgB,sBAAsB,IAAI,IAAI,CAE7C;AAqBD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,MAAM,EACnD,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,CAAC,EACX,SAAS,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,KAAK,CAAC,EAC3C,OAAO,GAAE,qBAAqB,CAAC,CAAC,CAAM,GACrC,cAAc,CAAC,CAAC,CAAC,CA8InB"}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
// An app's own Tune this app rows, kept on the device and synced to the
|
|
3
|
+
// account (0.232.0) — phase 2 of James's 2026-10-30 rule: "mobile app and web
|
|
4
|
+
// app settings in tune this app should be saved and synced to their account …
|
|
5
|
+
// Global rule". Phase 1 (0.229.0, TuneSync.tsx) synced what the SDK holds;
|
|
6
|
+
// this is the same thing for the switches an app keeps itself. It is
|
|
7
|
+
// BlackBook's settingsStore (`tunedAt`) generalised; BlackBook stays on its
|
|
8
|
+
// own E2EE vault.
|
|
9
|
+
//
|
|
10
|
+
// // src/stores/settings.ts — once, at module level
|
|
11
|
+
// export const useSettings = createSyncedSettings('universal_pdf',
|
|
12
|
+
// { compressOnSave: false, pageSize: 'a4' },
|
|
13
|
+
// (raw, d) => ({ compressOnSave: raw?.compressOnSave === true, pageSize: … ?? d.pageSize }))
|
|
14
|
+
// // in a component
|
|
15
|
+
// const compress = useSettings((s) => s.compressOnSave)
|
|
16
|
+
// useSettings.set({ compressOnSave: true })
|
|
17
|
+
//
|
|
18
|
+
// Zustand-shaped (`useStore(selector)`, `getState`, `setState`, `subscribe`),
|
|
19
|
+
// on React's own `useSyncExternalStore` — the SDK takes no zustand dependency
|
|
20
|
+
// (see themeStore.ts). Read synchronously from localStorage at import time.
|
|
21
|
+
//
|
|
22
|
+
// THE RULES are phase 1's, run by the same engine (rowSync.ts,
|
|
23
|
+
// decideTuneSync): signed in = synced, signed out = this device only; one
|
|
24
|
+
// stamp per store and the newer set wins; first sight is unstamped, so a
|
|
25
|
+
// device that never changed anything (existing values migrated from before
|
|
26
|
+
// included) says nothing — it takes the account's set if there is one and
|
|
27
|
+
// writes nothing; a guest's change is saved on sign-in; another account's
|
|
28
|
+
// stamp on a shared device is no stamp; Reset to defaults is a change.
|
|
29
|
+
//
|
|
30
|
+
// ⚠️ WHERE IT LIVES: a SIBLING row, `app = 'tune-app:<app>'`, with
|
|
31
|
+
// `data = { at, values }` — not a nested key in phase 1's `tune:<product>`
|
|
32
|
+
// row. Every SDK from 0.229.0 to 0.231.x writes that row whole as
|
|
33
|
+
// `{ at, colorScheme, language, views }`, so a phone a release behind would
|
|
34
|
+
// wipe a nested key on its next colour-scheme change; and sharing its one
|
|
35
|
+
// stamp would make a look change on one device and a switch change on
|
|
36
|
+
// another fight, the older losing BOTH. Two rows, two stamps: the SDK's look
|
|
37
|
+
// and the app's switches each go newest-wins on their own and never touch
|
|
38
|
+
// each other. Same reasoning as phase 1's own row and "Call me"'s. No
|
|
39
|
+
// migration: `app` is free text (0151), and the stamp is inside `data`.
|
|
40
|
+
//
|
|
41
|
+
// Privacy: the row holds the settings' values and the stamp. Nothing else.
|
|
42
|
+
// Keys marked `deviceOnly` (a shake gesture, a permission-backed choice that
|
|
43
|
+
// needs this device's own say) stay on the device and never go up.
|
|
44
|
+
import { useSyncExternalStore } from 'react';
|
|
45
|
+
/** The `user_app_prefs.app` value holding one app's own synced Tune rows. */
|
|
46
|
+
export function settingsRowApp(app) {
|
|
47
|
+
return `tune-app:${app}`;
|
|
48
|
+
}
|
|
49
|
+
/** The name the device stamp is kept under (`unisim:tune-stamp:app:<app>`). */
|
|
50
|
+
export function settingsStampName(app) {
|
|
51
|
+
return `app:${app}`;
|
|
52
|
+
}
|
|
53
|
+
/** A row's `data`, or `null` when it is not one (absent, corrupt, unstamped). */
|
|
54
|
+
export function parseSettingsRow(raw) {
|
|
55
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
56
|
+
return null;
|
|
57
|
+
const r = raw;
|
|
58
|
+
if (typeof r.at !== 'number' || !Number.isFinite(r.at) || r.at <= 0)
|
|
59
|
+
return null;
|
|
60
|
+
const values = r.values;
|
|
61
|
+
if (!values || typeof values !== 'object' || Array.isArray(values))
|
|
62
|
+
return null;
|
|
63
|
+
return { at: r.at, values: values };
|
|
64
|
+
}
|
|
65
|
+
/** `app` is a product code or short name: ≤ 31 characters, so the row's name fits the table's 40. */
|
|
66
|
+
const APP_NAME = /^[A-Za-z0-9_.-]{1,31}$/;
|
|
67
|
+
let entries = [];
|
|
68
|
+
const registryListeners = new Set();
|
|
69
|
+
/** Every synced store made on this page (one per `app`; a re-made one replaces it). */
|
|
70
|
+
export function getSyncedSettingsEntries() {
|
|
71
|
+
return entries;
|
|
72
|
+
}
|
|
73
|
+
export function subscribeSyncedSettingsEntries(listener) {
|
|
74
|
+
registryListeners.add(listener);
|
|
75
|
+
return () => { registryListeners.delete(listener); };
|
|
76
|
+
}
|
|
77
|
+
/** Reset every synced store on this page (Reset to defaults, via useResetAppDefaults). */
|
|
78
|
+
export function resetAllSyncedSettings() {
|
|
79
|
+
for (const e of entries)
|
|
80
|
+
e.reset();
|
|
81
|
+
}
|
|
82
|
+
function register(entry) {
|
|
83
|
+
entries = [...entries.filter((e) => e.app !== entry.app), entry];
|
|
84
|
+
for (const l of registryListeners)
|
|
85
|
+
l();
|
|
86
|
+
}
|
|
87
|
+
// ── The store ────────────────────────────────────────────────────────────────
|
|
88
|
+
function hasDom() {
|
|
89
|
+
return typeof window !== 'undefined';
|
|
90
|
+
}
|
|
91
|
+
function clone(v) {
|
|
92
|
+
return JSON.parse(JSON.stringify(v));
|
|
93
|
+
}
|
|
94
|
+
function same(a, b) {
|
|
95
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Create an app's synced Tune settings. Call it ONCE, at module level, and
|
|
99
|
+
* export the result.
|
|
100
|
+
*
|
|
101
|
+
* - `app`: the app's product code (`config.product`), ≤ 31 characters. The
|
|
102
|
+
* row is `tune-app:<app>`; the device stamp `unisim:tune-stamp:app:<app>`.
|
|
103
|
+
* - `defaults`: every setting's default. JSON values only. ⚠️ Use `null`, not
|
|
104
|
+
* `undefined`, for "none": an absent key in the account's row leaves this
|
|
105
|
+
* device's value as it is (so an older build that doesn't know a newer
|
|
106
|
+
* setting can't reset it), and `undefined` doesn't survive JSON.
|
|
107
|
+
* - `normalise(raw, defaults)`: turn anything — the stored object (in an old
|
|
108
|
+
* shape too), the account's values, a patched set — into a complete, valid
|
|
109
|
+
* set. It must not trust `raw` (it can be `undefined`, `null`, junk) and
|
|
110
|
+
* must fill every key; a throw reads as the defaults.
|
|
111
|
+
*
|
|
112
|
+
* ⚠️ Never change a setting's default by accident: a device that never
|
|
113
|
+
* changed anything shows the defaults, and so does a Reset. Tick boxes start
|
|
114
|
+
* unticked (BRANDING.md); word a default-on feature as an opt-out.
|
|
115
|
+
*/
|
|
116
|
+
export function createSyncedSettings(app, defaults, normalise, options = {}) {
|
|
117
|
+
if (!APP_NAME.test(app))
|
|
118
|
+
throw new Error(`createSyncedSettings: "${app}" is not a short app name (letters, digits, _ . -, ≤ 31)`);
|
|
119
|
+
const storageKey = options.storageKey ?? `unisim:settings:${app}`;
|
|
120
|
+
const deviceOnly = new Set(options.deviceOnly ?? []);
|
|
121
|
+
const listeners = new Set();
|
|
122
|
+
const changeListeners = new Set();
|
|
123
|
+
let quiet = 0;
|
|
124
|
+
const read = (raw) => {
|
|
125
|
+
try {
|
|
126
|
+
const v = normalise(raw, clone(defaults));
|
|
127
|
+
if (v && typeof v === 'object' && !Array.isArray(v))
|
|
128
|
+
return v;
|
|
129
|
+
}
|
|
130
|
+
catch { /* falls through to the defaults */ }
|
|
131
|
+
return clone(defaults);
|
|
132
|
+
};
|
|
133
|
+
const save = (v) => {
|
|
134
|
+
try {
|
|
135
|
+
window.localStorage.setItem(storageKey, JSON.stringify(v));
|
|
136
|
+
}
|
|
137
|
+
catch { /* storage off: held for this page */ }
|
|
138
|
+
};
|
|
139
|
+
const load = () => {
|
|
140
|
+
if (!hasDom())
|
|
141
|
+
return read(undefined);
|
|
142
|
+
let raw;
|
|
143
|
+
try {
|
|
144
|
+
const stored = window.localStorage.getItem(storageKey);
|
|
145
|
+
if (stored !== null)
|
|
146
|
+
raw = JSON.parse(stored);
|
|
147
|
+
}
|
|
148
|
+
catch { /* corrupt or unreadable */ }
|
|
149
|
+
if (raw === undefined && options.legacy) {
|
|
150
|
+
let old;
|
|
151
|
+
try {
|
|
152
|
+
old = options.legacy();
|
|
153
|
+
}
|
|
154
|
+
catch { /* nothing to migrate */ }
|
|
155
|
+
if (old !== undefined && old !== null) {
|
|
156
|
+
const migrated = read(old);
|
|
157
|
+
save(migrated);
|
|
158
|
+
return migrated;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return read(raw);
|
|
162
|
+
};
|
|
163
|
+
let state = load();
|
|
164
|
+
const emit = () => { for (const l of listeners)
|
|
165
|
+
l(); };
|
|
166
|
+
const noteChange = () => {
|
|
167
|
+
if (quiet > 0)
|
|
168
|
+
return;
|
|
169
|
+
for (const l of changeListeners)
|
|
170
|
+
l();
|
|
171
|
+
};
|
|
172
|
+
/** Commit `next`; returns which keys changed. */
|
|
173
|
+
const commit = (next) => {
|
|
174
|
+
const changed = Object.keys({ ...state, ...next }).filter((k) => !same(state[k], next[k]));
|
|
175
|
+
if (changed.length === 0)
|
|
176
|
+
return changed;
|
|
177
|
+
state = next;
|
|
178
|
+
save(state);
|
|
179
|
+
emit();
|
|
180
|
+
return changed;
|
|
181
|
+
};
|
|
182
|
+
const set = (patch) => {
|
|
183
|
+
const p = typeof patch === 'function' ? patch(state) : patch;
|
|
184
|
+
if (!p || typeof p !== 'object')
|
|
185
|
+
return;
|
|
186
|
+
const changed = commit(read({ ...state, ...p }));
|
|
187
|
+
if (changed.some((k) => !deviceOnly.has(k)))
|
|
188
|
+
noteChange();
|
|
189
|
+
};
|
|
190
|
+
const reset = () => {
|
|
191
|
+
const changed = commit(read(undefined));
|
|
192
|
+
if (changed.some((k) => !deviceOnly.has(k)))
|
|
193
|
+
noteChange();
|
|
194
|
+
};
|
|
195
|
+
const snapshot = () => {
|
|
196
|
+
const out = {};
|
|
197
|
+
for (const [k, v] of Object.entries(state)) {
|
|
198
|
+
if (!deviceOnly.has(k) && v !== undefined)
|
|
199
|
+
out[k] = v;
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
};
|
|
203
|
+
const applyRemote = (values) => {
|
|
204
|
+
// Only the synced keys the row has; this device keeps its device-only
|
|
205
|
+
// keys, and any key an older build's row doesn't know.
|
|
206
|
+
const incoming = {};
|
|
207
|
+
for (const [k, v] of Object.entries(values))
|
|
208
|
+
if (!deviceOnly.has(k))
|
|
209
|
+
incoming[k] = v;
|
|
210
|
+
quiet++;
|
|
211
|
+
try {
|
|
212
|
+
commit(read({ ...state, ...incoming }));
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
quiet--;
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
// Another tab on this device changed them.
|
|
219
|
+
if (hasDom()) {
|
|
220
|
+
try {
|
|
221
|
+
window.addEventListener('storage', (e) => {
|
|
222
|
+
if (e.key !== storageKey)
|
|
223
|
+
return;
|
|
224
|
+
let raw;
|
|
225
|
+
try {
|
|
226
|
+
raw = e.newValue === null ? undefined : JSON.parse(e.newValue);
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const next = read(raw);
|
|
232
|
+
if (same(next, state))
|
|
233
|
+
return;
|
|
234
|
+
state = next;
|
|
235
|
+
emit();
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
catch { /* no window events */ }
|
|
239
|
+
}
|
|
240
|
+
const subscribe = (l) => {
|
|
241
|
+
listeners.add(l);
|
|
242
|
+
return () => { listeners.delete(l); };
|
|
243
|
+
};
|
|
244
|
+
const getState = () => state;
|
|
245
|
+
function useSettings(selector) {
|
|
246
|
+
const s = useSyncExternalStore(subscribe, getState, getState);
|
|
247
|
+
return selector ? selector(s) : s;
|
|
248
|
+
}
|
|
249
|
+
const store = useSettings;
|
|
250
|
+
Object.defineProperties(store, {
|
|
251
|
+
getState: { value: getState },
|
|
252
|
+
set: { value: set },
|
|
253
|
+
setState: { value: set },
|
|
254
|
+
reset: { value: reset },
|
|
255
|
+
subscribe: { value: subscribe },
|
|
256
|
+
app: { value: app },
|
|
257
|
+
storageKey: { value: storageKey },
|
|
258
|
+
defaults: { get: () => read(undefined) },
|
|
259
|
+
});
|
|
260
|
+
register({
|
|
261
|
+
app,
|
|
262
|
+
snapshot,
|
|
263
|
+
applyRemote,
|
|
264
|
+
onChange: (l) => {
|
|
265
|
+
changeListeners.add(l);
|
|
266
|
+
return () => { changeListeners.delete(l); };
|
|
267
|
+
},
|
|
268
|
+
reset,
|
|
269
|
+
});
|
|
270
|
+
return store;
|
|
271
|
+
}
|
|
272
|
+
//# sourceMappingURL=syncedSettings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncedSettings.js","sourceRoot":"","sources":["../src/syncedSettings.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,wEAAwE;AACxE,8EAA8E;AAC9E,8EAA8E;AAC9E,2EAA2E;AAC3E,qEAAqE;AACrE,4EAA4E;AAC5E,kBAAkB;AAClB,EAAE;AACF,sDAAsD;AACtD,qEAAqE;AACrE,iDAAiD;AACjD,iGAAiG;AACjG,sBAAsB;AACtB,0DAA0D;AAC1D,8CAA8C;AAC9C,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,4EAA4E;AAC5E,EAAE;AACF,+DAA+D;AAC/D,0EAA0E;AAC1E,yEAAyE;AACzE,2EAA2E;AAC3E,0EAA0E;AAC1E,0EAA0E;AAC1E,uEAAuE;AACvE,EAAE;AACF,mEAAmE;AACnE,2EAA2E;AAC3E,kEAAkE;AAClE,4EAA4E;AAC5E,0EAA0E;AAC1E,sEAAsE;AACtE,6EAA6E;AAC7E,0EAA0E;AAC1E,sEAAsE;AACtE,wEAAwE;AACxE,EAAE;AACF,2EAA2E;AAC3E,6EAA6E;AAC7E,mEAAmE;AAEnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAA;AAE5C,6EAA6E;AAC7E,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,OAAO,YAAY,GAAG,EAAE,CAAA;AAC1B,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,iBAAiB,CAAC,GAAW;IAC3C,OAAO,OAAO,GAAG,EAAE,CAAA;AACrB,CAAC;AAQD,iFAAiF;AACj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,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACnC,CAAC;IAED,MAAM,KAAK,GAAG,WAAgC,CAAA;IAC9C,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE;QAC7B,QAAQ,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE;QAC7B,GAAG,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE;QACnB,QAAQ,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE;QACxB,KAAK,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE;QACvB,SAAS,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE;QAC/B,GAAG,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE;QACnB,UAAU,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE;QACjC,QAAQ,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;KACzC,CAAC,CAAA;IAEF,QAAQ,CAAC;QACP,GAAG;QACH,QAAQ;QACR,WAAW;QACX,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE;YACd,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;YACtB,OAAO,GAAG,EAAE,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA,CAAC,CAAC,CAAA;QAC5C,CAAC;QACD,KAAK;KACN,CAAC,CAAA;IAEF,OAAO,KAAK,CAAA;AACd,CAAC"}
|
package/electron/handoff.cjs
CHANGED
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
const { BrowserWindow, ipcMain, shell, session: electronSession } = require('electron')
|
|
27
27
|
// "Sign in with your browser" lives next door and ships from the same entry
|
|
28
28
|
// point, so an app needs one require for both halves of the desktop bridge.
|
|
29
|
-
const {
|
|
29
|
+
const {
|
|
30
|
+
installBrowserSignIn, CHANNEL_SIGN_IN, CHANNEL_SIGN_IN_CANCEL, CHANNEL_SUITE_SESSION, CHANNEL_SUITE_CHANGED,
|
|
31
|
+
} = require('./signin.cjs')
|
|
30
32
|
|
|
31
33
|
/** IPC channels. Preloads must use these exact strings — see PRELOAD_SNIPPET. */
|
|
32
34
|
const CHANNEL_OPEN = 'unisim:open-hub'
|
|
@@ -49,6 +51,12 @@ contextBridge.exposeInMainWorld('unisimDesktop', {
|
|
|
49
51
|
clearHub: () => ipcRenderer.invoke('${CHANNEL_CLEAR}'),
|
|
50
52
|
signInWithBrowser: (opts) => ipcRenderer.invoke('${CHANNEL_SIGN_IN}', opts),
|
|
51
53
|
cancelSignInWithBrowser: () => ipcRenderer.invoke('${CHANNEL_SIGN_IN_CANCEL}'),
|
|
54
|
+
suiteSession: (opts) => ipcRenderer.invoke('${CHANNEL_SUITE_SESSION}', opts),
|
|
55
|
+
onSuiteSessionChange: (cb) => {
|
|
56
|
+
const h = () => cb()
|
|
57
|
+
ipcRenderer.on('${CHANNEL_SUITE_CHANGED}', h)
|
|
58
|
+
return () => ipcRenderer.removeListener('${CHANNEL_SUITE_CHANGED}', h)
|
|
59
|
+
},
|
|
52
60
|
})
|
|
53
61
|
`.trim()
|
|
54
62
|
|
|
@@ -202,6 +210,8 @@ module.exports = {
|
|
|
202
210
|
installBrowserSignIn,
|
|
203
211
|
CHANNEL_SIGN_IN,
|
|
204
212
|
CHANNEL_SIGN_IN_CANCEL,
|
|
213
|
+
CHANNEL_SUITE_SESSION,
|
|
214
|
+
CHANNEL_SUITE_CHANGED,
|
|
205
215
|
CHANNEL_OPEN,
|
|
206
216
|
CHANNEL_CLEAR,
|
|
207
217
|
SUITE_SESSION_KEY,
|
package/electron/signin.cjs
CHANGED
|
@@ -32,14 +32,25 @@
|
|
|
32
32
|
// installBrowserSignIn({ app: 'pdf' })
|
|
33
33
|
//
|
|
34
34
|
// and two lines in the app's preload (see PRELOAD_SNIPPET in handoff.cjs).
|
|
35
|
+
//
|
|
36
|
+
// Since 0.233.0 it also gives the suite's desktop apps ONE sign-in per
|
|
37
|
+
// computer (suite-session.cjs): a browser sign-in here brings back a grant the
|
|
38
|
+
// other suite apps use, silently, to make sessions of their own; and an app
|
|
39
|
+
// that starts signed out uses one another app left. Two more preload lines
|
|
40
|
+
// (`suiteSession`, `onSuiteSessionChange`). `shareSession: false` turns it off.
|
|
35
41
|
|
|
36
42
|
const crypto = require('node:crypto')
|
|
37
43
|
const http = require('node:http')
|
|
44
|
+
const path = require('node:path')
|
|
38
45
|
const { BrowserWindow, app: electronApp, ipcMain, shell } = require('electron')
|
|
46
|
+
const { createSuiteSession, isPlatformUrl } = require('./suite-session.cjs')
|
|
39
47
|
|
|
40
48
|
/** IPC channels. Preloads must use these exact strings — see PRELOAD_SNIPPET. */
|
|
41
49
|
const CHANNEL_SIGN_IN = 'unisim:browser-sign-in'
|
|
42
50
|
const CHANNEL_SIGN_IN_CANCEL = 'unisim:browser-sign-in-cancel'
|
|
51
|
+
/** The suite session: one invoke channel with an `op`, and one event. */
|
|
52
|
+
const CHANNEL_SUITE_SESSION = 'unisim:suite-session'
|
|
53
|
+
const CHANNEL_SUITE_CHANGED = 'unisim:suite-session-changed'
|
|
43
54
|
|
|
44
55
|
const DEFAULT_HUB_URL = 'https://app.unisim.co.uk'
|
|
45
56
|
/** The sealed code itself lives 10 minutes; nobody should wait longer here. */
|
|
@@ -50,22 +61,6 @@ const APP_ID = /^[a-z0-9-]{1,32}$/
|
|
|
50
61
|
const b64url = (buf) =>
|
|
51
62
|
Buffer.from(buf).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
|
|
52
63
|
|
|
53
|
-
/**
|
|
54
|
-
* Only an https Supabase project (or a suite host) may be told the code and
|
|
55
|
-
* verifier. The renderer supplies the URL from its own config, and a renderer
|
|
56
|
-
* that has been talked into pointing it elsewhere must not get to hand the
|
|
57
|
-
* pair to a stranger.
|
|
58
|
-
*/
|
|
59
|
-
function isPlatformUrl(url) {
|
|
60
|
-
try {
|
|
61
|
-
const u = new URL(url)
|
|
62
|
-
if (u.protocol !== 'https:') return false
|
|
63
|
-
return u.hostname.endsWith('.supabase.co') || u.hostname === 'unisim.co.uk' || u.hostname.endsWith('.unisim.co.uk')
|
|
64
|
-
} catch {
|
|
65
|
-
return false
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
64
|
function listenLoopback() {
|
|
70
65
|
return new Promise((resolve, reject) => {
|
|
71
66
|
const server = http.createServer()
|
|
@@ -84,6 +79,8 @@ function listenLoopback() {
|
|
|
84
79
|
* UNISIM_HUB_URL when set (local testing).
|
|
85
80
|
* @param {(url: string) => Promise<void>} [options.openExternal]
|
|
86
81
|
* How to open the browser (tests).
|
|
82
|
+
* @param {boolean} [options.shareSession] One sign-in for every suite app on
|
|
83
|
+
* this computer (0.233.0). Default true.
|
|
87
84
|
* @returns {() => void} Removes the handlers again (tests, teardown).
|
|
88
85
|
*/
|
|
89
86
|
function installBrowserSignIn(options = {}) {
|
|
@@ -92,6 +89,21 @@ function installBrowserSignIn(options = {}) {
|
|
|
92
89
|
const hubUrl = String(options.hubUrl || process.env.UNISIM_HUB_URL || DEFAULT_HUB_URL).replace(/\/+$/, '')
|
|
93
90
|
const openExternal = options.openExternal || ((url) => shell.openExternal(url))
|
|
94
91
|
|
|
92
|
+
// The suite folder is shared by every suite app for this OS user; the
|
|
93
|
+
// state file is this app's own. Neither holds a secret (suite-session.cjs).
|
|
94
|
+
let suite = null
|
|
95
|
+
if (options.shareSession !== false) {
|
|
96
|
+
try {
|
|
97
|
+
suite = createSuiteSession({
|
|
98
|
+
dir: path.join(electronApp.getPath('appData'), 'UNISIM'),
|
|
99
|
+
stateFile: path.join(electronApp.getPath('userData'), 'unisim-suite-session.json'),
|
|
100
|
+
appId,
|
|
101
|
+
})
|
|
102
|
+
} catch {
|
|
103
|
+
suite = null
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
95
107
|
/** The sign-in in flight, if any: `{ cancel }`. One at a time. */
|
|
96
108
|
let pending = null
|
|
97
109
|
|
|
@@ -137,13 +149,15 @@ function installBrowserSignIn(options = {}) {
|
|
|
137
149
|
Promise.resolve(openExternal(link)).catch(() => finish({ error: 'no_browser' }))
|
|
138
150
|
})
|
|
139
151
|
if (got.error) return { ok: false, error: got.error }
|
|
152
|
+
const share = suite ? await suite.canStore() : false
|
|
140
153
|
|
|
141
154
|
let res
|
|
142
155
|
try {
|
|
143
156
|
res = await fetch(`${supabaseUrl}/functions/v1/desktop-sign-in`, {
|
|
144
157
|
method: 'POST',
|
|
145
158
|
headers: { 'content-type': 'application/json', apikey },
|
|
146
|
-
|
|
159
|
+
// `grant`: also a grant for the other suite apps here (0332).
|
|
160
|
+
body: JSON.stringify({ action: 'finish', code: got.code, verifier, ...(share ? { grant: true, app: appId } : {}) }),
|
|
147
161
|
signal: AbortSignal.timeout(20000),
|
|
148
162
|
})
|
|
149
163
|
} catch {
|
|
@@ -154,6 +168,10 @@ function installBrowserSignIn(options = {}) {
|
|
|
154
168
|
if (!data?.ok || typeof data.token_hash !== 'string') {
|
|
155
169
|
return { ok: false, error: data?.error || `http_${res.status}` }
|
|
156
170
|
}
|
|
171
|
+
// Best effort: the sign-in has worked whether or not this does.
|
|
172
|
+
if (suite && data.grant) {
|
|
173
|
+
await suite.saveGrant(data.grant, { supabaseUrl, apikey }).catch(() => {})
|
|
174
|
+
}
|
|
157
175
|
|
|
158
176
|
// The person is looking at their browser. Bring the window they started
|
|
159
177
|
// from back to the front, where the greeting is about to appear.
|
|
@@ -178,11 +196,25 @@ function installBrowserSignIn(options = {}) {
|
|
|
178
196
|
if (pending) pending.cancel()
|
|
179
197
|
return { ok: true }
|
|
180
198
|
})
|
|
199
|
+
ipcMain.handle(CHANNEL_SUITE_SESSION, (_event, payload) =>
|
|
200
|
+
suite ? suite.handle(payload) : { ok: false, action: 'none', error: 'off' })
|
|
201
|
+
|
|
202
|
+
// Another suite app signed in, or someone signed out of all of them: every
|
|
203
|
+
// window of this app asks again what it should do (`sync`).
|
|
204
|
+
const stopWatch = suite
|
|
205
|
+
? suite.watch(() => {
|
|
206
|
+
for (const win of BrowserWindow.getAllWindows()) {
|
|
207
|
+
if (!win.isDestroyed()) win.webContents.send(CHANNEL_SUITE_CHANGED)
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
: () => {}
|
|
181
211
|
|
|
182
212
|
return function uninstall() {
|
|
183
213
|
if (pending) pending.cancel()
|
|
214
|
+
stopWatch()
|
|
184
215
|
ipcMain.removeHandler(CHANNEL_SIGN_IN)
|
|
185
216
|
ipcMain.removeHandler(CHANNEL_SIGN_IN_CANCEL)
|
|
217
|
+
ipcMain.removeHandler(CHANNEL_SUITE_SESSION)
|
|
186
218
|
}
|
|
187
219
|
}
|
|
188
220
|
|
|
@@ -190,4 +222,6 @@ module.exports = {
|
|
|
190
222
|
installBrowserSignIn,
|
|
191
223
|
CHANNEL_SIGN_IN,
|
|
192
224
|
CHANNEL_SIGN_IN_CANCEL,
|
|
225
|
+
CHANNEL_SUITE_SESSION,
|
|
226
|
+
CHANNEL_SUITE_CHANGED,
|
|
193
227
|
}
|