summaryrefslogtreecommitdiffhomepage
path: root/packages/console/app/src/routes/workspace.tsx
blob: 3aa3f20d3c5675dad50f9cceab6f6bcd6e95182d (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
import "./workspace.css"
import { useAuthSession } from "~/context/auth.session"
import { IconLogo } from "../component/icon"
import { withActor } from "~/context/auth.withActor"
import {
  query,
  action,
  redirect,
  createAsync,
  RouteSectionProps,
  Navigate,
  useNavigate,
  useParams,
  A,
} from "@solidjs/router"
import { User } from "@opencode/console-core/user.js"
import { Actor } from "@opencode/console-core/actor.js"
import { getRequestEvent } from "solid-js/web"

const getUserInfo = query(async (workspaceID: string) => {
  "use server"
  return withActor(async () => {
    const actor = Actor.assert("user")
    return await User.fromID(actor.properties.userID)
  }, workspaceID)
}, "userInfo")

const logout = action(async () => {
  "use server"
  const auth = await useAuthSession()
  const event = getRequestEvent()
  const current = auth.data.current
  if (current)
    await auth.update((val) => {
      delete val.account?.[current]
      const first = Object.keys(val.account ?? {})[0]
      val.current = first
      event!.locals.actor = undefined
      return val
    })
  throw redirect("/")
})

export default function WorkspaceLayout(props: RouteSectionProps) {
  const params = useParams()
  const userInfo = createAsync(() => getUserInfo(params.id))
  return (
    <main data-page="workspace">
      <header data-component="workspace-header">
        <div data-slot="header-brand">
          <A href="/" data-component="site-title">
            <IconLogo />
          </A>
        </div>
        <div data-slot="header-actions">
          <span data-slot="user">{userInfo()?.email}</span>
          <form action={logout} method="post">
            <button type="submit" formaction={logout}>
              Logout
            </button>
          </form>
        </div>
      </header>
      <div>{props.children}</div>
    </main>
  )
}