({ tracks }: { tracks: TrackPros[] })
| 21 | } |
| 22 | |
| 23 | export function ContentSearch({ tracks }: { tracks: TrackPros[] }) { |
| 24 | const [dialogOpen, setDialogOpen] = useState(false); |
| 25 | const [input, setInput] = useState(""); |
| 26 | const [searchTracks, setSearchTracks] = useState<any[]>([]); |
| 27 | const [selectedIndex, setSelectedIndex] = useState(-1); |
| 28 | const scrollableContainerRef = useRef<HTMLDivElement>(null); |
| 29 | const deferredInput = useDeferredValue(input); |
| 30 | const [allTracks, setAllTracks] = useState<DataItem[]>([]); |
| 31 | useEffect(() => { |
| 32 | const updatedTracks: DataItem[] = []; |
| 33 | tracks.map((t) => { |
| 34 | t.problems.map((p) => { |
| 35 | updatedTracks.push({ |
| 36 | payload: { |
| 37 | problemId: p.id, |
| 38 | trackTitle: t.title, |
| 39 | problemTitle: p.title, |
| 40 | trackId: t.id, |
| 41 | image: t.image, |
| 42 | }, |
| 43 | }); |
| 44 | }); |
| 45 | }); |
| 46 | setAllTracks(updatedTracks); |
| 47 | }, []); |
| 48 | useEffect(() => { |
| 49 | const fuse = new Fuse(allTracks, { |
| 50 | keys: ["payload.problemTitle"], |
| 51 | }); |
| 52 | |
| 53 | async function fetchSearchResults() { |
| 54 | if (deferredInput.length > 0) { |
| 55 | /* const data = await getSearchResults(deferredInput); */ |
| 56 | const data = fuse.search(deferredInput); |
| 57 | const items = data.map((result) => result.item); |
| 58 | setSearchTracks(items); |
| 59 | } else { |
| 60 | setSearchTracks([]); |
| 61 | } |
| 62 | } |
| 63 | fetchSearchResults(); |
| 64 | }, [deferredInput]); |
| 65 | |
| 66 | useEffect(() => { |
| 67 | const handleKeyPress = (event: KeyboardEvent) => { |
| 68 | switch (event.code) { |
| 69 | case "KeyK": |
| 70 | if (event.ctrlKey) { |
| 71 | event.preventDefault(); |
| 72 | setDialogOpen(true); |
| 73 | } |
| 74 | break; |
| 75 | case "ArrowDown": |
| 76 | event.preventDefault(); |
| 77 | setSelectedIndex((prevIndex) => (prevIndex + 1) % searchTracks.length); |
| 78 | break; |
| 79 | case "ArrowUp": |
| 80 | event.preventDefault(); |
nothing calls this directly
no test coverage detected