import { useRef, useEffect, type ComponentType, type MouseEvent, type PointerEvent, type SVGAttributes } from 'react' import { cn } from '@/lib/utils' import { Badge } from '@/components/ui/badge' import { scrollSelectedHTMLChildIntoView } from '../utils/domScroll' import { NoteTitleIcon } from './NoteTitleIcon' import { WorkspaceInitialsBadge } from './WorkspaceInitialsBadge' import type { WorkspaceIdentity } from '../types' export interface NoteSearchResultItem { title: string noteIcon?: string | null noteType?: string typeColor?: string typeLightColor?: string TypeIcon?: ComponentType> workspace?: WorkspaceIdentity | null } interface NoteSearchListProps { items: T[] selectedIndex: number getItemKey: (item: T, index: number) => string onItemClick: (item: T, index: number) => void onItemHover?: (index: number) => void activateOnMouseDown?: boolean emptyMessage?: string className?: string } interface NoteSearchListItemProps { item: T index: number selected: boolean onItemClick: (item: T, index: number) => void onItemHover?: (index: number) => void activateOnMouseDown?: boolean } function NoteSearchListItem({ item, index, selected, onItemClick, onItemHover, activateOnMouseDown, }: NoteSearchListItemProps) { const pressActivatedRef = useRef(false) const activateFromPress = (event: MouseEvent | PointerEvent) => { event.preventDefault() if (!activateOnMouseDown) return event.stopPropagation() if (pressActivatedRef.current) return pressActivatedRef.current = true window.setTimeout(() => { pressActivatedRef.current = false }, 0) onItemClick(item, index) } const handleClick = (event: MouseEvent) => { if (activateOnMouseDown) { event.preventDefault() event.stopPropagation() return } onItemClick(item, index) } return (
onItemHover?.(index)} > {item.TypeIcon && ( )} {item.title} {(item.noteType || item.workspace) && ( {item.noteType && ( {item.noteType} )} )}
) } export function NoteSearchList({ items, selectedIndex, getItemKey, onItemClick, onItemHover, activateOnMouseDown, emptyMessage = 'No results', className, }: NoteSearchListProps) { const listRef = useRef(null) useEffect(() => { scrollSelectedHTMLChildIntoView(listRef.current, selectedIndex) }, [selectedIndex]) if (items.length === 0) { return (
{emptyMessage}
) } return (
{items.map((item, i) => ( ))}
) }