Skip to content

Commit 1eb9b89

Browse files
baxenmbot
andcommitted
Ask again with Replace once the account's key gets a backup
An OK on the no-backup prompt was remembered for the pair of keys alone, so after turning on Identity sync on the other device, Replace was never offered. The dismissal now also records whether Replace was offered, checked after the backup status is read. Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz> Signed-off-by: Bradley Axen <[email protected]>
1 parent b4f49ca commit 1eb9b89

3 files changed

Lines changed: 55 additions & 14 deletions

File tree

‎docs/identity.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ the account's key, or a backup status that can't be read, Replace is not
7777
offered, since Welcome back could not restore it: the dialog says the account's
7878
key lives on another device and offers only **OK**. **Not now**, **OK** and
7979
Close keep the key and are remembered for that pair of keys (the account's and
80-
this device's, `buzz.linked-key-dismissed.v1`), so it asks again only when
81-
either changes. The prompt waits while the launch screen shows or old Buzz's
80+
this device's) and whether Replace was offered (`buzz.linked-key-dismissed.v1`),
81+
so it asks again only when either key changes or the account's key gets a
82+
backup. The prompt waits while the launch screen shows or old Buzz's
8283
communities are being offered or added, and the backup consent prompt waits
8384
while it shows.
8485

‎src/features/identity/LinkedKeyCheck.tsx‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ type Session = Pick<FederatedIdentity, "snapshot" | "subscribe" | "request">;
2323

2424
const noSession = () => () => {};
2525

26-
/** The pair of keys a Not now was given for: `linked:viewer`. */
26+
/**
27+
* What a Not now or OK was given for: `linked:viewer:restorable`, so the
28+
* no-backup OK asks again, with Replace, once the account's key is backed up.
29+
*/
2730
const DISMISSED = "buzz.linked-key-dismissed.v1";
28-
const pair = (linked: string, viewer: string) => `${linked}:${viewer}`;
31+
const asked = (linked: string, viewer: string, restorable: boolean) =>
32+
`${linked}:${viewer}:${restorable}`;
2933
function dismissedPair() {
3034
try {
3135
return localStorage.getItem(DISMISSED);
@@ -61,8 +65,9 @@ export const useSession = (
6165
* Replace is offered only when Builderlab holds a backup of the account's key,
6266
* so the Welcome back it restarts into can restore it: it is setup's own
6367
* replace step, which removes this key and restarts. Otherwise it only says
64-
* where the account's key is. Not now is remembered for this pair of keys, so
65-
* it asks again only when either changes. While it asks, `children` (the
68+
* where the account's key is. Not now (or OK) is remembered for this pair of
69+
* keys and whether Replace was offered, so it asks again only when either key
70+
* changes or the account's key gets a backup. While it asks, `children` (the
6671
* backup prompt) waits.
6772
*/
6873
export function LinkedKeyCheck({
@@ -115,14 +120,15 @@ export function LinkedKeyCheck({
115120
);
116121
const linked = await account.linkedKey();
117122
if (!current || !linked || linked === viewer) return;
118-
const asked = pair(linked, viewer);
119-
if (dismissed.current === asked || dismissedPair() === asked) return;
120123
const [backups, backedUp] = await Promise.all([
121124
account.backupStatus().catch(() => "unavailable" as const),
122125
identity.backedUp(viewer).catch(() => false),
123126
]);
124127
const restorable =
125128
backups !== "unavailable" && backups.includes(linked);
129+
const question = asked(linked, viewer, restorable);
130+
if (dismissed.current === question || dismissedPair() === question)
131+
return;
126132
if (current)
127133
setFound({ revision, viewer, linked, restorable, backedUp });
128134
})
@@ -140,9 +146,9 @@ export function LinkedKeyCheck({
140146
)
141147
return children;
142148
const dismiss = () => {
143-
const asked = pair(found.linked, found.viewer);
144-
dismissed.current = asked;
145-
rememberDismissed(asked);
149+
const question = asked(found.linked, found.viewer, found.restorable);
150+
dismissed.current = question;
151+
rememberDismissed(question);
146152
setFound(undefined);
147153
};
148154
const account = session?.email ?? "Your Builderlab account";

‎src/features/identity/identity.test.tsx‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8695,7 +8695,7 @@ it("an account linked to another backed-up key asks over the open app, saying Re
86958695
expect(screen.getByText("Signed in")).toBeVisible();
86968696
// A renewal reads again, but this pair of keys is not asked about again.
86978697
fi.publish({ revision: "user:renewed" });
8698-
await waitFor(() => expect(fi.request).toHaveBeenCalledTimes(3));
8698+
await waitFor(() => expect(fi.request).toHaveBeenCalledTimes(4));
86998699
await act(settled);
87008700
expect(screen.queryByRole("alertdialog")).toBeNull();
87018701
expect(signOuts()).toEqual([]);
@@ -8710,7 +8710,7 @@ it("an account linked to another backed-up key asks over the open app, saying Re
87108710
);
87118711
const relaunched = openWithSession(again);
87128712
expect(await screen.findByText("Signed in")).toBeVisible();
8713-
await waitFor(() => expect(again.request).toHaveBeenCalledTimes(1));
8713+
await waitFor(() => expect(again.request).toHaveBeenCalledTimes(2));
87148714
await act(settled);
87158715
expect(screen.queryByRole("alertdialog")).toBeNull();
87168716
relaunched.dispose();
@@ -8744,14 +8744,48 @@ it.each([
87448744
await user.click(within(prompt).getByRole("button", { name: "OK" }));
87458745
await waitFor(() => expect(screen.queryByRole("alertdialog")).toBeNull());
87468746
expect(localStorage.getItem("buzz.linked-key-dismissed.v1")).toBe(
8747-
`${owned.pubkey}:${viewer}`,
8747+
`${owned.pubkey}:${viewer}:false`,
87488748
);
87498749
expect(signOuts()).toEqual([]);
87508750
expect(identity.snapshot()).toEqual({ status: "ready", viewer });
87518751
identity.dispose();
87528752
},
87538753
);
87548754

8755+
it("after OK on the no-backup prompt, a relaunch stays quiet while the account's key has no backup, and offers Replace once it does", async () => {
8756+
anotherKeyOnDevice(false, undefined, true);
8757+
const fi = federatedFixture();
8758+
fi.request.mockImplementation(accountAnswers(owned.pubkey, []));
8759+
const identity = openWithSession(fi);
8760+
const user = userEvent.setup();
8761+
const prompt = await mismatchPrompt();
8762+
expect(replaceButton(prompt)).toBeNull();
8763+
await user.click(within(prompt).getByRole("button", { name: "OK" }));
8764+
await waitFor(() => expect(screen.queryByRole("alertdialog")).toBeNull());
8765+
identity.dispose();
8766+
cleanup();
8767+
8768+
// Still no backup: the OK stands.
8769+
const still = federatedFixture();
8770+
still.request.mockImplementation(accountAnswers(owned.pubkey, []));
8771+
const quiet = openWithSession(still);
8772+
expect(await screen.findByText("Signed in")).toBeVisible();
8773+
await waitFor(() => expect(still.request).toHaveBeenCalledTimes(2));
8774+
await act(settled);
8775+
expect(screen.queryByRole("alertdialog")).toBeNull();
8776+
quiet.dispose();
8777+
cleanup();
8778+
8779+
// Identity sync on the other device backed it up: Replace is offered now.
8780+
const synced = federatedFixture();
8781+
synced.request.mockImplementation(
8782+
accountAnswers(owned.pubkey, [owned.pubkey]),
8783+
);
8784+
const asked = openWithSession(synced);
8785+
expect(replaceButton(await mismatchPrompt())).toBeEnabled();
8786+
asked.dispose();
8787+
});
8788+
87558789
it("Replace on the open app's prompt signs this key out to restart and sign in again, saying a backed-up key can be restored", async () => {
87568790
anotherKeyOnDevice(true, undefined, true);
87578791
const fi = federatedFixture();

0 commit comments

Comments
 (0)