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

fix(app): hide unavailable titlebar update (#30642)

Luke Parker 3 месяцев назад
Родитель
Сommit
f62ba5eb86

+ 3 - 1
packages/app/src/components/titlebar.tsx

@@ -707,7 +707,9 @@ type TitlebarV2RightState = {
 function TitlebarV2Right(props: { state: TitlebarV2RightState }) {
   return (
     <div class="relative z-20 flex shrink-0 items-center justify-end gap-0 overflow-visible">
-      <TitlebarUpdateIconButton state={props.state.update} />
+      <Show when={props.state.update.visible}>
+        <TitlebarUpdateIconButton state={props.state.update} />
+      </Show>
       <div id="opencode-titlebar-right" class="flex shrink-0 items-center justify-end gap-0" />
     </div>
   )

+ 2 - 5
packages/app/src/pages/layout.tsx

@@ -89,6 +89,7 @@ import {
 } from "./layout/sidebar-workspace"
 import { ProjectDragOverlay, SortableProject, type ProjectSidebarContext } from "./layout/sidebar-project"
 import { SidebarContent } from "./layout/sidebar-shell"
+import { runUpdateAndRestart } from "./layout/update"
 
 export default function Layout(props: ParentProps) {
   const [store, setStore, , ready] = persisted(
@@ -183,11 +184,7 @@ export default function Layout(props: ParentProps) {
     return updateQuery.data.version ?? ""
   }
   const installUpdate = () => {
-    if (!platform.updateAndRestart) return
-    setUpdate("installing", true)
-    void platform.updateAndRestart().catch(() => {
-      setUpdate("installing", false)
-    })
+    runUpdateAndRestart(platform.updateAndRestart, (installing) => setUpdate("installing", installing))
   }
   const titlebarUpdate: TitlebarUpdate = {
     version: updateVersion,

+ 19 - 0
packages/app/src/pages/layout/update.test.ts

@@ -0,0 +1,19 @@
+import { describe, expect, test } from "bun:test"
+import { runUpdateAndRestart } from "./update"
+
+describe("runUpdateAndRestart", () => {
+  test("clears the installing state when restart resolves without exiting", async () => {
+    const states: boolean[] = []
+    await new Promise<void>((resolve) => {
+      runUpdateAndRestart(
+        async () => {},
+        (installing) => {
+          states.push(installing)
+          if (states.length === 2) resolve()
+        },
+      )
+    })
+
+    expect(states).toEqual([true, false])
+  })
+})

+ 10 - 0
packages/app/src/pages/layout/update.ts

@@ -0,0 +1,10 @@
+export function runUpdateAndRestart(
+  updateAndRestart: (() => Promise<void>) | undefined,
+  setInstalling: (installing: boolean) => void,
+) {
+  if (!updateAndRestart) return
+  setInstalling(true)
+  void updateAndRestart()
+    .catch(() => undefined)
+    .finally(() => setInstalling(false))
+}