memorium/islands/Search.tsx

208 lines
5.8 KiB
TypeScript
Raw Normal View History

2023-08-10 16:59:18 +02:00
import { useEffect, useRef } from "preact/hooks";
2023-08-06 00:33:06 +02:00
import useDebouncedCallback from "@lib/hooks/useDebouncedCallback.ts";
2023-08-10 16:59:18 +02:00
import { IconLoader2, IconSearch } from "@components/icons.tsx";
2023-08-06 00:33:06 +02:00
import { useEventListener } from "@lib/hooks/useEventListener.ts";
2025-01-05 23:14:19 +01:00
import { GenericResource } from "@lib/types.ts";
2023-08-06 17:47:26 +02:00
import { resources } from "@lib/resources.ts";
import { getCookie } from "@lib/string.ts";
2023-08-08 11:18:54 +02:00
import { IS_BROWSER } from "$fresh/runtime.ts";
2023-08-08 21:50:23 +02:00
import Checkbox from "@components/Checkbox.tsx";
import { Rating } from "@components/Rating.tsx";
import { useSignal } from "@preact/signals";
2023-08-08 21:55:10 +02:00
import Image from "@components/Image.tsx";
2023-08-09 15:20:14 +02:00
import { Emoji } from "@components/Emoji.tsx";
2023-08-06 00:33:06 +02:00
2023-08-10 16:59:18 +02:00
export async function fetchQueryResource(url: URL, type = "") {
const query = url.searchParams.get("q");
const status = url.searchParams.get("status");
try {
2025-01-05 23:14:19 +01:00
url.pathname = "/api/query";
url.searchParams.set("q", encodeURIComponent(query || ""));
2023-08-10 16:59:18 +02:00
if (status) {
url.searchParams.set("status", "not-seen");
}
if (type) {
2025-01-05 23:14:19 +01:00
url.searchParams.set("types", type);
2023-08-10 16:59:18 +02:00
}
const response = await fetch(url);
const jsonData = await response.json();
return jsonData;
} catch (error) {
console.error("Error fetching data:", error);
}
}
2023-08-06 00:33:06 +02:00
export const RedirectSearchHandler = () => {
2023-08-10 10:42:38 +02:00
if (getCookie("session_cookie")) {
useEventListener("keydown", (e: KeyboardEvent) => {
if (e?.target?.nodeName == "INPUT") return;
if (
e.key === "?" &&
2025-01-05 23:14:19 +01:00
globalThis.location.search === ""
2023-08-10 10:42:38 +02:00
) {
2025-01-05 23:14:19 +01:00
globalThis.location.href += "?q=";
2023-08-10 10:42:38 +02:00
}
}, IS_BROWSER ? document?.body : undefined);
}
2023-08-06 00:33:06 +02:00
return <></>;
};
2023-08-06 17:47:26 +02:00
const SearchResultImage = ({ src }: { src: string }) => {
return (
2023-08-08 21:55:10 +02:00
<Image
2023-08-06 17:47:26 +02:00
class="object-cover w-12 h-12 rounded-full"
2025-01-05 23:14:19 +01:00
width="50"
height="50"
src={src}
2023-08-06 17:47:26 +02:00
alt="preview image"
/>
);
};
export const SearchResultItem = (
{ item, showEmoji = false }: {
2025-01-05 23:14:19 +01:00
item: GenericResource;
2023-08-06 17:47:26 +02:00
showEmoji?: boolean;
},
) => {
2025-01-05 23:14:19 +01:00
const resourceType = resources[item.type];
const href = resourceType ? `${resourceType.link}/${item.id}` : "";
2023-08-06 17:47:26 +02:00
return (
<a
href={href}
class="p-2 text-white flex gap-4 items-center rounded-2xl hover:bg-gray-700"
>
2023-08-09 15:20:14 +02:00
{showEmoji && resourceType
? <Emoji class="w-7 h-7" name={resourceType.emoji} />
: ""}
2025-01-05 23:14:19 +01:00
{item.meta?.image && <SearchResultImage src={item.meta?.image} />}
{item?.name}
2023-08-06 17:47:26 +02:00
</a>
);
};
export const SearchResultList = (
2025-01-05 23:14:19 +01:00
{ result, showEmoji }: { result: GenericResource[]; showEmoji?: boolean },
2023-08-06 17:47:26 +02:00
) => {
return (
<div class="mt-4">
2025-01-05 23:14:19 +01:00
{result?.length
2023-08-06 17:47:26 +02:00
? (
<div class="flex flex-col gap-4">
2025-01-05 23:14:19 +01:00
{result.map((hit) => (
2023-08-06 17:47:26 +02:00
<SearchResultItem item={hit} showEmoji={showEmoji} />
))}
</div>
)
: <div style={{ color: "#818181" }}>No Results</div>}
</div>
);
};
2023-08-08 21:55:10 +02:00
const Search = (
2023-08-10 16:59:18 +02:00
{ q = "*", type, results }: {
q: string;
type?: string;
2025-01-05 23:14:19 +01:00
results?: GenericResource[];
2023-08-10 16:59:18 +02:00
},
2023-08-06 00:33:06 +02:00
) => {
2023-08-10 16:59:18 +02:00
const searchQuery = useSignal(q);
2025-01-05 23:14:19 +01:00
const data = useSignal<GenericResource[] | undefined>(results);
2023-08-10 16:59:18 +02:00
const isLoading = useSignal(false);
2023-08-08 21:50:23 +02:00
const showSeenStatus = useSignal(false);
2023-08-06 00:33:06 +02:00
const inputRef = useRef<HTMLInputElement>(null);
2023-08-10 16:59:18 +02:00
2023-08-06 00:33:06 +02:00
if ("history" in globalThis) {
2025-01-05 23:14:19 +01:00
const u = new URL(globalThis.location.href);
2023-08-10 16:59:18 +02:00
if (u.searchParams.get("q") !== searchQuery.value) {
u.searchParams.set("q", searchQuery.value);
2023-08-06 00:33:06 +02:00
}
2023-08-08 21:50:23 +02:00
if (showSeenStatus.value) {
u.searchParams.set("rating", "0");
2023-08-08 21:50:23 +02:00
} else {
u.searchParams.delete("rating");
2023-08-08 21:50:23 +02:00
}
2025-01-05 23:14:19 +01:00
globalThis.history.replaceState({}, "", u);
2023-08-06 00:33:06 +02:00
}
2023-08-10 16:59:18 +02:00
const fetchData = async () => {
2023-08-06 00:33:06 +02:00
try {
2023-08-10 16:59:18 +02:00
isLoading.value = true;
const jsonData = await fetchQueryResource(
2025-01-05 23:14:19 +01:00
new URL(globalThis?.location.href),
2023-08-10 16:59:18 +02:00
type,
);
data.value = jsonData;
isLoading.value = false;
2023-08-06 00:33:06 +02:00
} catch (error) {
console.error("Error fetching data:", error);
2023-08-10 16:59:18 +02:00
isLoading.value = false;
2023-08-06 00:33:06 +02:00
}
};
2023-08-10 16:59:18 +02:00
const debouncedFetchData = useDebouncedCallback(fetchData, 500); // Debounce the fetchData function with a delay of 500ms
2023-08-06 00:33:06 +02:00
useEffect(() => {
2023-08-10 16:59:18 +02:00
if (inputRef.current && searchQuery?.value.length === 0) {
2023-08-06 00:33:06 +02:00
inputRef.current?.focus();
}
}, [inputRef.current, searchQuery]);
const handleInputChange = (event: Event) => {
const target = event.target as HTMLInputElement;
2023-08-10 16:59:18 +02:00
if (target.value !== searchQuery.value) {
searchQuery.value = target.value;
2023-08-06 17:47:26 +02:00
}
2023-08-06 00:33:06 +02:00
};
2023-08-06 17:47:26 +02:00
useEffect(() => {
2023-08-10 16:59:18 +02:00
debouncedFetchData(); // Call the debounced fetch function with the updated search query
}, [searchQuery.value, showSeenStatus.value]);
2023-08-09 13:32:28 +02:00
useEffect(() => {
2023-08-10 16:59:18 +02:00
debouncedFetchData();
2023-08-06 17:47:26 +02:00
}, []);
2023-08-06 00:33:06 +02:00
return (
2023-08-06 17:47:26 +02:00
<div class="mt-2">
2023-12-08 21:17:34 +01:00
<header class="flex items-center gap-4 items-52">
2023-08-08 21:50:23 +02:00
<div
2023-12-08 21:17:34 +01:00
class="flex items-center gap-1 rounded-2xl w-full shadow-2xl"
2023-08-08 21:50:23 +02:00
style={{ background: "#2B2930", color: "#818181" }}
>
2023-08-10 16:59:18 +02:00
{isLoading.value && searchQuery.value
2023-08-08 21:50:23 +02:00
? <IconLoader2 class="w-4 h-4 ml-4 mr-2 animate-spin" />
: <IconSearch class="w-4 h-4 ml-4 mr-2" />}
<input
type="text"
style={{ fontSize: "1.2em" }}
class="bg-transparent py-3 w-full"
ref={inputRef}
2023-08-10 16:59:18 +02:00
value={searchQuery.value}
2023-08-08 21:50:23 +02:00
onInput={handleInputChange}
/>
</div>
2023-12-08 21:17:34 +01:00
<Checkbox label="seen" checked={showSeenStatus} />
2023-08-08 21:50:23 +02:00
<Rating rating={4} />
</header>
2025-01-05 23:14:19 +01:00
{data.value?.length && !isLoading.value
2023-08-10 16:59:18 +02:00
? <SearchResultList showEmoji={!type} result={data.value} />
: isLoading.value
2023-08-06 17:47:26 +02:00
? <div />
: (
<div
class="flex items-center gap-2 p-2 my-4 mx-3"
style={{ color: "#818181" }}
>
2023-08-09 15:20:14 +02:00
<Emoji class="w-8 h-8" name="Ghost.png" />
2023-08-06 17:47:26 +02:00
No Results
</div>
)}
2023-08-06 00:33:06 +02:00
</div>
);
};
2023-08-08 21:55:10 +02:00
export default Search;