feat: add search menu to all resources
This commit is contained in:
24
lib/hooks/useDebouncedCallback.ts
Normal file
24
lib/hooks/useDebouncedCallback.ts
Normal 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;
|
||||
|
55
lib/hooks/useThrottledCallback.ts
Normal file
55
lib/hooks/useThrottledCallback.ts
Normal 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;
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user