|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useState, useEffect, useCallback } from "react"; |
| 4 | +import { |
| 5 | + Command, |
| 6 | + CommandEmpty, |
| 7 | + CommandGroup, |
| 8 | + CommandInput, |
| 9 | + CommandItem, |
| 10 | + CommandList, |
| 11 | +} from "@/components/ui/command"; |
| 12 | +import { |
| 13 | + Dialog, |
| 14 | + DialogContent, |
| 15 | + DialogDescription, |
| 16 | + DialogHeader, |
| 17 | + DialogTitle, |
| 18 | +} from "@/components/ui/dialog"; |
| 19 | +import { Button } from "@/components/ui/button"; |
| 20 | +import { Search, FileText, BookOpen, Loader2 } from "lucide-react"; |
| 21 | +import { ActiveFile } from "@/types/file"; |
| 22 | +import { searchFiles, FileSearchResult } from "@/lib/api/file"; |
| 23 | + |
| 24 | +interface AcademicsFileSearchProps { |
| 25 | + onFileSelect: (file: ActiveFile) => void; |
| 26 | +} |
| 27 | + |
| 28 | +export default function AcademicsFileSearch({ |
| 29 | + onFileSelect, |
| 30 | +}: AcademicsFileSearchProps) { |
| 31 | + const [open, setOpen] = useState(false); |
| 32 | + const [searchQuery, setSearchQuery] = useState(""); |
| 33 | + const [results, setResults] = useState<FileSearchResult[]>([]); |
| 34 | + const [isLoading, setIsLoading] = useState(false); |
| 35 | + |
| 36 | + // Debounced search |
| 37 | + useEffect(() => { |
| 38 | + if (!searchQuery.trim()) { |
| 39 | + setResults([]); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + setIsLoading(true); |
| 44 | + const timer = setTimeout(async () => { |
| 45 | + try { |
| 46 | + const data = await searchFiles({ |
| 47 | + query: searchQuery, |
| 48 | + page: 1, |
| 49 | + limit: 10, |
| 50 | + }); |
| 51 | + |
| 52 | + setResults(data.docs); |
| 53 | + } catch (error) { |
| 54 | + console.error("Search error:", error); |
| 55 | + setResults([]); |
| 56 | + } finally { |
| 57 | + setIsLoading(false); |
| 58 | + } |
| 59 | + }, 300); // 300ms debounce |
| 60 | + |
| 61 | + return () => clearTimeout(timer); |
| 62 | + }, [searchQuery]); |
| 63 | + |
| 64 | + // Keyboard shortcut |
| 65 | + useEffect(() => { |
| 66 | + const down = (e: KeyboardEvent) => { |
| 67 | + if (e.key === "k" && (e.metaKey || e.ctrlKey)) { |
| 68 | + e.preventDefault(); |
| 69 | + setOpen((open) => !open); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + document.addEventListener("keydown", down); |
| 74 | + return () => document.removeEventListener("keydown", down); |
| 75 | + }, []); |
| 76 | + |
| 77 | + const handleFileSelect = useCallback( |
| 78 | + (result: FileSearchResult) => { |
| 79 | + if (!result.driveFileId) { |
| 80 | + console.error("File does not have a valid ID:", result); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + onFileSelect({ |
| 85 | + id: result._id, |
| 86 | + driveId: result.driveFileId, |
| 87 | + fileName: result.fileName || "Unknown File", |
| 88 | + subject: result.subject.name, |
| 89 | + }); |
| 90 | + |
| 91 | + setOpen(false); |
| 92 | + setSearchQuery(""); |
| 93 | + setResults([]); |
| 94 | + }, |
| 95 | + [onFileSelect] |
| 96 | + ); |
| 97 | + |
| 98 | + const getCategoryLabel = (category: string, unitNumber?: number) => { |
| 99 | + if (category === "unit" && unitNumber) { |
| 100 | + return `Unit ${unitNumber}`; |
| 101 | + } |
| 102 | + if (category === "endsem") return "Previous Year - End-Sem"; |
| 103 | + if (category === "insem") return "Previous Year - In-Sem"; |
| 104 | + if (category === "decode") return "Decodes"; |
| 105 | + if (category === "book") return "Books"; |
| 106 | + return category; |
| 107 | + }; |
| 108 | + |
| 109 | + return ( |
| 110 | + <> |
| 111 | + <Button |
| 112 | + variant="outline" |
| 113 | + className="relative w-[200px] justify-start text-sm text-muted-foreground" |
| 114 | + onClick={() => setOpen(true)} |
| 115 | + > |
| 116 | + <Search className="mr-2 h-4 w-4" /> |
| 117 | + <span>Search files...</span> |
| 118 | + <kbd className="pointer-events-none absolute right-1.5 top-1.5 hidden h-6 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium opacity-100 sm:flex"> |
| 119 | + <span className="text-xs">⌘</span>K |
| 120 | + </kbd> |
| 121 | + </Button> |
| 122 | + <Dialog open={open} onOpenChange={setOpen}> |
| 123 | + <DialogContent className="p-0 max-w-2xl"> |
| 124 | + <DialogHeader className="px-4 pt-4 pb-2"> |
| 125 | + <DialogTitle>Search Files</DialogTitle> |
| 126 | + <DialogDescription> |
| 127 | + Search across all files globally |
| 128 | + </DialogDescription> |
| 129 | + </DialogHeader> |
| 130 | + <Command shouldFilter={false}> |
| 131 | + <CommandInput |
| 132 | + placeholder="Type to search files..." |
| 133 | + value={searchQuery} |
| 134 | + onValueChange={setSearchQuery} |
| 135 | + /> |
| 136 | + <CommandList className="max-h-[300px]"> |
| 137 | + {isLoading && ( |
| 138 | + <div className="flex items-center justify-center p-4"> |
| 139 | + <Loader2 className="h-5 w-5 animate-spin text-muted-foreground" /> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + {!isLoading && searchQuery && results.length === 0 && ( |
| 143 | + <CommandEmpty>No files found.</CommandEmpty> |
| 144 | + )} |
| 145 | + {!isLoading && results.length > 0 && ( |
| 146 | + <CommandGroup heading="Files"> |
| 147 | + {results.map((result) => ( |
| 148 | + <CommandItem |
| 149 | + key={result._id} |
| 150 | + onSelect={() => handleFileSelect(result)} |
| 151 | + className="flex items-start gap-2 px-3 py-2" |
| 152 | + > |
| 153 | + <FileText className="h-4 w-4 mt-0.5 shrink-0 text-muted-foreground" /> |
| 154 | + <div className="flex flex-col gap-0.5 flex-1 min-w-0"> |
| 155 | + <div className="font-medium truncate"> |
| 156 | + {result.fileName} |
| 157 | + </div> |
| 158 | + <div className="text-xs text-muted-foreground flex items-center gap-2"> |
| 159 | + <span className="truncate"> |
| 160 | + <BookOpen className="h-3 w-3 inline mr-1" /> |
| 161 | + {result.subject.code} - {result.subject.name} |
| 162 | + </span> |
| 163 | + <span className="shrink-0 text-[10px] bg-muted px-1.5 py-0.5 rounded"> |
| 164 | + {getCategoryLabel(result.category, result.unitNumber)} |
| 165 | + </span> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + </CommandItem> |
| 169 | + ))} |
| 170 | + </CommandGroup> |
| 171 | + )} |
| 172 | + </CommandList> |
| 173 | + </Command> |
| 174 | + </DialogContent> |
| 175 | + </Dialog> |
| 176 | + </> |
| 177 | + ); |
| 178 | +} |
0 commit comments