feat: add search menu to all resources

This commit is contained in:
2023-08-06 00:33:06 +02:00
parent 32a7f89309
commit 0e0d26c939
12 changed files with 208 additions and 8 deletions

View File

@ -0,0 +1,24 @@
// useDebouncedCallback.tsx
import { useEffect, useState } from "preact/hooks";
const useDebouncedCallback = (
callback: (...args: any[]) => void,
delay: number,
) => {
const [debouncedCallback, setDebouncedCallback] = useState(() => callback);
useEffect(() => {
const debounceHandler = setTimeout(() => {
setDebouncedCallback(() => callback);
}, delay);
return () => {
clearTimeout(debounceHandler);
};
}, [callback, delay]);
return debouncedCallback;
};
export default useDebouncedCallback;

View File

@ -0,0 +1,55 @@
import { useEffect, useState } from "preact/hooks";
type ThrottleOptions = {
leading?: boolean;
trailing?: boolean;
};
const useThrottledCallback = (
callback: (...args: any[]) => void,
delay: number,
options: ThrottleOptions = {},
) => {
const { leading = true, trailing = true } = options;
const [lastExecTime, setLastExecTime] = useState(0);
const [timer, setTimer] = useState<number | null>(null);
const [isLeading, setIsLeading] = useState(false);
useEffect(() => {
return () => {
if (timer) {
clearTimeout(timer);
}
};
}, [timer]);
const throttledCallback = (...args: any[]) => {
const now = Date.now();
if (leading && !isLeading) {
callback(...args);
setLastExecTime(now);
setIsLeading(true);
}
if (trailing) {
if (timer) {
clearTimeout(timer);
}
setTimer(setTimeout(() => {
if (now - lastExecTime >= delay) {
callback(...args);
setLastExecTime(now);
}
setIsLeading(false);
}, delay));
}
};
return throttledCallback;
};
export default useThrottledCallback;

View File

@ -81,6 +81,7 @@ async function initializeTypesense() {
{ name: "type", type: "string", facet: true },
{ name: "date", type: "string", optional: true },
{ name: "rating", type: "int32", facet: true },
{ name: "tags", type: "string[]", facet: true },
{ name: "description", type: "string", optional: true },
],
default_sorting_field: "rating", // Default field for sorting
@ -116,6 +117,7 @@ async function synchronizeWithTypesense() {
description: sanitizeStringForTypesense(
resource?.description || resource?.content || "",
),
tags: resource?.tags || [],
rating: resource?.meta?.rating || 0,
date: resource?.meta?.date?.toString() || "",
type: resource.type,