@sendbird/uikit-chat-hooks 1.0.0-rc.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.
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/lib/commonjs/channel/useActiveGroupChannel.js +22 -0
- package/lib/commonjs/channel/useActiveGroupChannel.js.map +1 -0
- package/lib/commonjs/channel/useGroupChannelList.js +21 -0
- package/lib/commonjs/channel/useGroupChannelList.js.map +1 -0
- package/lib/commonjs/channel/useGroupChannelListWithCollection.js +148 -0
- package/lib/commonjs/channel/useGroupChannelListWithCollection.js.map +1 -0
- package/lib/commonjs/channel/useGroupChannelListWithQuery.js +130 -0
- package/lib/commonjs/channel/useGroupChannelListWithQuery.js.map +1 -0
- package/lib/commonjs/channel/useGroupChannelMessages/index.js +21 -0
- package/lib/commonjs/channel/useGroupChannelMessages/index.js.map +1 -0
- package/lib/commonjs/channel/useGroupChannelMessages/reducer.js +158 -0
- package/lib/commonjs/channel/useGroupChannelMessages/reducer.js.map +1 -0
- package/lib/commonjs/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.js +305 -0
- package/lib/commonjs/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.js.map +1 -0
- package/lib/commonjs/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.js +249 -0
- package/lib/commonjs/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.js.map +1 -0
- package/lib/commonjs/common/useAppFeatures.js +44 -0
- package/lib/commonjs/common/useAppFeatures.js.map +1 -0
- package/lib/commonjs/common/usePushTrigger.js +33 -0
- package/lib/commonjs/common/usePushTrigger.js.map +1 -0
- package/lib/commonjs/common/useTotalUnreadChannelCount.js +29 -0
- package/lib/commonjs/common/useTotalUnreadChannelCount.js.map +1 -0
- package/lib/commonjs/common/useTotalUnreadMessageCount.js +40 -0
- package/lib/commonjs/common/useTotalUnreadMessageCount.js.map +1 -0
- package/lib/commonjs/common/useUserList.js +110 -0
- package/lib/commonjs/common/useUserList.js.map +1 -0
- package/lib/commonjs/handler/useChannelHandler.js +29 -0
- package/lib/commonjs/handler/useChannelHandler.js.map +1 -0
- package/lib/commonjs/handler/useConnectionHandler.js +29 -0
- package/lib/commonjs/handler/useConnectionHandler.js.map +1 -0
- package/lib/commonjs/handler/useUserEventHandler.js +29 -0
- package/lib/commonjs/handler/useUserEventHandler.js.map +1 -0
- package/lib/commonjs/index.js +102 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/model/CustomQuery.js +28 -0
- package/lib/commonjs/model/CustomQuery.js.map +1 -0
- package/lib/commonjs/types.js +6 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/channel/useActiveGroupChannel.js +12 -0
- package/lib/module/channel/useActiveGroupChannel.js.map +1 -0
- package/lib/module/channel/useGroupChannelList.js +10 -0
- package/lib/module/channel/useGroupChannelList.js.map +1 -0
- package/lib/module/channel/useGroupChannelListWithCollection.js +135 -0
- package/lib/module/channel/useGroupChannelListWithCollection.js.map +1 -0
- package/lib/module/channel/useGroupChannelListWithQuery.js +117 -0
- package/lib/module/channel/useGroupChannelListWithQuery.js.map +1 -0
- package/lib/module/channel/useGroupChannelMessages/index.js +10 -0
- package/lib/module/channel/useGroupChannelMessages/index.js.map +1 -0
- package/lib/module/channel/useGroupChannelMessages/reducer.js +148 -0
- package/lib/module/channel/useGroupChannelMessages/reducer.js.map +1 -0
- package/lib/module/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.js +289 -0
- package/lib/module/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.js.map +1 -0
- package/lib/module/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.js +234 -0
- package/lib/module/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.js.map +1 -0
- package/lib/module/common/useAppFeatures.js +33 -0
- package/lib/module/common/useAppFeatures.js.map +1 -0
- package/lib/module/common/usePushTrigger.js +22 -0
- package/lib/module/common/usePushTrigger.js.map +1 -0
- package/lib/module/common/useTotalUnreadChannelCount.js +17 -0
- package/lib/module/common/useTotalUnreadChannelCount.js.map +1 -0
- package/lib/module/common/useTotalUnreadMessageCount.js +28 -0
- package/lib/module/common/useTotalUnreadMessageCount.js.map +1 -0
- package/lib/module/common/useUserList.js +100 -0
- package/lib/module/common/useUserList.js.map +1 -0
- package/lib/module/handler/useChannelHandler.js +17 -0
- package/lib/module/handler/useChannelHandler.js.map +1 -0
- package/lib/module/handler/useConnectionHandler.js +17 -0
- package/lib/module/handler/useConnectionHandler.js.map +1 -0
- package/lib/module/handler/useUserEventHandler.js +17 -0
- package/lib/module/handler/useUserEventHandler.js.map +1 -0
- package/lib/module/index.js +13 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/model/CustomQuery.js +19 -0
- package/lib/module/model/CustomQuery.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/src/channel/useActiveGroupChannel.d.ts +5 -0
- package/lib/typescript/src/channel/useGroupChannelList.d.ts +2 -0
- package/lib/typescript/src/channel/useGroupChannelListWithCollection.d.ts +2 -0
- package/lib/typescript/src/channel/useGroupChannelListWithQuery.d.ts +2 -0
- package/lib/typescript/src/channel/useGroupChannelMessages/index.d.ts +2 -0
- package/lib/typescript/src/channel/useGroupChannelMessages/reducer.d.ts +15 -0
- package/lib/typescript/src/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.d.ts +2 -0
- package/lib/typescript/src/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.d.ts +2 -0
- package/lib/typescript/src/common/useAppFeatures.d.ts +7 -0
- package/lib/typescript/src/common/usePushTrigger.d.ts +7 -0
- package/lib/typescript/src/common/useTotalUnreadChannelCount.d.ts +6 -0
- package/lib/typescript/src/common/useTotalUnreadMessageCount.d.ts +8 -0
- package/lib/typescript/src/common/useUserList.d.ts +24 -0
- package/lib/typescript/src/handler/useChannelHandler.d.ts +4 -0
- package/lib/typescript/src/handler/useConnectionHandler.d.ts +4 -0
- package/lib/typescript/src/handler/useUserEventHandler.d.ts +4 -0
- package/lib/typescript/src/index.d.ts +13 -0
- package/lib/typescript/src/model/CustomQuery.d.ts +14 -0
- package/lib/typescript/src/types.d.ts +198 -0
- package/package.json +73 -0
- package/src/channel/useActiveGroupChannel.ts +13 -0
- package/src/channel/useGroupChannelList.ts +11 -0
- package/src/channel/useGroupChannelListWithCollection.ts +144 -0
- package/src/channel/useGroupChannelListWithQuery.ts +124 -0
- package/src/channel/useGroupChannelMessages/index.ts +11 -0
- package/src/channel/useGroupChannelMessages/reducer.ts +134 -0
- package/src/channel/useGroupChannelMessages/useGroupChannelMessagesWithCollection.ts +315 -0
- package/src/channel/useGroupChannelMessages/useGroupChannelMessagesWithQuery.ts +251 -0
- package/src/common/useAppFeatures.ts +28 -0
- package/src/common/usePushTrigger.ts +31 -0
- package/src/common/useTotalUnreadChannelCount.ts +26 -0
- package/src/common/useTotalUnreadMessageCount.ts +43 -0
- package/src/common/useUserList.ts +103 -0
- package/src/handler/useChannelHandler.ts +25 -0
- package/src/handler/useConnectionHandler.ts +25 -0
- package/src/handler/useUserEventHandler.ts +26 -0
- package/src/index.ts +26 -0
- package/src/model/CustomQuery.ts +20 -0
- package/src/types.ts +246 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
import { ApplicationAttributes, PremiumFeatures, SendbirdChatSDK } from '@sendbird/uikit-utils';
|
|
4
|
+
|
|
5
|
+
export const useAppFeatures = (sdk: SendbirdChatSDK) => {
|
|
6
|
+
const deliveryReceiptEnabled = useMemo(() => {
|
|
7
|
+
return Boolean(sdk.appInfo?.premiumFeatureList?.includes?.(PremiumFeatures.delivery_receipt));
|
|
8
|
+
}, [sdk.appInfo?.premiumFeatureList]);
|
|
9
|
+
|
|
10
|
+
const broadcastChannelEnabled = useMemo(() => {
|
|
11
|
+
return Boolean(sdk.appInfo?.applicationAttributes?.includes?.(ApplicationAttributes.allow_broadcast_channel));
|
|
12
|
+
}, [sdk.appInfo?.applicationAttributes]);
|
|
13
|
+
|
|
14
|
+
const superGroupChannelEnabled = useMemo(() => {
|
|
15
|
+
return Boolean(sdk.appInfo?.applicationAttributes?.includes?.(ApplicationAttributes.allow_super_group_channel));
|
|
16
|
+
}, [sdk.appInfo?.applicationAttributes]);
|
|
17
|
+
|
|
18
|
+
const reactionEnabled = useMemo(() => {
|
|
19
|
+
return Boolean(sdk.appInfo?.applicationAttributes?.includes?.(ApplicationAttributes.reactions));
|
|
20
|
+
}, [sdk.appInfo?.applicationAttributes]);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
deliveryReceiptEnabled,
|
|
24
|
+
broadcastChannelEnabled,
|
|
25
|
+
superGroupChannelEnabled,
|
|
26
|
+
reactionEnabled,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import type { SendbirdChatSDK } from '@sendbird/uikit-utils';
|
|
4
|
+
import { Logger, useAsyncEffect } from '@sendbird/uikit-utils';
|
|
5
|
+
|
|
6
|
+
type PushTriggerOptionType = 'all' | 'mention_only' | 'off';
|
|
7
|
+
export const usePushTrigger = (sdk: SendbirdChatSDK) => {
|
|
8
|
+
const [option, setOption] = useState<PushTriggerOptionType>('off');
|
|
9
|
+
|
|
10
|
+
const updateOption = useCallback(
|
|
11
|
+
async (value: PushTriggerOptionType) => {
|
|
12
|
+
try {
|
|
13
|
+
await sdk.setPushTriggerOption(value);
|
|
14
|
+
setOption(value);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
Logger.warn('[usePushTrigger]', 'Cannot update push trigger option', e);
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
[sdk, sdk.currentUser],
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
useAsyncEffect(async () => {
|
|
23
|
+
const currentOption = await sdk.getPushTriggerOption();
|
|
24
|
+
setOption(currentOption as PushTriggerOptionType);
|
|
25
|
+
}, [sdk, sdk.currentUser]);
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
option,
|
|
29
|
+
updateOption,
|
|
30
|
+
};
|
|
31
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import type { SendbirdChatSDK } from '@sendbird/uikit-utils';
|
|
4
|
+
import { truncatedBadgeCount, useAsyncEffect } from '@sendbird/uikit-utils';
|
|
5
|
+
import { useUniqId } from '@sendbird/uikit-utils';
|
|
6
|
+
|
|
7
|
+
import { useUserEventHandler } from '../handler/useUserEventHandler';
|
|
8
|
+
|
|
9
|
+
type Params = {
|
|
10
|
+
maxCount?: number;
|
|
11
|
+
};
|
|
12
|
+
const HOOK_NAME = 'useTotalUnreadChannelCount';
|
|
13
|
+
export const useTotalUnreadChannelCount = (sdk: SendbirdChatSDK, params?: Params) => {
|
|
14
|
+
const id = useUniqId(HOOK_NAME);
|
|
15
|
+
const [unreadChannelCount, setUnreadChannelCount] = useState(0);
|
|
16
|
+
|
|
17
|
+
useAsyncEffect(async () => {
|
|
18
|
+
setUnreadChannelCount(await sdk.getTotalUnreadChannelCount());
|
|
19
|
+
}, [sdk]);
|
|
20
|
+
|
|
21
|
+
useUserEventHandler(sdk, HOOK_NAME + id, {
|
|
22
|
+
onTotalUnreadMessageCountUpdated: async () => setUnreadChannelCount(await sdk.getTotalUnreadChannelCount()),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return truncatedBadgeCount(unreadChannelCount, params?.maxCount);
|
|
26
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import type Sendbird from 'sendbird';
|
|
3
|
+
|
|
4
|
+
import type { SendbirdChatSDK } from '@sendbird/uikit-utils';
|
|
5
|
+
import { truncatedBadgeCount, useAsyncEffect } from '@sendbird/uikit-utils';
|
|
6
|
+
import { useUniqId } from '@sendbird/uikit-utils';
|
|
7
|
+
|
|
8
|
+
import { useUserEventHandler } from '../handler/useUserEventHandler';
|
|
9
|
+
|
|
10
|
+
type Params = {
|
|
11
|
+
paramCreator?: () => Sendbird.GroupChannelTotalUnreadMessageCountParams;
|
|
12
|
+
maxCount?: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const HOOK_NAME = 'useTotalUnreadMessageCount';
|
|
16
|
+
export const useTotalUnreadMessageCount = (sdk: SendbirdChatSDK, params?: Params) => {
|
|
17
|
+
const id = useUniqId(HOOK_NAME);
|
|
18
|
+
const [unreadMessageCount, setUnreadMessageCount] = useState(0);
|
|
19
|
+
|
|
20
|
+
useAsyncEffect(async () => {
|
|
21
|
+
let param;
|
|
22
|
+
if (params?.paramCreator) {
|
|
23
|
+
param = params.paramCreator();
|
|
24
|
+
} else {
|
|
25
|
+
const params = new sdk.GroupChannelTotalUnreadMessageCountParams();
|
|
26
|
+
params.superChannelFilter = 'all';
|
|
27
|
+
param = params;
|
|
28
|
+
}
|
|
29
|
+
const unreadCount = await sdk.getTotalUnreadMessageCount(param);
|
|
30
|
+
setUnreadMessageCount(unreadCount);
|
|
31
|
+
}, [sdk, params?.paramCreator]);
|
|
32
|
+
|
|
33
|
+
useUserEventHandler(
|
|
34
|
+
sdk,
|
|
35
|
+
`${HOOK_NAME}_${id}`,
|
|
36
|
+
{
|
|
37
|
+
onTotalUnreadMessageCountUpdated: (totalCount: number) => setUnreadMessageCount(totalCount),
|
|
38
|
+
},
|
|
39
|
+
[sdk],
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
return truncatedBadgeCount(unreadMessageCount, params?.maxCount);
|
|
43
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { Optional, SendbirdChatSDK, SendbirdUser, useAsyncEffect } from '@sendbird/uikit-utils';
|
|
4
|
+
|
|
5
|
+
import type { CustomQueryInterface, UseUserListOptions, UseUserListReturn } from '../types';
|
|
6
|
+
|
|
7
|
+
const createUserQuery = <User>(sdk: SendbirdChatSDK, queryCreator?: UseUserListOptions<User>['queryCreator']) => {
|
|
8
|
+
if (queryCreator) return queryCreator();
|
|
9
|
+
return sdk.createApplicationUserListQuery() as unknown as CustomQueryInterface<User>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Get user list from query.
|
|
14
|
+
* default query uses 'instance.createApplicationUserListQuery'
|
|
15
|
+
* The response type of hook is depends on return type of 'query.next()'
|
|
16
|
+
*
|
|
17
|
+
* You can call hook with your custom query using {@link CustomQuery}
|
|
18
|
+
* Or you can create your 'CustomQueryClass' implemented {@link CustomQueryInterface}'
|
|
19
|
+
*
|
|
20
|
+
* ```example
|
|
21
|
+
* const { users } = useUserList(sdk, {
|
|
22
|
+
* queryCreator: () => {
|
|
23
|
+
* const friendQuery = sdk.createFriendListQuery();
|
|
24
|
+
* return new CustomQuery({
|
|
25
|
+
* next: () => friendQuery.next(),
|
|
26
|
+
* isLoading: () => friendQuery.isLoading,
|
|
27
|
+
* hasNext: () => friendQuery.hasMore,
|
|
28
|
+
* });
|
|
29
|
+
* }
|
|
30
|
+
* })
|
|
31
|
+
* ```
|
|
32
|
+
* */
|
|
33
|
+
export const useUserList = <
|
|
34
|
+
Options extends UseUserListOptions<QueriedUser>,
|
|
35
|
+
QueriedUser = Options['queryCreator'] extends Optional<() => CustomQueryInterface<infer User>> ? User : SendbirdUser,
|
|
36
|
+
>(
|
|
37
|
+
sdk: SendbirdChatSDK,
|
|
38
|
+
options?: Options,
|
|
39
|
+
): UseUserListReturn<QueriedUser> => {
|
|
40
|
+
const query = useRef<CustomQueryInterface<QueriedUser>>();
|
|
41
|
+
|
|
42
|
+
const [error, setError] = useState<unknown>(null);
|
|
43
|
+
const [loading, setLoading] = useState(false);
|
|
44
|
+
const [refreshing, setRefreshing] = useState(false);
|
|
45
|
+
|
|
46
|
+
const [users, setUsers] = useState<QueriedUser[]>([]);
|
|
47
|
+
const sortedUsers = useMemo((): QueriedUser[] => {
|
|
48
|
+
if (options?.sortComparator) return users.sort(options.sortComparator);
|
|
49
|
+
return users;
|
|
50
|
+
}, [users, options?.sortComparator]);
|
|
51
|
+
|
|
52
|
+
const updateUsers = (users: QueriedUser[], clearPrev: boolean) => {
|
|
53
|
+
if (clearPrev) setUsers(users);
|
|
54
|
+
else setUsers((prev) => prev.concat(users));
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const init = useCallback(async () => {
|
|
58
|
+
query.current = createUserQuery<QueriedUser>(sdk, options?.queryCreator);
|
|
59
|
+
if (query.current?.hasNext) {
|
|
60
|
+
const users = await query.current?.next();
|
|
61
|
+
updateUsers(users, true);
|
|
62
|
+
}
|
|
63
|
+
}, [sdk, options?.queryCreator]);
|
|
64
|
+
|
|
65
|
+
useAsyncEffect(async () => {
|
|
66
|
+
setLoading(true);
|
|
67
|
+
setError(null);
|
|
68
|
+
try {
|
|
69
|
+
await init();
|
|
70
|
+
} catch (e) {
|
|
71
|
+
setError(e);
|
|
72
|
+
} finally {
|
|
73
|
+
setLoading(false);
|
|
74
|
+
}
|
|
75
|
+
}, [init]);
|
|
76
|
+
|
|
77
|
+
const refresh = useCallback(async () => {
|
|
78
|
+
setRefreshing(true);
|
|
79
|
+
setError(null);
|
|
80
|
+
try {
|
|
81
|
+
await init();
|
|
82
|
+
} catch (e) {
|
|
83
|
+
setError(e);
|
|
84
|
+
} finally {
|
|
85
|
+
setRefreshing(false);
|
|
86
|
+
}
|
|
87
|
+
}, [init]);
|
|
88
|
+
|
|
89
|
+
const next = useCallback(async () => {
|
|
90
|
+
if (query.current && query.current?.hasNext) {
|
|
91
|
+
updateUsers(await query.current?.next(), false);
|
|
92
|
+
}
|
|
93
|
+
}, []);
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
loading,
|
|
97
|
+
error,
|
|
98
|
+
users: sortedUsers,
|
|
99
|
+
next,
|
|
100
|
+
refreshing,
|
|
101
|
+
refresh,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React, { useEffect } from 'react';
|
|
2
|
+
import type Sendbird from 'sendbird';
|
|
3
|
+
|
|
4
|
+
import { Logger, SendbirdChatSDK } from '@sendbird/uikit-utils';
|
|
5
|
+
|
|
6
|
+
export const useChannelHandler = (
|
|
7
|
+
sdk: SendbirdChatSDK,
|
|
8
|
+
handlerId: string,
|
|
9
|
+
hookHandler: Partial<Sendbird.ChannelHandler>,
|
|
10
|
+
deps: React.DependencyList = [],
|
|
11
|
+
) => {
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
Logger.debug('[useChannelHandler] hook called by', handlerId);
|
|
14
|
+
|
|
15
|
+
const handler = new sdk.ChannelHandler();
|
|
16
|
+
const handlerKeys = Object.keys(handler) as (keyof typeof handler)[];
|
|
17
|
+
handlerKeys.forEach((key) => {
|
|
18
|
+
const hookHandlerFn = hookHandler[key];
|
|
19
|
+
if (hookHandlerFn) handler[key] = hookHandlerFn as () => unknown;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
sdk.addChannelHandler(handlerId, handler);
|
|
23
|
+
return () => sdk.removeChannelHandler(handlerId);
|
|
24
|
+
}, [sdk, handlerId, ...deps]);
|
|
25
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React, { useEffect } from 'react';
|
|
2
|
+
import type Sendbird from 'sendbird';
|
|
3
|
+
|
|
4
|
+
import { Logger, SendbirdChatSDK } from '@sendbird/uikit-utils';
|
|
5
|
+
|
|
6
|
+
export const useConnectionHandler = (
|
|
7
|
+
sdk: SendbirdChatSDK,
|
|
8
|
+
handlerId: string,
|
|
9
|
+
hookHandler: Partial<Sendbird.ConnectionHandler>,
|
|
10
|
+
deps: React.DependencyList = [],
|
|
11
|
+
) => {
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
Logger.info('[useConnectionHandler]', handlerId);
|
|
14
|
+
|
|
15
|
+
const handler = new sdk.ConnectionHandler();
|
|
16
|
+
const handlerKeys = Object.keys(handler) as (keyof typeof handler)[];
|
|
17
|
+
handlerKeys.forEach((key) => {
|
|
18
|
+
const hookHandlerFn = hookHandler[key];
|
|
19
|
+
if (hookHandlerFn) handler[key] = hookHandlerFn;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
sdk.addConnectionHandler(handlerId, handler);
|
|
23
|
+
return () => sdk.removeConnectionHandler(handlerId);
|
|
24
|
+
}, [sdk, handlerId, ...deps]);
|
|
25
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React, { useEffect } from 'react';
|
|
2
|
+
import type Sendbird from 'sendbird';
|
|
3
|
+
|
|
4
|
+
import type { SendbirdChatSDK } from '@sendbird/uikit-utils';
|
|
5
|
+
import { Logger } from '@sendbird/uikit-utils';
|
|
6
|
+
|
|
7
|
+
export const useUserEventHandler = (
|
|
8
|
+
sdk: SendbirdChatSDK,
|
|
9
|
+
handlerId: string,
|
|
10
|
+
hookHandler: Partial<Sendbird.UserEventHandler>,
|
|
11
|
+
deps: React.DependencyList = [],
|
|
12
|
+
) => {
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
Logger.debug('[useUserEventHandler] hook called by', handlerId);
|
|
15
|
+
|
|
16
|
+
const handler = new sdk.UserEventHandler();
|
|
17
|
+
const handlerKeys = Object.keys(handler) as (keyof typeof handler)[];
|
|
18
|
+
handlerKeys.forEach((key) => {
|
|
19
|
+
const hookHandlerFn = hookHandler[key];
|
|
20
|
+
if (hookHandlerFn) handler[key] = hookHandlerFn as () => unknown;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
sdk.addUserEventHandler(handlerId, handler);
|
|
24
|
+
return () => sdk.removeUserEventHandler(handlerId);
|
|
25
|
+
}, [sdk, handlerId, ...deps]);
|
|
26
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export { useGroupChannelList } from './channel/useGroupChannelList';
|
|
2
|
+
export { useGroupChannelMessages } from './channel/useGroupChannelMessages';
|
|
3
|
+
export { useActiveGroupChannel } from './channel/useActiveGroupChannel';
|
|
4
|
+
|
|
5
|
+
export { useUserList } from './common/useUserList';
|
|
6
|
+
export { usePushTrigger } from './common/usePushTrigger';
|
|
7
|
+
export { useTotalUnreadMessageCount } from './common/useTotalUnreadMessageCount';
|
|
8
|
+
export { useTotalUnreadChannelCount } from './common/useTotalUnreadChannelCount';
|
|
9
|
+
export { useAppFeatures } from './common/useAppFeatures';
|
|
10
|
+
|
|
11
|
+
export { useChannelHandler } from './handler/useChannelHandler';
|
|
12
|
+
export { useConnectionHandler } from './handler/useConnectionHandler';
|
|
13
|
+
export { useUserEventHandler } from './handler/useUserEventHandler';
|
|
14
|
+
|
|
15
|
+
export { CustomQuery } from './model/CustomQuery';
|
|
16
|
+
|
|
17
|
+
export type {
|
|
18
|
+
UseGroupChannelList,
|
|
19
|
+
UseGroupChannelListOptions,
|
|
20
|
+
UseGroupChannelMessages,
|
|
21
|
+
UseUserListOptions,
|
|
22
|
+
UseGroupChannelMessagesOptions,
|
|
23
|
+
UseUserListReturn,
|
|
24
|
+
CustomQueryInterface,
|
|
25
|
+
CustomBidirectionalQueryInterface,
|
|
26
|
+
} from './types';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CustomQueryInterface } from '../types';
|
|
2
|
+
|
|
3
|
+
type ConstructorParams<T> = {
|
|
4
|
+
next: () => Promise<T[]>;
|
|
5
|
+
isLoading: () => boolean;
|
|
6
|
+
hasNext: () => boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export class CustomQuery<T> implements CustomQueryInterface<T> {
|
|
10
|
+
constructor(private params: ConstructorParams<T>) {}
|
|
11
|
+
get isLoading(): boolean {
|
|
12
|
+
return this.params.isLoading();
|
|
13
|
+
}
|
|
14
|
+
get hasNext(): boolean {
|
|
15
|
+
return this.params.hasNext();
|
|
16
|
+
}
|
|
17
|
+
next() {
|
|
18
|
+
return this.params.next();
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import type Sendbird from 'sendbird';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
SendbirdChatSDK,
|
|
5
|
+
SendbirdError,
|
|
6
|
+
SendbirdFileMessage,
|
|
7
|
+
SendbirdFileMessageParams,
|
|
8
|
+
SendbirdGroupChannel,
|
|
9
|
+
SendbirdGroupChannelCollection,
|
|
10
|
+
SendbirdMessage,
|
|
11
|
+
SendbirdMessageCollection,
|
|
12
|
+
SendbirdUserMessage,
|
|
13
|
+
SendbirdUserMessageParams,
|
|
14
|
+
} from '@sendbird/uikit-utils';
|
|
15
|
+
|
|
16
|
+
export interface CustomQueryInterface<Data> {
|
|
17
|
+
isLoading: boolean;
|
|
18
|
+
next: () => Promise<Data[]>;
|
|
19
|
+
hasNext: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface CustomBidirectionalQueryInterface<Data> {
|
|
22
|
+
isLoading: boolean;
|
|
23
|
+
next: () => Promise<Data[]>;
|
|
24
|
+
hasNext: () => boolean;
|
|
25
|
+
prev: () => Promise<Data[]>;
|
|
26
|
+
hasPrev: () => boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @interface UseGroupChannelList
|
|
31
|
+
* @description interface for group channel list hook
|
|
32
|
+
* */
|
|
33
|
+
export interface UseGroupChannelList {
|
|
34
|
+
(sdk: SendbirdChatSDK, userId?: string, options?: UseGroupChannelListOptions): {
|
|
35
|
+
/**
|
|
36
|
+
* Loading state, only available on first render
|
|
37
|
+
* */
|
|
38
|
+
loading: boolean;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Refreshing state, status is changes when the refresh is called.
|
|
42
|
+
* */
|
|
43
|
+
refreshing: boolean;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Refresh, clear and reload messages from latest
|
|
47
|
+
* @return {Promise<void>}
|
|
48
|
+
* */
|
|
49
|
+
refresh: () => Promise<void>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Update channel, update or insert channel to state
|
|
53
|
+
* @param {SendbirdGroupChannel} channel
|
|
54
|
+
* @return {void}
|
|
55
|
+
* */
|
|
56
|
+
update: (channel: SendbirdGroupChannel) => void;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get group channels state
|
|
60
|
+
* */
|
|
61
|
+
groupChannels: SendbirdGroupChannel[];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Fetch next channels to state
|
|
65
|
+
* @return {Promise<void>}
|
|
66
|
+
* */
|
|
67
|
+
next: () => Promise<void>;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export type UseGroupChannelListOptions = {
|
|
71
|
+
sortComparator?: (a: SendbirdGroupChannel, b: SendbirdGroupChannel) => number;
|
|
72
|
+
queryCreator?: () => Sendbird.GroupChannelListQuery;
|
|
73
|
+
collectionCreator?: () => SendbirdGroupChannelCollection;
|
|
74
|
+
enableCollectionWithoutLocalCache?: boolean;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @interface UseGroupChannelMessages
|
|
79
|
+
* @description interface for group channel messages hook
|
|
80
|
+
* - Receive new messages from other users -> append to state(nextMessages)
|
|
81
|
+
* - onTopReached -> prev() -> fetch prev messages and append to state(messages)
|
|
82
|
+
* - onBottomReached -> next() -> nextMessages append to state(messages)
|
|
83
|
+
* */
|
|
84
|
+
export interface UseGroupChannelMessages {
|
|
85
|
+
(sdk: SendbirdChatSDK, channel: SendbirdGroupChannel, userId?: string, options?: UseGroupChannelMessagesOptions): {
|
|
86
|
+
/**
|
|
87
|
+
* Loading state, only available on first render
|
|
88
|
+
* */
|
|
89
|
+
loading: boolean;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Refreshing state, status is changes when the refresh is called.
|
|
93
|
+
* */
|
|
94
|
+
refreshing: boolean;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Refresh, clear and reload messages from latest
|
|
98
|
+
* @return {Promise<void>}
|
|
99
|
+
* */
|
|
100
|
+
refresh: () => Promise<void>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Get messages, this state is for render
|
|
104
|
+
* For example, if a user receives a new messages while searching for an old message
|
|
105
|
+
* for this case, new messages will be included here.
|
|
106
|
+
* @return {SendbirdMessage[]}
|
|
107
|
+
* */
|
|
108
|
+
messages: SendbirdMessage[];
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Fetch prev messages to state
|
|
112
|
+
* @return {Promise<void>}
|
|
113
|
+
* */
|
|
114
|
+
prev: () => Promise<void>;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Get messages, this state is for temporary data before render
|
|
118
|
+
* For example, if a user receives a new messages while searching for an old message
|
|
119
|
+
* for this case, new messages will be included here.
|
|
120
|
+
* */
|
|
121
|
+
nextMessages: SendbirdMessage[];
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Get new messages from nextMessages
|
|
125
|
+
* A new message means a message that meets the below conditions
|
|
126
|
+
* - Not admin message
|
|
127
|
+
* - Not updated message
|
|
128
|
+
* - Not current user's message
|
|
129
|
+
* */
|
|
130
|
+
newMessagesFromMembers: SendbirdMessage[];
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Fetch next messages to state
|
|
134
|
+
* @return {Promise<void>}
|
|
135
|
+
* */
|
|
136
|
+
next: () => Promise<void>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Send file message
|
|
140
|
+
* @param params file message params
|
|
141
|
+
* @param callback sent message callback
|
|
142
|
+
* @return pending message
|
|
143
|
+
* */
|
|
144
|
+
sendFileMessage: (
|
|
145
|
+
params: SendbirdFileMessageParams,
|
|
146
|
+
onPending?: (message: SendbirdFileMessage, error?: SendbirdError) => void,
|
|
147
|
+
) => Promise<SendbirdFileMessage>;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Send user message
|
|
151
|
+
* @param params user message params
|
|
152
|
+
* @param callback sent message callback
|
|
153
|
+
* @return pending message
|
|
154
|
+
* */
|
|
155
|
+
sendUserMessage: (
|
|
156
|
+
params: SendbirdUserMessageParams,
|
|
157
|
+
onPending?: (message: SendbirdUserMessage, error?: SendbirdError) => void,
|
|
158
|
+
) => Promise<SendbirdUserMessage>;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Update file message
|
|
162
|
+
* @param messageId
|
|
163
|
+
* @param params file message params
|
|
164
|
+
* @return updated message
|
|
165
|
+
* */
|
|
166
|
+
updateFileMessage: (messageId: number, params: SendbirdFileMessageParams) => Promise<SendbirdFileMessage>;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Update user message
|
|
170
|
+
* @param messageId
|
|
171
|
+
* @param params user message params
|
|
172
|
+
* @return updated message
|
|
173
|
+
* */
|
|
174
|
+
updateUserMessage: (messageId: number, params: SendbirdUserMessageParams) => Promise<SendbirdUserMessage>;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Resend failed message
|
|
178
|
+
* @param failedMessage failed message to resend
|
|
179
|
+
* @return {Promise<void>}
|
|
180
|
+
* */
|
|
181
|
+
resendMessage: (failedMessage: SendbirdFileMessage | SendbirdUserMessage) => Promise<void>;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Delete message
|
|
185
|
+
* @param message sent or failed message
|
|
186
|
+
* @return {Promise<void>}
|
|
187
|
+
* */
|
|
188
|
+
deleteMessage: (message: SendbirdFileMessage | SendbirdUserMessage) => Promise<void>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Activated channel
|
|
192
|
+
* */
|
|
193
|
+
activeChannel: SendbirdGroupChannel;
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export type UseGroupChannelMessagesOptions = {
|
|
198
|
+
sortComparator?: (a: SendbirdMessage, b: SendbirdMessage) => number;
|
|
199
|
+
queryCreator?: () => Sendbird.PreviousMessageListQuery;
|
|
200
|
+
collectionCreator?: () => SendbirdMessageCollection;
|
|
201
|
+
enableCollectionWithoutLocalCache?: boolean;
|
|
202
|
+
onChannelDeleted?: () => void;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* @interface UseUserListReturn
|
|
207
|
+
* @description interface for user list hook return value
|
|
208
|
+
* */
|
|
209
|
+
export interface UseUserListReturn<User> {
|
|
210
|
+
/**
|
|
211
|
+
* Loading state, only available on first render
|
|
212
|
+
* */
|
|
213
|
+
loading: boolean;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Refreshing state, status is changes when the refresh is called.
|
|
217
|
+
* */
|
|
218
|
+
refreshing: boolean;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Error state
|
|
222
|
+
* */
|
|
223
|
+
error: unknown | null;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Refresh, clear and reload messages from latest
|
|
227
|
+
* @return {Promise<void>}
|
|
228
|
+
* */
|
|
229
|
+
refresh: () => Promise<void>;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Get users state
|
|
233
|
+
* */
|
|
234
|
+
users: User[];
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Fetch next users to state
|
|
238
|
+
* @return {Promise<void>}
|
|
239
|
+
* */
|
|
240
|
+
next: () => Promise<void>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export type UseUserListOptions<User> = {
|
|
244
|
+
sortComparator?: (a: User, b: User) => number;
|
|
245
|
+
queryCreator?: () => CustomQueryInterface<User>;
|
|
246
|
+
};
|