Consolidate two overlapping cache systems (broken TanStack persister + per-query Redis hash + pub/sub) into one: TanStack Query as single source of truth with Redis as its persistence layer via createAsyncStoragePersister. - Replace QueryClientProvider with PersistQueryClientProvider backed by Redis - Set global query defaults (staleTime: 0, refetchInterval: 30s, gcTime: 24h) - Remove all per-widget staleTime/refetchInterval/retry/refetchOnWindowFocus overrides - Replace cached request handlers with simplified fetch-only versions - Remove all tRPC subscription procedures and useSubscription calls in favor of polling - Delete 20 cron jobs that only pre-warmed server-side cache - Simplify Redis from per-query hash to single blob per user+board
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
"use client";
|
|
|
|
import { useMemo } from "react";
|
|
import { Card, Flex, Group, ScrollArea, Stack, Text } from "@mantine/core";
|
|
import { IconClock } from "@tabler/icons-react";
|
|
|
|
import { clientApi } from "@homarr/api/client";
|
|
import { useRequiredBoard } from "@homarr/boards/context";
|
|
import { useTimeAgo } from "@homarr/common";
|
|
import { useScopedI18n } from "@homarr/translation/client";
|
|
|
|
import type { WidgetComponentProps } from "../definition";
|
|
|
|
export default function NotificationsWidget({ options, integrationIds }: WidgetComponentProps<"notifications">) {
|
|
const { data: notificationIntegrations = [] } = clientApi.widget.notifications.getNotifications.useQuery({
|
|
...options,
|
|
integrationIds,
|
|
});
|
|
|
|
const t = useScopedI18n("widget.notifications");
|
|
|
|
const board = useRequiredBoard();
|
|
|
|
const sortedNotifications = useMemo(
|
|
() =>
|
|
notificationIntegrations
|
|
.flatMap((integration) => integration.data)
|
|
.sort((entryA, entryB) => entryB.time.getTime() - entryA.time.getTime()),
|
|
[notificationIntegrations],
|
|
);
|
|
|
|
return (
|
|
<ScrollArea className="scroll-area-w100" w="100%" p="sm">
|
|
<Stack w={"100%"} gap="sm">
|
|
{sortedNotifications.length > 0 ? (
|
|
sortedNotifications.map((notification) => (
|
|
<Card key={notification.id} radius={board.itemRadius} w="100%" p="sm">
|
|
<Flex gap="sm" direction="column" w="100%">
|
|
{notification.title && (
|
|
<Text fz="sm" lh="sm" lineClamp={2}>
|
|
{notification.title}
|
|
</Text>
|
|
)}
|
|
<Text c="dimmed" size="sm" lineClamp={4} style={{ whiteSpace: "pre-line" }}>
|
|
{notification.body}
|
|
</Text>
|
|
|
|
<InfoDisplay date={notification.time} />
|
|
</Flex>
|
|
</Card>
|
|
))
|
|
) : (
|
|
<Text size="sm" c="dimmed">
|
|
{t("noItems")}
|
|
</Text>
|
|
)}
|
|
</Stack>
|
|
</ScrollArea>
|
|
);
|
|
}
|
|
|
|
const InfoDisplay = ({ date }: { date: Date }) => {
|
|
const timeAgo = useTimeAgo(date, 30000); // update every 30sec
|
|
|
|
return (
|
|
<Group gap={5} align={"center"}>
|
|
<IconClock size={"1rem"} color={"var(--mantine-color-dimmed)"} />
|
|
<Text size="sm" c="dimmed">
|
|
{timeAgo}
|
|
</Text>
|
|
</Group>
|
|
);
|
|
};
|