Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 36 additions & 3 deletions src/SearchBar.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import clsx from "clsx";
import { useEffect, useState } from "react";
import SearchResults from "./SearchResults";
import { useSearchStore } from "./state/search";
Expand All @@ -6,8 +7,17 @@ const capitalPDangit = (query) => {
return query.replace(/Wordpress/i, "WordPress");
};

const types = {
Comment thread
zzap marked this conversation as resolved.
"": "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);

Expand All @@ -31,7 +41,14 @@ const SearchBar = () => {

return (
<div className="search-bar-wrap">
<form method="get" onSubmit={(e) => e.preventDefault()}>
<form
className="flex flex-col gap-3"
method="get"
onSubmit={(e) => e.preventDefault()}
>
<label htmlFor="search" className="sr-only">
Search
</label>
<input
className="w-full rounded-full py-4 px-6 border-2 font-mono"
type="search"
Expand All @@ -41,10 +58,26 @@ const SearchBar = () => {
placeholder="Search for a WordPress function, hook, or class."
autoFocus
/>
<div className="flex gap-2 items-center">
<p className="text-sm">Filter by type:</p>
{Object.entries(types).map(([key, value]) => (
<button
type="button"
onClick={() => setType(key)}
className={clsx("p-1 px-3 rounded-xl text-sm", {
"bg-blue-700 text-white": type === key,
"bg-gray-200": type !== key,
})}
>
{value}
</button>
))}
</div>
</form>

<SearchResults
query={search}
search={search}
type={type}
selectedResult={selectedResult}
selectResult={selectResult}
/>
Expand Down
12 changes: 6 additions & 6 deletions src/SearchResults.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <p className="mt-8 text-gray-600">Failed to load, dangit.</p>;
Expand Down Expand Up @@ -71,12 +71,12 @@ const SearchResults = ({ query, selectedResult, selectResult }) => {

return (
<div className="results-wrap mt-8 container">
{query && query.length < 3 ? (
{search && search.length < 3 ? (
<p className="text-gray-600">Keep typing...</p>
) : data.length === 0 ? (
<h2 className="font-bold">No search results for: {query}</h2>
<h2 className="font-bold">No search results for: {search}</h2>
) : (
<h2 className="font-bold">Search results for: {query}</h2>
<h2 className="font-bold">Search results for: {search}</h2>
)}
{data && data?.length > 0 && (
<div className="grid gap-8 mt-8 grid-cols-1 md:grid-cols-2 lg:grid-cols-3">
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/state/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)] }),
}),
Expand Down