2022-08-27 21:01:16 +00:00
|
|
|
import React, { createContext, useEffect, useRef, useState } from "react";
|
|
|
|
import MiniSearch from "minisearch";
|
|
|
|
import useSWRImmutable from "swr/immutable";
|
|
|
|
|
|
|
|
import { jsonFetcher } from "../lib/api";
|
2022-09-07 16:50:30 +00:00
|
|
|
import type { GameName } from "../lib/games";
|
2022-08-27 21:01:16 +00:00
|
|
|
|
|
|
|
interface Mod {
|
|
|
|
name: string;
|
|
|
|
id: number;
|
|
|
|
}
|
|
|
|
|
2022-09-03 06:51:57 +00:00
|
|
|
interface ModWithGame {
|
|
|
|
name: string;
|
|
|
|
id: number;
|
|
|
|
game: GameName;
|
|
|
|
}
|
|
|
|
|
2022-08-27 21:01:16 +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"],
|
|
|
|
tokenize: (s) => [s.replace(/(cell\s?)|\s/gi, "")],
|
|
|
|
searchOptions: {
|
|
|
|
fields: ["id"],
|
|
|
|
prefix: true,
|
|
|
|
fuzzy: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
cellSearch.addAll(cells);
|
|
|
|
|
|
|
|
type SearchContext = {
|
|
|
|
cellSearch: MiniSearch;
|
|
|
|
modSearch?: MiniSearch;
|
|
|
|
loading: boolean;
|
|
|
|
loadError?: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const SearchContext = createContext<SearchContext>({
|
|
|
|
cellSearch,
|
|
|
|
loading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const SearchProvider: React.FC = ({ children }) => {
|
2022-09-03 06:51:57 +00:00
|
|
|
const modSearch = useRef<MiniSearch<ModWithGame>>(
|
|
|
|
new MiniSearch({
|
|
|
|
fields: ["name"],
|
|
|
|
storeFields: ["name", "id", "game"],
|
|
|
|
searchOptions: {
|
|
|
|
fields: ["name"],
|
|
|
|
fuzzy: 0.2,
|
|
|
|
prefix: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
) as React.MutableRefObject<MiniSearch<ModWithGame>>;
|
2022-08-27 21:01:16 +00:00
|
|
|
const [loading, setLoading] = useState(true);
|
2022-09-03 06:51:57 +00:00
|
|
|
const [skyrimLoading, setSkyrimLoading] = useState(true);
|
|
|
|
const [skyrimspecialEditionLoading, setSkyrimspecialeditionLoading] =
|
|
|
|
useState(true);
|
2022-08-27 21:01:16 +00:00
|
|
|
|
2022-09-03 06:51:57 +00:00
|
|
|
const { data: skyrimData, error: skyrimError } = useSWRImmutable(
|
2022-09-03 19:41:44 +00:00
|
|
|
`https://mods.modmapper.com/skyrim/mod_search_index.json`,
|
2022-09-03 06:51:57 +00:00
|
|
|
(_) => jsonFetcher<Mod[]>(_, { notFoundOk: false })
|
2022-08-27 21:01:16 +00:00
|
|
|
);
|
2022-09-03 06:51:57 +00:00
|
|
|
const { data: skyrimspecialeditionData, error: skyrimspecialeditionError } =
|
|
|
|
useSWRImmutable(
|
2022-09-03 19:41:44 +00:00
|
|
|
`https://mods.modmapper.com/skyrimspecialedition/mod_search_index.json`,
|
2022-09-03 06:51:57 +00:00
|
|
|
(_) => jsonFetcher<Mod[]>(_, { notFoundOk: false })
|
|
|
|
);
|
2022-08-27 21:01:16 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-09-03 06:51:57 +00:00
|
|
|
if (skyrimData) {
|
|
|
|
modSearch.current
|
|
|
|
.addAllAsync(skyrimData.map((mod) => ({ ...mod, game: "skyrim" })))
|
|
|
|
.then(() => {
|
|
|
|
setSkyrimLoading(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [skyrimData]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (skyrimspecialeditionData) {
|
|
|
|
modSearch.current
|
|
|
|
.addAllAsync(
|
|
|
|
skyrimspecialeditionData.map((mod) => ({
|
|
|
|
...mod,
|
|
|
|
game: "skyrimspecialedition",
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
setSkyrimspecialeditionLoading(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [skyrimspecialeditionData]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-09-03 19:41:44 +00:00
|
|
|
if (
|
|
|
|
(!skyrimLoading || skyrimError) &&
|
|
|
|
(!skyrimspecialEditionLoading || skyrimspecialeditionError)
|
|
|
|
) {
|
2022-09-03 06:51:57 +00:00
|
|
|
setLoading(false);
|
2022-08-27 21:01:16 +00:00
|
|
|
}
|
2022-09-03 19:41:44 +00:00
|
|
|
}, [
|
|
|
|
skyrimLoading,
|
|
|
|
skyrimError,
|
|
|
|
skyrimspecialEditionLoading,
|
|
|
|
skyrimspecialeditionError,
|
|
|
|
]);
|
2022-08-27 21:01:16 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SearchContext.Provider
|
|
|
|
value={{
|
|
|
|
modSearch: modSearch.current,
|
|
|
|
cellSearch,
|
|
|
|
loading,
|
2022-09-03 06:51:57 +00:00
|
|
|
loadError: skyrimspecialeditionError || skyrimError,
|
2022-08-27 21:01:16 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</SearchContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SearchProvider;
|