forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory-node.tsx
More file actions
162 lines (153 loc) · 5.5 KB
/
directory-node.tsx
File metadata and controls
162 lines (153 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { For, Show, createSignal, onCleanup } from "solid-js"
import { Collapsible } from "@opencode-ai/ui/collapsible"
import { Icon } from "@opencode-ai/ui/icon"
import { Spinner } from "@opencode-ai/ui/spinner"
import { SessionNode } from "./session-node"
import type { Session } from "@opencode-ai/sdk/v2/client"
export type TreeNode = {
name: string
fullPath: string
count: number
ownCount: number
isLeaf: boolean
children: TreeNode[]
}
export type DirectoryNodeProps = {
node: TreeNode
depth: number
sessionsByDir: Record<string, Session[]>
metaByDir: Record<string, { loading: boolean; cursor?: number; complete: boolean }>
expandedDirs: Record<string, boolean>
sessionsVisible: Record<string, boolean>
onToggleDir: (directory: string) => void
onToggleSessions: (directory: string) => void
onSelectSession: (directory: string, sessionId: string) => void
onLoadMore: (directory: string) => void
currentSessionId?: string
}
function useSentinel(onVisible: () => void) {
return (el: HTMLDivElement) => {
if (typeof IntersectionObserver === "undefined") return
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) onVisible()
},
{ threshold: 0.1 },
)
observer.observe(el)
onCleanup(() => observer.disconnect())
}
}
export function DirectoryNode(props: DirectoryNodeProps) {
const [treeExpanded, setTreeExpanded] = createSignal(false)
const isOpen = () =>
props.node.isLeaf
? (props.expandedDirs[props.node.fullPath] ?? false)
: treeExpanded()
const toggle = () => {
if (props.node.isLeaf) {
props.onToggleDir(props.node.fullPath)
} else {
setTreeExpanded((prev) => !prev)
}
}
const sessionsShown = () => props.sessionsVisible[props.node.fullPath] ?? false
const sessions = () => props.sessionsByDir[props.node.fullPath] ?? []
const meta = () => props.metaByDir[props.node.fullPath]
const loading = () => meta()?.loading ?? false
const hasMore = () => !(meta()?.complete ?? true)
const indent = () => props.depth * 12 + 8
return (
<Collapsible variant="ghost" open={isOpen()} onOpenChange={toggle}>
<Collapsible.Trigger
class="group flex w-full items-center gap-1.5 rounded py-1.5 pr-2 text-left hover:bg-surface-raised-base-hover/50"
style={{ "padding-left": `${indent()}px` }}
>
<span class="shrink-0 text-text-weak">
<Icon name="folder" size="small" />
</span>
<span
class="min-w-0 flex-1 truncate text-13-regular text-text-standard"
title={props.node.fullPath}
>
{props.node.name}
</span>
<span class="shrink-0 text-12-regular text-text-weak">{props.node.count}</span>
<Show when={loading()}>
<span class="shrink-0 text-text-weak">
<Spinner class="size-3" />
</span>
</Show>
<Collapsible.Arrow />
</Collapsible.Trigger>
<Collapsible.Content>
<div
class="flex items-center gap-1 py-0.5"
style={{ "padding-left": `${indent() + 12}px` }}
>
<button
class="flex items-center gap-1 rounded px-1.5 py-0.5 text-11-medium text-text-weak hover:bg-surface-raised-base-hover/50 hover:text-text-base"
onClick={(e) => {
e.stopPropagation()
props.onToggleSessions(props.node.fullPath)
}}
>
<Icon name={sessionsShown() ? "chevron-down" : "chevron-right"} size="small" />
<span>{sessionsShown() ? "Hide sessions" : `Show sessions (${props.node.ownCount})`}</span>
<Show when={loading()}>
<Spinner class="size-3" />
</Show>
</button>
</div>
<Show when={sessionsShown()}>
<Show when={sessions().length > 0 || loading()}>
<div style={{ "padding-left": `${indent() + 12}px` }}>
<For each={sessions()}>
{(session) => (
<SessionNode
session={session}
active={session.id === props.currentSessionId}
onClick={() => props.onSelectSession(session.directory, session.id)}
/>
)}
</For>
<Show when={hasMore()}>
<div
ref={useSentinel(() => {
if (!loading()) props.onLoadMore(props.node.fullPath)
})}
class="h-4"
/>
</Show>
</div>
</Show>
<Show when={sessions().length === 0 && !loading()}>
<div
class="px-2 py-2 text-12-regular text-text-weak"
style={{ "padding-left": `${indent() + 20}px` }}
>
No sessions
</div>
</Show>
</Show>
<For each={props.node.children}>
{(child) => (
<DirectoryNode
node={child}
depth={props.depth + 1}
sessionsByDir={props.sessionsByDir}
metaByDir={props.metaByDir}
expandedDirs={props.expandedDirs}
sessionsVisible={props.sessionsVisible}
onToggleDir={props.onToggleDir}
onToggleSessions={props.onToggleSessions}
onSelectSession={props.onSelectSession}
onLoadMore={props.onLoadMore}
currentSessionId={props.currentSessionId}
/>
)}
</For>
</Collapsible.Content>
</Collapsible>
)
}