layout.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import { createMemo, For, ParentProps, Show } from "solid-js"
  2. import { DateTime } from "luxon"
  3. import { A, useNavigate, useParams } from "@solidjs/router"
  4. import { useLayout } from "@/context/layout"
  5. import { useGlobalSync } from "@/context/global-sync"
  6. import { base64Decode, base64Encode } from "@opencode-ai/util/encode"
  7. import { Mark } from "@opencode-ai/ui/logo"
  8. import { ResizeHandle } from "@opencode-ai/ui/resize-handle"
  9. import { Button } from "@opencode-ai/ui/button"
  10. import { Icon } from "@opencode-ai/ui/icon"
  11. import { IconButton } from "@opencode-ai/ui/icon-button"
  12. import { Tooltip } from "@opencode-ai/ui/tooltip"
  13. import { Collapsible } from "@opencode-ai/ui/collapsible"
  14. import { DiffChanges } from "@opencode-ai/ui/diff-changes"
  15. import { getFilename } from "@opencode-ai/util/path"
  16. import { Select } from "@opencode-ai/ui/select"
  17. import { Session } from "@opencode-ai/sdk/v2/client"
  18. export default function Layout(props: ParentProps) {
  19. const navigate = useNavigate()
  20. const params = useParams()
  21. const globalSync = useGlobalSync()
  22. const layout = useLayout()
  23. const currentDirectory = createMemo(() => base64Decode(params.dir ?? ""))
  24. const sessions = createMemo(() => globalSync.child(currentDirectory())[0].session ?? [])
  25. const currentSession = createMemo(() => sessions().find((s) => s.id === params.id))
  26. function navigateToSession(session: Session | undefined) {
  27. if (!session) return
  28. navigate(`/${params.dir}/session/${session?.id}`)
  29. }
  30. const handleOpenProject = async () => {
  31. // layout.projects.open(dir.)
  32. }
  33. return (
  34. <div class="relative h-screen flex flex-col">
  35. <header class="h-12 shrink-0 bg-background-base border-b border-border-weak-base flex" data-tauri-drag-region>
  36. <A
  37. href="/"
  38. classList={{
  39. "w-12 shrink-0 px-4 py-3.5": true,
  40. "flex items-center justify-start self-stretch": true,
  41. "border-r border-border-weak-base": true,
  42. }}
  43. style={{ width: layout.sidebar.opened() ? `${layout.sidebar.width()}px` : undefined }}
  44. data-tauri-drag-region
  45. >
  46. <Mark class="shrink-0" />
  47. </A>
  48. <div class="pl-4 px-6 flex items-center justify-between gap-4 w-full">
  49. <div class="flex items-center gap-3">
  50. <div class="flex items-center gap-2">
  51. <Select
  52. options={layout.projects.list().map((project) => getFilename(project.directory))}
  53. current={getFilename(currentDirectory())}
  54. class="text-14-regular text-text-base"
  55. variant="ghost"
  56. />
  57. <div class="text-text-weaker">/</div>
  58. <Select
  59. options={sessions()}
  60. current={currentSession()}
  61. placeholder="Select session"
  62. label={(x) => x.title}
  63. value={(x) => x.id}
  64. onSelect={navigateToSession}
  65. class="text-14-regular text-text-base max-w-md"
  66. variant="ghost"
  67. />
  68. </div>
  69. <Button as={A} href={`/${params.dir}/session`} icon="plus-small">
  70. New session
  71. </Button>
  72. </div>
  73. <div class="flex items-center gap-4">
  74. <Tooltip
  75. class="shrink-0"
  76. value={
  77. <div class="flex items-center gap-2">
  78. <span>Toggle terminal</span>
  79. <span class="text-icon-base text-12-medium">Ctrl `</span>
  80. </div>
  81. }
  82. >
  83. <Button variant="ghost" class="group/terminal-toggle size-6 p-0" onClick={layout.terminal.toggle}>
  84. <div class="relative flex items-center justify-center size-4 [&>*]:absolute [&>*]:inset-0">
  85. <Icon
  86. size="small"
  87. name={layout.terminal.opened() ? "layout-bottom-full" : "layout-bottom"}
  88. class="group-hover/terminal-toggle:hidden"
  89. />
  90. <Icon
  91. size="small"
  92. name="layout-bottom-partial"
  93. class="hidden group-hover/terminal-toggle:inline-block"
  94. />
  95. <Icon
  96. size="small"
  97. name={layout.terminal.opened() ? "layout-bottom" : "layout-bottom-full"}
  98. class="hidden group-active/terminal-toggle:inline-block"
  99. />
  100. </div>
  101. </Button>
  102. </Tooltip>
  103. </div>
  104. </div>
  105. </header>
  106. <div class="h-[calc(100vh-3rem)] flex">
  107. <div
  108. classList={{
  109. "relative @container w-12 pb-5 shrink-0 bg-background-base": true,
  110. "flex flex-col gap-5.5 items-start self-stretch justify-between": true,
  111. "border-r border-border-weak-base": true,
  112. }}
  113. style={{ width: layout.sidebar.opened() ? `${layout.sidebar.width()}px` : undefined }}
  114. >
  115. <Show when={layout.sidebar.opened()}>
  116. <ResizeHandle
  117. direction="horizontal"
  118. size={layout.sidebar.width()}
  119. min={150}
  120. max={window.innerWidth * 0.3}
  121. collapseThreshold={80}
  122. onResize={layout.sidebar.resize}
  123. onCollapse={layout.sidebar.close}
  124. />
  125. </Show>
  126. <div class="grow flex flex-col items-start self-stretch gap-4 p-2 min-h-0">
  127. <Tooltip class="shrink-0" placement="right" value="Toggle sidebar" inactive={layout.sidebar.opened()}>
  128. <Button
  129. variant="ghost"
  130. size="large"
  131. class="group/sidebar-toggle shrink-0 w-full text-left justify-start"
  132. onClick={layout.sidebar.toggle}
  133. >
  134. <div class="relative -ml-px flex items-center justify-center size-4 [&>*]:absolute [&>*]:inset-0">
  135. <Icon
  136. name={layout.sidebar.opened() ? "layout-left" : "layout-right"}
  137. size="small"
  138. class="group-hover/sidebar-toggle:hidden"
  139. />
  140. <Icon
  141. name={layout.sidebar.opened() ? "layout-left-partial" : "layout-right-partial"}
  142. size="small"
  143. class="hidden group-hover/sidebar-toggle:inline-block"
  144. />
  145. <Icon
  146. name={layout.sidebar.opened() ? "layout-left-full" : "layout-right-full"}
  147. size="small"
  148. class="hidden group-active/sidebar-toggle:inline-block"
  149. />
  150. </div>
  151. <Show when={layout.sidebar.opened()}>
  152. <div class="hidden group-hover/sidebar-toggle:block group-active/sidebar-toggle:block text-text-base">
  153. Toggle sidebar
  154. </div>
  155. </Show>
  156. </Button>
  157. </Tooltip>
  158. <div class="flex flex-col justify-center items-start gap-4 self-stretch min-h-0">
  159. <div class="hidden @[4rem]:flex size-full flex-col grow overflow-y-auto no-scrollbar">
  160. <For each={layout.projects.list()}>
  161. {(project) => {
  162. const [store] = globalSync.child(project.directory)
  163. const slug = createMemo(() => base64Encode(project.directory))
  164. return (
  165. <Collapsible variant="ghost" defaultOpen class="gap-2">
  166. <Button
  167. as={"div"}
  168. variant="ghost"
  169. class="flex items-center justify-between gap-3 w-full h-8 pl-2 pr-2.25 self-stretch"
  170. >
  171. <Collapsible.Trigger class="p-0 text-left text-14-medium text-text-strong grow min-w-0 truncate">
  172. {getFilename(project.directory)}
  173. </Collapsible.Trigger>
  174. <IconButton as={A} href={`${slug()}/session`} icon="plus-small" size="normal" />
  175. </Button>
  176. <Collapsible.Content>
  177. <nav class="w-full flex flex-col gap-1.5">
  178. <For each={store.session}>
  179. {(session) => {
  180. const updated = createMemo(() => DateTime.fromMillis(session.time.updated))
  181. return (
  182. <A
  183. data-active={session.id === params.id}
  184. href={`${slug()}/session/${session.id}`}
  185. class="group/session focus:outline-none cursor-default"
  186. >
  187. <Tooltip placement="right" value={session.title}>
  188. <div
  189. class="w-full px-2 py-1 rounded-md
  190. group-data-[active=true]/session:bg-surface-raised-base-hover
  191. group-hover/session:bg-surface-raised-base-hover
  192. group-focus/session:bg-surface-raised-base-hover"
  193. >
  194. <div class="flex items-center self-stretch gap-6 justify-between">
  195. <span class="text-14-regular text-text-strong overflow-hidden text-ellipsis truncate">
  196. {session.title}
  197. </span>
  198. <span class="text-12-regular text-text-weak text-right whitespace-nowrap">
  199. {Math.abs(updated().diffNow().as("seconds")) < 60
  200. ? "Now"
  201. : updated()
  202. .toRelative({ style: "short", unit: ["days", "hours", "minutes"] })
  203. ?.replace(" ago", "")
  204. ?.replace(/ days?/, "d")
  205. ?.replace(" min.", "m")
  206. ?.replace(" hr.", "h")}
  207. </span>
  208. </div>
  209. <div class="hidden _flex justify-between items-center self-stretch">
  210. <span class="text-12-regular text-text-weak">{`${session.summary?.files || "No"} file${session.summary?.files !== 1 ? "s" : ""} changed`}</span>
  211. <Show when={session.summary}>
  212. {(summary) => <DiffChanges changes={summary()} />}
  213. </Show>
  214. </div>
  215. </div>
  216. </Tooltip>
  217. </A>
  218. )
  219. }}
  220. </For>
  221. </nav>
  222. {/* <Show when={sync.session.more()}> */}
  223. {/* <button */}
  224. {/* class="shrink-0 self-start p-3 text-12-medium text-text-weak hover:text-text-strong" */}
  225. {/* onClick={() => sync.session.fetch()} */}
  226. {/* > */}
  227. {/* Show more */}
  228. {/* </button> */}
  229. {/* </Show> */}
  230. </Collapsible.Content>
  231. </Collapsible>
  232. )
  233. }}
  234. </For>
  235. </div>
  236. </div>
  237. </div>
  238. <div class="flex flex-col gap-1.5 self-stretch items-start shrink-0 px-2 py-3">
  239. <Tooltip placement="right" value="Open project" inactive={layout.sidebar.opened()}>
  240. <Button
  241. disabled
  242. class="flex w-full text-left justify-start text-12-medium text-text-base stroke-[1.5px]"
  243. variant="ghost"
  244. size="large"
  245. icon="folder-add-left"
  246. onClick={handleOpenProject}
  247. >
  248. <Show when={layout.sidebar.opened()}>Open project</Show>
  249. </Button>
  250. </Tooltip>
  251. <Tooltip placement="right" value="Settings" inactive={layout.sidebar.opened()}>
  252. <Button
  253. disabled
  254. class="flex w-full text-left justify-start text-12-medium text-text-base stroke-[1.5px]"
  255. variant="ghost"
  256. size="large"
  257. icon="settings-gear"
  258. >
  259. <Show when={layout.sidebar.opened()}>Settings</Show>
  260. </Button>
  261. </Tooltip>
  262. <Tooltip placement="right" value="Share feedback" inactive={layout.sidebar.opened()}>
  263. <Button
  264. as={"a"}
  265. href="https://opencode.ai/desktop-feedback"
  266. target="_blank"
  267. class="flex w-full text-left justify-start text-12-medium text-text-base stroke-[1.5px]"
  268. variant="ghost"
  269. size="large"
  270. icon="bubble-5"
  271. >
  272. <Show when={layout.sidebar.opened()}>Share feedback</Show>
  273. </Button>
  274. </Tooltip>
  275. </div>
  276. </div>
  277. <main class="size-full overflow-x-hidden flex flex-col items-start">{props.children}</main>
  278. </div>
  279. </div>
  280. )
  281. }