@dashx/react 0.3.2 → 0.3.4
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.
- package/dist/cjs/hooks/use-in-app.d.ts +4 -0
- package/dist/cjs/hooks/use-in-app.js +1 -1
- package/dist/cjs/hooks/use-in-app.js.map +3 -3
- package/dist/esm/hooks/use-in-app.d.ts +4 -0
- package/dist/esm/hooks/use-in-app.js +1 -1
- package/dist/esm/hooks/use-in-app.js.map +3 -3
- package/package.json +2 -2
- package/src/hooks/use-in-app.ts +35 -2
|
@@ -2,8 +2,12 @@ import type { InAppMessages } from '@dashx/browser';
|
|
|
2
2
|
type UseInAppHookResponse = {
|
|
3
3
|
messages: InAppMessages;
|
|
4
4
|
unreadMessagesCount: number | null;
|
|
5
|
+
hasMore: boolean;
|
|
6
|
+
isLoadingMore: boolean;
|
|
7
|
+
loadMore: () => Promise<void>;
|
|
5
8
|
markMessageAsRead: (id: string) => Promise<any>;
|
|
6
9
|
markMessageAsUnread: (id: string) => Promise<any>;
|
|
10
|
+
markAllMessagesAsRead: () => Promise<any>;
|
|
7
11
|
};
|
|
8
12
|
declare const useInApp: () => UseInAppHookResponse;
|
|
9
13
|
export default useInApp;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var E=Object.create;var p=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var U=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty;var w=(s,e)=>{for(var a in e)p(s,a,{get:e[a],enumerable:!0})},A=(s,e,a,g)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of S(e))!_.call(s,t)&&t!==a&&p(s,t,{get:()=>e[t],enumerable:!(g=R(e,t))||g.enumerable});return s};var d=(s,e,a)=>(a=s!=null?E(U(s)):{},A(e||!s||!s.__esModule?p(a,"default",{value:s,enumerable:!0}):a,s)),C=s=>A(p({},"__esModule",{value:!0}),s);var G={};w(G,{default:()=>D});module.exports=C(G);var l=d(require("react-hot-toast")),r=require("react"),n=require("@dashx/browser"),f=d(require("./use-dashx-provider.js")),b=require("../providers/DashXProvider.js");const W=()=>{let s=(0,f.default)();const{subscribe:e}=(0,b.useWebSocket)(),[a,g]=(0,r.useState)([]),[t,k]=(0,r.useState)(null),[M,i]=(0,r.useState)(!1),[c,u]=(0,r.useState)(!1),h=o=>s.trackMessage({id:o,status:"READ"}),y=async o=>s.trackMessage({id:o,status:"UNREAD"}),I=()=>s.trackAllMessages(),P=async()=>{if(!(c||!M)){u(!0);try{const{hasMore:o}=await s.fetchMoreInAppMessages();i(o)}finally{u(!1)}}};return(0,r.useEffect)(()=>{s.watchFetchInAppMessages(o=>{g(o),o.length<=n.IN_APP_MESSAGES_PAGE_SIZE&&i(o.length===n.IN_APP_MESSAGES_PAGE_SIZE)}),s.watchFetchInAppMessagesAggregate(k)},[s]),(0,r.useEffect)(()=>e(m=>{m?.type===n.WebsocketMessage.IN_APP_MESSAGE&&(0,l.default)(m.data.renderedContent.body)}),[e]),{messages:a,unreadMessagesCount:t,hasMore:M,isLoadingMore:c,loadMore:P,markMessageAsRead:h,markMessageAsUnread:y,markAllMessagesAsRead:I}};var D=W;
|
|
2
2
|
//# sourceMappingURL=use-in-app.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/use-in-app.ts"],
|
|
4
|
-
"sourcesContent": ["import toast from 'react-hot-toast';\nimport { useEffect, useState } from 'react';\nimport { WebsocketMessage } from '@dashx/browser';\nimport type { InAppMessages, WebsocketMessageType } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\nimport { useWebSocket } from '../providers/DashXProvider.js';\n\ntype UseInAppHookResponse = {\n messages: InAppMessages;\n unreadMessagesCount: number | null;\n markMessageAsRead: (id: string) => Promise<any>;\n markMessageAsUnread: (id: string) => Promise<any>;\n};\n\nconst useInApp = (): UseInAppHookResponse => {\n let dashX = useDashXProvider();\n const { subscribe } = useWebSocket();\n const [messages, setMessages] = useState<InAppMessages>([]);\n const [unreadMessagesCount, setUnreadMessagesCount] = useState<number | null>(null);\n\n const markMessageAsRead = (id: string) => dashX.trackMessage({ id, status: 'READ' });\n\n const markMessageAsUnread = async (id: string) =>\n dashX.trackMessage({ id, status: 'UNREAD' });\n\n\n useEffect(() => {\n // Set up in-app message watchers (automatically refetch on WebSocket reconnection)\n dashX.watchFetchInAppMessages(setMessages);\n dashX.watchFetchInAppMessagesAggregate(setUnreadMessagesCount);\n }, [dashX]);\n\n useEffect(() => {\n // Subscribe to WebSocket messages for in-app messages\n const unsubscribe = subscribe((message: WebsocketMessageType) => {\n // Handle only IN_APP_MESSAGE for toast display\n // The client handles all other message processing internally\n if (message?.type === WebsocketMessage.IN_APP_MESSAGE) {\n toast(message.data.renderedContent.body);\n }\n });\n\n return unsubscribe;\n }, [subscribe]);\n\n return {\n messages,\n unreadMessagesCount,\n markMessageAsRead,\n markMessageAsUnread,\n };\n};\n\nexport default useInApp;\n"],
|
|
5
|
-
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAkB,8BAClBC,EAAoC,iBACpCC,
|
|
6
|
-
"names": ["use_in_app_exports", "__export", "use_in_app_default", "__toCommonJS", "import_react_hot_toast", "import_react", "import_browser", "import_use_dashx_provider", "import_DashXProvider", "useInApp", "dashX", "useDashXProvider", "subscribe", "messages", "setMessages", "unreadMessagesCount", "setUnreadMessagesCount", "markMessageAsRead", "id", "markMessageAsUnread", "message", "toast"]
|
|
4
|
+
"sourcesContent": ["import toast from 'react-hot-toast';\nimport { useEffect, useState } from 'react';\nimport { WebsocketMessage, IN_APP_MESSAGES_PAGE_SIZE } from '@dashx/browser';\nimport type { InAppMessages, WebsocketMessageType } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\nimport { useWebSocket } from '../providers/DashXProvider.js';\n\ntype UseInAppHookResponse = {\n messages: InAppMessages;\n unreadMessagesCount: number | null;\n hasMore: boolean;\n isLoadingMore: boolean;\n loadMore: () => Promise<void>;\n markMessageAsRead: (id: string) => Promise<any>;\n markMessageAsUnread: (id: string) => Promise<any>;\n markAllMessagesAsRead: () => Promise<any>;\n};\n\nconst useInApp = (): UseInAppHookResponse => {\n let dashX = useDashXProvider();\n const { subscribe } = useWebSocket();\n const [messages, setMessages] = useState<InAppMessages>([]);\n const [unreadMessagesCount, setUnreadMessagesCount] = useState<number | null>(null);\n const [hasMore, setHasMore] = useState(false);\n const [isLoadingMore, setIsLoadingMore] = useState(false);\n\n const markMessageAsRead = (id: string) => dashX.trackMessage({ id, status: 'READ' });\n\n const markMessageAsUnread = async (id: string) =>\n dashX.trackMessage({ id, status: 'UNREAD' });\n\n // One mutation marking everything unread (sent up to now) as read - including messages\n // beyond the loaded pages - instead of one trackMessage call per message.\n const markAllMessagesAsRead = () => dashX.trackAllMessages();\n\n const loadMore = async () => {\n if (isLoadingMore || !hasMore) return;\n setIsLoadingMore(true);\n try {\n const { hasMore: more } = await dashX.fetchMoreInAppMessages();\n setHasMore(more);\n } finally {\n setIsLoadingMore(false);\n }\n };\n\n useEffect(() => {\n // Set up in-app message watchers (automatically refetch on WebSocket reconnection)\n dashX.watchFetchInAppMessages((nextMessages) => {\n setMessages(nextMessages);\n // Re-derive hasMore whenever the list is a fresh first page (<= one page): an empty\n // inbox (no more), the initial load, or after the client refetches page 1 on\n // reconnect (which resets the cache). A full first page implies more; a short or\n // empty one doesn't. Once paged past the first page, fetchMoreInAppMessages owns it.\n if (nextMessages.length <= IN_APP_MESSAGES_PAGE_SIZE) {\n setHasMore(nextMessages.length === IN_APP_MESSAGES_PAGE_SIZE);\n }\n });\n dashX.watchFetchInAppMessagesAggregate(setUnreadMessagesCount);\n }, [dashX]);\n\n useEffect(() => {\n // Subscribe to WebSocket messages for in-app messages\n const unsubscribe = subscribe((message: WebsocketMessageType) => {\n // Handle only IN_APP_MESSAGE for toast display\n // The client handles all other message processing internally\n if (message?.type === WebsocketMessage.IN_APP_MESSAGE) {\n toast(message.data.renderedContent.body);\n }\n });\n\n return unsubscribe;\n }, [subscribe]);\n\n return {\n messages,\n unreadMessagesCount,\n hasMore,\n isLoadingMore,\n loadMore,\n markMessageAsRead,\n markMessageAsUnread,\n markAllMessagesAsRead,\n };\n};\n\nexport default useInApp;\n"],
|
|
5
|
+
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAkB,8BAClBC,EAAoC,iBACpCC,EAA4D,0BAG5DC,EAA6B,sCAC7BC,EAA6B,yCAa7B,MAAMC,EAAW,IAA4B,CAC3C,IAAIC,KAAQ,EAAAC,SAAiB,EAC7B,KAAM,CAAE,UAAAC,CAAU,KAAI,gBAAa,EAC7B,CAACC,EAAUC,CAAW,KAAI,YAAwB,CAAC,CAAC,EACpD,CAACC,EAAqBC,CAAsB,KAAI,YAAwB,IAAI,EAC5E,CAACC,EAASC,CAAU,KAAI,YAAS,EAAK,EACtC,CAACC,EAAeC,CAAgB,KAAI,YAAS,EAAK,EAElDC,EAAqBC,GAAeZ,EAAM,aAAa,CAAE,GAAAY,EAAI,OAAQ,MAAO,CAAC,EAE7EC,EAAsB,MAAOD,GACjCZ,EAAM,aAAa,CAAE,GAAAY,EAAI,OAAQ,QAAS,CAAC,EAIvCE,EAAwB,IAAMd,EAAM,iBAAiB,EAErDe,EAAW,SAAY,CAC3B,GAAI,EAAAN,GAAiB,CAACF,GACtB,CAAAG,EAAiB,EAAI,EACrB,GAAI,CACF,KAAM,CAAE,QAASM,CAAK,EAAI,MAAMhB,EAAM,uBAAuB,EAC7DQ,EAAWQ,CAAI,CACjB,QAAE,CACAN,EAAiB,EAAK,CACxB,EACF,EAEA,sBAAU,IAAM,CAEdV,EAAM,wBAAyBiB,GAAiB,CAC9Cb,EAAYa,CAAY,EAKpBA,EAAa,QAAU,6BACzBT,EAAWS,EAAa,SAAW,2BAAyB,CAEhE,CAAC,EACDjB,EAAM,iCAAiCM,CAAsB,CAC/D,EAAG,CAACN,CAAK,CAAC,KAEV,aAAU,IAEYE,EAAWgB,GAAkC,CAG3DA,GAAS,OAAS,mBAAiB,mBACrC,EAAAC,SAAMD,EAAQ,KAAK,gBAAgB,IAAI,CAE3C,CAAC,EAGA,CAAChB,CAAS,CAAC,EAEP,CACL,SAAAC,EACA,oBAAAE,EACA,QAAAE,EACA,cAAAE,EACA,SAAAM,EACA,kBAAAJ,EACA,oBAAAE,EACA,sBAAAC,CACF,CACF,EAEA,IAAOtB,EAAQO",
|
|
6
|
+
"names": ["use_in_app_exports", "__export", "use_in_app_default", "__toCommonJS", "import_react_hot_toast", "import_react", "import_browser", "import_use_dashx_provider", "import_DashXProvider", "useInApp", "dashX", "useDashXProvider", "subscribe", "messages", "setMessages", "unreadMessagesCount", "setUnreadMessagesCount", "hasMore", "setHasMore", "isLoadingMore", "setIsLoadingMore", "markMessageAsRead", "id", "markMessageAsUnread", "markAllMessagesAsRead", "loadMore", "more", "nextMessages", "message", "toast"]
|
|
7
7
|
}
|
|
@@ -2,8 +2,12 @@ import type { InAppMessages } from '@dashx/browser';
|
|
|
2
2
|
type UseInAppHookResponse = {
|
|
3
3
|
messages: InAppMessages;
|
|
4
4
|
unreadMessagesCount: number | null;
|
|
5
|
+
hasMore: boolean;
|
|
6
|
+
isLoadingMore: boolean;
|
|
7
|
+
loadMore: () => Promise<void>;
|
|
5
8
|
markMessageAsRead: (id: string) => Promise<any>;
|
|
6
9
|
markMessageAsUnread: (id: string) => Promise<any>;
|
|
10
|
+
markAllMessagesAsRead: () => Promise<any>;
|
|
7
11
|
};
|
|
8
12
|
declare const useInApp: () => UseInAppHookResponse;
|
|
9
13
|
export default useInApp;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
1
|
+
import k from"react-hot-toast";import{useEffect as M,useState as a}from"react";import{WebsocketMessage as h,IN_APP_MESSAGES_PAGE_SIZE as i}from"@dashx/browser";import y from"./use-dashx-provider.js";import{useWebSocket as I}from"../providers/DashXProvider.js";const P=()=>{let e=y();const{subscribe:o}=I(),[c,u]=a([]),[m,A]=a(null),[r,t]=a(!1),[n,g]=a(!1),d=s=>e.trackMessage({id:s,status:"READ"}),l=async s=>e.trackMessage({id:s,status:"UNREAD"}),f=()=>e.trackAllMessages(),b=async()=>{if(!(n||!r)){g(!0);try{const{hasMore:s}=await e.fetchMoreInAppMessages();t(s)}finally{g(!1)}}};return M(()=>{e.watchFetchInAppMessages(s=>{u(s),s.length<=i&&t(s.length===i)}),e.watchFetchInAppMessagesAggregate(A)},[e]),M(()=>o(p=>{p?.type===h.IN_APP_MESSAGE&&k(p.data.renderedContent.body)}),[o]),{messages:c,unreadMessagesCount:m,hasMore:r,isLoadingMore:n,loadMore:b,markMessageAsRead:d,markMessageAsUnread:l,markAllMessagesAsRead:f}};var w=P;export{w as default};
|
|
2
2
|
//# sourceMappingURL=use-in-app.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/use-in-app.ts"],
|
|
4
|
-
"sourcesContent": ["import toast from 'react-hot-toast';\nimport { useEffect, useState } from 'react';\nimport { WebsocketMessage } from '@dashx/browser';\nimport type { InAppMessages, WebsocketMessageType } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\nimport { useWebSocket } from '../providers/DashXProvider.js';\n\ntype UseInAppHookResponse = {\n messages: InAppMessages;\n unreadMessagesCount: number | null;\n markMessageAsRead: (id: string) => Promise<any>;\n markMessageAsUnread: (id: string) => Promise<any>;\n};\n\nconst useInApp = (): UseInAppHookResponse => {\n let dashX = useDashXProvider();\n const { subscribe } = useWebSocket();\n const [messages, setMessages] = useState<InAppMessages>([]);\n const [unreadMessagesCount, setUnreadMessagesCount] = useState<number | null>(null);\n\n const markMessageAsRead = (id: string) => dashX.trackMessage({ id, status: 'READ' });\n\n const markMessageAsUnread = async (id: string) =>\n dashX.trackMessage({ id, status: 'UNREAD' });\n\n\n useEffect(() => {\n // Set up in-app message watchers (automatically refetch on WebSocket reconnection)\n dashX.watchFetchInAppMessages(setMessages);\n dashX.watchFetchInAppMessagesAggregate(setUnreadMessagesCount);\n }, [dashX]);\n\n useEffect(() => {\n // Subscribe to WebSocket messages for in-app messages\n const unsubscribe = subscribe((message: WebsocketMessageType) => {\n // Handle only IN_APP_MESSAGE for toast display\n // The client handles all other message processing internally\n if (message?.type === WebsocketMessage.IN_APP_MESSAGE) {\n toast(message.data.renderedContent.body);\n }\n });\n\n return unsubscribe;\n }, [subscribe]);\n\n return {\n messages,\n unreadMessagesCount,\n markMessageAsRead,\n markMessageAsUnread,\n };\n};\n\nexport default useInApp;\n"],
|
|
5
|
-
"mappings": "AAAA,OAAOA,MAAW,kBAClB,OAAS,aAAAC,EAAW,YAAAC,MAAgB,QACpC,OAAS,oBAAAC,
|
|
6
|
-
"names": ["toast", "useEffect", "useState", "WebsocketMessage", "useDashXProvider", "useWebSocket", "useInApp", "dashX", "subscribe", "messages", "setMessages", "unreadMessagesCount", "setUnreadMessagesCount", "markMessageAsRead", "id", "markMessageAsUnread", "message", "use_in_app_default"]
|
|
4
|
+
"sourcesContent": ["import toast from 'react-hot-toast';\nimport { useEffect, useState } from 'react';\nimport { WebsocketMessage, IN_APP_MESSAGES_PAGE_SIZE } from '@dashx/browser';\nimport type { InAppMessages, WebsocketMessageType } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\nimport { useWebSocket } from '../providers/DashXProvider.js';\n\ntype UseInAppHookResponse = {\n messages: InAppMessages;\n unreadMessagesCount: number | null;\n hasMore: boolean;\n isLoadingMore: boolean;\n loadMore: () => Promise<void>;\n markMessageAsRead: (id: string) => Promise<any>;\n markMessageAsUnread: (id: string) => Promise<any>;\n markAllMessagesAsRead: () => Promise<any>;\n};\n\nconst useInApp = (): UseInAppHookResponse => {\n let dashX = useDashXProvider();\n const { subscribe } = useWebSocket();\n const [messages, setMessages] = useState<InAppMessages>([]);\n const [unreadMessagesCount, setUnreadMessagesCount] = useState<number | null>(null);\n const [hasMore, setHasMore] = useState(false);\n const [isLoadingMore, setIsLoadingMore] = useState(false);\n\n const markMessageAsRead = (id: string) => dashX.trackMessage({ id, status: 'READ' });\n\n const markMessageAsUnread = async (id: string) =>\n dashX.trackMessage({ id, status: 'UNREAD' });\n\n // One mutation marking everything unread (sent up to now) as read - including messages\n // beyond the loaded pages - instead of one trackMessage call per message.\n const markAllMessagesAsRead = () => dashX.trackAllMessages();\n\n const loadMore = async () => {\n if (isLoadingMore || !hasMore) return;\n setIsLoadingMore(true);\n try {\n const { hasMore: more } = await dashX.fetchMoreInAppMessages();\n setHasMore(more);\n } finally {\n setIsLoadingMore(false);\n }\n };\n\n useEffect(() => {\n // Set up in-app message watchers (automatically refetch on WebSocket reconnection)\n dashX.watchFetchInAppMessages((nextMessages) => {\n setMessages(nextMessages);\n // Re-derive hasMore whenever the list is a fresh first page (<= one page): an empty\n // inbox (no more), the initial load, or after the client refetches page 1 on\n // reconnect (which resets the cache). A full first page implies more; a short or\n // empty one doesn't. Once paged past the first page, fetchMoreInAppMessages owns it.\n if (nextMessages.length <= IN_APP_MESSAGES_PAGE_SIZE) {\n setHasMore(nextMessages.length === IN_APP_MESSAGES_PAGE_SIZE);\n }\n });\n dashX.watchFetchInAppMessagesAggregate(setUnreadMessagesCount);\n }, [dashX]);\n\n useEffect(() => {\n // Subscribe to WebSocket messages for in-app messages\n const unsubscribe = subscribe((message: WebsocketMessageType) => {\n // Handle only IN_APP_MESSAGE for toast display\n // The client handles all other message processing internally\n if (message?.type === WebsocketMessage.IN_APP_MESSAGE) {\n toast(message.data.renderedContent.body);\n }\n });\n\n return unsubscribe;\n }, [subscribe]);\n\n return {\n messages,\n unreadMessagesCount,\n hasMore,\n isLoadingMore,\n loadMore,\n markMessageAsRead,\n markMessageAsUnread,\n markAllMessagesAsRead,\n };\n};\n\nexport default useInApp;\n"],
|
|
5
|
+
"mappings": "AAAA,OAAOA,MAAW,kBAClB,OAAS,aAAAC,EAAW,YAAAC,MAAgB,QACpC,OAAS,oBAAAC,EAAkB,6BAAAC,MAAiC,iBAG5D,OAAOC,MAAsB,0BAC7B,OAAS,gBAAAC,MAAoB,gCAa7B,MAAMC,EAAW,IAA4B,CAC3C,IAAIC,EAAQH,EAAiB,EAC7B,KAAM,CAAE,UAAAI,CAAU,EAAIH,EAAa,EAC7B,CAACI,EAAUC,CAAW,EAAIT,EAAwB,CAAC,CAAC,EACpD,CAACU,EAAqBC,CAAsB,EAAIX,EAAwB,IAAI,EAC5E,CAACY,EAASC,CAAU,EAAIb,EAAS,EAAK,EACtC,CAACc,EAAeC,CAAgB,EAAIf,EAAS,EAAK,EAElDgB,EAAqBC,GAAeX,EAAM,aAAa,CAAE,GAAAW,EAAI,OAAQ,MAAO,CAAC,EAE7EC,EAAsB,MAAOD,GACjCX,EAAM,aAAa,CAAE,GAAAW,EAAI,OAAQ,QAAS,CAAC,EAIvCE,EAAwB,IAAMb,EAAM,iBAAiB,EAErDc,EAAW,SAAY,CAC3B,GAAI,EAAAN,GAAiB,CAACF,GACtB,CAAAG,EAAiB,EAAI,EACrB,GAAI,CACF,KAAM,CAAE,QAASM,CAAK,EAAI,MAAMf,EAAM,uBAAuB,EAC7DO,EAAWQ,CAAI,CACjB,QAAE,CACAN,EAAiB,EAAK,CACxB,EACF,EAEA,OAAAhB,EAAU,IAAM,CAEdO,EAAM,wBAAyBgB,GAAiB,CAC9Cb,EAAYa,CAAY,EAKpBA,EAAa,QAAUpB,GACzBW,EAAWS,EAAa,SAAWpB,CAAyB,CAEhE,CAAC,EACDI,EAAM,iCAAiCK,CAAsB,CAC/D,EAAG,CAACL,CAAK,CAAC,EAEVP,EAAU,IAEYQ,EAAWgB,GAAkC,CAG3DA,GAAS,OAAStB,EAAiB,gBACrCH,EAAMyB,EAAQ,KAAK,gBAAgB,IAAI,CAE3C,CAAC,EAGA,CAAChB,CAAS,CAAC,EAEP,CACL,SAAAC,EACA,oBAAAE,EACA,QAAAE,EACA,cAAAE,EACA,SAAAM,EACA,kBAAAJ,EACA,oBAAAE,EACA,sBAAAC,CACF,CACF,EAEA,IAAOK,EAAQnB",
|
|
6
|
+
"names": ["toast", "useEffect", "useState", "WebsocketMessage", "IN_APP_MESSAGES_PAGE_SIZE", "useDashXProvider", "useWebSocket", "useInApp", "dashX", "subscribe", "messages", "setMessages", "unreadMessagesCount", "setUnreadMessagesCount", "hasMore", "setHasMore", "isLoadingMore", "setIsLoadingMore", "markMessageAsRead", "id", "markMessageAsUnread", "markAllMessagesAsRead", "loadMore", "more", "nextMessages", "message", "use_in_app_default"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dashx/react",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"clean": "rm -rf .turbo node_modules dist tokens layout *.css"
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
|
-
"@dashx/browser": "^0.8.
|
|
78
|
+
"@dashx/browser": "^0.8.4",
|
|
79
79
|
"@radix-ui/colors": "^3.0.0",
|
|
80
80
|
"@radix-ui/primitive": "^1.1.3",
|
|
81
81
|
"@radix-ui/react-accessible-icon": "^1.1.8",
|
package/src/hooks/use-in-app.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import toast from 'react-hot-toast';
|
|
2
2
|
import { useEffect, useState } from 'react';
|
|
3
|
-
import { WebsocketMessage } from '@dashx/browser';
|
|
3
|
+
import { WebsocketMessage, IN_APP_MESSAGES_PAGE_SIZE } from '@dashx/browser';
|
|
4
4
|
import type { InAppMessages, WebsocketMessageType } from '@dashx/browser';
|
|
5
5
|
|
|
6
6
|
import useDashXProvider from './use-dashx-provider.js';
|
|
@@ -9,8 +9,12 @@ import { useWebSocket } from '../providers/DashXProvider.js';
|
|
|
9
9
|
type UseInAppHookResponse = {
|
|
10
10
|
messages: InAppMessages;
|
|
11
11
|
unreadMessagesCount: number | null;
|
|
12
|
+
hasMore: boolean;
|
|
13
|
+
isLoadingMore: boolean;
|
|
14
|
+
loadMore: () => Promise<void>;
|
|
12
15
|
markMessageAsRead: (id: string) => Promise<any>;
|
|
13
16
|
markMessageAsUnread: (id: string) => Promise<any>;
|
|
17
|
+
markAllMessagesAsRead: () => Promise<any>;
|
|
14
18
|
};
|
|
15
19
|
|
|
16
20
|
const useInApp = (): UseInAppHookResponse => {
|
|
@@ -18,16 +22,41 @@ const useInApp = (): UseInAppHookResponse => {
|
|
|
18
22
|
const { subscribe } = useWebSocket();
|
|
19
23
|
const [messages, setMessages] = useState<InAppMessages>([]);
|
|
20
24
|
const [unreadMessagesCount, setUnreadMessagesCount] = useState<number | null>(null);
|
|
25
|
+
const [hasMore, setHasMore] = useState(false);
|
|
26
|
+
const [isLoadingMore, setIsLoadingMore] = useState(false);
|
|
21
27
|
|
|
22
28
|
const markMessageAsRead = (id: string) => dashX.trackMessage({ id, status: 'READ' });
|
|
23
29
|
|
|
24
30
|
const markMessageAsUnread = async (id: string) =>
|
|
25
31
|
dashX.trackMessage({ id, status: 'UNREAD' });
|
|
26
32
|
|
|
33
|
+
// One mutation marking everything unread (sent up to now) as read - including messages
|
|
34
|
+
// beyond the loaded pages - instead of one trackMessage call per message.
|
|
35
|
+
const markAllMessagesAsRead = () => dashX.trackAllMessages();
|
|
36
|
+
|
|
37
|
+
const loadMore = async () => {
|
|
38
|
+
if (isLoadingMore || !hasMore) return;
|
|
39
|
+
setIsLoadingMore(true);
|
|
40
|
+
try {
|
|
41
|
+
const { hasMore: more } = await dashX.fetchMoreInAppMessages();
|
|
42
|
+
setHasMore(more);
|
|
43
|
+
} finally {
|
|
44
|
+
setIsLoadingMore(false);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
27
47
|
|
|
28
48
|
useEffect(() => {
|
|
29
49
|
// Set up in-app message watchers (automatically refetch on WebSocket reconnection)
|
|
30
|
-
dashX.watchFetchInAppMessages(
|
|
50
|
+
dashX.watchFetchInAppMessages((nextMessages) => {
|
|
51
|
+
setMessages(nextMessages);
|
|
52
|
+
// Re-derive hasMore whenever the list is a fresh first page (<= one page): an empty
|
|
53
|
+
// inbox (no more), the initial load, or after the client refetches page 1 on
|
|
54
|
+
// reconnect (which resets the cache). A full first page implies more; a short or
|
|
55
|
+
// empty one doesn't. Once paged past the first page, fetchMoreInAppMessages owns it.
|
|
56
|
+
if (nextMessages.length <= IN_APP_MESSAGES_PAGE_SIZE) {
|
|
57
|
+
setHasMore(nextMessages.length === IN_APP_MESSAGES_PAGE_SIZE);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
31
60
|
dashX.watchFetchInAppMessagesAggregate(setUnreadMessagesCount);
|
|
32
61
|
}, [dashX]);
|
|
33
62
|
|
|
@@ -47,8 +76,12 @@ const useInApp = (): UseInAppHookResponse => {
|
|
|
47
76
|
return {
|
|
48
77
|
messages,
|
|
49
78
|
unreadMessagesCount,
|
|
79
|
+
hasMore,
|
|
80
|
+
isLoadingMore,
|
|
81
|
+
loadMore,
|
|
50
82
|
markMessageAsRead,
|
|
51
83
|
markMessageAsUnread,
|
|
84
|
+
markAllMessagesAsRead,
|
|
52
85
|
};
|
|
53
86
|
};
|
|
54
87
|
|