@supersuit/suggest-a-change 0.1.2 → 0.1.3
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/CHANGELOG.md +8 -0
- package/README.md +1 -1
- package/dist/client/detail.js +27 -4
- package/dist/client/mine.js +29 -4
- package/dist/client/reports.d.ts +2 -0
- package/dist/client/view-cache.d.ts +21 -6
- package/dist/client/view-cache.js +51 -0
- package/package.json +1 -1
- package/src/client/detail.tsx +14 -3
- package/src/client/mine.tsx +20 -4
- package/src/client/reports.ts +2 -0
- package/src/client/view-cache.ts +41 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.3 (2026-10-13)
|
|
4
|
+
|
|
5
|
+
- **The phone's saved answers belong to whoever asked.** A list or report that the app answers after the signed-in account has changed is neither saved under the earlier account's key nor drawn: the account is asked again after the answer, as the outbox does before a write.
|
|
6
|
+
- **A refusal is not "could not reach the app".** `mine()` and `one(id)` signal a refusal by throwing an error with `refused: true` or a `status` of 401, 403 or 404 (`one` may also answer an object with a string `refused`). On a refusal the saved copy is deleted and the screen says the suggestion could not be opened (or the list could not be loaded); any other failure keeps the saved copy under the "Could not reach the app" line, as in 0.1.2. Documented on `StoreAdapter`.
|
|
7
|
+
- **The detail view follows its `id`.** Changing the id prop resets what is shown, so one report is never drawn under another's address.
|
|
8
|
+
- **The saved views are kept tidy.** After a successful list, saved reports the app no longer lists are deleted, and signing out clears the account's saved views from the phone.
|
|
9
|
+
- "Updating" shows only until the first answer after the screen opens, not on every periodic reload.
|
|
10
|
+
|
|
3
11
|
## 0.1.2 (2026-10-13)
|
|
4
12
|
|
|
5
13
|
- **My suggestions and the detail view draw at once.** Each keeps its last successful answer (`mine()` and each `one(id)`) in the module's own IndexedDB store, keyed by app and account like a draft, and draws it as soon as the phone has read it, with a quiet "Updating" line until the app answers. The app's answer replaces it (a report that vanished on the app disappears). If the app cannot be reached, the saved rows stay and the screen says so: "Could not reach the app. Showing what this phone last saw." A first open with nothing saved shows the loading state, as before. Reports waiting in the outbox show as they did. Another account never reads this account's saved answer. New strings `updating` and `offlineShowingSaved` (replaceable through `words.text`).
|
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ const adapters: Adapters = {
|
|
|
47
47
|
// then: {open && <SuggestSheet adapters={adapters} where={{ screen: "Photos", route: location.pathname }} onClose={() => setOpen(false)} />}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
`SuggestEntry` also takes `className` and `role` (for example `role="menuitem"` when a host shows it as a menu row); no role is set by default. My suggestions and the detail view keep their last answer from the app on the phone (module-owned IndexedDB, keyed by app and account like a draft), draw it at once with a quiet "Updating" line, and replace it when the app answers; if the app cannot be reached they keep the saved rows and say so. A first open with nothing saved shows "Loading".
|
|
50
|
+
`SuggestEntry` also takes `className` and `role` (for example `role="menuitem"` when a host shows it as a menu row); no role is set by default. My suggestions and the detail view keep their last answer from the app on the phone (module-owned IndexedDB, keyed by app and account like a draft), draw it at once with a quiet "Updating" line, and replace it when the app answers; if the app cannot be reached they keep the saved rows and say so. An adapter tells a refusal from an unreachable app: `mine()` and `one()` throw an error with `refused: true` or a `status` of 401, 403 or 404 for a refusal (the saved copy is then deleted), and anything else thrown is unreachable. Saved reports the list no longer holds are pruned, and signing out clears them. A first open with nothing saved shows "Loading".
|
|
51
51
|
|
|
52
52
|
The entry also keeps the phone's outbox moving while it is mounted (a report sent with the Mac away goes when it is back, with the sheet closed); a host with no always-mounted entry renders `<KeepSending adapters={adapters} />` once, high in the app. The entry carries `data-suggest-entry` (the attribute the screen check looks for; see "The check" below). Signed out, it opens nothing and says why. The receipt says "Got it" only after the adapter has answered `done` for the report and every attachment; until then it says the report is on the phone. Every text field the module renders sits in a `[data-voice-field]` container with a `[data-voice-control]`; a host's `Field` slot must keep that shape (it receives `FieldProps`, which is the Helm's voice field props). A voice note alone is a whole report: the recording is kept and uploaded as a part, and the receiving Mac writes the transcript.
|
|
53
53
|
|
package/dist/client/detail.js
CHANGED
|
@@ -11,7 +11,7 @@ import { recoverTakes } from './recorder.js';
|
|
|
11
11
|
import { queueFollowUp } from './reports.js';
|
|
12
12
|
import { useRuntime, useTick } from './runtime.js';
|
|
13
13
|
import { Toasts } from './toast.js';
|
|
14
|
-
import { oneCacheKey, readCache, writeCache } from './view-cache.js';
|
|
14
|
+
import { dropCache, isRefusal, oneCacheKey, readCache, useClearOnSignOut, writeCache } from './view-cache.js';
|
|
15
15
|
import { effectiveCeilings, lengthWords, sizeWords, statusMeaning, statusWords, stringsFor } from './words.js';
|
|
16
16
|
export function statusOf(r) {
|
|
17
17
|
if (typeof r.status === 'string')
|
|
@@ -96,7 +96,8 @@ export function SuggestionDetail({ adapters, id }) {
|
|
|
96
96
|
// Anything that writes the follow-up waits until its draft has been read, so the counter is right.
|
|
97
97
|
const restored = useRef(makeDeferred());
|
|
98
98
|
// A different account (or none) shows nothing of the last one.
|
|
99
|
-
|
|
99
|
+
useClearOnSignOut(rt, who, adapters.words.app);
|
|
100
|
+
useEffect(() => { loads.current++; shown.current = false; setUpdating(false); setOffline(false); setReport(undefined); setPending([]); setReady(false); setMore(''); setHeard([]); followId.current = null; n.current = 0; restored.current = makeDeferred(); }, [rt, id]);
|
|
100
101
|
const load = useCallback(async () => {
|
|
101
102
|
if (!rt)
|
|
102
103
|
return;
|
|
@@ -127,14 +128,30 @@ export function SuggestionDetail({ adapters, id }) {
|
|
|
127
128
|
}
|
|
128
129
|
}
|
|
129
130
|
let r;
|
|
131
|
+
let refused = false;
|
|
130
132
|
try {
|
|
131
|
-
|
|
133
|
+
const a = (await adapters.store.one(id));
|
|
134
|
+
if (a && typeof a === 'object' && typeof a.refused === 'string')
|
|
135
|
+
refused = true;
|
|
136
|
+
else
|
|
137
|
+
r = a;
|
|
132
138
|
}
|
|
133
|
-
catch {
|
|
139
|
+
catch (e) {
|
|
134
140
|
r = undefined;
|
|
141
|
+
refused = isRefusal(e);
|
|
142
|
+
}
|
|
143
|
+
// The answer belongs to whoever asked: if the signed-in account is no longer that one, nothing is kept or drawn.
|
|
144
|
+
let now = null;
|
|
145
|
+
try {
|
|
146
|
+
now = await adapters.identity();
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
now = null;
|
|
135
150
|
}
|
|
136
151
|
if (!fresh())
|
|
137
152
|
return;
|
|
153
|
+
if (!now || now.account !== rt.who.account)
|
|
154
|
+
return;
|
|
138
155
|
setUpdating(false);
|
|
139
156
|
if (r) {
|
|
140
157
|
shown.current = true;
|
|
@@ -142,6 +159,12 @@ export function SuggestionDetail({ adapters, id }) {
|
|
|
142
159
|
setReport(r);
|
|
143
160
|
void writeCache(rt.store, ckey, r);
|
|
144
161
|
}
|
|
162
|
+
else if (refused) {
|
|
163
|
+
shown.current = false;
|
|
164
|
+
setOffline(false);
|
|
165
|
+
void dropCache(rt.store, ckey);
|
|
166
|
+
setReport(null);
|
|
167
|
+
}
|
|
145
168
|
else if (shown.current)
|
|
146
169
|
setOffline(true);
|
|
147
170
|
else
|
package/dist/client/mine.js
CHANGED
|
@@ -7,7 +7,7 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
|
|
|
7
7
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
8
8
|
import { SuggestionDetail } from './detail.js';
|
|
9
9
|
import { useRuntime, useTick } from './runtime.js';
|
|
10
|
-
import { mineCacheKey, readCache, writeCache } from './view-cache.js';
|
|
10
|
+
import { dropCache, isRefusal, mineCacheKey, pruneOnes, readCache, useClearOnSignOut, writeCache } from './view-cache.js';
|
|
11
11
|
import { Toasts } from './toast.js';
|
|
12
12
|
import { statusMeaning, statusWords, stringsFor } from './words.js';
|
|
13
13
|
export function MySuggestions({ adapters, onOpen }) {
|
|
@@ -23,7 +23,10 @@ export function MySuggestions({ adapters, onOpen }) {
|
|
|
23
23
|
const loads = useRef(0);
|
|
24
24
|
// What the app last said (or the phone kept of it) for this account; null until either is known.
|
|
25
25
|
const known = useRef(null);
|
|
26
|
-
|
|
26
|
+
// The first answer after mount ends "Updating"; later periodic reloads refresh quietly.
|
|
27
|
+
const answered = useRef(false);
|
|
28
|
+
useClearOnSignOut(rt, who, adapters.words.app);
|
|
29
|
+
useEffect(() => { loads.current++; known.current = null; answered.current = false; setRows(null); setOpen(null); setBad(''); setUpdating(false); }, [rt]);
|
|
27
30
|
const load = useCallback(async () => {
|
|
28
31
|
if (!rt)
|
|
29
32
|
return;
|
|
@@ -49,25 +52,47 @@ export function MySuggestions({ adapters, onOpen }) {
|
|
|
49
52
|
if (mine_ !== loads.current)
|
|
50
53
|
return;
|
|
51
54
|
paint(known.current, local);
|
|
52
|
-
|
|
55
|
+
if (!answered.current)
|
|
56
|
+
setUpdating(true);
|
|
53
57
|
}
|
|
54
58
|
let theirs = null;
|
|
59
|
+
let refused = false;
|
|
55
60
|
try {
|
|
56
61
|
theirs = await adapters.store.mine();
|
|
57
62
|
}
|
|
58
|
-
catch {
|
|
63
|
+
catch (e) {
|
|
59
64
|
theirs = null;
|
|
65
|
+
refused = isRefusal(e);
|
|
66
|
+
}
|
|
67
|
+
// The answer belongs to whoever asked. If the signed-in account is no longer that one, nothing is kept or drawn.
|
|
68
|
+
let now = null;
|
|
69
|
+
try {
|
|
70
|
+
now = await adapters.identity();
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
now = null;
|
|
60
74
|
}
|
|
61
75
|
const local = await rt.waiting();
|
|
62
76
|
if (mine_ !== loads.current)
|
|
63
77
|
return;
|
|
78
|
+
if (!now || now.account !== rt.who.account)
|
|
79
|
+
return;
|
|
80
|
+
answered.current = true;
|
|
64
81
|
setUpdating(false);
|
|
65
82
|
if (theirs) {
|
|
66
83
|
known.current = theirs;
|
|
67
84
|
void writeCache(rt.store, key, theirs);
|
|
85
|
+
void pruneOnes(rt.store, { app: adapters.words.app, account: rt.who.account }, theirs.map((t) => t.id));
|
|
68
86
|
setBad('');
|
|
69
87
|
paint(theirs, local);
|
|
70
88
|
}
|
|
89
|
+
else if (refused) {
|
|
90
|
+
// The app said no: what the phone held is no longer this person's to see.
|
|
91
|
+
known.current = null;
|
|
92
|
+
void dropCache(rt.store, key);
|
|
93
|
+
setBad(S.couldNotLoadMine);
|
|
94
|
+
paint([], local);
|
|
95
|
+
}
|
|
71
96
|
else if (known.current !== null) {
|
|
72
97
|
setBad(S.offlineShowingSaved);
|
|
73
98
|
paint(known.current, local);
|
package/dist/client/reports.d.ts
CHANGED
|
@@ -22,7 +22,9 @@ export interface StoreAdapter {
|
|
|
22
22
|
uploadPart(reportId: string, file: string, part: number, bytes: Blob): Promise<'done' | {
|
|
23
23
|
refused: string;
|
|
24
24
|
}>;
|
|
25
|
+
/** An unreachable app THROWS (a network error). A refusal (the list is no longer this person's) throws an error with `refused: true` or a `status` of 401, 403 or 404: the phone then drops what it kept. */
|
|
25
26
|
mine(): Promise<Summary[]>;
|
|
27
|
+
/** Same contract as `mine`: a report that is gone or not this person's is a refusal (`refused: true`, or status 401/403/404, or an answer object with a string `refused`), never a plain failure, so the phone stops showing its saved copy. */
|
|
26
28
|
one(id: string): Promise<Report>;
|
|
27
29
|
/** Adds to a report. Also at-least-once: it must dedupe by (id, `more.followId`). */
|
|
28
30
|
append(id: string, more: Record<string, unknown>): Promise<'done' | {
|
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
import type { Store } from './idb.ts';
|
|
2
|
-
|
|
2
|
+
type Id = {
|
|
3
3
|
app: string;
|
|
4
4
|
account: string;
|
|
5
|
-
}
|
|
6
|
-
export declare const
|
|
7
|
-
|
|
8
|
-
account: string;
|
|
9
|
-
}, reportId: string) => string;
|
|
5
|
+
};
|
|
6
|
+
export declare const mineCacheKey: (id: Id) => string;
|
|
7
|
+
export declare const oneCacheKey: (id: Id, reportId: string) => string;
|
|
10
8
|
export declare function readCache<T>(store: Store, key: string): Promise<T | undefined>;
|
|
11
9
|
export declare function writeCache(store: Store, key: string, value: unknown): Promise<void>;
|
|
10
|
+
export declare function dropCache(store: Store, key: string): Promise<void>;
|
|
11
|
+
/** After a successful list: cached reports the app no longer lists (deleted, or no longer this person's) are dropped. */
|
|
12
|
+
export declare function pruneOnes(store: Store, id: Id, keep: string[]): Promise<void>;
|
|
13
|
+
/** Everything cached for one account: the list and every report. */
|
|
14
|
+
export declare function clearViewCache(store: Store, id: Id): Promise<void>;
|
|
15
|
+
/** True when the app REFUSED (the report is gone, or this person may not see it), as against being unreachable.
|
|
16
|
+
* The adapter contract: `mine()` and `one()` throw an error carrying `refused: true`, or a `status` of 401, 403 or 404;
|
|
17
|
+
* `one()` may instead answer an object with a string `refused`. Anything else thrown is a failure to reach the app. */
|
|
18
|
+
export declare function isRefusal(e: unknown): boolean;
|
|
19
|
+
/** When the person signs out (identity becomes null), the account that was signed in loses its cached views on this phone. */
|
|
20
|
+
export declare function useClearOnSignOut(rt: {
|
|
21
|
+
store: Store;
|
|
22
|
+
who: {
|
|
23
|
+
account: string;
|
|
24
|
+
};
|
|
25
|
+
} | null, who: unknown, app: string): void;
|
|
26
|
+
export {};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
// THE LAST ANSWER, KEPT ON THE PHONE. My suggestions and the detail view draw from this at once and then
|
|
2
3
|
// refresh from the app, so opening them never waits on the app being reachable. The key names the app and
|
|
3
4
|
// the account exactly as a draft's does, so one person's cache is never read for another.
|
|
5
|
+
import { useEffect, useRef } from 'react';
|
|
4
6
|
import { draftKey } from './drafts.js';
|
|
5
7
|
const base = (id) => `${draftKey(id)}#view`;
|
|
6
8
|
export const mineCacheKey = (id) => `${base(id)}:mine`;
|
|
@@ -19,3 +21,52 @@ export async function writeCache(store, key, value) {
|
|
|
19
21
|
}
|
|
20
22
|
catch { /* a full or blocked database only costs the next open its head start */ }
|
|
21
23
|
}
|
|
24
|
+
export async function dropCache(store, key) {
|
|
25
|
+
try {
|
|
26
|
+
await store.del('kv', key);
|
|
27
|
+
}
|
|
28
|
+
catch { /* nothing to drop */ }
|
|
29
|
+
}
|
|
30
|
+
/** After a successful list: cached reports the app no longer lists (deleted, or no longer this person's) are dropped. */
|
|
31
|
+
export async function pruneOnes(store, id, keep) {
|
|
32
|
+
try {
|
|
33
|
+
const prefix = `${base(id)}:one:`;
|
|
34
|
+
const live = new Set(keep);
|
|
35
|
+
for (const row of await store.all('kv', prefix))
|
|
36
|
+
if (!live.has(row.key.slice(prefix.length)))
|
|
37
|
+
await store.del('kv', row.key);
|
|
38
|
+
}
|
|
39
|
+
catch { /* the next list prunes again */ }
|
|
40
|
+
}
|
|
41
|
+
/** Everything cached for one account: the list and every report. */
|
|
42
|
+
export async function clearViewCache(store, id) {
|
|
43
|
+
try {
|
|
44
|
+
for (const row of await store.all('kv', base(id)))
|
|
45
|
+
await store.del('kv', row.key);
|
|
46
|
+
}
|
|
47
|
+
catch { /* best effort */ }
|
|
48
|
+
}
|
|
49
|
+
/** True when the app REFUSED (the report is gone, or this person may not see it), as against being unreachable.
|
|
50
|
+
* The adapter contract: `mine()` and `one()` throw an error carrying `refused: true`, or a `status` of 401, 403 or 404;
|
|
51
|
+
* `one()` may instead answer an object with a string `refused`. Anything else thrown is a failure to reach the app. */
|
|
52
|
+
export function isRefusal(e) {
|
|
53
|
+
if (!e || typeof e !== 'object')
|
|
54
|
+
return false;
|
|
55
|
+
const x = e;
|
|
56
|
+
return x.refused === true || (typeof x.refused === 'string' && x.refused !== '') || x.status === 401 || x.status === 403 || x.status === 404;
|
|
57
|
+
}
|
|
58
|
+
/** When the person signs out (identity becomes null), the account that was signed in loses its cached views on this phone. */
|
|
59
|
+
export function useClearOnSignOut(rt, who, app) {
|
|
60
|
+
const last = useRef(null);
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (rt) {
|
|
63
|
+
last.current = { store: rt.store, id: { app, account: rt.who.account } };
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (who === null && last.current) {
|
|
67
|
+
const l = last.current;
|
|
68
|
+
last.current = null;
|
|
69
|
+
void clearViewCache(l.store, l.id);
|
|
70
|
+
}
|
|
71
|
+
}, [rt, who, app]);
|
|
72
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supersuit/suggest-a-change",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "The report format and flow behind a Suggest a change button: what a report is, who acts at each step, the screens, and the check every app runs. The contract has zero dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/src/client/detail.tsx
CHANGED
|
@@ -11,7 +11,7 @@ import { recoverTakes } from './recorder.ts'
|
|
|
11
11
|
import { queueFollowUp, type Report } from './reports.ts'
|
|
12
12
|
import { useRuntime, useTick, type FollowUp } from './runtime.ts'
|
|
13
13
|
import { Toasts } from './toast.tsx'
|
|
14
|
-
import { oneCacheKey, readCache, writeCache } from './view-cache.ts'
|
|
14
|
+
import { dropCache, isRefusal, oneCacheKey, readCache, useClearOnSignOut, writeCache } from './view-cache.ts'
|
|
15
15
|
import { effectiveCeilings, lengthWords, sizeWords, statusMeaning, statusWords, stringsFor } from './words.ts'
|
|
16
16
|
|
|
17
17
|
export function statusOf(r: Record<string, unknown>): string {
|
|
@@ -103,7 +103,8 @@ export function SuggestionDetail({ adapters, id }: { adapters: Adapters; id: str
|
|
|
103
103
|
const restored = useRef(makeDeferred())
|
|
104
104
|
|
|
105
105
|
// A different account (or none) shows nothing of the last one.
|
|
106
|
-
|
|
106
|
+
useClearOnSignOut(rt, who, adapters.words.app)
|
|
107
|
+
useEffect(() => { loads.current++; shown.current = false; setUpdating(false); setOffline(false); setReport(undefined); setPending([]); setReady(false); setMore(''); setHeard([]); followId.current = null; n.current = 0; restored.current = makeDeferred() }, [rt, id])
|
|
107
108
|
|
|
108
109
|
const load = useCallback(async () => {
|
|
109
110
|
if (!rt) return
|
|
@@ -124,10 +125,20 @@ export function SuggestionDetail({ adapters, id }: { adapters: Adapters; id: str
|
|
|
124
125
|
if (saved && typeof saved === 'object') { shown.current = true; setReport(saved); setUpdating(true) }
|
|
125
126
|
}
|
|
126
127
|
let r: (Report & Record<string, unknown>) | undefined
|
|
127
|
-
|
|
128
|
+
let refused = false
|
|
129
|
+
try {
|
|
130
|
+
const a = (await adapters.store.one(id)) as unknown
|
|
131
|
+
if (a && typeof a === 'object' && typeof (a as { refused?: unknown }).refused === 'string') refused = true
|
|
132
|
+
else r = a as Report & Record<string, unknown>
|
|
133
|
+
} catch (e) { r = undefined; refused = isRefusal(e) }
|
|
134
|
+
// The answer belongs to whoever asked: if the signed-in account is no longer that one, nothing is kept or drawn.
|
|
135
|
+
let now: { account: string } | null = null
|
|
136
|
+
try { now = await adapters.identity() } catch { now = null }
|
|
128
137
|
if (!fresh()) return
|
|
138
|
+
if (!now || now.account !== rt.who.account) return
|
|
129
139
|
setUpdating(false)
|
|
130
140
|
if (r) { shown.current = true; setOffline(false); setReport(r); void writeCache(rt.store, ckey, r) }
|
|
141
|
+
else if (refused) { shown.current = false; setOffline(false); void dropCache(rt.store, ckey); setReport(null) }
|
|
131
142
|
else if (shown.current) setOffline(true)
|
|
132
143
|
else setReport(null)
|
|
133
144
|
} catch { if (fresh()) setReport(null) }
|
package/src/client/mine.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import type { Adapters } from './adapters.ts'
|
|
|
8
8
|
import { SuggestionDetail } from './detail.tsx'
|
|
9
9
|
import type { Summary } from './reports.ts'
|
|
10
10
|
import { useRuntime, useTick } from './runtime.ts'
|
|
11
|
-
import { mineCacheKey, readCache, writeCache } from './view-cache.ts'
|
|
11
|
+
import { dropCache, isRefusal, mineCacheKey, pruneOnes, readCache, useClearOnSignOut, writeCache } from './view-cache.ts'
|
|
12
12
|
import { Toasts } from './toast.tsx'
|
|
13
13
|
import { statusMeaning, statusWords, stringsFor } from './words.ts'
|
|
14
14
|
|
|
@@ -28,7 +28,10 @@ export function MySuggestions({ adapters, onOpen }: { adapters: Adapters; onOpen
|
|
|
28
28
|
const loads = useRef(0)
|
|
29
29
|
// What the app last said (or the phone kept of it) for this account; null until either is known.
|
|
30
30
|
const known = useRef<Summary[] | null>(null)
|
|
31
|
-
|
|
31
|
+
// The first answer after mount ends "Updating"; later periodic reloads refresh quietly.
|
|
32
|
+
const answered = useRef(false)
|
|
33
|
+
useClearOnSignOut(rt, who, adapters.words.app)
|
|
34
|
+
useEffect(() => { loads.current++; known.current = null; answered.current = false; setRows(null); setOpen(null); setBad(''); setUpdating(false) }, [rt])
|
|
32
35
|
|
|
33
36
|
const load = useCallback(async () => {
|
|
34
37
|
if (!rt) return
|
|
@@ -51,18 +54,31 @@ export function MySuggestions({ adapters, onOpen }: { adapters: Adapters; onOpen
|
|
|
51
54
|
const local = await rt.waiting()
|
|
52
55
|
if (mine_ !== loads.current) return
|
|
53
56
|
paint(known.current, local)
|
|
54
|
-
setUpdating(true)
|
|
57
|
+
if (!answered.current) setUpdating(true)
|
|
55
58
|
}
|
|
56
59
|
let theirs: Summary[] | null = null
|
|
57
|
-
|
|
60
|
+
let refused = false
|
|
61
|
+
try { theirs = await adapters.store.mine() } catch (e) { theirs = null; refused = isRefusal(e) }
|
|
62
|
+
// The answer belongs to whoever asked. If the signed-in account is no longer that one, nothing is kept or drawn.
|
|
63
|
+
let now: { account: string } | null = null
|
|
64
|
+
try { now = await adapters.identity() } catch { now = null }
|
|
58
65
|
const local = await rt.waiting()
|
|
59
66
|
if (mine_ !== loads.current) return
|
|
67
|
+
if (!now || now.account !== rt.who.account) return
|
|
68
|
+
answered.current = true
|
|
60
69
|
setUpdating(false)
|
|
61
70
|
if (theirs) {
|
|
62
71
|
known.current = theirs
|
|
63
72
|
void writeCache(rt.store, key, theirs)
|
|
73
|
+
void pruneOnes(rt.store, { app: adapters.words.app, account: rt.who.account }, theirs.map((t) => t.id))
|
|
64
74
|
setBad('')
|
|
65
75
|
paint(theirs, local)
|
|
76
|
+
} else if (refused) {
|
|
77
|
+
// The app said no: what the phone held is no longer this person's to see.
|
|
78
|
+
known.current = null
|
|
79
|
+
void dropCache(rt.store, key)
|
|
80
|
+
setBad(S.couldNotLoadMine)
|
|
81
|
+
paint([], local)
|
|
66
82
|
} else if (known.current !== null) {
|
|
67
83
|
setBad(S.offlineShowingSaved)
|
|
68
84
|
paint(known.current, local)
|
package/src/client/reports.ts
CHANGED
|
@@ -21,7 +21,9 @@ export interface Summary { id: string; status: string; kind?: string; at?: numbe
|
|
|
21
21
|
export interface StoreAdapter {
|
|
22
22
|
submit(report: Report): Promise<'done' | { refused: string }>
|
|
23
23
|
uploadPart(reportId: string, file: string, part: number, bytes: Blob): Promise<'done' | { refused: string }>
|
|
24
|
+
/** An unreachable app THROWS (a network error). A refusal (the list is no longer this person's) throws an error with `refused: true` or a `status` of 401, 403 or 404: the phone then drops what it kept. */
|
|
24
25
|
mine(): Promise<Summary[]>
|
|
26
|
+
/** Same contract as `mine`: a report that is gone or not this person's is a refusal (`refused: true`, or status 401/403/404, or an answer object with a string `refused`), never a plain failure, so the phone stops showing its saved copy. */
|
|
25
27
|
one(id: string): Promise<Report>
|
|
26
28
|
/** Adds to a report. Also at-least-once: it must dedupe by (id, `more.followId`). */
|
|
27
29
|
append(id: string, more: Record<string, unknown>): Promise<'done' | { refused: string }>
|
package/src/client/view-cache.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
'use client'
|
|
1
2
|
// THE LAST ANSWER, KEPT ON THE PHONE. My suggestions and the detail view draw from this at once and then
|
|
2
3
|
// refresh from the app, so opening them never waits on the app being reachable. The key names the app and
|
|
3
4
|
// the account exactly as a draft's does, so one person's cache is never read for another.
|
|
5
|
+
import { useEffect, useRef } from 'react'
|
|
4
6
|
import { draftKey } from './drafts.ts'
|
|
5
7
|
import type { Store } from './idb.ts'
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export const
|
|
9
|
+
type Id = { app: string; account: string }
|
|
10
|
+
const base = (id: Id) => `${draftKey(id)}#view`
|
|
11
|
+
export const mineCacheKey = (id: Id) => `${base(id)}:mine`
|
|
12
|
+
export const oneCacheKey = (id: Id, reportId: string) => `${base(id)}:one:${reportId}`
|
|
10
13
|
|
|
11
14
|
export async function readCache<T>(store: Store, key: string): Promise<T | undefined> {
|
|
12
15
|
try { return await store.get<T>('kv', key) } catch { return undefined }
|
|
@@ -14,3 +17,38 @@ export async function readCache<T>(store: Store, key: string): Promise<T | undef
|
|
|
14
17
|
export async function writeCache(store: Store, key: string, value: unknown): Promise<void> {
|
|
15
18
|
try { await store.put('kv', key, value) } catch { /* a full or blocked database only costs the next open its head start */ }
|
|
16
19
|
}
|
|
20
|
+
export async function dropCache(store: Store, key: string): Promise<void> {
|
|
21
|
+
try { await store.del('kv', key) } catch { /* nothing to drop */ }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** After a successful list: cached reports the app no longer lists (deleted, or no longer this person's) are dropped. */
|
|
25
|
+
export async function pruneOnes(store: Store, id: Id, keep: string[]): Promise<void> {
|
|
26
|
+
try {
|
|
27
|
+
const prefix = `${base(id)}:one:`
|
|
28
|
+
const live = new Set(keep)
|
|
29
|
+
for (const row of await store.all('kv', prefix)) if (!live.has(row.key.slice(prefix.length))) await store.del('kv', row.key)
|
|
30
|
+
} catch { /* the next list prunes again */ }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Everything cached for one account: the list and every report. */
|
|
34
|
+
export async function clearViewCache(store: Store, id: Id): Promise<void> {
|
|
35
|
+
try { for (const row of await store.all('kv', base(id))) await store.del('kv', row.key) } catch { /* best effort */ }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** True when the app REFUSED (the report is gone, or this person may not see it), as against being unreachable.
|
|
39
|
+
* The adapter contract: `mine()` and `one()` throw an error carrying `refused: true`, or a `status` of 401, 403 or 404;
|
|
40
|
+
* `one()` may instead answer an object with a string `refused`. Anything else thrown is a failure to reach the app. */
|
|
41
|
+
export function isRefusal(e: unknown): boolean {
|
|
42
|
+
if (!e || typeof e !== 'object') return false
|
|
43
|
+
const x = e as { refused?: unknown; status?: unknown }
|
|
44
|
+
return x.refused === true || (typeof x.refused === 'string' && x.refused !== '') || x.status === 401 || x.status === 403 || x.status === 404
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** When the person signs out (identity becomes null), the account that was signed in loses its cached views on this phone. */
|
|
48
|
+
export function useClearOnSignOut(rt: { store: Store; who: { account: string } } | null, who: unknown, app: string): void {
|
|
49
|
+
const last = useRef<{ store: Store; id: Id } | null>(null)
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (rt) { last.current = { store: rt.store, id: { app, account: rt.who.account } }; return }
|
|
52
|
+
if (who === null && last.current) { const l = last.current; last.current = null; void clearViewCache(l.store, l.id) }
|
|
53
|
+
}, [rt, who, app])
|
|
54
|
+
}
|