Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

1.0 KB
Raw Blame
import { useSyncExternalStore } from "react";
import {
  emitLocalStorageChange,
  getLocalStorageItem,
  LOCAL_STORAGE_EVENT,
  setLocalStorageItem,
} from "@/utils/localStorageUtils";

function subscribe(callback: () => void) {
  window.addEventListener("storage", callback);
  window.addEventListener(LOCAL_STORAGE_EVENT, callback);
  return () => {
    window.removeEventListener("storage", callback);
    window.removeEventListener(LOCAL_STORAGE_EVENT, callback);
  };
}

export function useAnnouncementRead(readKey: string): boolean {
  return useSyncExternalStore(
    subscribe,
    () => getLocalStorageItem(readKey) === "true",
    () => false,
  );
}

export function markAnnouncementRead(readKey: string): void {
  setLocalStorageItem(readKey, "true");
  emitLocalStorageChange(readKey);
}

export function useUnreadAnnouncementCount(readKeys: readonly string[]): number {
  return useSyncExternalStore(
    subscribe,
    () => readKeys.filter((readKey) => getLocalStorageItem(readKey) !== "true").length,
    () => readKeys.length,
  );
}