session-header.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 { useSync } from "@/context/sync"
  10. import { useGlobalSDK } from "@/context/global-sdk"
  11. import { getFilename } from "@opencode-ai/util/path"
  12. import { decode64 } from "@/utils/base64"
  13. import { Icon } from "@opencode-ai/ui/icon"
  14. import { IconButton } from "@opencode-ai/ui/icon-button"
  15. import { Button } from "@opencode-ai/ui/button"
  16. import { Tooltip, TooltipKeybind } from "@opencode-ai/ui/tooltip"
  17. import { Popover } from "@opencode-ai/ui/popover"
  18. import { TextField } from "@opencode-ai/ui/text-field"
  19. import { Keybind } from "@opencode-ai/ui/keybind"
  20. import { StatusPopover } from "../status-popover"
  21. export function SessionHeader() {
  22. const globalSDK = useGlobalSDK()
  23. const layout = useLayout()
  24. const params = useParams()
  25. const command = useCommand()
  26. const sync = useSync()
  27. const platform = usePlatform()
  28. const language = useLanguage()
  29. const projectDirectory = createMemo(() => decode64(params.dir) ?? "")
  30. const project = createMemo(() => {
  31. const directory = projectDirectory()
  32. if (!directory) return
  33. return layout.projects.list().find((p) => p.worktree === directory || p.sandboxes?.includes(directory))
  34. })
  35. const name = createMemo(() => {
  36. const current = project()
  37. if (current) return current.name || getFilename(current.worktree)
  38. return getFilename(projectDirectory())
  39. })
  40. const hotkey = createMemo(() => command.keybind("file.open"))
  41. const currentSession = createMemo(() => sync.data.session.find((s) => s.id === params.id))
  42. const shareEnabled = createMemo(() => sync.data.config.share !== "disabled")
  43. const showShare = createMemo(() => shareEnabled() && !!currentSession())
  44. const sessionKey = createMemo(() => `${params.dir}${params.id ? "/" + params.id : ""}`)
  45. const view = createMemo(() => layout.view(sessionKey))
  46. const [state, setState] = createStore({
  47. share: false,
  48. unshare: false,
  49. copied: false,
  50. timer: undefined as number | undefined,
  51. })
  52. const shareUrl = createMemo(() => currentSession()?.share?.url)
  53. createEffect(() => {
  54. const url = shareUrl()
  55. if (url) return
  56. if (state.timer) window.clearTimeout(state.timer)
  57. setState({ copied: false, timer: undefined })
  58. })
  59. onCleanup(() => {
  60. if (state.timer) window.clearTimeout(state.timer)
  61. })
  62. function shareSession() {
  63. const session = currentSession()
  64. if (!session || state.share) return
  65. setState("share", true)
  66. globalSDK.client.session
  67. .share({ sessionID: session.id, directory: projectDirectory() })
  68. .catch((error) => {
  69. console.error("Failed to share session", error)
  70. })
  71. .finally(() => {
  72. setState("share", false)
  73. })
  74. }
  75. function unshareSession() {
  76. const session = currentSession()
  77. if (!session || state.unshare) return
  78. setState("unshare", true)
  79. globalSDK.client.session
  80. .unshare({ sessionID: session.id, directory: projectDirectory() })
  81. .catch((error) => {
  82. console.error("Failed to unshare session", error)
  83. })
  84. .finally(() => {
  85. setState("unshare", false)
  86. })
  87. }
  88. function copyLink() {
  89. const url = shareUrl()
  90. if (!url) return
  91. navigator.clipboard
  92. .writeText(url)
  93. .then(() => {
  94. if (state.timer) window.clearTimeout(state.timer)
  95. setState("copied", true)
  96. const timer = window.setTimeout(() => {
  97. setState("copied", false)
  98. setState("timer", undefined)
  99. }, 3000)
  100. setState("timer", timer)
  101. })
  102. .catch((error) => {
  103. console.error("Failed to copy share link", error)
  104. })
  105. }
  106. function viewShare() {
  107. const url = shareUrl()
  108. if (!url) return
  109. platform.openLink(url)
  110. }
  111. const centerMount = createMemo(() => document.getElementById("opencode-titlebar-center"))
  112. const rightMount = createMemo(() => document.getElementById("opencode-titlebar-right"))
  113. return (
  114. <>
  115. <Show when={centerMount()}>
  116. {(mount) => (
  117. <Portal mount={mount()}>
  118. <button
  119. type="button"
  120. class="hidden md:flex w-[320px] p-1 pl-1.5 items-center gap-2 justify-between rounded-md border border-border-weak-base bg-surface-raised-base transition-colors cursor-default hover:bg-surface-raised-base-hover focus-visible:bg-surface-raised-base-hover active:bg-surface-raised-base-active"
  121. onClick={() => command.trigger("file.open")}
  122. aria-label={language.t("session.header.searchFiles")}
  123. >
  124. <div class="flex min-w-0 flex-1 items-center gap-2 overflow-visible">
  125. <Icon name="magnifying-glass" size="normal" class="icon-base shrink-0" />
  126. <span class="flex-1 min-w-0 text-14-regular text-text-weak truncate h-4.5 flex items-center">
  127. {language.t("session.header.search.placeholder", { project: name() })}
  128. </span>
  129. </div>
  130. <Show when={hotkey()}>{(keybind) => <Keybind class="shrink-0">{keybind()}</Keybind>}</Show>
  131. </button>
  132. </Portal>
  133. )}
  134. </Show>
  135. <Show when={rightMount()}>
  136. {(mount) => (
  137. <Portal mount={mount()}>
  138. <div class="flex items-center gap-3">
  139. <StatusPopover />
  140. <Show when={showShare()}>
  141. <div class="flex items-center">
  142. <Popover
  143. title={language.t("session.share.popover.title")}
  144. description={
  145. shareUrl()
  146. ? language.t("session.share.popover.description.shared")
  147. : language.t("session.share.popover.description.unshared")
  148. }
  149. gutter={6}
  150. placement="bottom-end"
  151. shift={-64}
  152. class="rounded-xl [&_[data-slot=popover-close-button]]:hidden"
  153. triggerAs={Button}
  154. triggerProps={{
  155. variant: "secondary",
  156. class: "rounded-sm w-[60px] h-[24px]",
  157. classList: { "rounded-r-none": shareUrl() !== undefined },
  158. style: { scale: 1 },
  159. }}
  160. trigger={language.t("session.share.action.share")}
  161. >
  162. <div class="flex flex-col gap-2">
  163. <Show
  164. when={shareUrl()}
  165. fallback={
  166. <div class="flex">
  167. <Button
  168. size="large"
  169. variant="primary"
  170. class="w-1/2"
  171. onClick={shareSession}
  172. disabled={state.share}
  173. >
  174. {state.share
  175. ? language.t("session.share.action.publishing")
  176. : language.t("session.share.action.publish")}
  177. </Button>
  178. </div>
  179. }
  180. >
  181. <div class="flex flex-col gap-2">
  182. <TextField value={shareUrl() ?? ""} readOnly copyable tabIndex={-1} class="w-full" />
  183. <div class="grid grid-cols-2 gap-2">
  184. <Button
  185. size="large"
  186. variant="secondary"
  187. class="w-full shadow-none border border-border-weak-base"
  188. onClick={unshareSession}
  189. disabled={state.unshare}
  190. >
  191. {state.unshare
  192. ? language.t("session.share.action.unpublishing")
  193. : language.t("session.share.action.unpublish")}
  194. </Button>
  195. <Button
  196. size="large"
  197. variant="primary"
  198. class="w-full"
  199. onClick={viewShare}
  200. disabled={state.unshare}
  201. >
  202. {language.t("session.share.action.view")}
  203. </Button>
  204. </div>
  205. </div>
  206. </Show>
  207. </div>
  208. </Popover>
  209. <Show when={shareUrl()} fallback={<div aria-hidden="true" />}>
  210. <Tooltip
  211. value={
  212. state.copied
  213. ? language.t("session.share.copy.copied")
  214. : language.t("session.share.copy.copyLink")
  215. }
  216. placement="top"
  217. gutter={8}
  218. >
  219. <IconButton
  220. icon={state.copied ? "check" : "link"}
  221. variant="secondary"
  222. class="rounded-l-none"
  223. onClick={copyLink}
  224. disabled={state.unshare}
  225. aria-label={
  226. state.copied
  227. ? language.t("session.share.copy.copied")
  228. : language.t("session.share.copy.copyLink")
  229. }
  230. />
  231. </Tooltip>
  232. </Show>
  233. </div>
  234. </Show>
  235. <div class="hidden md:flex items-center gap-3 ml-2 shrink-0">
  236. <TooltipKeybind
  237. title={language.t("command.terminal.toggle")}
  238. keybind={command.keybind("terminal.toggle")}
  239. >
  240. <Button
  241. variant="ghost"
  242. class="group/terminal-toggle size-6 p-0"
  243. onClick={() => view().terminal.toggle()}
  244. aria-label={language.t("command.terminal.toggle")}
  245. aria-expanded={view().terminal.opened()}
  246. aria-controls="terminal-panel"
  247. >
  248. <div class="relative flex items-center justify-center size-4 [&>*]:absolute [&>*]:inset-0">
  249. <Icon
  250. size="small"
  251. name={view().terminal.opened() ? "layout-bottom-full" : "layout-bottom"}
  252. class="group-hover/terminal-toggle:hidden"
  253. />
  254. <Icon
  255. size="small"
  256. name="layout-bottom-partial"
  257. class="hidden group-hover/terminal-toggle:inline-block"
  258. />
  259. <Icon
  260. size="small"
  261. name={view().terminal.opened() ? "layout-bottom" : "layout-bottom-full"}
  262. class="hidden group-active/terminal-toggle:inline-block"
  263. />
  264. </div>
  265. </Button>
  266. </TooltipKeybind>
  267. </div>
  268. <div class="hidden md:block shrink-0">
  269. <TooltipKeybind
  270. title={language.t("command.fileTree.toggle")}
  271. keybind={command.keybind("fileTree.toggle")}
  272. >
  273. <Button
  274. variant="ghost"
  275. class="group/file-tree-toggle size-6 p-0"
  276. onClick={() => layout.fileTree.toggle()}
  277. aria-label={language.t("command.fileTree.toggle")}
  278. aria-expanded={layout.fileTree.opened()}
  279. aria-controls="file-tree-panel"
  280. >
  281. <div class="relative flex items-center justify-center size-4 [&>*]:absolute [&>*]:inset-0">
  282. <Icon
  283. size="small"
  284. name={layout.fileTree.opened() ? "layout-right-full" : "layout-right"}
  285. class="group-hover/file-tree-toggle:hidden"
  286. />
  287. <Icon
  288. size="small"
  289. name="layout-right-partial"
  290. class="hidden group-hover/file-tree-toggle:inline-block"
  291. />
  292. <Icon
  293. size="small"
  294. name={layout.fileTree.opened() ? "layout-right" : "layout-right-full"}
  295. class="hidden group-active/file-tree-toggle:inline-block"
  296. />
  297. </div>
  298. </Button>
  299. </TooltipKeybind>
  300. </div>
  301. </div>
  302. </Portal>
  303. )}
  304. </Show>
  305. </>
  306. )
  307. }