Spaces:
Sleeping
Sleeping
File size: 9,527 Bytes
f871fed | 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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | 'use client'
import { useEffect, useState, useCallback, useMemo } from 'react'
import { useRouter } from 'next/navigation'
import { useCreateDialogs } from '@/lib/hooks/use-create-dialogs'
import { useNotebooks } from '@/lib/hooks/use-notebooks'
import { useTheme } from '@/lib/stores/theme-store'
import {
CommandDialog,
CommandInput,
CommandList,
CommandGroup,
CommandItem,
CommandSeparator,
} from '@/components/ui/command'
import {
Book,
Search,
Mic,
Bot,
Shuffle,
Settings,
FileText,
Wrench,
MessageCircleQuestion,
Plus,
Sun,
Moon,
Monitor,
Loader2,
} from 'lucide-react'
const navigationItems = [
{ name: 'Sources', href: '/sources', icon: FileText, keywords: ['files', 'documents', 'upload'] },
{ name: 'Notebooks', href: '/notebooks', icon: Book, keywords: ['notes', 'research', 'projects'] },
{ name: 'Ask and Search', href: '/search', icon: Search, keywords: ['find', 'query'] },
{ name: 'Podcasts', href: '/podcasts', icon: Mic, keywords: ['audio', 'episodes', 'generate'] },
{ name: 'Models', href: '/models', icon: Bot, keywords: ['ai', 'llm', 'providers', 'openai', 'anthropic'] },
{ name: 'Transformations', href: '/transformations', icon: Shuffle, keywords: ['prompts', 'templates', 'actions'] },
{ name: 'Settings', href: '/settings', icon: Settings, keywords: ['preferences', 'config', 'options'] },
{ name: 'Advanced', href: '/advanced', icon: Wrench, keywords: ['debug', 'system', 'tools'] },
]
const createItems = [
{ name: 'Create Source', action: 'source', icon: FileText },
{ name: 'Create Notebook', action: 'notebook', icon: Book },
{ name: 'Create Podcast', action: 'podcast', icon: Mic },
]
const themeItems = [
{ name: 'Light Theme', value: 'light' as const, icon: Sun, keywords: ['bright', 'day'] },
{ name: 'Dark Theme', value: 'dark' as const, icon: Moon, keywords: ['night'] },
{ name: 'System Theme', value: 'system' as const, icon: Monitor, keywords: ['auto', 'default'] },
]
export function CommandPalette() {
const [open, setOpen] = useState(false)
const [query, setQuery] = useState('')
const router = useRouter()
const { openSourceDialog, openNotebookDialog, openPodcastDialog } = useCreateDialogs()
const { setTheme } = useTheme()
const { data: notebooks, isLoading: notebooksLoading } = useNotebooks(false)
// Global keyboard listener for ⌘K / Ctrl+K
useEffect(() => {
const down = (e: KeyboardEvent) => {
// Skip if focus is inside editable elements
const target = e.target as HTMLElement | null
if (
target &&
(target.isContentEditable ||
['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName))
) {
return
}
if (e.key === 'k' && (e.metaKey || e.ctrlKey)) {
e.preventDefault()
e.stopPropagation()
setOpen((open) => !open)
}
}
// Use capture phase to intercept before other handlers
document.addEventListener('keydown', down, true)
return () => document.removeEventListener('keydown', down, true)
}, [])
// Reset query when dialog closes
useEffect(() => {
if (!open) {
setQuery('')
}
}, [open])
const handleSelect = useCallback((callback: () => void) => {
setOpen(false)
setQuery('')
// Use setTimeout to ensure dialog closes before action
setTimeout(callback, 0)
}, [])
const handleNavigate = useCallback((href: string) => {
handleSelect(() => router.push(href))
}, [handleSelect, router])
const handleSearch = useCallback(() => {
if (!query.trim()) return
handleSelect(() => router.push(`/search?q=${encodeURIComponent(query)}&mode=search`))
}, [handleSelect, router, query])
const handleAsk = useCallback(() => {
if (!query.trim()) return
handleSelect(() => router.push(`/search?q=${encodeURIComponent(query)}&mode=ask`))
}, [handleSelect, router, query])
const handleCreate = useCallback((action: string) => {
handleSelect(() => {
if (action === 'source') openSourceDialog()
else if (action === 'notebook') openNotebookDialog()
else if (action === 'podcast') openPodcastDialog()
})
}, [handleSelect, openSourceDialog, openNotebookDialog, openPodcastDialog])
const handleTheme = useCallback((theme: 'light' | 'dark' | 'system') => {
handleSelect(() => setTheme(theme))
}, [handleSelect, setTheme])
// Check if query matches any command (navigation, create, theme, or notebook)
const queryLower = query.toLowerCase().trim()
const hasCommandMatch = useMemo(() => {
if (!queryLower) return false
return (
navigationItems.some(item =>
item.name.toLowerCase().includes(queryLower) ||
item.keywords.some(k => k.includes(queryLower))
) ||
createItems.some(item =>
item.name.toLowerCase().includes(queryLower)
) ||
themeItems.some(item =>
item.name.toLowerCase().includes(queryLower) ||
item.keywords.some(k => k.includes(queryLower))
) ||
(notebooks?.some(nb =>
nb.name.toLowerCase().includes(queryLower) ||
(nb.description && nb.description.toLowerCase().includes(queryLower))
) ?? false)
)
}, [queryLower, notebooks])
// Determine if we should show the Search/Ask section at the top
const showSearchFirst = query.trim() && !hasCommandMatch
return (
<CommandDialog
open={open}
onOpenChange={setOpen}
title="Command Palette"
description="Navigate, search, or ask your knowledge base"
className="sm:max-w-lg"
>
<CommandInput
placeholder="Type a command or search..."
value={query}
onValueChange={setQuery}
/>
<CommandList>
{/* Search/Ask - show FIRST when there's a query with no command match */}
{showSearchFirst && (
<CommandGroup heading="Search & Ask" forceMount>
<CommandItem
value={`__search__ ${query}`}
onSelect={handleSearch}
forceMount
>
<Search className="h-4 w-4" />
<span>Search for “{query}”</span>
</CommandItem>
<CommandItem
value={`__ask__ ${query}`}
onSelect={handleAsk}
forceMount
>
<MessageCircleQuestion className="h-4 w-4" />
<span>Ask about “{query}”</span>
</CommandItem>
</CommandGroup>
)}
{/* Navigation */}
<CommandGroup heading="Navigation">
{navigationItems.map((item) => (
<CommandItem
key={item.href}
value={`${item.name} ${item.keywords.join(' ')}`}
onSelect={() => handleNavigate(item.href)}
>
<item.icon className="h-4 w-4" />
<span>{item.name}</span>
</CommandItem>
))}
</CommandGroup>
{/* Notebooks */}
<CommandGroup heading="Notebooks">
{notebooksLoading ? (
<CommandItem disabled>
<Loader2 className="h-4 w-4 animate-spin" />
<span>Loading notebooks...</span>
</CommandItem>
) : notebooks && notebooks.length > 0 ? (
notebooks.map((notebook) => (
<CommandItem
key={notebook.id}
value={`notebook ${notebook.name} ${notebook.description || ''}`}
onSelect={() => handleNavigate(`/notebooks/${notebook.id}`)}
>
<Book className="h-4 w-4" />
<span>{notebook.name}</span>
</CommandItem>
))
) : null}
</CommandGroup>
{/* Create */}
<CommandGroup heading="Create">
{createItems.map((item) => (
<CommandItem
key={item.action}
value={`create ${item.name}`}
onSelect={() => handleCreate(item.action)}
>
<Plus className="h-4 w-4" />
<span>{item.name}</span>
</CommandItem>
))}
</CommandGroup>
{/* Theme */}
<CommandGroup heading="Theme">
{themeItems.map((item) => (
<CommandItem
key={item.value}
value={`theme ${item.name} ${item.keywords.join(' ')}`}
onSelect={() => handleTheme(item.value)}
>
<item.icon className="h-4 w-4" />
<span>{item.name}</span>
</CommandItem>
))}
</CommandGroup>
{/* Search/Ask - show at bottom when there IS a command match */}
{query.trim() && hasCommandMatch && (
<>
<CommandSeparator />
<CommandGroup heading="Or search your knowledge base" forceMount>
<CommandItem
value={`__search__ ${query}`}
onSelect={handleSearch}
forceMount
>
<Search className="h-4 w-4" />
<span>Search for “{query}”</span>
</CommandItem>
<CommandItem
value={`__ask__ ${query}`}
onSelect={handleAsk}
forceMount
>
<MessageCircleQuestion className="h-4 w-4" />
<span>Ask about “{query}”</span>
</CommandItem>
</CommandGroup>
</>
)}
</CommandList>
</CommandDialog>
)
}
|