|
| 1 | +import { |
| 2 | + type ClipboardEventHandler, |
| 3 | + type ReactNode, |
| 4 | + type RefObject, |
| 5 | + useLayoutEffect, |
| 6 | + useRef, |
| 7 | + useState, |
| 8 | +} from 'react' |
| 9 | +import { cn } from '@sim/emcn' |
| 10 | +import { Search } from '@sim/emcn/icons' |
| 11 | +import { PROMPT_TEXT_CLASSES } from '@/app/workspace/[workspaceId]/home/components/user-input/components/constants' |
| 12 | +import { GrowingTextarea } from '@/app/workspace/[workspaceId]/home/components/user-input/components/growing-textarea' |
| 13 | +import { InputToolbar } from '@/app/workspace/[workspaceId]/home/components/user-input/components/input-toolbar' |
| 14 | + |
| 15 | +interface SearchInputBarProps { |
| 16 | + inputRef: RefObject<HTMLTextAreaElement | null> |
| 17 | + value: string |
| 18 | + onChange: (value: string) => void |
| 19 | + onSubmit: () => void |
| 20 | + onPaste?: ClipboardEventHandler<HTMLTextAreaElement> |
| 21 | + placeholder?: string |
| 22 | + 'aria-label'?: string |
| 23 | + leadingControls?: ReactNode |
| 24 | + trailingControls: ReactNode |
| 25 | + floating?: boolean |
| 26 | +} |
| 27 | + |
| 28 | +/** Canonical Search bar shared by raw search and conversational Search. */ |
| 29 | +export function SearchInputBar({ |
| 30 | + leadingControls, |
| 31 | + inputRef, |
| 32 | + value, |
| 33 | + onChange, |
| 34 | + onSubmit, |
| 35 | + onPaste, |
| 36 | + placeholder = 'Search your sources', |
| 37 | + 'aria-label': ariaLabel = 'Search your sources', |
| 38 | + trailingControls, |
| 39 | + floating = false, |
| 40 | +}: SearchInputBarProps) { |
| 41 | + const scrollerRef = useRef<HTMLDivElement>(null) |
| 42 | + const leadingRef = useRef<HTMLDivElement>(null) |
| 43 | + const trailingRef = useRef<HTMLDivElement>(null) |
| 44 | + const textMeasureRef = useRef<HTMLSpanElement>(null) |
| 45 | + const [expanded, setExpanded] = useState(value.includes('\n')) |
| 46 | + |
| 47 | + /** Measure against the compact width so expanding the field cannot cause a wrap/collapse loop. */ |
| 48 | + useLayoutEffect(() => { |
| 49 | + const bar = scrollerRef.current |
| 50 | + const leading = leadingRef.current |
| 51 | + const trailing = trailingRef.current |
| 52 | + const text = textMeasureRef.current |
| 53 | + if (!bar || !leading || !trailing || !text) return |
| 54 | + const measure = () => { |
| 55 | + const style = getComputedStyle(bar) |
| 56 | + const compactWidth = |
| 57 | + bar.clientWidth - |
| 58 | + Number.parseFloat(style.paddingLeft) - |
| 59 | + Number.parseFloat(style.paddingRight) - |
| 60 | + leading.offsetWidth - |
| 61 | + trailing.offsetWidth - |
| 62 | + Number.parseFloat(getComputedStyle(leading.parentElement!).columnGap) * 2 |
| 63 | + setExpanded( |
| 64 | + value.includes('\n') || text.getBoundingClientRect().width > Math.max(0, compactWidth) |
| 65 | + ) |
| 66 | + } |
| 67 | + measure() |
| 68 | + const observer = new ResizeObserver(measure) |
| 69 | + for (const element of [bar, leading, trailing]) observer.observe(element) |
| 70 | + return () => observer.disconnect() |
| 71 | + }, [value]) |
| 72 | + |
| 73 | + return ( |
| 74 | + <div |
| 75 | + ref={scrollerRef} |
| 76 | + className={cn( |
| 77 | + 'relative min-h-[46px] w-full rounded-[23px] border border-[var(--border-1)] bg-[var(--white)] py-[7px] pr-2.5 pl-4 dark:bg-[var(--surface-4)]', |
| 78 | + floating && 'shadow-ambient' |
| 79 | + )} |
| 80 | + > |
| 81 | + <InputToolbar |
| 82 | + leadingRef={leadingRef} |
| 83 | + trailingRef={trailingRef} |
| 84 | + leadingControls={ |
| 85 | + leadingControls ?? <Search className='size-[16px] shrink-0 text-[var(--text-icon)]' /> |
| 86 | + } |
| 87 | + trailingControls={trailingControls} |
| 88 | + expanded={expanded} |
| 89 | + editor={ |
| 90 | + <GrowingTextarea |
| 91 | + inputRef={inputRef} |
| 92 | + scrollerRef={scrollerRef} |
| 93 | + value={value} |
| 94 | + compact={!expanded} |
| 95 | + maxHeight={200} |
| 96 | + onChange={(event) => onChange(event.target.value)} |
| 97 | + onKeyDown={(event) => { |
| 98 | + if (event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing) { |
| 99 | + event.preventDefault() |
| 100 | + onSubmit() |
| 101 | + } |
| 102 | + }} |
| 103 | + onPaste={onPaste} |
| 104 | + placeholder={placeholder} |
| 105 | + aria-label={ariaLabel} |
| 106 | + autoComplete='off' |
| 107 | + spellCheck={false} |
| 108 | + /> |
| 109 | + } |
| 110 | + /> |
| 111 | + <div aria-hidden className='pointer-events-none absolute h-0 w-0 overflow-hidden'> |
| 112 | + <span ref={textMeasureRef} className={cn(PROMPT_TEXT_CLASSES, 'whitespace-pre')}> |
| 113 | + {value} |
| 114 | + </span> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + ) |
| 118 | +} |
0 commit comments