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.
- package/index.tsx +87 -30
- 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
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
.
|
|
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
|
-
<
|
|
41
|
-
{
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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().
|
|
56
|
-
|
|
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
|
-
|
|
107
|
+
{active ? ">" : " "} {trunc(session.title, 30)}
|
|
59
108
|
</text>
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
}
|