rules.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import useSWR from "swr";
  2. import { useState, useMemo } from "react";
  3. import { useTranslation } from "react-i18next";
  4. import { Virtuoso } from "react-virtuoso";
  5. import { Box } from "@mui/material";
  6. import { getRules } from "@/services/api";
  7. import { BaseEmpty, BasePage } from "@/components/base";
  8. import RuleItem from "@/components/rule/rule-item";
  9. import { ProviderButton } from "@/components/rule/provider-button";
  10. import { useCustomTheme } from "@/components/layout/use-custom-theme";
  11. import { BaseSearchBox } from "@/components/base/base-search-box";
  12. const RulesPage = () => {
  13. const { t } = useTranslation();
  14. const { data = [] } = useSWR("getRules", getRules);
  15. const { theme } = useCustomTheme();
  16. const isDark = theme.palette.mode === "dark";
  17. const [match, setMatch] = useState(() => (_: string) => true);
  18. const rules = useMemo(() => {
  19. return data.filter((item) => match(item.payload));
  20. }, [data, match]);
  21. return (
  22. <BasePage
  23. full
  24. title={t("Rules")}
  25. contentStyle={{ height: "100%" }}
  26. header={
  27. <Box display="flex" alignItems="center" gap={1}>
  28. <ProviderButton />
  29. </Box>
  30. }
  31. >
  32. <Box
  33. sx={{
  34. pt: 1,
  35. mb: 0.5,
  36. mx: "10px",
  37. height: "36px",
  38. display: "flex",
  39. alignItems: "center",
  40. }}
  41. >
  42. <BaseSearchBox onSearch={(match) => setMatch(() => match)} />
  43. </Box>
  44. <Box
  45. height="calc(100% - 65px)"
  46. sx={{
  47. margin: "10px",
  48. borderRadius: "8px",
  49. bgcolor: isDark ? "#282a36" : "#ffffff",
  50. }}
  51. >
  52. {rules.length > 0 ? (
  53. <Virtuoso
  54. data={rules}
  55. itemContent={(index, item) => (
  56. <RuleItem index={index + 1} value={item} />
  57. )}
  58. followOutput={"smooth"}
  59. />
  60. ) : (
  61. <BaseEmpty text="No Rules" />
  62. )}
  63. </Box>
  64. </BasePage>
  65. );
  66. };
  67. export default RulesPage;