2
0

session-header.tsx 24 KB

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