2022-03-18 00:24:25 +00:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
2022-02-07 00:38:48 +00:00
|
|
|
import Link from "next/link";
|
2022-03-18 00:24:25 +00:00
|
|
|
import MiniSearch from "minisearch";
|
2022-08-28 06:27:53 +00:00
|
|
|
import ReactPaginate from "react-paginate";
|
2022-02-07 00:38:48 +00:00
|
|
|
|
2022-03-18 00:24:25 +00:00
|
|
|
import styles from "../styles/CellList.module.css";
|
2022-02-07 00:38:48 +00:00
|
|
|
import type { CellCoord } from "./ModData";
|
|
|
|
|
2022-08-28 06:27:53 +00:00
|
|
|
const PAGE_SIZE = 100;
|
|
|
|
|
2022-02-07 00:38:48 +00:00
|
|
|
type Props = {
|
|
|
|
cells: CellCoord[];
|
|
|
|
};
|
|
|
|
|
2022-03-15 01:55:52 +00:00
|
|
|
const CellList: React.FC<Props> = ({ cells }) => {
|
2022-03-18 00:24:25 +00:00
|
|
|
const cellSearch = useRef<MiniSearch<CellCoord> | null>(
|
|
|
|
null
|
|
|
|
) as React.MutableRefObject<MiniSearch<CellCoord>>;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
cellSearch.current = new MiniSearch({
|
|
|
|
fields: ["id"],
|
|
|
|
storeFields: ["id", "x", "y"],
|
|
|
|
tokenize: (s) => [s.replace(/cell\s?|\s/gi, "")],
|
|
|
|
searchOptions: {
|
|
|
|
fields: ["id"],
|
|
|
|
prefix: true,
|
|
|
|
fuzzy: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
cellSearch.current.addAll(
|
|
|
|
cells.map((cell) => ({ ...cell, id: `${cell.x},${cell.y}` }))
|
|
|
|
);
|
|
|
|
}, [cells]);
|
|
|
|
|
|
|
|
const [filter, setFilter] = useState<string>("");
|
|
|
|
const [filterResults, setFilterResults] = useState<Set<string>>(new Set());
|
2022-08-28 06:27:53 +00:00
|
|
|
const [page, setPage] = useState<number>(0);
|
2022-03-18 00:24:25 +00:00
|
|
|
|
|
|
|
const filteredCells = cells
|
|
|
|
.filter((cell) => !filter || filterResults.has(`${cell.x},${cell.y}`))
|
|
|
|
.sort((a, b) => (a.x - b.x) * 1000 + a.y - b.y);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (cellSearch.current) {
|
|
|
|
setFilterResults(
|
|
|
|
new Set(cellSearch.current.search(filter).map((result) => result.id))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [filter]);
|
|
|
|
|
2022-08-28 06:27:53 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setPage(0);
|
|
|
|
}, [filterResults]);
|
|
|
|
|
2022-08-29 01:51:56 +00:00
|
|
|
const renderPagination = () => (
|
|
|
|
<ReactPaginate
|
|
|
|
breakLabel="..."
|
|
|
|
nextLabel=">"
|
|
|
|
forcePage={page}
|
|
|
|
onPageChange={(event) => {
|
|
|
|
setPage(event.selected);
|
2022-08-29 03:43:44 +00:00
|
|
|
document.getElementById("exterior-cells")?.scrollIntoView();
|
2022-08-29 01:51:56 +00:00
|
|
|
}}
|
|
|
|
pageRangeDisplayed={3}
|
|
|
|
marginPagesDisplayed={2}
|
|
|
|
pageCount={Math.ceil(filteredCells.length / PAGE_SIZE)}
|
|
|
|
previousLabel="<"
|
|
|
|
renderOnZeroPageCount={() => null}
|
|
|
|
className={styles.pagination}
|
|
|
|
activeClassName={styles["active-page"]}
|
|
|
|
hrefBuilder={() => "#"}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2022-02-07 00:38:48 +00:00
|
|
|
return (
|
2022-03-18 00:24:25 +00:00
|
|
|
filteredCells && (
|
2022-02-07 00:38:48 +00:00
|
|
|
<>
|
2022-08-29 03:43:44 +00:00
|
|
|
<h2 id="exterior-cells">Exterior Cells ({filteredCells.length})</h2>
|
2022-03-18 00:24:25 +00:00
|
|
|
<div className={styles.filters}>
|
|
|
|
<hr />
|
|
|
|
<div className={styles["filter-row"]}>
|
|
|
|
<label htmlFor="filter">Filter:</label>
|
|
|
|
<input
|
|
|
|
type="search"
|
|
|
|
id="filter"
|
|
|
|
className={styles.filter}
|
|
|
|
value={filter}
|
|
|
|
onChange={(event) => setFilter(event.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<hr />
|
|
|
|
</div>
|
2022-08-29 01:51:56 +00:00
|
|
|
{renderPagination()}
|
2022-02-07 00:38:48 +00:00
|
|
|
<ul className={styles["cell-list"]}>
|
2022-08-28 06:27:53 +00:00
|
|
|
{filteredCells
|
|
|
|
.slice(page * PAGE_SIZE, page * PAGE_SIZE + PAGE_SIZE)
|
|
|
|
.map((cell) => (
|
|
|
|
<li
|
|
|
|
key={`cell-${cell.x},${cell.y}`}
|
|
|
|
className={styles["cell-list-item"]}
|
|
|
|
>
|
|
|
|
<div className={styles["cell-title"]}>
|
|
|
|
<strong>
|
|
|
|
<Link
|
|
|
|
href={`/?cell=${encodeURIComponent(
|
|
|
|
`${cell.x},${cell.y}`
|
|
|
|
)}`}
|
|
|
|
>
|
|
|
|
<a>
|
|
|
|
{cell.x}, {cell.y}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</strong>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
))}
|
2022-02-07 00:38:48 +00:00
|
|
|
</ul>
|
2022-08-29 01:51:56 +00:00
|
|
|
{renderPagination()}
|
2022-02-07 00:38:48 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-15 01:55:52 +00:00
|
|
|
export default CellList;
|