|
1 | | -import { createEffect } from "solid-js"; |
2 | | -import { createInitializedContext } from "../util/context"; |
3 | | -import { makePersisted } from "@solid-primitives/storage"; |
4 | | -import { createStore } from "solid-js/store"; |
| 1 | +import { createEffect } from "solid-js" |
| 2 | +import { createInitializedContext } from "../util/context" |
| 3 | +import { makePersisted } from "@solid-primitives/storage" |
| 4 | +import { createStore } from "solid-js/store" |
5 | 5 | import { useOpenAuth } from "@openauthjs/solid" |
6 | 6 |
|
7 | 7 | type Storage = { |
8 | | - accounts: Record<string, { |
9 | | - id: string |
10 | | - email: string |
11 | | - workspaces: { |
| 8 | + accounts: Record< |
| 9 | + string, |
| 10 | + { |
12 | 11 | id: string |
13 | | - name: string |
14 | | - slug: string |
15 | | - }[] |
16 | | - }> |
| 12 | + email: string |
| 13 | + workspaces: { |
| 14 | + id: string |
| 15 | + name: string |
| 16 | + slug: string |
| 17 | + }[] |
| 18 | + } |
| 19 | + > |
17 | 20 | } |
18 | 21 |
|
19 | | -export const { use: useAccount, provider: AccountProvider } = createInitializedContext("AccountContext", () => { |
20 | | - const auth = useOpenAuth() |
21 | | - const [store, setStore] = makePersisted( |
22 | | - createStore<Storage>({ |
23 | | - accounts: {}, |
24 | | - }), |
25 | | - { |
26 | | - name: "radiant.account", |
27 | | - }, |
28 | | - ); |
29 | | - |
30 | | - async function refresh(id: string) { |
31 | | - return fetch(import.meta.env.VITE_API_URL + "/rest/account", { |
32 | | - headers: { |
33 | | - authorization: `Bearer ${await auth.access(id)}`, |
| 22 | +export const { use: useAccount, provider: AccountProvider } = |
| 23 | + createInitializedContext("AccountContext", () => { |
| 24 | + const auth = useOpenAuth() |
| 25 | + const [store, setStore] = makePersisted( |
| 26 | + createStore<Storage>({ |
| 27 | + accounts: {}, |
| 28 | + }), |
| 29 | + { |
| 30 | + name: "radiant.account", |
34 | 31 | }, |
35 | | - }) |
36 | | - .then(val => val.json()) |
37 | | - .then(val => setStore("accounts", id, val as any)) |
38 | | - } |
| 32 | + ) |
39 | 33 |
|
40 | | - createEffect((previous: string[]) => { |
41 | | - if (Object.keys(auth.all).length === 0) { |
42 | | - return [] |
43 | | - } |
44 | | - for (const item of Object.values(auth.all)) { |
45 | | - if (previous.includes(item.id)) continue |
46 | | - refresh(item.id) |
| 34 | + async function refresh(id: string) { |
| 35 | + return fetch(import.meta.env.VITE_API_URL + "/rest/account", { |
| 36 | + headers: { |
| 37 | + authorization: `Bearer ${await auth.access(id)}`, |
| 38 | + }, |
| 39 | + }) |
| 40 | + .then((val) => val.json()) |
| 41 | + .then((val) => setStore("accounts", id, val as any)) |
47 | 42 | } |
48 | | - return Object.keys(auth.all) |
49 | | - }, [] as string[]) |
50 | 43 |
|
| 44 | + createEffect((previous: string[]) => { |
| 45 | + if (Object.keys(auth.all).length === 0) { |
| 46 | + return [] |
| 47 | + } |
| 48 | + for (const item of Object.values(auth.all)) { |
| 49 | + if (previous.includes(item.id)) continue |
| 50 | + refresh(item.id) |
| 51 | + } |
| 52 | + return Object.keys(auth.all) |
| 53 | + }, [] as string[]) |
51 | 54 |
|
52 | | - |
53 | | - return { |
54 | | - get all() { |
55 | | - return store.accounts |
56 | | - }, |
57 | | - get current() { |
58 | | - if (!auth.subject) return undefined |
59 | | - return store.accounts[auth.subject.id] |
60 | | - }, |
61 | | - refresh, |
62 | | - get ready() { |
63 | | - return Object.keys(auth.all).length === Object.keys(store.accounts).length |
| 55 | + return { |
| 56 | + get all() { |
| 57 | + return store.accounts |
| 58 | + }, |
| 59 | + get current() { |
| 60 | + if (!auth.subject) return undefined |
| 61 | + return store.accounts[auth.subject.id] |
| 62 | + }, |
| 63 | + refresh, |
| 64 | + get ready() { |
| 65 | + return ( |
| 66 | + Object.keys(auth.all).length === Object.keys(store.accounts).length |
| 67 | + ) |
| 68 | + }, |
64 | 69 | } |
65 | | - } |
66 | | -}) |
67 | | - |
| 70 | + }) |
0 commit comments