doover-js 0.4.6 → 0.4.7
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.
|
@@ -19,6 +19,20 @@ export interface UseChannelMessagesOptions {
|
|
|
19
19
|
* snowflake so server-stamped messages don't get missed).
|
|
20
20
|
*/
|
|
21
21
|
initialBefore?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Optional lower-bound snowflake id. The server only returns messages
|
|
24
|
+
* newer than this on every page, so paginating older eventually returns
|
|
25
|
+
* an empty page and `hasNextPage` flips to false — useful for fetching
|
|
26
|
+
* a bounded time window (e.g. last 24h) without a manual cutoff loop on
|
|
27
|
+
* the consumer side.
|
|
28
|
+
*/
|
|
29
|
+
after?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Keep paging older automatically until `hasNextPage` is false. Almost
|
|
32
|
+
* always combined with `after` (or a small channel) — without a lower
|
|
33
|
+
* bound this will walk the channel back to its first message.
|
|
34
|
+
*/
|
|
35
|
+
autoPaginate?: boolean;
|
|
22
36
|
}
|
|
23
37
|
type Page<TData> = MessageStructure<TData>[];
|
|
24
38
|
export interface UseChannelMessagesResult<TData> extends Omit<UseInfiniteQueryResult<InfiniteData<Page<TData>>>, "data"> {
|
|
@@ -22,6 +22,8 @@ function useChannelMessages(identifier, options) {
|
|
|
22
22
|
const liveUpdates = options?.liveUpdates ?? true;
|
|
23
23
|
const fields = options?.fields;
|
|
24
24
|
const initialBefore = options?.initialBefore;
|
|
25
|
+
const after = options?.after;
|
|
26
|
+
const autoPaginate = options?.autoPaginate ?? false;
|
|
25
27
|
const key = channelMessagesQueryKey(agentId, channelName);
|
|
26
28
|
const onMessage = (0, react_1.useCallback)((message) => {
|
|
27
29
|
queryClient.setQueryData(key, (current) => {
|
|
@@ -50,10 +52,18 @@ function useChannelMessages(identifier, options) {
|
|
|
50
52
|
...(typeof pageParam === "string" ? { before: pageParam } : {}),
|
|
51
53
|
...(limit !== undefined ? { limit } : {}),
|
|
52
54
|
...(fields && fields.length > 0 ? { field_name: fields } : {}),
|
|
55
|
+
...(after !== undefined ? { after } : {}),
|
|
53
56
|
});
|
|
54
57
|
return (page ?? []);
|
|
55
58
|
},
|
|
56
59
|
});
|
|
60
|
+
(0, react_1.useEffect)(() => {
|
|
61
|
+
if (!autoPaginate)
|
|
62
|
+
return;
|
|
63
|
+
if (query.isFetching || !query.hasNextPage)
|
|
64
|
+
return;
|
|
65
|
+
query.fetchNextPage();
|
|
66
|
+
}, [autoPaginate, query.isFetching, query.hasNextPage, query.fetchNextPage]);
|
|
57
67
|
const messages = (0, react_1.useMemo)(() => (query.data?.pages ?? []).flat(), [query.data]);
|
|
58
68
|
return { ...query, messages };
|
|
59
69
|
}
|