Skip to content

Commit

Permalink
add debounce to font search to reduce loaded data even more
Browse files Browse the repository at this point in the history
  • Loading branch information
rgodha24 committed Aug 28, 2024
1 parent 68816bd commit c35b081
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions apps/dashboard/src/components/RightPanel/AddFont.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Popover, Button, TextField, Flex, Badge } from "@radix-ui/themes";
import Fuse from "fuse.js";
import { useFontsStore } from "../../stores/fontsStore";
import { useElementsStore } from "../../stores/elementsStore";
import { useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { FontPreview } from "../FontPreview";
import { OGElement } from "../../lib/types";

Expand All @@ -21,9 +21,10 @@ export function AddFont({
);
const [search, setSearch] = useState("");
const [open, setOpen] = useState(false);
const debouncedSearch = useDebounce(search, 200);

const searchedFonts = fuse
.search(search)
.search(debouncedSearch)
.slice(0, 5)
.map(({ item }) => item.name);

Expand Down Expand Up @@ -77,3 +78,20 @@ export function AddFont({
</Popover.Root>
);
}

// source: https://github.com/uidotdev/usehooks/blob/90fbbb4cc085e74e50c36a62a5759a40c62bb98e/index.js#L239
function useDebounce<T>(value: T, delay: number) {
const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(handler);
};
}, [value, delay]);

return debouncedValue;
}

0 comments on commit c35b081

Please sign in to comment.