Frank hai 5 meses
pai
achega
a590b32a10

+ 2 - 4
packages/console/app/src/component/header.tsx

@@ -4,11 +4,9 @@ import { A, createAsync } from "@solidjs/router"
 import { createMemo, Match, Show, Switch } from "solid-js"
 import { createStore } from "solid-js/store"
 import { github } from "~/lib/github"
-import { queryIsLoggedIn } from "~/routes/workspace/common"
 
 export function Header(props: { zen?: boolean }) {
   const githubData = createAsync(() => github())
-  const isLoggedIn = createAsync(() => queryIsLoggedIn())
   const starCount = createMemo(() =>
     githubData()?.stars
       ? new Intl.NumberFormat("en-US", {
@@ -41,7 +39,7 @@ export function Header(props: { zen?: boolean }) {
           <li>
             <Switch>
               <Match when={props.zen}>
-                <a href="/auth">{isLoggedIn() ? "Workspace" : "Login"}</a>
+                <a href="/auth">Login</a>
               </Match>
               <Match when={!props.zen}>
                 <A href="/zen">Zen</A>
@@ -112,7 +110,7 @@ export function Header(props: { zen?: boolean }) {
                 <li>
                   <Switch>
                     <Match when={props.zen}>
-                      <a href="/auth">{isLoggedIn() ? "Workspace" : "Login"}</a>
+                      <a href="/auth">Login</a>
                     </Match>
                     <Match when={!props.zen}>
                       <A href="/zen">Zen</A>

+ 2 - 24
packages/console/app/src/routes/auth/index.ts

@@ -1,32 +1,10 @@
-import { Actor } from "@opencode-ai/console-core/actor.js"
-import { and, Database, desc, eq, isNull } from "@opencode-ai/console-core/drizzle/index.js"
-import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js"
-import { WorkspaceTable } from "@opencode-ai/console-core/schema/workspace.sql.js"
 import { redirect } from "@solidjs/router"
 import type { APIEvent } from "@solidjs/start/server"
-import { withActor } from "~/context/auth.withActor"
+import { getLastSeenWorkspaceID } from "../workspace/common"
 
 export async function GET(input: APIEvent) {
   try {
-    const workspaceID = await withActor(async () => {
-      const actor = Actor.assert("account")
-      return Database.transaction(async (tx) =>
-        tx
-          .select({ id: WorkspaceTable.id })
-          .from(UserTable)
-          .innerJoin(WorkspaceTable, eq(UserTable.workspaceID, WorkspaceTable.id))
-          .where(
-            and(
-              eq(UserTable.accountID, actor.properties.accountID),
-              isNull(UserTable.timeDeleted),
-              isNull(WorkspaceTable.timeDeleted),
-            ),
-          )
-          .orderBy(desc(UserTable.timeSeen))
-          .limit(1)
-          .then((x) => x[0]?.id),
-      )
-    })
+    const workspaceID = await getLastSeenWorkspaceID()
     return redirect(`/workspace/${workspaceID}`)
   } catch {
     return redirect("/auth/authorize")

+ 24 - 9
packages/console/app/src/routes/workspace/common.tsx

@@ -3,6 +3,10 @@ import { Actor } from "@opencode-ai/console-core/actor.js"
 import { action, query } from "@solidjs/router"
 import { withActor } from "~/context/auth.withActor"
 import { Billing } from "@opencode-ai/console-core/billing.js"
+import { User } from "@opencode-ai/console-core/user.js"
+import { and, Database, desc, eq, isNull } from "@opencode-ai/console-core/drizzle/index.js"
+import { WorkspaceTable } from "@opencode-ai/console-core/schema/workspace.sql.js"
+import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js"
 
 export function formatDateForTable(date: Date) {
   const options: Intl.DateTimeFormatOptions = {
@@ -30,17 +34,28 @@ export function formatDateUTC(date: Date) {
   return date.toLocaleDateString("en-US", options)
 }
 
-export const queryIsLoggedIn = query(async () => {
+export async function getLastSeenWorkspaceID() {
   "use server"
-  return withActor(() => {
-    try {
-      Actor.assert("account")
-      return true
-    } catch {
-      return false
-    }
+  return withActor(async () => {
+    const actor = Actor.assert("account")
+    return Database.use(async (tx) =>
+      tx
+        .select({ id: WorkspaceTable.id })
+        .from(UserTable)
+        .innerJoin(WorkspaceTable, eq(UserTable.workspaceID, WorkspaceTable.id))
+        .where(
+          and(
+            eq(UserTable.accountID, actor.properties.accountID),
+            isNull(UserTable.timeDeleted),
+            isNull(WorkspaceTable.timeDeleted),
+          ),
+        )
+        .orderBy(desc(UserTable.timeSeen))
+        .limit(1)
+        .then((x) => x[0]?.id),
+    )
   })
-}, "isLoggedIn.get")
+}
 
 export const querySessionInfo = query(async (workspaceID: string) => {
   "use server"

+ 10 - 4
packages/console/app/src/routes/zen/index.tsx

@@ -1,5 +1,5 @@
 import "./index.css"
-import { createAsync } from "@solidjs/router"
+import { createAsync, query, redirect } from "@solidjs/router"
 import { Title, Meta, Link } from "@solidjs/meta"
 import { HttpHeader } from "@solidjs/start"
 import zenLogoLight from "../../asset/zen-ornate-light.svg"
@@ -16,10 +16,16 @@ import { Faq } from "~/component/faq"
 import { Legal } from "~/component/legal"
 import { Footer } from "~/component/footer"
 import { Header } from "~/component/header"
-import { queryIsLoggedIn } from "~/routes/workspace/common"
+import { getLastSeenWorkspaceID } from "../workspace/common"
+
+const checkLoggedIn = query(async () => {
+  "use server"
+  const workspaceID = await getLastSeenWorkspaceID()
+  if (workspaceID) throw redirect(`/workspace/${workspaceID}`)
+}, "checkLoggedIn.get")
 
 export default function Home() {
-  const isLoggedIn = createAsync(() => queryIsLoggedIn())
+  createAsync(() => checkLoggedIn())
   return (
     <main data-page="zen">
       <HttpHeader name="Cache-Control" value="public, max-age=1, s-maxage=3600, stale-while-revalidate=86400" />
@@ -105,7 +111,7 @@ export default function Home() {
                 </div>
               </div>
               <a href="/auth">
-                <span>{isLoggedIn() ? "Go to workspace " : "Get started with Zen "}</span>
+                <span>Get started with Zen </span>
                 <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                   <path
                     d="M6.5 12L17 12M13 16.5L17.5 12L13 7.5"