diff --git a/apps/spu-ui/src/app/_components/history.tsx b/apps/spu-ui/src/app/_components/history.tsx index c056463..7fb71d6 100644 --- a/apps/spu-ui/src/app/_components/history.tsx +++ b/apps/spu-ui/src/app/_components/history.tsx @@ -1,6 +1,6 @@ "use client"; -import React from "react"; +import React, { useEffect, useState } from "react"; import { format, fromUnixTime } from "date-fns"; import { RotateCcwIcon } from "lucide-react"; import { useQueue } from "@sophys-web/api-client/hooks"; @@ -14,6 +14,7 @@ import { CardHeader, CardTitle, } from "@sophys-web/ui/card"; +import { Input } from "@sophys-web/ui/input"; import { ScrollArea } from "@sophys-web/ui/scroll-area"; import { Tooltip, @@ -86,22 +87,91 @@ function HistoryItem(props: HistoryItemProps) { ); } +function useSearchDebounced(search: string) { + const [debouncedSearch, setDebouncedSearch] = useState(search); + + useEffect(() => { + const timeout = setTimeout(() => { + setDebouncedSearch(search); + }, 300); + + return () => { + clearTimeout(timeout); + }; + }, [search]); + + return debouncedSearch; +} + export function History() { - const { data } = api.history.get.useQuery(undefined, { + const [search, setSearch] = useState(""); + const debouncedSearch = useSearchDebounced(search); + const { data, isLoading } = api.history.get.useQuery(undefined, { refetchOnMount: "always", + refetchInterval: 10 * 1000, + select: (data) => ({ + items: data.items.filter((item) => + formatPlanNames(item.name) + .toLowerCase() + .includes(debouncedSearch.toLowerCase()), + ), + }), }); + if (isLoading || data === undefined) { + return ( +
Loading...
+History is empty.
+History is empty.
+ setSearch(e.target.value)} + placeholder="Search history by name" + /> ++ No history items found. +
) : (