'use client'; import { useState, useCallback, useEffect, useRef } from 'react'; import { IconListCheck, IconFile, IconFolder, IconSearch, IconAt } from '@tabler/icons-react'; import { Popover, PopoverContent, PopoverTrigger } from '@kandev/ui/popover'; import { Checkbox } from '@kandev/ui/checkbox'; import { Input } from '@kandev/ui/input'; import { getWebSocketClient } from '@/lib/ws/connection'; import { searchWorkspaceFiles } from '@/lib/ws/workspace-files'; import { useCustomPrompts } from '@/hooks/domains/settings/use-custom-prompts'; import { isDirectory, getFileName } from '@/lib/utils/file-path'; import type { ContextFile } from '@/lib/state/context-files-store'; type ContextPopoverProps = { open: boolean; onOpenChange: (open: boolean) => void; trigger: React.ReactNode; sessionId: string | null; /** Whether plan is included as context (from context files store, not plan panel) */ planContextEnabled: boolean; contextFiles: ContextFile[]; onToggleFile: (file: ContextFile) => void; }; const FILE_SEARCH_DEBOUNCE = 300; const PLAN_CONTEXT_FILE: ContextFile = { path: 'plan:context', name: 'Plan', pinned: true }; export function ContextPopover({ open, onOpenChange, trigger, sessionId, planContextEnabled, contextFiles, onToggleFile, }: ContextPopoverProps) { const [query, setQuery] = useState(''); const [fileResults, setFileResults] = useState([]); const [isLoading, setIsLoading] = useState(false); const searchTimeoutRef = useRef | null>(null); const inputRef = useRef(null); const { prompts } = useCustomPrompts(); // Focus input when popover opens /* eslint-disable react-hooks/set-state-in-effect -- resetting state on open/close is intentional */ useEffect(() => { if (open) { requestAnimationFrame(() => inputRef.current?.focus()); } else { setQuery(''); setFileResults([]); } }, [open]); /* eslint-enable react-hooks/set-state-in-effect */ // Debounced file search /* eslint-disable react-hooks/set-state-in-effect -- loading state sync is intentional for UX */ useEffect(() => { if (!open || !sessionId) return; if (searchTimeoutRef.current) { clearTimeout(searchTimeoutRef.current); } const delay = query === '' ? 0 : FILE_SEARCH_DEBOUNCE; setIsLoading(true); let cancelled = false; searchTimeoutRef.current = setTimeout(async () => { try { const client = getWebSocketClient(); if (!client) { if (!cancelled) { setFileResults([]); setIsLoading(false); } return; } const response = await searchWorkspaceFiles(client, sessionId, query || '', 20); if (!cancelled) setFileResults(response.files || []); } catch { if (!cancelled) setFileResults([]); } if (!cancelled) setIsLoading(false); }, delay); return () => { cancelled = true; if (searchTimeoutRef.current) clearTimeout(searchTimeoutRef.current); }; }, [open, sessionId, query]); /* eslint-enable react-hooks/set-state-in-effect */ const handleToggleFile = useCallback( (filePath: string) => { const name = getFileName(filePath); onToggleFile({ path: filePath, name, pinned: true }); }, [onToggleFile] ); const isFileSelected = useCallback( (path: string) => contextFiles.some((f) => f.path === path), [contextFiles] ); // Filter prompts by query const filteredPrompts = query ? prompts.filter((p) => p.name.toLowerCase().includes(query.toLowerCase())) : prompts; return ( {trigger} e.preventDefault()} > {/* Header */}

Context

ยท Select files and prompts to include

{/* Search */}
setQuery(e.target.value)} placeholder="Search files and prompts..." className="h-7 pl-7 text-xs" />
{/* Items */}
{/* Plan item - always first, only when no search query */} {!query && (
onToggleFile(PLAN_CONTEXT_FILE)} > onToggleFile(PLAN_CONTEXT_FILE)} className="h-3.5 w-3.5" /> Plan {planContextEnabled && ( ACTIVE )}
)} {/* Prompts section */} {filteredPrompts.length > 0 && ( <> {!query && (

Prompts

)} {filteredPrompts.map((prompt) => (
{ // Insert prompt content by simulating a mention select - for now just copy to clipboard or similar // Prompts in context popover: clicking inserts the prompt name as a context file reference // This toggles the prompt as a context file onToggleFile({ path: `prompt:${prompt.id}`, name: prompt.name, pinned: true }); }} > f.path === `prompt:${prompt.id}`)} onCheckedChange={() => onToggleFile({ path: `prompt:${prompt.id}`, name: prompt.name, pinned: true })} className="h-3.5 w-3.5" />
{prompt.name} {prompt.content && ( {prompt.content.length > 60 ? prompt.content.slice(0, 60) + '...' : prompt.content} )}
))} )} {/* Files section header */} {!query && fileResults.length > 0 && (

Files

)} {/* File results */} {isLoading ? (
Loading...
) : fileResults.length === 0 && query && filteredPrompts.length === 0 ? (
No results found
) : ( fileResults.map((filePath) => { const isDir = isDirectory(filePath); const name = getFileName(filePath); const parent = filePath.slice(0, filePath.length - name.length); return (
handleToggleFile(filePath)} > handleToggleFile(filePath)} className="h-3.5 w-3.5" /> {isDir ? ( ) : ( )}
{name} {parent && ( {parent} )}
); }) )}
); }