Просмотр исходного кода

fix(app): register new home projects (#41018)

Brendan Allan 1 неделя назад
Родитель
Сommit
5347b5e009

+ 14 - 1
packages/app/src/pages/home/home-controller.ts

@@ -90,7 +90,20 @@ export function createHomeController() {
         const directory = directories[0]
         if (!directory) return
         const ctx = global.ensureServerCtx(conn)
-        directories.forEach((item) => ctx.projects.open(item))
+        directories.forEach((item) => {
+          if (ctx.projects.list().some((project) => project.worktree === item)) return
+          const location = { directory: item }
+          void ctx.sdk.api.file
+            .list({ path: ".", location })
+            .then(async (files) => {
+              if (files.data.length > 0) return ctx.sdk.api.project.current({ location })
+              const result = await ctx.sdk.client.project.initGit({ directory: item })
+              return result.data ?? ctx.sdk.api.project.current({ location })
+            })
+            .then((project) => ctx.sync.child(item, { bootstrap: false })[1]("project", project.id))
+            .catch(() => undefined)
+          ctx.projects.open(item)
+        })
         ctx.projects.touch(directory)
         setSelection({ server: ServerConnection.key(conn), directory })
       },

+ 15 - 0
packages/opencode/test/server/httpapi-instance-context.test.ts

@@ -161,6 +161,21 @@ describe("HttpApi instance context middleware", () => {
     }),
   )
 
+  it.live("persists the routed project while loading instance context", () =>
+    Effect.gen(function* () {
+      const dir = yield* tmpdirScoped({ git: true })
+      const project = yield* Project.Service
+      yield* serveProbe()
+
+      const response = yield* HttpClient.get(`/probe?directory=${encodeURIComponent(dir)}`)
+
+      expect(response.status).toBe(200)
+      const saved = (yield* project.list()).find((item) => item.worktree === dir)
+      expect(saved).toBeDefined()
+      expect(saved?.id).not.toBe("global")
+    }),
+  )
+
   it.live("falls back to the raw directory when URI decoding fails", () =>
     Effect.gen(function* () {
       yield* serveProbe()