2022-02-07 01:45:18 +00:00
|
|
|
import { useCombobox } from "downshift";
|
2022-01-24 05:59:36 +00:00
|
|
|
import React, { useEffect, useState, useRef } from "react";
|
|
|
|
import { useRouter } from "next/router";
|
2022-01-25 05:52:52 +00:00
|
|
|
import MiniSearch, { SearchResult } from "minisearch";
|
2022-01-25 05:31:25 +00:00
|
|
|
import useSWRImmutable from "swr/immutable";
|
2022-01-24 05:59:36 +00:00
|
|
|
|
|
|
|
import styles from "../styles/SearchBar.module.css";
|
2022-03-18 05:06:20 +00:00
|
|
|
import { jsonFetcher } from "../lib/api";
|
2022-01-24 05:59:36 +00:00
|
|
|
|
|
|
|
type Props = {
|
2022-01-30 21:55:50 +00:00
|
|
|
counts: Record<number, [number, number, number]> | null;
|
2022-02-27 06:17:52 +00:00
|
|
|
sidebarOpen: boolean;
|
2022-01-24 05:59:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
interface Mod {
|
2022-01-25 05:31:25 +00:00
|
|
|
name: string;
|
|
|
|
id: number;
|
2022-01-24 05:59:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 03:55:58 +00:00
|
|
|
let cells = [];
|
|
|
|
|
|
|
|
for (let x = -77; x < 76; x++) {
|
|
|
|
for (let y = -50; y < 45; y++) {
|
|
|
|
const id = `${x},${y}`;
|
|
|
|
cells.push({ id, name: `Cell ${id}`, x, y });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const cellSearch = new MiniSearch({
|
|
|
|
fields: ["id"],
|
|
|
|
storeFields: ["id", "name", "x", "y"],
|
2022-03-23 15:16:25 +00:00
|
|
|
tokenize: (s) => [s.replace(/(cell\s?)|\s/gi, "")],
|
2022-02-07 03:55:58 +00:00
|
|
|
searchOptions: {
|
|
|
|
fields: ["id"],
|
|
|
|
prefix: true,
|
|
|
|
fuzzy: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
cellSearch.addAll(cells);
|
|
|
|
|
2022-02-27 06:17:52 +00:00
|
|
|
const SearchBar: React.FC<Props> = ({ counts, sidebarOpen }) => {
|
2022-01-24 05:59:36 +00:00
|
|
|
const router = useRouter();
|
2022-01-25 05:31:25 +00:00
|
|
|
|
2022-02-07 03:55:58 +00:00
|
|
|
const modSearch = useRef<MiniSearch<Mod> | null>(
|
2022-01-25 05:52:52 +00:00
|
|
|
null
|
|
|
|
) as React.MutableRefObject<MiniSearch<Mod>>;
|
2022-01-25 05:31:25 +00:00
|
|
|
|
|
|
|
const { data, error } = useSWRImmutable(
|
|
|
|
`https://mods.modmapper.com/mod_search_index.json`,
|
2022-03-18 05:06:20 +00:00
|
|
|
(_) => jsonFetcher<Mod[]>(_)
|
2022-01-25 05:31:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-07 03:55:58 +00:00
|
|
|
if (data && !modSearch.current) {
|
|
|
|
modSearch.current = new MiniSearch({
|
2022-01-25 05:52:52 +00:00
|
|
|
fields: ["name"],
|
|
|
|
storeFields: ["name", "id"],
|
|
|
|
searchOptions: {
|
|
|
|
fields: ["name"],
|
|
|
|
fuzzy: 0.2,
|
2022-02-07 15:43:51 +00:00
|
|
|
prefix: true,
|
2022-01-25 05:52:52 +00:00
|
|
|
},
|
|
|
|
});
|
2022-03-18 05:06:20 +00:00
|
|
|
modSearch.current.addAll(data);
|
2022-01-25 05:31:25 +00:00
|
|
|
}
|
|
|
|
}, [data]);
|
2022-01-24 05:59:36 +00:00
|
|
|
|
|
|
|
const searchInput = useRef<HTMLInputElement | null>(null);
|
|
|
|
const [searchFocused, setSearchFocused] = useState<boolean>(false);
|
|
|
|
const [results, setResults] = useState<SearchResult[]>([]);
|
|
|
|
|
2022-02-07 01:45:18 +00:00
|
|
|
const {
|
|
|
|
isOpen,
|
|
|
|
getMenuProps,
|
|
|
|
getInputProps,
|
|
|
|
getComboboxProps,
|
|
|
|
highlightedIndex,
|
|
|
|
getItemProps,
|
|
|
|
reset,
|
|
|
|
} = useCombobox({
|
|
|
|
items: results,
|
|
|
|
itemToString: (item) => (item ? item.name : ""),
|
|
|
|
onInputValueChange: ({ inputValue }) => {
|
2022-02-07 03:55:58 +00:00
|
|
|
if (inputValue) {
|
|
|
|
let results: SearchResult[] = [];
|
2022-03-23 15:16:25 +00:00
|
|
|
if (
|
|
|
|
modSearch.current &&
|
|
|
|
!/(^cell\s?-?\d+\s?,?\s?-?\d*$)|(^-?\d+\s?,\s?-?\d*$)/i.test(
|
|
|
|
inputValue
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
console.log("searching mods");
|
2022-02-07 03:55:58 +00:00
|
|
|
results = results.concat(
|
|
|
|
modSearch.current.search(inputValue).sort((resultA, resultB) => {
|
|
|
|
if (counts) {
|
|
|
|
const countA = counts[resultA.id];
|
|
|
|
const countB = counts[resultB.id];
|
2022-02-07 15:43:51 +00:00
|
|
|
const scoreA = resultA.score;
|
|
|
|
const scoreB = resultB.score;
|
|
|
|
if (countA && countB && scoreA && scoreB) {
|
|
|
|
if (scoreA === scoreB) {
|
|
|
|
return countB[2] - countA[2];
|
|
|
|
} else {
|
|
|
|
return scoreB - scoreA;
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 03:55:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
results = results.concat(cellSearch.search(inputValue));
|
|
|
|
setResults(results.splice(0, 30));
|
2022-01-24 05:59:36 +00:00
|
|
|
}
|
2022-02-07 01:45:18 +00:00
|
|
|
},
|
|
|
|
onSelectedItemChange: ({ selectedItem }) => {
|
|
|
|
if (selectedItem) {
|
|
|
|
setSearchFocused(false);
|
2022-03-23 15:16:25 +00:00
|
|
|
if (selectedItem.x !== undefined && selectedItem.y !== undefined) {
|
2022-02-07 03:55:58 +00:00
|
|
|
router.push({
|
|
|
|
query: { cell: `${selectedItem.x},${selectedItem.y}` },
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
router.push({ query: { mod: selectedItem.id } });
|
|
|
|
}
|
2022-02-07 01:45:18 +00:00
|
|
|
if (searchInput.current) searchInput.current.blur();
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2022-01-24 05:59:36 +00:00
|
|
|
|
|
|
|
return (
|
2022-02-07 01:45:18 +00:00
|
|
|
<>
|
|
|
|
<div
|
|
|
|
className={`${styles["search-bar"]} ${
|
|
|
|
searchFocused ? styles["search-bar-focused"] : ""
|
2022-02-27 06:17:52 +00:00
|
|
|
} ${sidebarOpen ? styles["search-bar-sidebar-open"] : ""}`}
|
2022-02-07 01:45:18 +00:00
|
|
|
{...getComboboxProps()}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
{...getInputProps({
|
|
|
|
type: "text",
|
|
|
|
placeholder: "Search mods or cells…",
|
|
|
|
onFocus: () => setSearchFocused(true),
|
|
|
|
onBlur: () => {
|
|
|
|
if (!isOpen) setSearchFocused(false);
|
|
|
|
},
|
|
|
|
disabled: !data,
|
|
|
|
ref: searchInput,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<ul
|
|
|
|
className={styles["search-results"]}
|
|
|
|
style={!isOpen ? { display: "none" } : {}}
|
|
|
|
{...getMenuProps()}
|
|
|
|
>
|
|
|
|
{isOpen &&
|
2022-02-07 03:55:58 +00:00
|
|
|
results.map((result, index) => (
|
|
|
|
<li
|
|
|
|
key={result.id}
|
|
|
|
{...getItemProps({ item: result, index })}
|
|
|
|
className={`${
|
|
|
|
highlightedIndex === index ? styles["highlighted-result"] : ""
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{result.name}
|
|
|
|
</li>
|
|
|
|
))}
|
2022-01-24 05:59:36 +00:00
|
|
|
</ul>
|
2022-02-07 01:45:18 +00:00
|
|
|
</div>
|
|
|
|
</>
|
2022-01-24 05:59:36 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SearchBar;
|