aboutsummaryrefslogtreecommitdiff
path: root/packages/memory-graph/src/components/spaces-dropdown.tsx
blob: d8a56fe36cb3b98ea7c6005ca7a57ddcb6da4ae0 (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"use client"

import { Badge } from "@/ui/badge"
import { ChevronDown, Eye, Search, X } from "lucide-react"
import { memo, useEffect, useRef, useState } from "react"
import type { SpacesDropdownProps } from "@/types"
import * as styles from "./spaces-dropdown.css"

export const SpacesDropdown = memo<SpacesDropdownProps>(
	({ selectedSpace, availableSpaces, spaceMemoryCounts, onSpaceChange }) => {
		const [isOpen, setIsOpen] = useState(false)
		const [searchQuery, setSearchQuery] = useState("")
		const [highlightedIndex, setHighlightedIndex] = useState(-1)
		const dropdownRef = useRef<HTMLDivElement>(null)
		const searchInputRef = useRef<HTMLInputElement>(null)
		const itemRefs = useRef<Map<number, HTMLButtonElement>>(new Map())

		// Close dropdown when clicking outside
		useEffect(() => {
			const handleClickOutside = (event: MouseEvent) => {
				if (
					dropdownRef.current &&
					!dropdownRef.current.contains(event.target as Node)
				) {
					setIsOpen(false)
				}
			}

			document.addEventListener("mousedown", handleClickOutside)
			return () => document.removeEventListener("mousedown", handleClickOutside)
		}, [])

		// Focus search input when dropdown opens
		useEffect(() => {
			if (isOpen && searchInputRef.current) {
				searchInputRef.current.focus()
			}
		}, [isOpen])

		// Clear search query and reset highlighted index when dropdown closes
		useEffect(() => {
			if (!isOpen) {
				setSearchQuery("")
				setHighlightedIndex(-1)
			}
		}, [isOpen])

		// Filter spaces based on search query (client-side)
		const filteredSpaces = searchQuery
			? availableSpaces.filter((space) =>
					space.toLowerCase().includes(searchQuery.toLowerCase()),
				)
			: availableSpaces

		const totalMemories = Object.values(spaceMemoryCounts).reduce(
			(sum, count) => sum + count,
			0,
		)

		// Total items including "Latest" option
		const totalItems = filteredSpaces.length + 1

		// Scroll highlighted item into view
		useEffect(() => {
			if (highlightedIndex >= 0 && highlightedIndex < totalItems) {
				const element = itemRefs.current.get(highlightedIndex)
				if (element) {
					element.scrollIntoView({
						block: "nearest",
						behavior: "smooth",
					})
				}
			}
		}, [highlightedIndex, totalItems])

		// Handle keyboard navigation
		const handleKeyDown = (e: React.KeyboardEvent) => {
			if (!isOpen) return

			switch (e.key) {
				case "ArrowDown":
					e.preventDefault()
					setHighlightedIndex((prev) => (prev < totalItems - 1 ? prev + 1 : 0))
					break
				case "ArrowUp":
					e.preventDefault()
					setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : totalItems - 1))
					break
				case "Enter":
					e.preventDefault()
					if (highlightedIndex === 0) {
						onSpaceChange("all")
						setIsOpen(false)
					} else if (
						highlightedIndex > 0 &&
						highlightedIndex <= filteredSpaces.length
					) {
						const selectedSpace = filteredSpaces[highlightedIndex - 1]
						if (selectedSpace) {
							onSpaceChange(selectedSpace)
							setIsOpen(false)
						}
					}
					break
				case "Escape":
					e.preventDefault()
					setIsOpen(false)
					break
			}
		}

		return (
			<div
				className={styles.container}
				ref={dropdownRef}
				onKeyDown={handleKeyDown}
			>
				<button
					className={styles.trigger}
					onClick={() => setIsOpen(!isOpen)}
					type="button"
				>
					{/*@ts-ignore */}
					<Eye className={styles.triggerIcon} />
					<div className={styles.triggerContent}>
						<span className={styles.triggerLabel}>
							{selectedSpace === "all"
								? "Latest"
								: selectedSpace || "Select space"}
						</span>
						<div className={styles.triggerSubtext}>
							{selectedSpace === "all"
								? ""
								: `${spaceMemoryCounts[selectedSpace] || 0} memories`}
						</div>
					</div>
					{/*@ts-ignore */}
					<ChevronDown
						className={`${styles.triggerChevron} ${isOpen ? styles.triggerChevronOpen : ""}`}
					/>
				</button>

				{isOpen && (
					<div className={styles.dropdown}>
						<div className={styles.dropdownInner}>
							{/* Search Input - Always show for filtering */}
							<div className={styles.searchContainer}>
								<div className={styles.searchForm}>
									{/*@ts-ignore */}
									<Search className={styles.searchIcon} />
									<input
										className={styles.searchInput}
										onChange={(e) => setSearchQuery(e.target.value)}
										placeholder="Search spaces..."
										ref={searchInputRef}
										type="text"
										value={searchQuery}
									/>
									{searchQuery && (
										<button
											className={styles.searchClearButton}
											onClick={() => setSearchQuery("")}
											type="button"
											aria-label="Clear search"
										>
											{/*@ts-ignore */}
											<X className={styles.searchIcon} />
										</button>
									)}
								</div>
							</div>

							{/* Spaces List */}
							<div className={styles.dropdownList}>
								{/* Always show "Latest" option */}
								<button
									ref={(el) => {
										if (el) itemRefs.current.set(0, el)
									}}
									className={
										selectedSpace === "all"
											? styles.dropdownItemActive
											: highlightedIndex === 0
												? styles.dropdownItemHighlighted
												: styles.dropdownItem
									}
									onClick={() => {
										onSpaceChange("all")
										setIsOpen(false)
									}}
									onMouseEnter={() => setHighlightedIndex(0)}
									type="button"
								>
									<span className={styles.dropdownItemLabel}>Latest</span>
									<Badge className={styles.dropdownItemBadge}>
										{totalMemories}
									</Badge>
								</button>

								{/* Show all spaces, filtered by search query */}
								{filteredSpaces.length > 0
									? filteredSpaces.map((space, index) => {
											const itemIndex = index + 1
											return (
												<button
													ref={(el) => {
														if (el) itemRefs.current.set(itemIndex, el)
													}}
													className={
														selectedSpace === space
															? styles.dropdownItemActive
															: highlightedIndex === itemIndex
																? styles.dropdownItemHighlighted
																: styles.dropdownItem
													}
													key={space}
													onClick={() => {
														onSpaceChange(space)
														setIsOpen(false)
													}}
													onMouseEnter={() => setHighlightedIndex(itemIndex)}
													type="button"
												>
													<span className={styles.dropdownItemLabelTruncate}>
														{space}
													</span>
													<Badge className={styles.dropdownItemBadge}>
														{spaceMemoryCounts[space] || 0}
													</Badge>
												</button>
											)
										})
									: searchQuery && (
											<div className={styles.emptyState}>
												No spaces found matching "{searchQuery}"
											</div>
										)}
							</div>
						</div>
					</div>
				)}
			</div>
		)
	},
)

SpacesDropdown.displayName = "SpacesDropdown"