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 ( +
+ + setSearch(e.target.value)} + placeholder="Search history by name" + /> +
+

Loading...

+
+
+ ); + } + + const isEmpty = data.items.length === 0 && debouncedSearch.length === 0; + + if (isEmpty) { + return ( +
+ + setSearch(e.target.value)} + placeholder="Search history by name" + /> +
+

History is empty.

+
+
+ ); + } + return (
- - {data?.items.length === 0 ? ( -

History is empty.

+ setSearch(e.target.value)} + placeholder="Search history by name" + /> + + {data.items.length === 0 ? ( +

+ No history items found. +

) : (
    - {data?.items + {[...data.items] .sort((a, b) => b.result.timeStop - a.result.timeStop) - .map((item) => )} + .map((item) => ( + + ))}
)}
@@ -110,10 +180,7 @@ export function History() { } function HistoryCounter() { - const { data } = api.history.get.useQuery(undefined, { - refetchOnMount: "always", - refetchInterval: 10 * 1000, - }); + const { data } = api.history.get.useQuery(); return (