1
0
mirror of https://github.com/barthuijgen/factorio-sites.git synced 2024-11-28 09:33:51 +02:00

feat: Make search options based on actual data

This commit is contained in:
Bart 2021-03-31 13:48:50 +02:00
parent a1e36e4099
commit 178c962c28
8 changed files with 69 additions and 8 deletions

View File

@ -13,17 +13,17 @@ SELECT distinct concat(type,'/',name) FROM blueprint, jsonb_to_recordset(data ->
### Unique entities
```
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'entities'))) as entities FROM blueprint
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'entities'))) as entity FROM blueprint
```
### Unique items
```
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'items'))) as entities FROM blueprint
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'items'))) as item FROM blueprint
```
### Unique recipes
```
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'recipes'))) as entities FROM blueprint
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'recipes'))) as recipe FROM blueprint
```

View File

@ -106,3 +106,15 @@ model user_favorites {
@@id([user_id, blueprint_page_id])
}
model blueprint_entities {
entity String @unique
}
model blueprint_items {
item String @unique
}
model blueprint_recipes {
recipe String @unique
}

View File

@ -0,0 +1,6 @@
CREATE OR REPLACE VIEW public.blueprint_entities
AS
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'entities'))) as entity FROM blueprint;
ALTER TABLE public.blueprint_entities
OWNER TO "factorio-blueprints";

View File

@ -0,0 +1,6 @@
CREATE OR REPLACE VIEW public.blueprint_items
AS
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'items'))) as item FROM blueprint;
ALTER TABLE public.blueprint_items
OWNER TO "factorio-blueprints";

View File

@ -0,0 +1,6 @@
CREATE OR REPLACE VIEW public.blueprint_recipes
AS
SELECT distinct UNNEST(ARRAY(SELECT jsonb_object_keys(data -> 'recipes'))) as recipe FROM blueprint;
ALTER TABLE public.blueprint_recipes
OWNER TO "factorio-blueprints";

View File

@ -0,0 +1,17 @@
import {
getBlueprintEntities,
getBlueprintItems,
getBlueprintRecipes,
} from "@factorio-sites/database";
import { apiHandler } from "../../utils/api-handler";
const handler = apiHandler(async (_, res) => {
const [entities, items, recipes] = await Promise.all([
getBlueprintEntities(),
getBlueprintItems(),
getBlueprintRecipes(),
]);
res.status(200).json({ entities, items, recipes });
});
export default handler;

View File

@ -32,6 +32,7 @@ import { TAGS } from "@factorio-sites/common-utils";
import { mq } from "@factorio-sites/web-utils";
import { useAuth } from "../providers/auth";
import { pageHandler } from "../utils/page-handler";
import { useFetch } from "../hooks/fetch";
const pageCss = css({
display: "flex",
@ -92,6 +93,9 @@ export const Index: NextPage<IndexProps> = ({
const [blueprints, setBlueprints] = useState<BlueprintPageWithUserFavorite[]>([]);
const routerQueryToHref = useRouterQueryToHref();
const data = useFbeData();
const searchOptions = useFetch<{ entities: string[]; items: string[]; recipes: string[] }>(
"/api/searchoptions"
);
useEffect(() => {
setSearchQuery((router.query.q as string) || "");
@ -103,11 +107,9 @@ export const Index: NextPage<IndexProps> = ({
if (!data) return null;
const entityOptions = Object.keys(data.entities).filter(
(key) => !key.startsWith("factorio-logo") && !key.startsWith("crash-site")
);
const recipeOptions = Object.keys(data.recipes);
const itemOptions = Object.keys(data.items).filter((key) => key.includes("module"));
const entityOptions = searchOptions.data?.entities || [];
const itemOptions = searchOptions.data?.items || [];
const recipeOptions = searchOptions.data?.recipes || [];
const tagsOptions = TAGS.map((tag) => ({
label: `${tag.category}: ${tag.label}`,
value: tag.value,

View File

@ -73,3 +73,15 @@ export async function createBlueprint(
return mapBlueprintInstanceToEntry(result);
}
export async function getBlueprintEntities() {
return (await prisma.blueprint_entities.findMany()).map((item) => item.entity);
}
export async function getBlueprintItems() {
return (await prisma.blueprint_items.findMany()).map((item) => item.item);
}
export async function getBlueprintRecipes() {
return (await prisma.blueprint_recipes.findMany()).map((item) => item.recipe);
}