From 311a66c5b03de211b3cacdf5c50695f64e270b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Rafael=20Siqueira?= Date: Fri, 25 Apr 2025 16:51:25 -0300 Subject: [PATCH] add fuse --- superpage/astro.config.mjs | 2 - superpage/package-lock.json | 17 ++++++ superpage/package.json | 2 + superpage/src/components/Carousel.astro | 3 +- superpage/src/components/Course/Contact.astro | 2 +- .../Course/{_Contact.jsx => Contact.jsx} | 0 ...as-praticas-em-manipulcao-de-alimentos.mdx | 3 +- .../content/courses/cipa-grau-de-risco-1.mdx | 1 - .../content/courses/cipa-grau-de-risco-2.mdx | 1 - .../content/courses/cipa-grau-de-risco-3.mdx | 2 - superpage/src/pages/_components/Search.astro | 10 ++++ superpage/src/pages/_components/Search.jsx | 60 +++++++++++++++++++ superpage/src/pages/index.astro | 12 ++-- 13 files changed, 97 insertions(+), 18 deletions(-) rename superpage/src/components/Course/{_Contact.jsx => Contact.jsx} (100%) create mode 100644 superpage/src/pages/_components/Search.astro create mode 100644 superpage/src/pages/_components/Search.jsx diff --git a/superpage/astro.config.mjs b/superpage/astro.config.mjs index 3e78ad2..44032ee 100644 --- a/superpage/astro.config.mjs +++ b/superpage/astro.config.mjs @@ -6,9 +6,7 @@ import partytown from "@astrojs/partytown"; import sitemap from "@astrojs/sitemap"; import alpinejs from "@astrojs/alpinejs"; import icon from "astro-icon"; - import react from "@astrojs/react"; - import mdx from "@astrojs/mdx"; // https://astro.build/config diff --git a/superpage/package-lock.json b/superpage/package-lock.json index c513c71..5015eaa 100644 --- a/superpage/package-lock.json +++ b/superpage/package-lock.json @@ -22,6 +22,8 @@ "astro": "^5.7.2", "astro-icon": "^1.1.5", "clsx": "^2.1.1", + "fuse.js": "^7.1.0", + "meilisearch": "^0.50.0", "react": "^19.1.0", "react-dom": "^19.1.0", "react-hook-form": "^7.56.0", @@ -3858,6 +3860,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/fuse.js": { + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-7.1.0.tgz", + "integrity": "sha512-trLf4SzuuUxfusZADLINj+dE8clK1frKdmqiJNb1Es75fmI5oY6X2mxLVUciLLjxqw/xr72Dhy+lER6dGd02FQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=10" + } + }, "node_modules/gensync": { "version": "1.0.0-beta.2", "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", @@ -5145,6 +5156,12 @@ "integrity": "sha512-IEn+pegP1aManZuckezWCO+XZQDplx1366JoVhTpMpBB1sPey/SbveZQUosKiKiGYjg1wH4pMlNgXbCiYgihQA==", "license": "CC0-1.0" }, + "node_modules/meilisearch": { + "version": "0.50.0", + "resolved": "https://registry.npmjs.org/meilisearch/-/meilisearch-0.50.0.tgz", + "integrity": "sha512-9IzIkobvnuS18Eg4dq/eJB9W+eXqeLZjNRgq/kKMswSmVYYSQsXqGgSuCA0JkF+o5RwJlwIsieQee6rh313VhA==", + "license": "MIT" + }, "node_modules/micromark": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/micromark/-/micromark-4.0.2.tgz", diff --git a/superpage/package.json b/superpage/package.json index 37e3b24..e65b0dc 100644 --- a/superpage/package.json +++ b/superpage/package.json @@ -23,6 +23,8 @@ "astro": "^5.7.2", "astro-icon": "^1.1.5", "clsx": "^2.1.1", + "fuse.js": "^7.1.0", + "meilisearch": "^0.50.0", "react": "^19.1.0", "react-dom": "^19.1.0", "react-hook-form": "^7.56.0", diff --git a/superpage/src/components/Carousel.astro b/superpage/src/components/Carousel.astro index ae5c968..4833d3a 100644 --- a/superpage/src/components/Carousel.astro +++ b/superpage/src/components/Carousel.astro @@ -30,8 +30,7 @@ const { items = [] } = Astro.props; class="snap-center flex-shrink-0 max-lg:w-full flex justify-center itens-center" id={`slide${idx + 1}`} > - - + )) } diff --git a/superpage/src/components/Course/Contact.astro b/superpage/src/components/Course/Contact.astro index d6de7e3..5224030 100644 --- a/superpage/src/components/Course/Contact.astro +++ b/superpage/src/components/Course/Contact.astro @@ -1,6 +1,6 @@ --- import Container from "~/components/Container.astro"; -import Form from "./_Contact.jsx"; +import Form from "./Contact.jsx"; --- diff --git a/superpage/src/components/Course/_Contact.jsx b/superpage/src/components/Course/Contact.jsx similarity index 100% rename from superpage/src/components/Course/_Contact.jsx rename to superpage/src/components/Course/Contact.jsx diff --git a/superpage/src/content/courses/boas-praticas-em-manipulcao-de-alimentos.mdx b/superpage/src/content/courses/boas-praticas-em-manipulcao-de-alimentos.mdx index c276569..576156d 100644 --- a/superpage/src/content/courses/boas-praticas-em-manipulcao-de-alimentos.mdx +++ b/superpage/src/content/courses/boas-praticas-em-manipulcao-de-alimentos.mdx @@ -3,9 +3,8 @@ id: 386f7086-2871-436f-85f5-31d632fbf624 title: Boas Práticas em Manipulação de Alimentos slug: boas-praticas-em-manipulcao-de-alimentos excerpt: ... -draft: true +draft: false course: hours: 8 - trainer: francis --- diff --git a/superpage/src/content/courses/cipa-grau-de-risco-1.mdx b/superpage/src/content/courses/cipa-grau-de-risco-1.mdx index 85167c2..e6027b3 100644 --- a/superpage/src/content/courses/cipa-grau-de-risco-1.mdx +++ b/superpage/src/content/courses/cipa-grau-de-risco-1.mdx @@ -7,5 +7,4 @@ draft: false course: hours: 8 - trainer: francis --- diff --git a/superpage/src/content/courses/cipa-grau-de-risco-2.mdx b/superpage/src/content/courses/cipa-grau-de-risco-2.mdx index d4f9f4c..e375c90 100644 --- a/superpage/src/content/courses/cipa-grau-de-risco-2.mdx +++ b/superpage/src/content/courses/cipa-grau-de-risco-2.mdx @@ -7,5 +7,4 @@ draft: false course: hours: 8 - trainer: francis --- diff --git a/superpage/src/content/courses/cipa-grau-de-risco-3.mdx b/superpage/src/content/courses/cipa-grau-de-risco-3.mdx index c2c0969..8e27f17 100644 --- a/superpage/src/content/courses/cipa-grau-de-risco-3.mdx +++ b/superpage/src/content/courses/cipa-grau-de-risco-3.mdx @@ -7,6 +7,4 @@ draft: false course: hours: 8 - trainer: francis - modules: [] --- diff --git a/superpage/src/pages/_components/Search.astro b/superpage/src/pages/_components/Search.astro new file mode 100644 index 0000000..7f2ab07 --- /dev/null +++ b/superpage/src/pages/_components/Search.astro @@ -0,0 +1,10 @@ +--- +import Search from "./Search"; +import { getCollection, render } from "astro:content"; +const courses = await getCollection( + "courses", + ({ data }) => data.draft != true, +); +--- + + diff --git a/superpage/src/pages/_components/Search.jsx b/superpage/src/pages/_components/Search.jsx new file mode 100644 index 0000000..7df9c65 --- /dev/null +++ b/superpage/src/pages/_components/Search.jsx @@ -0,0 +1,60 @@ +import { useState } from "react"; +import Fuse from "fuse.js"; + +// https://fusejs.io/api/options.html +const options = { + keys: ["data.title", "data.excerpt", "data.slug"], + includeMatches: true, + minMatchCharLength: 2, + threshold: 0.5, +}; + +export default function Search({ searchlist }) { + const [query, setQuery] = useState(""); + + const fuse = new Fuse(searchlist, options); + // Set a limit to the items: 5 + const items = fuse + .search(query) + .map((result) => result.item) + .slice(0, 5); + + const onChange = (e) => { + setQuery(e.target.value); + }; + + return ( + <> + + {query.length > 1 && ( +
+

+ Encontrado {items.length} resultado(s) para ' + {query}' +

+ + {items.length > 1 && ( + + )} +
+ )} + + ); +} diff --git a/superpage/src/pages/index.astro b/superpage/src/pages/index.astro index 879789e..b08f986 100644 --- a/superpage/src/pages/index.astro +++ b/superpage/src/pages/index.astro @@ -4,6 +4,7 @@ import { Icon } from "astro-icon/components"; import Layout from "~/layouts/Layout.astro"; import Container from "~/components/Container.astro"; import { Clients } from "~/components/Course"; +import Search from "./_components/Search.astro"; import mulhercomepi from "~/assets/mulher-com-epi.png"; @@ -19,7 +20,7 @@ let posts = await res.json(); >
-
+