@@ -12,7 +12,6 @@ import {
1212import { Search } from 'lucide-react'
1313import { cn } from '@/lib/utils'
1414
15- // Context for the command component
1615type CommandContextType = {
1716 searchQuery : string
1817 setSearchQuery : ( value : string ) => void
@@ -26,7 +25,6 @@ type CommandContextType = {
2625
2726const CommandContext = createContext < CommandContextType | undefined > ( undefined )
2827
29- // Hook to use the command context
3028const useCommandContext = ( ) => {
3129 const context = useContext ( CommandContext )
3230 if ( ! context ) {
@@ -35,7 +33,6 @@ const useCommandContext = () => {
3533 return context
3634}
3735
38- // Types for the components
3936interface CommandProps {
4037 children : ReactNode
4138 className ?: string
@@ -76,17 +73,14 @@ interface CommandSeparatorProps {
7673 className ?: string
7774}
7875
79- // Main Command component
8076export function Command ( { children, className, filter } : CommandProps ) {
8177 const [ searchQuery , setSearchQuery ] = useState ( '' )
8278 const [ activeIndex , setActiveIndex ] = useState ( - 1 )
8379 const [ items , setItems ] = useState < string [ ] > ( [ ] )
8480 const [ filteredItems , setFilteredItems ] = useState < string [ ] > ( [ ] )
8581
86- // Register and unregister items - memoize to prevent infinite loops
8782 const registerItem = useCallback ( ( id : string ) => {
8883 setItems ( ( prev ) => {
89- // Only add if not already in the array
9084 if ( prev . includes ( id ) ) return prev
9185 return [ ...prev , id ]
9286 } )
@@ -96,7 +90,6 @@ export function Command({ children, className, filter }: CommandProps) {
9690 setItems ( ( prev ) => prev . filter ( ( item ) => item !== id ) )
9791 } , [ ] )
9892
99- // Handle item selection
10093 const selectItem = useCallback (
10194 ( id : string ) => {
10295 const index = filteredItems . indexOf ( id )
@@ -107,7 +100,6 @@ export function Command({ children, className, filter }: CommandProps) {
107100 [ filteredItems ]
108101 )
109102
110- // Filter items based on search query
111103 useEffect ( ( ) => {
112104 if ( ! searchQuery ) {
113105 setFilteredItems ( items )
@@ -127,7 +119,6 @@ export function Command({ children, className, filter }: CommandProps) {
127119 setActiveIndex ( filtered . length > 0 ? 0 : - 1 )
128120 } , [ searchQuery , items , filter ] )
129121
130- // Default filter function
131122 const defaultFilter = useCallback ( ( value : string , search : string ) : number => {
132123 const normalizedValue = value . toLowerCase ( )
133124 const normalizedSearch = search . toLowerCase ( )
@@ -138,7 +129,6 @@ export function Command({ children, className, filter }: CommandProps) {
138129 return 0
139130 } , [ ] )
140131
141- // Handle keyboard navigation
142132 const handleKeyDown = useCallback (
143133 ( e : React . KeyboardEvent ) => {
144134 if ( filteredItems . length === 0 ) return
@@ -163,7 +153,6 @@ export function Command({ children, className, filter }: CommandProps) {
163153 [ filteredItems , activeIndex ]
164154 )
165155
166- // Memoize context value to prevent unnecessary re-renders
167156 const contextValue = useMemo (
168157 ( ) => ( {
169158 searchQuery,
@@ -193,7 +182,6 @@ export function Command({ children, className, filter }: CommandProps) {
193182 )
194183}
195184
196- // Command Input component
197185export function CommandInput ( {
198186 placeholder = 'Search...' ,
199187 className,
@@ -208,7 +196,6 @@ export function CommandInput({
208196 onValueChange ?.( value )
209197 }
210198
211- // Focus input on mount
212199 useEffect ( ( ) => {
213200 inputRef . current ?. focus ( )
214201 } , [ ] )
@@ -230,7 +217,6 @@ export function CommandInput({
230217 )
231218}
232219
233- // Command List component
234220export function CommandList ( { children, className } : CommandListProps ) {
235221 return (
236222 < div className = { cn ( 'max-h-[300px] overflow-y-auto overflow-x-hidden' , className ) } >
@@ -239,7 +225,6 @@ export function CommandList({ children, className }: CommandListProps) {
239225 )
240226}
241227
242- // Command Empty component
243228export function CommandEmpty ( { children, className } : CommandEmptyProps ) {
244229 const { filteredItems } = useCommandContext ( )
245230
@@ -252,7 +237,6 @@ export function CommandEmpty({ children, className }: CommandEmptyProps) {
252237 )
253238}
254239
255- // Command Group component
256240export function CommandGroup ( { children, className, heading } : CommandGroupProps ) {
257241 return (
258242 < div
@@ -269,7 +253,6 @@ export function CommandGroup({ children, className, heading }: CommandGroupProps
269253 )
270254}
271255
272- // Command Item component
273256export function CommandItem ( {
274257 children,
275258 className,
@@ -281,16 +264,13 @@ export function CommandItem({
281264 const isActive = filteredItems . indexOf ( value ) === activeIndex
282265 const [ isHovered , setIsHovered ] = useState ( false )
283266
284- // Register and unregister item
285267 useEffect ( ( ) => {
286- // Only register if value is defined
287268 if ( value ) {
288269 registerItem ( value )
289270 return ( ) => unregisterItem ( value )
290271 }
291272 } , [ value , registerItem , unregisterItem ] )
292273
293- // Check if item should be displayed based on search
294274 const shouldDisplay = filteredItems . includes ( value )
295275
296276 if ( ! shouldDisplay ) return null
@@ -315,12 +295,10 @@ export function CommandItem({
315295 )
316296}
317297
318- // Command Separator component
319298export function CommandSeparator ( { className } : CommandSeparatorProps ) {
320299 return < div className = { cn ( '-mx-1 h-px bg-border' , className ) } />
321300}
322301
323- // Export all components
324302export const ToolCommand = {
325303 Root : Command ,
326304 Input : CommandInput ,
0 commit comments