forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserviceWorkerStore.ts
More file actions
67 lines (56 loc) · 1.52 KB
/
Copy pathserviceWorkerStore.ts
File metadata and controls
67 lines (56 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { create } from "zustand";
/**
* PWA / service worker UI state. Updated by `registerServiceWorker` in sw-register
* and by user actions (dismiss, retry) from InstallPrompt.
*/
export type ServiceWorkerUiPhase =
| "idle"
| "registering"
| "ready"
| "update_available"
| "error"
| "unsupported";
export type ServiceWorkerStore = {
phase: ServiceWorkerUiPhase;
error: string | null;
setPhase: (phase: ServiceWorkerUiPhase) => void;
setError: (message: string | null) => void;
setUpdateAvailable: () => void;
/** Hide the "new version" bar until a future update is detected. */
dismissUpdateBanner: () => void;
clearError: () => void;
/** Tests only: reset to initial. */
reset: () => void;
};
const initial: Pick<ServiceWorkerStore, "phase" | "error"> = {
phase: "idle",
error: null,
};
export const useServiceWorkerStore = create<ServiceWorkerStore>((set, get) => ({
...initial,
setPhase: (phase) => set({ phase }),
setError: (message) => {
if (message) {
set({ error: message, phase: "error" });
return;
}
set((s) => ({
error: null,
phase: s.phase === "error" ? "ready" : s.phase,
}));
},
setUpdateAvailable: () => {
if (get().phase === "error") return;
set({ phase: "update_available", error: null });
},
dismissUpdateBanner: () => {
set({ phase: "ready" });
},
clearError: () => {
set((s) => ({
error: null,
phase: s.phase === "error" ? "idle" : s.phase,
}));
},
reset: () => set({ ...initial }),
}));