session-header.tsx 23 KB

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