diff --git a/package-lock.json b/package-lock.json index 28abfcc..c9df8c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "dependencies": { "canvas-confetti": "^1.6.0", + "clsx": "^1.2.1", "postcss-flexbugs-fixes": "^5.0.2", "postcss-normalize": "^10.0.1", "postcss-preset-env": "^8.0.1", @@ -5483,6 +5484,14 @@ "wrap-ansi": "^7.0.0" } }, + "node_modules/clsx": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/clsx/-/clsx-1.2.1.tgz", + "integrity": "sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg==", + "engines": { + "node": ">=6" + } + }, "node_modules/co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -21731,6 +21740,11 @@ "wrap-ansi": "^7.0.0" } }, + "clsx": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/clsx/-/clsx-1.2.1.tgz", + "integrity": "sha512-EcR6r5a8bj6pu3ycsa/E/cKVGuTgZJZdsyUYHOksG/UHIiKfjxzRxYJpyVBwYaQeOvghal9fcc4PidlgzugAQg==" + }, "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", diff --git a/package.json b/package.json index 3558680..4530dc8 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "private": true, "dependencies": { "canvas-confetti": "^1.6.0", + "clsx": "^1.2.1", "postcss-flexbugs-fixes": "^5.0.2", "postcss-normalize": "^10.0.1", "postcss-preset-env": "^8.0.1", diff --git a/src/SearchBar.jsx b/src/SearchBar.jsx index 4fd0317..dde135a 100644 --- a/src/SearchBar.jsx +++ b/src/SearchBar.jsx @@ -1,3 +1,4 @@ +import clsx from "clsx"; import { useEffect, useState } from "react"; import SearchResults from "./SearchResults"; import { useSearchStore } from "./state/search"; @@ -6,8 +7,17 @@ const capitalPDangit = (query) => { return query.replace(/Wordpress/i, "WordPress"); }; +const types = { + "": "All", + wordpress_reference: "WordPress reference", + wordpress_dev_reference: "WordPress dev notes", + wpcli: "WP-CLI", + php_reference: "PHP reference", +}; + const SearchBar = () => { - const { search, setSearch, setSearchHistory } = useSearchStore(); + const { search, setSearch, setSearchHistory, type, setType } = + useSearchStore(); const [searchTerm, setSearchTerm] = useState(""); const [selectedResult, selectResult] = useState(null); @@ -31,7 +41,14 @@ const SearchBar = () => { return (
-
e.preventDefault()}> + e.preventDefault()} + > + { placeholder="Search for a WordPress function, hook, or class." autoFocus /> +
+

Filter by type:

+ {Object.entries(types).map(([key, value]) => ( + + ))} +
diff --git a/src/SearchResults.jsx b/src/SearchResults.jsx index 3af5803..4063a92 100644 --- a/src/SearchResults.jsx +++ b/src/SearchResults.jsx @@ -9,15 +9,15 @@ const decodeHTMLEntities = (text) => { return textArea.value; }; -const SearchResults = ({ query, selectedResult, selectResult }) => { +const SearchResults = ({ search, type, selectedResult, selectResult }) => { const [copyStatus, setCopyStatus] = useState(""); - const { data, error, loading } = useSearch(query); + const { data, error, loading } = useSearch({ search, type }); const copyToClipboard = async (text) => { return navigator.clipboard.writeText(text); }; - if (!query) return null; + if (!search) return null; if (error) { return

Failed to load, dangit.

; @@ -71,12 +71,12 @@ const SearchResults = ({ query, selectedResult, selectResult }) => { return (
- {query && query.length < 3 ? ( + {search && search.length < 3 ? (

Keep typing...

) : data.length === 0 ? ( -

No search results for: {query}

+

No search results for: {search}

) : ( -

Search results for: {query}

+

Search results for: {search}

)} {data && data?.length > 0 && (
diff --git a/src/hooks/useSearch.js b/src/hooks/useSearch.js index 174565e..34a7507 100644 --- a/src/hooks/useSearch.js +++ b/src/hooks/useSearch.js @@ -2,9 +2,9 @@ import useSWR from "swr"; const fetcher = (...args) => fetch(...args).then((res) => res.json()); const url = "https://heigl.docs-dang.it/api/docs"; -export const useSearch = (query) => { - const fullUrl = query ? `${url}?search=${query}` : url; - const { data, error } = useSWR(fullUrl, fetcher); +export const useSearch = ({ search, type }) => { + const query = new URLSearchParams({ search, type }); + const { data, error } = useSWR(`${url}?${query.toString()}`, fetcher); return { data, error, diff --git a/src/state/search.js b/src/state/search.js index db5886a..c03f506 100644 --- a/src/state/search.js +++ b/src/state/search.js @@ -5,8 +5,10 @@ export const useSearchStore = create( persist( (set, get) => ({ search: "", + type: "", searchHistory: [], setSearch: (search) => set({ search }), + setType: (type) => set({ type }), setSearchHistory: (search) => set({ searchHistory: [search, ...get().searchHistory.slice(-9)] }), }),