Repository: StanGirard/quivr
File: frontend/lib/context/NotificationsProvider/notifications-provider.tsx (TypeScript)

Code before:
  unreadNotifications: number;
  setUnreadNotifications: React.Dispatch<React.SetStateAction<number>>;
};

export const NotificationsContext = createContext<NotificationsContextType | undefined>(undefined);

export const NotificationsProvider = ({
  children,
}: {
  children: React.ReactNode;
}): JSX.Element => {
  const [isVisible, setIsVisible] = useState<boolean>(false);
  const [unreadNotifications, setUnreadNotifications] = useState<number>(0);
  const [bulkNotifications, setBulkNotifications] = useState<BulkNotification[]>([]);
  const { supabase } = useSupabase();

  const fetchNotifications = async (): Promise<NotificationType[]> => {
    const { data, error } = await supabase.from("notifications").select();
    if (error) {

      return [];
    }

    // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unnecessary-condition
    return data || [];
  };

  const processNotifications = (notifications: NotificationType[]): BulkNotification[] => {
    const bulkMap: { [key: string]: NotificationType[] } = {};
    notifications.forEach((notif) => {
      if (!bulkMap[notif.bulk_id]) {
        bulkMap[notif.bulk_id] = [];
      }
      bulkMap[notif.bulk_id].push(notif);
    });

    return Object.keys(bulkMap).map((bulkId) => ({
      bulk_id: bulkId,
      notifications: bulkMap[bulkId],
      category: bulkMap[bulkId][0].category,
      brain_id: bulkMap[bulkId][0].brain_id,
      datetime: bulkMap[bulkId][0].datetime,
    }));
  };

  const updateNotifications = async (): Promise<void> => {
    try {
      const lastRetrieved = localStorage.getItem("lastRetrieved");
      const now = Date.now();

      if (lastRetrieved && now - parseInt(lastRetrieved) < 1000) {

Review comment (suggestion):
```suggestion
   return (error || !data) ? [] : data
```

Code after:
  setBulkNotifications: React.Dispatch<
    React.SetStateAction<BulkNotification[]>
  >;
  updateNotifications: () => Promise<void>;
  unreadNotifications: number;
  setUnreadNotifications: React.Dispatch<React.SetStateAction<number>>;
};

export const NotificationsContext = createContext<
  NotificationsContextType | undefined
>(undefined);

export const NotificationsProvider = ({
  children,
}: {
  children: React.ReactNode;
}): JSX.Element => {
  const [isVisible, setIsVisible] = useState<boolean>(false);
  const [unreadNotifications, setUnreadNotifications] = useState<number>(0);
  const [bulkNotifications, setBulkNotifications] = useState<
    BulkNotification[]
  >([]);
  const { supabase } = useSupabase();

  const fetchNotifications = async (): Promise<NotificationType[]> => {
    const { data, error } = await supabase.from("notifications").select();

    return error ? [] : (data as NotificationType[]);
  };

  const processNotifications = (
    notifications: NotificationType[]
  ): BulkNotification[] => {
    const bulkMap: { [key: string]: NotificationType[] } = {};
    notifications.forEach((notif) => {
      if (!bulkMap[notif.bulk_id]) {
        bulkMap[notif.bulk_id] = [];
      }
      bulkMap[notif.bulk_id].push(notif);
    });

    return Object.keys(bulkMap).map((bulkId) => ({
      bulk_id: bulkId,
      notifications: bulkMap[bulkId],
      category: bulkMap[bulkId][0].category,
      brain_id: bulkMap[bulkId][0].brain_id,
      datetime: bulkMap[bulkId][0].datetime,
    }));
  };

  const updateNotifications = async (): Promise<void> => {