session-header.tsx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. import { AppIcon } from "@opencode-ai/ui/app-icon"
  2. import { Button } from "@opencode-ai/ui/button"
  3. import { DropdownMenu } from "@opencode-ai/ui/dropdown-menu"
  4. import { Icon } from "@opencode-ai/ui/icon"
  5. import { IconButton } from "@opencode-ai/ui/icon-button"
  6. import { Keybind } from "@opencode-ai/ui/keybind"
  7. import { Spinner } from "@opencode-ai/ui/spinner"
  8. import { showToast } from "@opencode-ai/ui/toast"
  9. import { Tooltip, TooltipKeybind } from "@opencode-ai/ui/tooltip"
  10. import { getFilename } from "@opencode-ai/core/util/path"
  11. import { createEffect, createMemo, createSignal, For, onMount, Show } from "solid-js"
  12. import { createStore } from "solid-js/store"
  13. import { Portal } from "solid-js/web"
  14. import { useCommand } from "@/context/command"
  15. import { useLanguage } from "@/context/language"
  16. import { useLayout } from "@/context/layout"
  17. import { usePlatform } from "@/context/platform"
  18. import { useServer } from "@/context/server"
  19. import { useSettings } from "@/context/settings"
  20. import { useSync } from "@/context/sync"
  21. import { useTerminal } from "@/context/terminal"
  22. import { focusTerminalById } from "@/pages/session/helpers"
  23. import { useSessionLayout } from "@/pages/session/session-layout"
  24. import { messageAgentColor } from "@/utils/agent"
  25. import { decode64 } from "@/utils/base64"
  26. import { Persist, persisted } from "@/utils/persist"
  27. import { StatusPopover, StatusPopoverV2 } from "../status-popover"
  28. import { IconButtonV2 } from "@opencode-ai/ui/v2/components/icon-button-v2.jsx"
  29. import { Icon as IconV2 } from "@opencode-ai/ui/v2/components/icon.jsx"
  30. const OPEN_APPS = [
  31. "vscode",
  32. "cursor",
  33. "zed",
  34. "textmate",
  35. "antigravity",
  36. "finder",
  37. "terminal",
  38. "iterm2",
  39. "ghostty",
  40. "warp",
  41. "xcode",
  42. "android-studio",
  43. "powershell",
  44. "sublime-text",
  45. ] as const
  46. const USE_V2_TITLEBAR = import.meta.env.VITE_OPENCODE_CHANNEL !== "prod"
  47. type OpenApp = (typeof OPEN_APPS)[number]
  48. type OS = "macos" | "windows" | "linux" | "unknown"
  49. const MAC_APPS = [
  50. {
  51. id: "vscode",
  52. label: "session.header.open.app.vscode",
  53. icon: "vscode",
  54. openWith: "Visual Studio Code",
  55. },
  56. { id: "cursor", label: "session.header.open.app.cursor", icon: "cursor", openWith: "Cursor" },
  57. { id: "zed", label: "session.header.open.app.zed", icon: "zed", openWith: "Zed" },
  58. { id: "textmate", label: "session.header.open.app.textmate", icon: "textmate", openWith: "TextMate" },
  59. {
  60. id: "antigravity",
  61. label: "session.header.open.app.antigravity",
  62. icon: "antigravity",
  63. openWith: "Antigravity",
  64. },
  65. { id: "terminal", label: "session.header.open.app.terminal", icon: "terminal", openWith: "Terminal" },
  66. { id: "iterm2", label: "session.header.open.app.iterm2", icon: "iterm2", openWith: "iTerm" },
  67. { id: "ghostty", label: "session.header.open.app.ghostty", icon: "ghostty", openWith: "Ghostty" },
  68. { id: "warp", label: "session.header.open.app.warp", icon: "warp", openWith: "Warp" },
  69. { id: "xcode", label: "session.header.open.app.xcode", icon: "xcode", openWith: "Xcode" },
  70. {
  71. id: "android-studio",
  72. label: "session.header.open.app.androidStudio",
  73. icon: "android-studio",
  74. openWith: "Android Studio",
  75. },
  76. {
  77. id: "sublime-text",
  78. label: "session.header.open.app.sublimeText",
  79. icon: "sublime-text",
  80. openWith: "Sublime Text",
  81. },
  82. ] as const
  83. const WINDOWS_APPS = [
  84. { id: "vscode", label: "session.header.open.app.vscode", icon: "vscode", openWith: "code" },
  85. { id: "cursor", label: "session.header.open.app.cursor", icon: "cursor", openWith: "cursor" },
  86. { id: "zed", label: "session.header.open.app.zed", icon: "zed", openWith: "zed" },
  87. {
  88. id: "powershell",
  89. label: "session.header.open.app.powershell",
  90. icon: "powershell",
  91. openWith: "powershell",
  92. },
  93. {
  94. id: "sublime-text",
  95. label: "session.header.open.app.sublimeText",
  96. icon: "sublime-text",
  97. openWith: "Sublime Text",
  98. },
  99. ] as const
  100. const LINUX_APPS = [
  101. { id: "vscode", label: "session.header.open.app.vscode", icon: "vscode", openWith: "code" },
  102. { id: "cursor", label: "session.header.open.app.cursor", icon: "cursor", openWith: "cursor" },
  103. { id: "zed", label: "session.header.open.app.zed", icon: "zed", openWith: "zed" },
  104. {
  105. id: "sublime-text",
  106. label: "session.header.open.app.sublimeText",
  107. icon: "sublime-text",
  108. openWith: "Sublime Text",
  109. },
  110. ] as const
  111. const detectOS = (platform: ReturnType<typeof usePlatform>): OS => {
  112. if (platform.platform === "desktop" && platform.os) return platform.os
  113. if (typeof navigator !== "object") return "unknown"
  114. const value = navigator.platform || navigator.userAgent
  115. if (/Mac/i.test(value)) return "macos"
  116. if (/Win/i.test(value)) return "windows"
  117. if (/Linux/i.test(value)) return "linux"
  118. return "unknown"
  119. }
  120. const showRequestError = (language: ReturnType<typeof useLanguage>, err: unknown) => {
  121. showToast({
  122. variant: "error",
  123. title: language.t("common.requestFailed"),
  124. description: err instanceof Error ? err.message : String(err),
  125. })
  126. }
  127. export function SessionHeader() {
  128. const layout = useLayout()
  129. const command = useCommand()
  130. const server = useServer()
  131. const platform = usePlatform()
  132. const language = useLanguage()
  133. const settings = useSettings()
  134. const sync = useSync()
  135. const terminal = useTerminal()
  136. const { params, view } = useSessionLayout()
  137. const projectDirectory = createMemo(() => decode64(params.dir) ?? "")
  138. const project = createMemo(() => {
  139. const directory = projectDirectory()
  140. if (!directory) return
  141. return layout.projects.list().find((p) => p.worktree === directory || p.sandboxes?.includes(directory))
  142. })
  143. const name = createMemo(() => {
  144. const current = project()
  145. if (current) return current.name || getFilename(current.worktree)
  146. return getFilename(projectDirectory())
  147. })
  148. const hotkey = createMemo(() => command.keybind("file.open"))
  149. const os = createMemo(() => detectOS(platform))
  150. const isDesktopV2 = platform.platform === "desktop" && USE_V2_TITLEBAR
  151. const search = createMemo(() => (isDesktopV2 ? settings.general.showSearch() : true))
  152. const tree = createMemo(() => (isDesktopV2 ? settings.general.showFileTree() : true))
  153. const term = createMemo(() => (isDesktopV2 ? settings.general.showTerminal() : true))
  154. const status = createMemo(() => (isDesktopV2 ? settings.general.showStatus() : true))
  155. const [exists, setExists] = createStore<Partial<Record<OpenApp, boolean>>>({
  156. finder: true,
  157. })
  158. const apps = createMemo(() => {
  159. if (os() === "macos") return MAC_APPS
  160. if (os() === "windows") return WINDOWS_APPS
  161. return LINUX_APPS
  162. })
  163. const fileManager = createMemo(() => {
  164. if (os() === "macos") return { label: "session.header.open.finder", icon: "finder" as const }
  165. if (os() === "windows") return { label: "session.header.open.fileExplorer", icon: "file-explorer" as const }
  166. return { label: "session.header.open.fileManager", icon: "finder" as const }
  167. })
  168. createEffect(() => {
  169. if (platform.platform !== "desktop") return
  170. if (!platform.checkAppExists) return
  171. const list = apps()
  172. setExists(Object.fromEntries(list.map((app) => [app.id, undefined])) as Partial<Record<OpenApp, boolean>>)
  173. void Promise.all(
  174. list.map((app) =>
  175. Promise.resolve(platform.checkAppExists?.(app.openWith))
  176. .then((value) => Boolean(value))
  177. .catch(() => false)
  178. .then((ok) => [app.id, ok] as const),
  179. ),
  180. ).then((entries) => {
  181. setExists(Object.fromEntries(entries) as Partial<Record<OpenApp, boolean>>)
  182. })
  183. })
  184. const options = createMemo(() => {
  185. return [
  186. { id: "finder", label: language.t(fileManager().label), icon: fileManager().icon },
  187. ...apps()
  188. .filter((app) => exists[app.id])
  189. .map((app) => ({ ...app, label: language.t(app.label) })),
  190. ] as const
  191. })
  192. const toggleTerminal = () => {
  193. const next = !view().terminal.opened()
  194. view().terminal.toggle()
  195. if (!next) return
  196. const id = terminal.active()
  197. if (!id) return
  198. focusTerminalById(id)
  199. }
  200. const [prefs, setPrefs] = persisted(Persist.global("open.app"), createStore({ app: "finder" as OpenApp }))
  201. const [menu, setMenu] = createStore({ open: false })
  202. const [openRequest, setOpenRequest] = createStore({
  203. app: undefined as OpenApp | undefined,
  204. })
  205. const canOpen = createMemo(() => platform.platform === "desktop" && !!platform.openPath && server.isLocal())
  206. const current = createMemo(
  207. () =>
  208. options().find((o) => o.id === prefs.app) ??
  209. options()[0] ??
  210. ({ id: "finder", label: fileManager().label, icon: fileManager().icon } as const),
  211. )
  212. const opening = createMemo(() => openRequest.app !== undefined)
  213. const tint = createMemo(() =>
  214. messageAgentColor(params.id ? sync.data.message[params.id] : undefined, sync.data.agent),
  215. )
  216. const v2ActionsState = createMemo<SessionHeaderV2ActionsState>(() => ({
  217. statusVisible: status(),
  218. statusLabel: language.t("status.popover.trigger"),
  219. reviewLabel: language.t("command.review.toggle"),
  220. reviewKeybind: command.keybind("review.toggle"),
  221. reviewOpened: view().reviewPanel.opened(),
  222. onReviewToggle: () => view().reviewPanel.toggle(),
  223. }))
  224. const selectApp = (app: OpenApp) => {
  225. if (!options().some((item) => item.id === app)) return
  226. setPrefs("app", app)
  227. }
  228. const openDir = (app: OpenApp) => {
  229. if (opening() || !canOpen() || !platform.openPath) return
  230. const directory = projectDirectory()
  231. if (!directory) return
  232. const item = options().find((o) => o.id === app)
  233. const openWith = item && "openWith" in item ? item.openWith : undefined
  234. setOpenRequest("app", app)
  235. platform
  236. .openPath(directory, openWith)
  237. .catch((err: unknown) => showRequestError(language, err))
  238. .finally(() => {
  239. setOpenRequest("app", undefined)
  240. })
  241. }
  242. const copyPath = () => {
  243. const directory = projectDirectory()
  244. if (!directory) return
  245. navigator.clipboard
  246. .writeText(directory)
  247. .then(() => {
  248. showToast({
  249. variant: "success",
  250. icon: "circle-check",
  251. title: language.t("session.share.copy.copied"),
  252. description: directory,
  253. })
  254. })
  255. .catch((err: unknown) => showRequestError(language, err))
  256. }
  257. const [centerMount, setCenterMount] = createSignal<HTMLElement | null>(null)
  258. const [rightMount, setRightMount] = createSignal<HTMLElement | null>(null)
  259. onMount(() => {
  260. setCenterMount(document.getElementById("opencode-titlebar-center"))
  261. setRightMount(document.getElementById("opencode-titlebar-right"))
  262. })
  263. return (
  264. <>
  265. <Show when={search() && centerMount()}>
  266. {(mount) => (
  267. <Portal mount={mount()}>
  268. <Button
  269. type="button"
  270. variant="ghost"
  271. size="small"
  272. class="hidden md:flex w-[240px] max-w-full min-w-0 items-center gap-2 justify-between rounded-md border border-border-weak-base bg-surface-panel shadow-none cursor-default"
  273. onClick={() => command.trigger("file.open")}
  274. aria-label={language.t("session.header.searchFiles")}
  275. >
  276. <div class="flex min-w-0 flex-1 items-center overflow-visible">
  277. <span class="flex-1 min-w-0 text-12-regular text-text-weak truncate text-left">
  278. {language.t("session.header.search.placeholder", {
  279. project: name(),
  280. })}
  281. </span>
  282. </div>
  283. <Show when={hotkey()}>
  284. {(keybind) => (
  285. <Keybind class="shrink-0 !border-0 !bg-transparent !shadow-none px-0 text-text-weaker">
  286. {keybind()}
  287. </Keybind>
  288. )}
  289. </Show>
  290. </Button>
  291. </Portal>
  292. )}
  293. </Show>
  294. <Show when={rightMount()}>
  295. {(mount) => (
  296. <Portal mount={mount()}>
  297. <Show
  298. when={isDesktopV2}
  299. fallback={
  300. <div class="flex items-center gap-2">
  301. <Show when={projectDirectory()}>
  302. <div class="hidden xl:flex items-center">
  303. <Show
  304. when={canOpen()}
  305. fallback={
  306. <div class="flex h-[24px] box-border items-center rounded-md border border-border-weak-base bg-surface-panel overflow-hidden">
  307. <Button
  308. variant="ghost"
  309. class="rounded-none h-full py-0 pr-3 pl-0.5 gap-1.5 border-none shadow-none"
  310. onClick={copyPath}
  311. aria-label={language.t("session.header.open.copyPath")}
  312. >
  313. <Icon name="copy" size="small" class="text-icon-base" />
  314. <span class="text-12-regular text-text-strong">
  315. {language.t("session.header.open.copyPath")}
  316. </span>
  317. </Button>
  318. </div>
  319. }
  320. >
  321. <div class="flex items-center">
  322. <div class="flex h-[24px] box-border items-center rounded-md border border-border-weak-base bg-surface-panel overflow-hidden">
  323. <Button
  324. variant="ghost"
  325. class="rounded-none h-full px-0.5 border-none shadow-none disabled:!cursor-default"
  326. classList={{
  327. "bg-surface-raised-base-active": opening(),
  328. }}
  329. onClick={() => openDir(current().id)}
  330. disabled={opening()}
  331. aria-label={language.t("session.header.open.ariaLabel", { app: current().label })}
  332. >
  333. <div class="flex size-5 shrink-0 items-center justify-center [&_[data-component=app-icon]]:size-5">
  334. <Show when={opening()} fallback={<AppIcon id={current().icon} />}>
  335. <Spinner class="size-3.5" style={{ color: tint() ?? "var(--icon-base)" }} />
  336. </Show>
  337. </div>
  338. </Button>
  339. <DropdownMenu
  340. gutter={4}
  341. placement="bottom-end"
  342. open={menu.open}
  343. onOpenChange={(open) => setMenu("open", open)}
  344. >
  345. <DropdownMenu.Trigger
  346. as={IconButton}
  347. icon="chevron-down"
  348. variant="ghost"
  349. disabled={opening()}
  350. class="rounded-none h-full w-[20px] p-0 border-none shadow-none data-[expanded]:bg-surface-raised-base-active disabled:!cursor-default"
  351. classList={{
  352. "bg-surface-raised-base-active": opening(),
  353. }}
  354. aria-label={language.t("session.header.open.menu")}
  355. />
  356. <DropdownMenu.Portal>
  357. <DropdownMenu.Content class="[&_[data-slot=dropdown-menu-item]]:pl-1 [&_[data-slot=dropdown-menu-radio-item]]:pl-1 [&_[data-slot=dropdown-menu-radio-item]+[data-slot=dropdown-menu-radio-item]]:mt-1">
  358. <DropdownMenu.Group>
  359. <DropdownMenu.GroupLabel class="!px-1 !py-1">
  360. {language.t("session.header.openIn")}
  361. </DropdownMenu.GroupLabel>
  362. <DropdownMenu.RadioGroup
  363. class="mt-1"
  364. value={current().id}
  365. onChange={(value) => {
  366. if (!OPEN_APPS.includes(value as OpenApp)) return
  367. selectApp(value as OpenApp)
  368. }}
  369. >
  370. <For each={options()}>
  371. {(o) => (
  372. <DropdownMenu.RadioItem
  373. value={o.id}
  374. disabled={opening()}
  375. onSelect={() => {
  376. setMenu("open", false)
  377. openDir(o.id)
  378. }}
  379. >
  380. <div class="flex size-5 shrink-0 items-center justify-center [&_[data-component=app-icon]]:size-5">
  381. <AppIcon id={o.icon} />
  382. </div>
  383. <DropdownMenu.ItemLabel>{o.label}</DropdownMenu.ItemLabel>
  384. <DropdownMenu.ItemIndicator>
  385. <Icon name="check-small" size="small" class="text-icon-weak" />
  386. </DropdownMenu.ItemIndicator>
  387. </DropdownMenu.RadioItem>
  388. )}
  389. </For>
  390. </DropdownMenu.RadioGroup>
  391. </DropdownMenu.Group>
  392. <DropdownMenu.Separator />
  393. <DropdownMenu.Item
  394. onSelect={() => {
  395. setMenu("open", false)
  396. copyPath()
  397. }}
  398. >
  399. <div class="flex size-5 shrink-0 items-center justify-center">
  400. <Icon name="copy" size="small" class="text-icon-weak" />
  401. </div>
  402. <DropdownMenu.ItemLabel>
  403. {language.t("session.header.open.copyPath")}
  404. </DropdownMenu.ItemLabel>
  405. </DropdownMenu.Item>
  406. </DropdownMenu.Content>
  407. </DropdownMenu.Portal>
  408. </DropdownMenu>
  409. </div>
  410. </div>
  411. </Show>
  412. </div>
  413. </Show>
  414. <div class="flex items-center gap-1">
  415. <Show when={status()}>
  416. <Tooltip placement="bottom" value={language.t("status.popover.trigger")}>
  417. <StatusPopover />
  418. </Tooltip>
  419. </Show>
  420. <Show when={term()}>
  421. <TooltipKeybind
  422. title={language.t("command.terminal.toggle")}
  423. keybind={command.keybind("terminal.toggle")}
  424. >
  425. <Button
  426. variant="ghost"
  427. class="group/terminal-toggle titlebar-icon w-8 h-6 p-0 box-border shrink-0"
  428. onClick={toggleTerminal}
  429. aria-label={language.t("command.terminal.toggle")}
  430. aria-expanded={view().terminal.opened()}
  431. aria-controls="terminal-panel"
  432. >
  433. <Icon size="small" name={view().terminal.opened() ? "terminal-active" : "terminal"} />
  434. </Button>
  435. </TooltipKeybind>
  436. </Show>
  437. <div class="hidden md:flex items-center gap-1 shrink-0">
  438. <TooltipKeybind
  439. title={language.t("command.review.toggle")}
  440. keybind={command.keybind("review.toggle")}
  441. >
  442. <Button
  443. variant="ghost"
  444. class="group/review-toggle titlebar-icon w-8 h-6 p-0 box-border"
  445. onClick={() => view().reviewPanel.toggle()}
  446. aria-label={language.t("command.review.toggle")}
  447. aria-expanded={view().reviewPanel.opened()}
  448. aria-controls="review-panel"
  449. >
  450. <Icon size="small" name={view().reviewPanel.opened() ? "review-active" : "review"} />
  451. </Button>
  452. </TooltipKeybind>
  453. <Show when={tree()}>
  454. <TooltipKeybind
  455. title={language.t("command.fileTree.toggle")}
  456. keybind={command.keybind("fileTree.toggle")}
  457. >
  458. <Button
  459. variant="ghost"
  460. class="titlebar-icon w-8 h-6 p-0 box-border"
  461. onClick={() => layout.fileTree.toggle()}
  462. aria-label={language.t("command.fileTree.toggle")}
  463. aria-expanded={layout.fileTree.opened()}
  464. aria-controls="file-tree-panel"
  465. >
  466. <div class="relative flex items-center justify-center size-4">
  467. <Icon
  468. size="small"
  469. name={layout.fileTree.opened() ? "file-tree-active" : "file-tree"}
  470. classList={{
  471. "text-icon-strong": layout.fileTree.opened(),
  472. "text-icon-weak": !layout.fileTree.opened(),
  473. }}
  474. />
  475. </div>
  476. </Button>
  477. </TooltipKeybind>
  478. </Show>
  479. </div>
  480. </div>
  481. </div>
  482. }
  483. >
  484. <SessionHeaderV2Actions state={v2ActionsState()} />
  485. </Show>
  486. </Portal>
  487. )}
  488. </Show>
  489. </>
  490. )
  491. }
  492. type SessionHeaderV2ActionsState = {
  493. statusVisible: boolean
  494. statusLabel: string
  495. reviewLabel: string
  496. reviewKeybind: string
  497. reviewOpened: boolean
  498. onReviewToggle: () => void
  499. }
  500. function SessionHeaderV2Actions(props: { state: SessionHeaderV2ActionsState }) {
  501. return (
  502. <div class="flex items-center gap-0">
  503. <Show when={props.state.statusVisible}>
  504. <Tooltip placement="bottom" value={props.state.statusLabel}>
  505. <StatusPopoverV2 />
  506. </Tooltip>
  507. </Show>
  508. <TooltipKeybind title={props.state.reviewLabel} keybind={props.state.reviewKeybind}>
  509. <IconButtonV2
  510. type="button"
  511. variant="ghost-muted"
  512. size="large"
  513. class="!w-9 shrink-0"
  514. state={props.state.reviewOpened ? "pressed" : undefined}
  515. onClick={props.state.onReviewToggle}
  516. aria-label={props.state.reviewLabel}
  517. aria-expanded={props.state.reviewOpened}
  518. aria-controls="review-panel"
  519. icon={<IconV2 name="sidebar-right" />}
  520. />
  521. </TooltipKeybind>
  522. </div>
  523. )
  524. }