@savantlink/sdk 2.2.25 → 2.2.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationContext.js","sources":["../../src/context/NotificationContext.tsx"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n'use client'\n\nimport { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'\nimport type { ReactNode } from 'react'\n\nimport { initializeNotificationSound, playNotificationSound } from '../../assets/sounds/notification'\n\nimport { useNotificationStream } from '@/hooks'\nimport { Notification } from '@/hooks/use-notification-stream'\n\nexport type NotificationContextValue = {\n notifications: Notification[]\n unreadCount: number\n isConnected: boolean\n readNotification: (id: string) => void\n readAllNotifications: () => void\n}\n\nconst NotificationContext = createContext<NotificationContextValue | undefined>(undefined)\n\nexport type NotificationProviderProps = {\n children: ReactNode\n baseAPIUrl: string\n notificationsData: Notification[]\n markNotificationAsRead: any\n markAllNotificationsAsRead: any\n}\n\nexport const NotificationProvider = ({\n children,\n baseAPIUrl,\n notificationsData,\n markAllNotificationsAsRead,\n markNotificationAsRead,\n}: NotificationProviderProps) => {\n /**\n * Notifications received during the current SSE session.\n */\n const [realtimeNotifications, setRealtimeNotifications] = useState<Notification[]>([])\n\n /**\n * Allows us to optimistically hide notifications immediately\n * without waiting for the notifications query to refetch.\n */\n const [readNotificationIds, setReadNotificationIds] = useState<Set<string>>(new Set())\n\n const handleNotification = useCallback((notification: Notification) => {\n setRealtimeNotifications((current) => {\n /**\n * Protect against the same SSE notification being\n * received more than once.\n */\n if (current.some((item) => item.id === notification.id)) {\n return current\n }\n\n return [notification, ...current]\n })\n\n playNotificationSound()\n }, [])\n\n const { isConnected } = useNotificationStream({\n baseAPIUrl,\n onNotification: handleNotification,\n })\n\n /**\n * Merge:\n *\n * 1. Newly received SSE notifications\n * 2. Persisted notifications returned by the API\n *\n * Map prevents duplicates when an SSE notification later\n * appears in a query/refetch response.\n */\n const notifications = useMemo(() => {\n const persistedNotifications = notificationsData ?? []\n\n const notificationMap = new Map<string, Notification>()\n\n for (const notification of [...realtimeNotifications, ...persistedNotifications]) {\n if (readNotificationIds.has(notification.id)) {\n continue\n }\n\n notificationMap.set(notification.id, notification)\n }\n\n return Array.from(notificationMap.values()).sort(\n (a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()\n )\n }, [realtimeNotifications, notificationsData, readNotificationIds])\n\n const unreadCount = notifications.length\n\n const readNotification = useCallback(\n (id: string) => {\n /**\n * Optimistically remove it from the UI.\n */\n setReadNotificationIds((current) => {\n const updated = new Set(current)\n\n updated.add(id)\n\n return updated\n })\n\n markNotificationAsRead(id)\n },\n [markNotificationAsRead]\n )\n\n const readAllNotifications = useCallback(() => {\n const notificationIds = notifications.map((notification) => notification.id)\n\n /**\n * Optimistically mark everything currently visible\n * as read.\n */\n setReadNotificationIds((current) => {\n const updated = new Set(current)\n\n notificationIds.forEach((id) => updated.add(id))\n\n return updated\n })\n\n markAllNotificationsAsRead()\n }, [notifications, markAllNotificationsAsRead])\n\n /**\n * Browsers require user interaction before audio\n * can be played.\n */\n useEffect(() => {\n const enableNotificationSound = () => {\n initializeNotificationSound()\n }\n\n window.addEventListener('pointerdown', enableNotificationSound, {\n once: true,\n })\n\n return () => {\n window.removeEventListener('pointerdown', enableNotificationSound)\n }\n }, [])\n\n return (\n <NotificationContext.Provider\n value={{\n notifications,\n unreadCount,\n isConnected,\n readNotification,\n readAllNotifications,\n }}\n >\n {children}\n </NotificationContext.Provider>\n )\n}\n\nexport const useNotifications = () => {\n const context = useContext(NotificationContext)\n\n if (!context) {\n throw new Error('useNotifications must be used within NotificationProvider')\n }\n\n return context\n}\n"],"names":["NotificationContext","createContext","undefined","children","baseAPIUrl","notificationsData","markAllNotificationsAsRead","markNotificationAsRead","realtimeNotifications","setRealtimeNotifications","useState","readNotificationIds","setReadNotificationIds","Set","handleNotification","useCallback","notification","current","some","item","id","playNotificationSound","isConnected","useNotificationStream","onNotification","notifications","useMemo","persistedNotifications","notificationMap","Map","has","set","Array","from","values","sort","a","b","Date","createdAt","getTime","unreadCount","length","readNotification","updated","add","readAllNotifications","notificationIds","map","forEach","useEffect","enableNotificationSound","initializeNotificationSound","window","addEventListener","once","removeEventListener","_jsx","Provider","value","context","useContext","Error"],"mappings":"qPAmBA,MAAMA,EAAsBC,qBAAoDC,gCAU5C,EAClCC,WACAC,aACAC,oBACAC,6BACAC,6BAKA,MAAOC,EAAuBC,GAA4BC,WAAyB,KAM5EC,EAAqBC,GAA0BF,WAAsB,IAAIG,KAE1EC,EAAqBC,cAAaC,IACtCP,EAA0BQ,GAKpBA,EAAQC,KAAMC,GAASA,EAAKC,KAAOJ,EAAaI,IAC3CH,EAGF,CAACD,KAAiBC,IAG3BI,2BACC,KAEGC,YAAEA,GAAgBC,UAAsB,CAC5CnB,aACAoB,eAAgBV,IAYZW,EAAgBC,UAAQ,KAC5B,MAAMC,EAAyBtB,QAAAA,EAAqB,GAE9CuB,EAAkB,IAAIC,IAE5B,IAAK,MAAMb,IAAgB,IAAIR,KAA0BmB,GACnDhB,EAAoBmB,IAAId,EAAaI,KAIzCQ,EAAgBG,IAAIf,EAAaI,GAAIJ,GAGvC,OAAOgB,MAAMC,KAAKL,EAAgBM,UAAUC,KAC1C,CAACC,EAAGC,IAAM,IAAIC,KAAKD,EAAEE,WAAWC,UAAY,IAAIF,KAAKF,EAAEG,WAAWC,YAEnE,CAAChC,EAAuBH,EAAmBM,IAExC8B,EAAchB,EAAciB,OAE5BC,EAAmB5B,cACtBK,IAICR,EAAwBK,IACtB,MAAM2B,EAAU,IAAI/B,IAAII,GAIxB,OAFA2B,EAAQC,IAAIzB,GAELwB,IAGTrC,EAAuBa,IAEzB,CAACb,IAGGuC,EAAuB/B,cAAY,KACvC,MAAMgC,EAAkBtB,EAAcuB,IAAKhC,GAAiBA,EAAaI,IAMzER,EAAwBK,IACtB,MAAM2B,EAAU,IAAI/B,IAAII,GAIxB,OAFA8B,EAAgBE,QAAS7B,GAAOwB,EAAQC,IAAIzB,IAErCwB,IAGTtC,KACC,CAACmB,EAAenB,IAoBnB,OAdA4C,YAAU,KACR,MAAMC,wBAA0B,KAC9BC,iCAOF,OAJAC,OAAOC,iBAAiB,cAAeH,wBAAyB,CAC9DI,MAAM,IAGD,KACLF,OAAOG,oBAAoB,cAAeL,2BAE3C,IAGDM,MAACzD,EAAoB0D,wBACnBC,MAAO,CACLlC,gBACAgB,cACAnB,cACAqB,mBACAG,mCAGD3C,sCAKyB,KAC9B,MAAMyD,EAAUC,aAAW7D,GAE3B,IAAK4D,EACH,MAAM,IAAIE,MAAM,6DAGlB,OAAOF"}
1
+ {"version":3,"file":"NotificationContext.js","sources":["../../src/context/NotificationContext.tsx"],"sourcesContent":["'use client'\n\nimport { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'\nimport type { ReactNode } from 'react'\n\nimport { initializeNotificationSound, playNotificationSound } from '../../assets/sounds/notification'\n\nimport { useNotificationStream } from '@/hooks'\nimport { Notification } from '@/hooks/use-notification-stream'\n\nexport type NotificationContextValue = {\n notifications: Notification[]\n unreadCount: number\n isConnected: boolean\n readNotification: (id: string) => void\n readAllNotifications: () => void\n}\n\nexport type NotificationProviderProps = {\n children: ReactNode\n baseAPIUrl: string\n notificationsData: Notification[]\n markNotificationAsRead: (id: string) => void\n markAllNotificationsAsRead: () => void\n}\n\nconst NotificationContext = createContext<NotificationContextValue | undefined>(undefined)\n\nexport const NotificationProvider = ({\n children,\n baseAPIUrl,\n notificationsData,\n markAllNotificationsAsRead,\n markNotificationAsRead,\n}: NotificationProviderProps) => {\n /**\n * Notifications received during the current SSE session.\n */\n const [realtimeNotifications, setRealtimeNotifications] = useState<Notification[]>([])\n\n /**\n * Allows us to optimistically hide notifications immediately\n * without waiting for the notifications query to refetch.\n */\n const [readNotificationIds, setReadNotificationIds] = useState<Set<string>>(new Set())\n\n const handleNotification = useCallback((notification: Notification) => {\n setRealtimeNotifications((current) => {\n /**\n * Protect against the same SSE notification being\n * received more than once.\n */\n if (current.some((item) => item.id === notification.id)) {\n return current\n }\n\n return [notification, ...current]\n })\n\n playNotificationSound()\n }, [])\n\n const { isConnected } = useNotificationStream({\n baseAPIUrl,\n onNotification: handleNotification,\n })\n\n /**\n * Merge:\n *\n * 1. Newly received SSE notifications\n * 2. Persisted notifications returned by the API\n *\n * Map prevents duplicates when an SSE notification later\n * appears in a query/refetch response.\n */\n const notifications = useMemo(() => {\n const persistedNotifications = notificationsData ?? []\n\n const notificationMap = new Map<string, Notification>()\n\n for (const notification of [...realtimeNotifications, ...persistedNotifications]) {\n if (readNotificationIds.has(notification.id)) {\n continue\n }\n\n notificationMap.set(notification.id, notification)\n }\n\n return Array.from(notificationMap.values()).sort(\n (a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()\n )\n }, [realtimeNotifications, notificationsData, readNotificationIds])\n\n const unreadCount = notifications.length\n\n const readNotification = useCallback(\n (id: string) => {\n /**\n * Optimistically remove it from the UI.\n */\n setReadNotificationIds((current) => {\n const updated = new Set(current)\n\n updated.add(id)\n\n return updated\n })\n\n markNotificationAsRead(id)\n },\n [markNotificationAsRead]\n )\n\n const readAllNotifications = useCallback(() => {\n const notificationIds = notifications.map((notification) => notification.id)\n\n /**\n * Optimistically mark everything currently visible\n * as read.\n */\n setReadNotificationIds((current) => {\n const updated = new Set(current)\n\n notificationIds.forEach((id) => updated.add(id))\n\n return updated\n })\n\n markAllNotificationsAsRead()\n }, [notifications, markAllNotificationsAsRead])\n\n /**\n * Browsers require user interaction before audio\n * can be played.\n */\n useEffect(() => {\n const enableNotificationSound = () => {\n initializeNotificationSound()\n }\n\n window.addEventListener('pointerdown', enableNotificationSound, {\n once: true,\n })\n\n return () => {\n window.removeEventListener('pointerdown', enableNotificationSound)\n }\n }, [])\n\n return (\n <NotificationContext.Provider\n value={{\n notifications,\n unreadCount,\n isConnected,\n readNotification,\n readAllNotifications,\n }}\n >\n {children}\n </NotificationContext.Provider>\n )\n}\n\nexport const useNotifications = () => {\n const context = useContext(NotificationContext)\n\n if (!context) {\n throw new Error('useNotifications must be used within NotificationProvider')\n }\n\n return context\n}\n"],"names":["NotificationContext","createContext","undefined","children","baseAPIUrl","notificationsData","markAllNotificationsAsRead","markNotificationAsRead","realtimeNotifications","setRealtimeNotifications","useState","readNotificationIds","setReadNotificationIds","Set","handleNotification","useCallback","notification","current","some","item","id","playNotificationSound","isConnected","useNotificationStream","onNotification","notifications","useMemo","persistedNotifications","notificationMap","Map","has","set","Array","from","values","sort","a","b","Date","createdAt","getTime","unreadCount","length","readNotification","updated","add","readAllNotifications","notificationIds","map","forEach","useEffect","enableNotificationSound","initializeNotificationSound","window","addEventListener","once","removeEventListener","_jsx","Provider","value","context","useContext","Error"],"mappings":"qPA0BA,MAAMA,EAAsBC,qBAAoDC,gCAE5C,EAClCC,WACAC,aACAC,oBACAC,6BACAC,6BAKA,MAAOC,EAAuBC,GAA4BC,WAAyB,KAM5EC,EAAqBC,GAA0BF,WAAsB,IAAIG,KAE1EC,EAAqBC,cAAaC,IACtCP,EAA0BQ,GAKpBA,EAAQC,KAAMC,GAASA,EAAKC,KAAOJ,EAAaI,IAC3CH,EAGF,CAACD,KAAiBC,IAG3BI,2BACC,KAEGC,YAAEA,GAAgBC,UAAsB,CAC5CnB,aACAoB,eAAgBV,IAYZW,EAAgBC,UAAQ,KAC5B,MAAMC,EAAyBtB,QAAAA,EAAqB,GAE9CuB,EAAkB,IAAIC,IAE5B,IAAK,MAAMb,IAAgB,IAAIR,KAA0BmB,GACnDhB,EAAoBmB,IAAId,EAAaI,KAIzCQ,EAAgBG,IAAIf,EAAaI,GAAIJ,GAGvC,OAAOgB,MAAMC,KAAKL,EAAgBM,UAAUC,KAC1C,CAACC,EAAGC,IAAM,IAAIC,KAAKD,EAAEE,WAAWC,UAAY,IAAIF,KAAKF,EAAEG,WAAWC,YAEnE,CAAChC,EAAuBH,EAAmBM,IAExC8B,EAAchB,EAAciB,OAE5BC,EAAmB5B,cACtBK,IAICR,EAAwBK,IACtB,MAAM2B,EAAU,IAAI/B,IAAII,GAIxB,OAFA2B,EAAQC,IAAIzB,GAELwB,IAGTrC,EAAuBa,IAEzB,CAACb,IAGGuC,EAAuB/B,cAAY,KACvC,MAAMgC,EAAkBtB,EAAcuB,IAAKhC,GAAiBA,EAAaI,IAMzER,EAAwBK,IACtB,MAAM2B,EAAU,IAAI/B,IAAII,GAIxB,OAFA8B,EAAgBE,QAAS7B,GAAOwB,EAAQC,IAAIzB,IAErCwB,IAGTtC,KACC,CAACmB,EAAenB,IAoBnB,OAdA4C,YAAU,KACR,MAAMC,wBAA0B,KAC9BC,iCAOF,OAJAC,OAAOC,iBAAiB,cAAeH,wBAAyB,CAC9DI,MAAM,IAGD,KACLF,OAAOG,oBAAoB,cAAeL,2BAE3C,IAGDM,MAACzD,EAAoB0D,wBACnBC,MAAO,CACLlC,gBACAgB,cACAnB,cACAqB,mBACAG,mCAGD3C,sCAKyB,KAC9B,MAAMyD,EAAUC,aAAW7D,GAE3B,IAAK4D,EACH,MAAM,IAAIE,MAAM,6DAGlB,OAAOF"}
@@ -11,8 +11,8 @@ export declare type NotificationProviderProps = {
11
11
  children: ReactNode;
12
12
  baseAPIUrl: string;
13
13
  notificationsData: Notification[];
14
- markNotificationAsRead: any;
15
- markAllNotificationsAsRead: any;
14
+ markNotificationAsRead: (id: string) => void;
15
+ markAllNotificationsAsRead: () => void;
16
16
  };
17
17
  export declare const NotificationProvider: ({ children, baseAPIUrl, notificationsData, markAllNotificationsAsRead, markNotificationAsRead, }: NotificationProviderProps) => import("react/jsx-runtime").JSX.Element;
18
18
  export declare const useNotifications: () => NotificationContextValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@savantlink/sdk",
3
- "version": "2.2.25",
3
+ "version": "2.2.26",
4
4
  "description": "A package that contains reusable components and services for the SavantLink design system and architecture.",
5
5
  "homepage": "https://github.com/savantlink/savantlink-sdk",
6
6
  "repository": {