summaryrefslogtreecommitdiffhomepage
path: root/cloud/web/src/components/context-account.tsx
blob: e6aabafd3ecc478a940e473e3165be92d1239bbe (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { createContext, createEffect, ParentProps, Suspense, useContext } from "solid-js"
import { makePersisted } from "@solid-primitives/storage"
import { createStore } from "solid-js/store"
import { useOpenAuth } from "./context-openauth"
import { createAsync } from "@solidjs/router"
import { isServer } from "solid-js/web"

type Storage = {
  accounts: Record<
    string,
    {
      id: string
      email: string
      workspaces: {
        id: string
        name: string
        slug: string
      }[]
    }
  >
}

const context = createContext<ReturnType<typeof init>>()

function init() {
  const auth = useOpenAuth()
  const [store, setStore] = makePersisted(
    createStore<Storage>({
      accounts: {},
    }),
    {
      name: "opencontrol.account",
    },
  )

  async function refresh(id: string) {
    return fetch(import.meta.env.VITE_API_URL + "/rest/account", {
      headers: {
        authorization: `Bearer ${await auth.access(id)}`,
      },
    })
      .then((val) => val.json())
      .then((val) => setStore("accounts", id, val as any))
  }

  createEffect((previous: string[]) => {
    if (Object.keys(auth.all).length === 0) {
      return []
    }
    for (const item of Object.values(auth.all)) {
      if (previous.includes(item.id)) continue
      refresh(item.id)
    }
    return Object.keys(auth.all)
  }, [] as string[])

  const result = {
    get all() {
      return Object.keys(auth.all)
        .map((id) => store.accounts[id])
        .filter(Boolean)
    },
    get current() {
      if (!auth.subject) return undefined
      return store.accounts[auth.subject.id]
    },
    refresh,
    get ready() {
      return Object.keys(auth.all).length === result.all.length
    },
  }

  return result
}

export function AccountProvider(props: ParentProps) {
  const ctx = init()
  const resource = createAsync(async () => {
    await new Promise<void>((resolve) => {
      if (isServer) return resolve()
      createEffect(() => {
        if (ctx.ready) resolve()
      })
    })
    return null
  })
  return (
    <Suspense>
      {resource()}
      <context.Provider value={ctx}>{props.children}</context.Provider>
    </Suspense>
  )
}

export function useAccount() {
  const result = useContext(context)
  if (!result) throw new Error("no account context")
  return result
}