session-header.tsx 14 KB

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