@sendbird/uikit-chat-hooks 1.1.0 → 1.1.1

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.
Files changed (63) hide show
  1. package/lib/commonjs/channel/{useGroupChannelList.js → useGroupChannelList/index.js} +1 -1
  2. package/lib/commonjs/channel/useGroupChannelList/index.js.map +1 -0
  3. package/lib/commonjs/channel/useGroupChannelList/reducer.js +184 -0
  4. package/lib/commonjs/channel/useGroupChannelList/reducer.js.map +1 -0
  5. package/lib/commonjs/channel/useGroupChannelList/useGroupChannelListWithCollection.js +134 -0
  6. package/lib/commonjs/channel/useGroupChannelList/useGroupChannelListWithCollection.js.map +1 -0
  7. package/lib/commonjs/channel/useGroupChannelList/useGroupChannelListWithQuery.js +118 -0
  8. package/lib/commonjs/channel/useGroupChannelList/useGroupChannelListWithQuery.js.map +1 -0
  9. package/lib/commonjs/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.js +3 -3
  10. package/lib/commonjs/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.js.map +1 -1
  11. package/lib/commonjs/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.js +2 -2
  12. package/lib/commonjs/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.js.map +1 -1
  13. package/lib/commonjs/common/useMessageOutgoingStatus.js +49 -0
  14. package/lib/commonjs/common/useMessageOutgoingStatus.js.map +1 -0
  15. package/lib/commonjs/index.js +12 -4
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/commonjs/types.js.map +1 -1
  18. package/lib/module/channel/{useGroupChannelList.js → useGroupChannelList/index.js} +1 -1
  19. package/lib/module/channel/useGroupChannelList/index.js.map +1 -0
  20. package/lib/module/channel/useGroupChannelList/reducer.js +174 -0
  21. package/lib/module/channel/useGroupChannelList/reducer.js.map +1 -0
  22. package/lib/module/channel/useGroupChannelList/useGroupChannelListWithCollection.js +120 -0
  23. package/lib/module/channel/useGroupChannelList/useGroupChannelListWithCollection.js.map +1 -0
  24. package/lib/module/channel/useGroupChannelList/useGroupChannelListWithQuery.js +104 -0
  25. package/lib/module/channel/useGroupChannelList/useGroupChannelListWithQuery.js.map +1 -0
  26. package/lib/module/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.js +3 -3
  27. package/lib/module/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.js.map +1 -1
  28. package/lib/module/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.js +2 -2
  29. package/lib/module/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.js.map +1 -1
  30. package/lib/module/common/useMessageOutgoingStatus.js +36 -0
  31. package/lib/module/common/useMessageOutgoingStatus.js.map +1 -0
  32. package/lib/module/index.js +4 -3
  33. package/lib/module/index.js.map +1 -1
  34. package/lib/module/types.js.map +1 -1
  35. package/lib/typescript/src/channel/{useGroupChannelList.d.ts → useGroupChannelList/index.d.ts} +1 -1
  36. package/lib/typescript/src/channel/useGroupChannelList/reducer.d.ts +14 -0
  37. package/lib/typescript/src/channel/{useGroupChannelListWithCollection.d.ts → useGroupChannelList/useGroupChannelListWithCollection.d.ts} +1 -1
  38. package/lib/typescript/src/channel/{useGroupChannelListWithQuery.d.ts → useGroupChannelList/useGroupChannelListWithQuery.d.ts} +1 -1
  39. package/lib/typescript/src/common/useMessageOutgoingStatus.d.ts +3 -0
  40. package/lib/typescript/src/index.d.ts +4 -3
  41. package/lib/typescript/src/types.d.ts +0 -7
  42. package/package.json +3 -3
  43. package/src/channel/{useGroupChannelList.ts → useGroupChannelList/index.ts} +1 -1
  44. package/src/channel/useGroupChannelList/reducer.ts +154 -0
  45. package/src/channel/useGroupChannelList/useGroupChannelListWithCollection.ts +116 -0
  46. package/src/channel/useGroupChannelList/useGroupChannelListWithQuery.ts +114 -0
  47. package/src/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.ts +3 -3
  48. package/src/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.ts +2 -2
  49. package/src/common/useMessageOutgoingStatus.ts +56 -0
  50. package/src/index.ts +4 -3
  51. package/src/types.ts +0 -8
  52. package/lib/commonjs/channel/useGroupChannelList.js.map +0 -1
  53. package/lib/commonjs/channel/useGroupChannelListWithCollection.js +0 -148
  54. package/lib/commonjs/channel/useGroupChannelListWithCollection.js.map +0 -1
  55. package/lib/commonjs/channel/useGroupChannelListWithQuery.js +0 -130
  56. package/lib/commonjs/channel/useGroupChannelListWithQuery.js.map +0 -1
  57. package/lib/module/channel/useGroupChannelList.js.map +0 -1
  58. package/lib/module/channel/useGroupChannelListWithCollection.js +0 -135
  59. package/lib/module/channel/useGroupChannelListWithCollection.js.map +0 -1
  60. package/lib/module/channel/useGroupChannelListWithQuery.js +0 -117
  61. package/lib/module/channel/useGroupChannelListWithQuery.js.map +0 -1
  62. package/src/channel/useGroupChannelListWithCollection.ts +0 -139
  63. package/src/channel/useGroupChannelListWithQuery.ts +0 -119
@@ -1,119 +0,0 @@
1
- import { useCallback, useMemo, useRef, useState } from 'react';
2
- import type Sendbird from 'sendbird';
3
-
4
- import type { SendbirdChannel, SendbirdChatSDK, SendbirdGroupChannel } from '@sendbird/uikit-utils';
5
- import { arrayToMap, useAsyncEffect } from '@sendbird/uikit-utils';
6
-
7
- import { useAppFeatures } from '../common/useAppFeatures';
8
- import { useChannelHandler } from '../handler/useChannelHandler';
9
- import type { UseGroupChannelList, UseGroupChannelListOptions } from '../types';
10
-
11
- type GroupChannelMap = Record<string, SendbirdGroupChannel>;
12
-
13
- const HOOK_NAME = 'useGroupChannelListWithQuery';
14
- const createGroupChannelListQuery = (
15
- sdk: SendbirdChatSDK,
16
- queryCreator: UseGroupChannelListOptions['queryCreator'],
17
- ) => {
18
- const passedQuery = queryCreator?.();
19
- if (passedQuery) return passedQuery;
20
-
21
- const defaultQuery = sdk.GroupChannel.createMyGroupChannelListQuery();
22
- defaultQuery.limit = 20;
23
- return defaultQuery;
24
- };
25
-
26
- export const useGroupChannelListWithQuery: UseGroupChannelList = (sdk, userId, options) => {
27
- const { deliveryReceiptEnabled } = useAppFeatures(sdk);
28
- const queryRef = useRef<Sendbird.GroupChannelListQuery>();
29
- const [loading, setLoading] = useState(false);
30
- const [refreshing, setRefreshing] = useState(false);
31
-
32
- const [groupChannelMap, setGroupChannelMap] = useState<GroupChannelMap>({});
33
- const groupChannels = useMemo(() => {
34
- const channels = Object.values(groupChannelMap);
35
- if (options?.sortComparator) return channels.sort(options?.sortComparator);
36
- return channels;
37
- }, [groupChannelMap, options?.sortComparator]);
38
-
39
- // ---------- internal methods ---------- //
40
- const updateChannels = (channels: SendbirdChannel[], clearPrev: boolean) => {
41
- const groupChannels = channels.filter((c): c is SendbirdGroupChannel => c.isGroupChannel());
42
- if (clearPrev) setGroupChannelMap(arrayToMap(groupChannels, 'url'));
43
- else setGroupChannelMap((prev) => ({ ...prev, ...arrayToMap(groupChannels, 'url') }));
44
-
45
- if (deliveryReceiptEnabled) groupChannels.forEach((channel) => sdk.markAsDelivered(channel.url));
46
- };
47
- const deleteChannels = (channelUrls: string[]) => {
48
- setGroupChannelMap(({ ...draft }) => {
49
- channelUrls.forEach((url) => delete draft[url]);
50
- return draft;
51
- });
52
- };
53
- const init = useCallback(
54
- async (uid?: string) => {
55
- if (uid) {
56
- queryRef.current = createGroupChannelListQuery(sdk, options?.queryCreator);
57
- if (queryRef.current?.hasNext) {
58
- const channels = await queryRef.current.next();
59
- updateChannels(channels, true);
60
- }
61
- }
62
- },
63
- [sdk, options?.queryCreator],
64
- );
65
- // ---------- internal methods ends ---------- //
66
-
67
- // ---------- internal hooks ---------- //
68
- useAsyncEffect(async () => {
69
- setLoading(true);
70
- await init(userId);
71
- setLoading(false);
72
- }, [init, userId]);
73
-
74
- useChannelHandler(sdk, HOOK_NAME, {
75
- onChannelChanged: (channel) => updateChannels([channel], false),
76
- onChannelFrozen: (channel) => updateChannels([channel], false),
77
- onChannelUnfrozen: (channel) => updateChannels([channel], false),
78
- onChannelMemberCountChanged: (channels) => updateChannels(channels, false),
79
- onChannelDeleted: (url) => deleteChannels([url]),
80
- onUserJoined: (channel) => updateChannels([channel], false),
81
- onUserLeft: (channel, user) => {
82
- const isMe = user.userId === userId;
83
- if (isMe) deleteChannels([channel.url]);
84
- else updateChannels([channel], false);
85
- },
86
- onUserBanned(channel, user) {
87
- const isMe = user.userId === userId;
88
- if (isMe) deleteChannels([channel.url]);
89
- else updateChannels([channel], false);
90
- },
91
- });
92
- // ---------- internal hooks ends ---------- //
93
-
94
- // ---------- returns methods ---------- //
95
- const refresh = useCallback(async () => {
96
- setRefreshing(true);
97
- await init(userId);
98
- setRefreshing(false);
99
- }, [init, userId]);
100
-
101
- const update = useCallback(
102
- (channel: SendbirdGroupChannel) => {
103
- if (deliveryReceiptEnabled) sdk.markAsDelivered(channel.url);
104
- setGroupChannelMap((prev) => ({ ...prev, [channel.url]: channel }));
105
- },
106
- [sdk],
107
- );
108
-
109
- const next = useCallback(async () => {
110
- if (queryRef.current?.hasNext) {
111
- const channels = await queryRef.current.next();
112
- setGroupChannelMap((prev) => ({ ...prev, ...arrayToMap(channels, 'url') }));
113
- if (deliveryReceiptEnabled) channels.forEach((channel) => sdk.markAsDelivered(channel.url));
114
- }
115
- }, [sdk]);
116
- // ---------- returns methods ends ---------- //
117
-
118
- return { loading, groupChannels, update, refresh, refreshing, next };
119
- };