Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 104x 13x 13x 26x 13x 104x 14x 14x 13x 1x 1x 104x 43x 43x 43x 13x 13x 13x 43x 12x 43x 43x 19x 43x 43x 43x | import { useQuery } from "@tanstack/react-query";
import { API } from "aws-amplify";
import { useEffect, useState } from "react";
import {
BannerNotificationSchema,
ReactQueryApiError,
ValidBannerNotification,
} from "shared-types";
import { useGetUser } from "@/api";
const mapValidNotifications = (notifications: unknown[]): ValidBannerNotification[] => {
Iif (!Array.isArray(notifications)) return [];
return notifications
.filter((notification) => BannerNotificationSchema.safeParse(notification).success)
.map((notification) => BannerNotificationSchema.parse(notification));
};
export const getSystemNotifs = async (): Promise<ValidBannerNotification[]> => {
try {
const notifications = await API.get("os", "/systemNotifs", {});
return mapValidNotifications(notifications);
} catch (error) {
console.error("Error fetching notifications:", error);
return [];
}
};
export const useGetSystemNotifs = () => {
const userQuery = useGetUser();
const [dismissed, setDismissed] = useState<string[]>([]);
useEffect(() => {
const dismissedNotifs = localStorage.getItem(`notifs.${userQuery?.data?.user?.username}`);
const parsed: string[] = JSON.parse(dismissedNotifs) ?? [];
setDismissed(parsed);
}, [userQuery?.data?.user?.username]);
const result = useQuery<ValidBannerNotification[], ReactQueryApiError>(
["systemBannerNotifs"],
() => getSystemNotifs(),
);
const notDismissed = result.data?.filter((i) => !dismissed.includes(i.notifId)) ?? []; //check dismissed
const currentNotifs = notDismissed.filter(
(i) => i.expDate && new Date(i.expDate).getTime() > new Date().getTime(),
); //check expired
const clearNotif = (id?: string) => {
const toBeRemoved = id ?? currentNotifs?.[0]?.notifId ?? "";
const cleared = [...dismissed, toBeRemoved].filter((v, i, a) => a.indexOf(v) === i);
setDismissed(cleared);
localStorage.setItem(`notifs.${userQuery?.data?.user?.username}`, JSON.stringify(cleared));
};
const resetNotifs = () => {
setDismissed([]);
localStorage.setItem(`notifs.${userQuery?.data?.user?.username}`, JSON.stringify([]));
};
return {
notifications: currentNotifs,
dismissed: dismissed,
allNotifications: result.data ?? [],
clearNotif: clearNotif,
resetNotifs: resetNotifs,
};
};
|