opencode-session-bar 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.tsx +87 -30
  2. package/package.json +1 -1
package/index.tsx CHANGED
@@ -1,16 +1,28 @@
1
1
  /** @jsxImportSource @opentui/solid */
2
- import { createResource, createSignal, For, Show } from "solid-js"
2
+ import { createResource, createSignal, For, Show, onMount, onCleanup } from "solid-js"
3
3
  import type { TuiPlugin, TuiPluginApi, TuiPluginModule } from "@opencode-ai/plugin/tui"
4
+ import type { KeyEvent } from "@opentui/core"
4
5
 
5
6
  function View(props: { api: TuiPluginApi; sessionID: string }) {
6
7
  const theme = () => props.api.theme.current
7
8
  const [deleting, setDeleting] = createSignal<Set<string>>(new Set())
9
+ const [selectedIndex, setSelectedIndex] = createSignal(0)
10
+ const [listFocused, setListFocused] = createSignal(false)
11
+ const [boxRef, setBoxRef] = createSignal<any>()
8
12
 
9
- const [sessions, { refetch }] = createResource(async () => {
13
+ const [sessions, { refetch }] = createResource(async () => {
10
14
  const res = await props.api.client.session.list()
11
15
  return (res.data ?? []) as Array<{ id: string; title: string; time: { created: number; updated: number } }>
12
16
  })
13
17
 
18
+ const sorted = () => (sessions() ?? [])
19
+ .slice()
20
+ .sort((a, b) => b.time.updated - a.time.updated)
21
+
22
+ function trunc(s: string, n: number): string {
23
+ return s.length > n ? s.slice(0, n) + "…" : s
24
+ }
25
+
14
26
  async function handleSwitch(id: string) {
15
27
  props.api.route.navigate("session", { sessionID: id })
16
28
  }
@@ -27,9 +39,45 @@ function View(props: { api: TuiPluginApi; sessionID: string }) {
27
39
  }
28
40
  }
29
41
 
30
- const sorted = () => (sessions() ?? [])
31
- .slice()
32
- .sort((a, b) => b.time.updated - a.time.updated)
42
+ function handleKeyDown(e: KeyEvent) {
43
+ const list = sorted()
44
+ if (!list.length) return
45
+
46
+ if (e.name === "down" || e.name === "j") {
47
+ e.preventDefault()
48
+ setSelectedIndex(i => Math.min(i + 1, list.length - 1))
49
+ } else if (e.name === "up" || e.name === "k") {
50
+ e.preventDefault()
51
+ setSelectedIndex(i => Math.max(i - 1, 0))
52
+ } else if (e.name === "enter" || e.name === "space") {
53
+ e.preventDefault()
54
+ const s = list[selectedIndex()]
55
+ if (s && s.id !== props.sessionID) handleSwitch(s.id)
56
+ } else if (e.name === "escape") {
57
+ e.preventDefault()
58
+ setListFocused(false)
59
+ boxRef()?.blur()
60
+ }
61
+ }
62
+
63
+ function focusList() {
64
+ setListFocused(true)
65
+ boxRef()?.focus()
66
+ const list = sorted()
67
+ const idx = list.findIndex(s => s.id === props.sessionID)
68
+ if (idx >= 0) setSelectedIndex(idx)
69
+ }
70
+
71
+ onMount(() => {
72
+ const dispose = props.api.command?.register(() => [{
73
+ title: "Session Bar: Focus List",
74
+ value: "session-bar:focus",
75
+ category: "Session Bar",
76
+ keybind: "ctrl+s",
77
+ onSelect: focusList,
78
+ }])
79
+ onCleanup(() => dispose?.())
80
+ })
33
81
 
34
82
  return (
35
83
  <box>
@@ -37,34 +85,43 @@ function View(props: { api: TuiPluginApi; sessionID: string }) {
37
85
  <Show when={sessions.loading}>
38
86
  <text fg={theme().textMuted}>loading...</text>
39
87
  </Show>
40
- <For each={sorted()}>
41
- {(session) => {
42
- const active = session.id === props.sessionID
43
- const isDeleting = deleting().has(session.id)
44
- return (
45
- <box flexDirection="row" gap={1}>
46
- <text
47
- fg={active ? theme().accent : theme().text}
48
- attributes={{ bold: active }}
49
- onMouseDown={() => !active && handleSwitch(session.id)}
50
- >
51
- {active ? ">" : " "} {session.title}
52
- </text>
53
- <Show when={!isDeleting}>
88
+ <box
89
+ ref={setBoxRef}
90
+ focusable
91
+ focused={listFocused()}
92
+ onKeyDown={handleKeyDown}
93
+ >
94
+ <For each={sorted()}>
95
+ {(session, i) => {
96
+ const active = session.id === props.sessionID
97
+ const isDeleting = deleting().has(session.id)
98
+ const isSelected = listFocused() && selectedIndex() === i()
99
+ return (
100
+ <box flexDirection="row" justifyContent="space-between">
54
101
  <text
55
- fg={theme().textMuted}
56
- onMouseDown={() => handleDelete(session.id)}
102
+ fg={active ? theme().accent : theme().text}
103
+ attributes={{ bold: active, inverse: isSelected }}
104
+ wrapMode="none"
105
+ onMouseDown={() => !active && handleSwitch(session.id)}
57
106
  >
58
- [x]
107
+ {active ? ">" : " "} {trunc(session.title, 30)}
59
108
  </text>
60
- </Show>
61
- <Show when={isDeleting}>
62
- <text fg={theme().textMuted}>...</text>
63
- </Show>
64
- </box>
65
- )
66
- }}
67
- </For>
109
+ <Show when={!isDeleting}>
110
+ <text
111
+ fg={theme().textMuted}
112
+ onMouseDown={() => handleDelete(session.id)}
113
+ >
114
+ [x]
115
+ </text>
116
+ </Show>
117
+ <Show when={isDeleting}>
118
+ <text fg={theme().textMuted}>...</text>
119
+ </Show>
120
+ </box>
121
+ )
122
+ }}
123
+ </For>
124
+ </box>
68
125
  </box>
69
126
  )
70
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-session-bar",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "OpenCode TUI sidebar plugin — list all sessions with quick switch and delete",
5
5
  "type": "module",
6
6
  "exports": {