@webinex/chatify 2.0.0-build10 → 2.0.0-build2

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 (131) hide show
  1. package/dist/esm/index.js +2 -2
  2. package/dist/esm/index.js.map +1 -1
  3. package/dist/esm/types/core/api/accountApi.d.ts +3 -3
  4. package/dist/esm/types/core/api/api.d.ts +1751 -1593
  5. package/dist/esm/types/core/api/baseApi.d.ts +1 -6
  6. package/dist/esm/types/core/api/chatApi.d.ts +11 -2
  7. package/dist/esm/types/core/api/chatListApi.d.ts +4 -3
  8. package/dist/esm/types/core/api/chatMemberApi.d.ts +3 -3
  9. package/dist/esm/types/core/api/chatMessageApi.d.ts +34 -3
  10. package/dist/esm/types/core/api/index.d.ts +8 -0
  11. package/dist/esm/types/core/api/threadApi.d.ts +95 -1
  12. package/dist/esm/types/core/api/threadMessageApi.d.ts +24 -2
  13. package/package.json +6 -5
  14. package/src/core/api/accountApi.ts +7 -5
  15. package/src/core/api/api.ts +42 -642
  16. package/src/core/api/baseApi.ts +7 -15
  17. package/src/core/api/chatApi.ts +8 -6
  18. package/src/core/api/chatListApi.ts +20 -5
  19. package/src/core/api/chatMemberApi.ts +7 -5
  20. package/src/core/api/chatMessageApi.ts +148 -4
  21. package/src/core/api/index.ts +8 -0
  22. package/src/core/api/threadApi.ts +125 -1
  23. package/src/core/api/threadMessageApi.ts +108 -1
  24. package/src/ui/Chat/ChatEditableHeaderName.tsx +2 -2
  25. package/src/ui/Chat/ChatPanel.tsx +2 -2
  26. package/src/ui/Chat/ChatView.tsx +14 -6
  27. package/src/ui/Chat/Members/ChatMembersPanel.tsx +12 -7
  28. package/src/ui/Chat/Members/ChatMembersPreview.tsx +2 -2
  29. package/src/ui/ChatGroup/ChatGroupPanel.tsx +2 -2
  30. package/src/ui/ChatGroup/Create/CreateChatMemberInput.tsx +2 -2
  31. package/src/ui/ChatGroup/Create/CreateChatPanel.tsx +2 -2
  32. package/src/ui/ChatGroup/Layout/ChatGroupMain.tsx +2 -2
  33. package/src/ui/ChatGroup/List/ChatList.tsx +2 -2
  34. package/src/ui/Conversation/NextObserver/NextMessagesObserver.tsx +2 -2
  35. package/src/ui/Thread/ThreadPanel.tsx +19 -8
  36. package/dist/index.d.ts +0 -3004
  37. package/src/types/core/api/accountApi.d.ts +0 -5
  38. package/src/types/core/api/api.d.ts +0 -2380
  39. package/src/types/core/api/baseApi.d.ts +0 -15
  40. package/src/types/core/api/chatApi.d.ts +0 -16
  41. package/src/types/core/api/chatListApi.d.ts +0 -5
  42. package/src/types/core/api/chatMemberApi.d.ts +0 -6
  43. package/src/types/core/api/chatMessageApi.d.ts +0 -3
  44. package/src/types/core/api/index.d.ts +0 -1
  45. package/src/types/core/api/threadApi.d.ts +0 -1
  46. package/src/types/core/api/threadMessageApi.d.ts +0 -6
  47. package/src/types/core/client.d.ts +0 -83
  48. package/src/types/core/constants.d.ts +0 -1
  49. package/src/types/core/debounce.d.ts +0 -27
  50. package/src/types/core/index.d.ts +0 -4
  51. package/src/types/core/types.d.ts +0 -105
  52. package/src/types/index.d.ts +0 -2
  53. package/src/types/ui/Chat/ChatContext.d.ts +0 -10
  54. package/src/types/ui/Chat/ChatEditableHeaderName.d.ts +0 -2
  55. package/src/types/ui/Chat/ChatHeaderActions.d.ts +0 -2
  56. package/src/types/ui/Chat/ChatPanel.d.ts +0 -13
  57. package/src/types/ui/Chat/ChatView.d.ts +0 -17
  58. package/src/types/ui/Chat/Members/ChatMembersButton.d.ts +0 -2
  59. package/src/types/ui/Chat/Members/ChatMembersPanel.d.ts +0 -2
  60. package/src/types/ui/Chat/Members/ChatMembersPreview.d.ts +0 -2
  61. package/src/types/ui/Chat/Members/index.d.ts +0 -11
  62. package/src/types/ui/Chat/index.d.ts +0 -6
  63. package/src/types/ui/ChatGroup/ChatGroupContext.d.ts +0 -12
  64. package/src/types/ui/ChatGroup/ChatGroupPanel.d.ts +0 -15
  65. package/src/types/ui/ChatGroup/Create/CreateChatButton.d.ts +0 -2
  66. package/src/types/ui/ChatGroup/Create/CreateChatForm.d.ts +0 -11
  67. package/src/types/ui/ChatGroup/Create/CreateChatMemberInput.d.ts +0 -5
  68. package/src/types/ui/ChatGroup/Create/CreateChatNameInput.d.ts +0 -5
  69. package/src/types/ui/ChatGroup/Create/CreateChatPanel.d.ts +0 -2
  70. package/src/types/ui/ChatGroup/Create/index.d.ts +0 -17
  71. package/src/types/ui/ChatGroup/Layout/ChatGroupAside.d.ts +0 -2
  72. package/src/types/ui/ChatGroup/Layout/ChatGroupBody.d.ts +0 -2
  73. package/src/types/ui/ChatGroup/Layout/ChatGroupFooter.d.ts +0 -2
  74. package/src/types/ui/ChatGroup/Layout/ChatGroupHeader.d.ts +0 -2
  75. package/src/types/ui/ChatGroup/Layout/ChatGroupMain.d.ts +0 -2
  76. package/src/types/ui/ChatGroup/Layout/index.d.ts +0 -17
  77. package/src/types/ui/ChatGroup/List/ChatList.d.ts +0 -2
  78. package/src/types/ui/ChatGroup/List/ChatListItemAvatar.d.ts +0 -3
  79. package/src/types/ui/ChatGroup/List/ChatListItemBox.d.ts +0 -6
  80. package/src/types/ui/ChatGroup/List/ChatListItemLastMessage.d.ts +0 -3
  81. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageAuthor.d.ts +0 -3
  82. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageContent.d.ts +0 -3
  83. package/src/types/ui/ChatGroup/List/ChatListItemLastMessageSentAt.d.ts +0 -3
  84. package/src/types/ui/ChatGroup/List/ChatListItemName.d.ts +0 -3
  85. package/src/types/ui/ChatGroup/List/ChatListItemUnreadCount.d.ts +0 -3
  86. package/src/types/ui/ChatGroup/List/index.d.ts +0 -29
  87. package/src/types/ui/ChatGroup/index.d.ts +0 -5
  88. package/src/types/ui/Chatify.d.ts +0 -8
  89. package/src/types/ui/Conversation/Conversation.d.ts +0 -21
  90. package/src/types/ui/Conversation/ConversationActions.d.ts +0 -2
  91. package/src/types/ui/Conversation/ConversationBody.d.ts +0 -2
  92. package/src/types/ui/Conversation/ConversationContext.d.ts +0 -20
  93. package/src/types/ui/Conversation/ConversationHeader.d.ts +0 -2
  94. package/src/types/ui/Conversation/ConversationName.d.ts +0 -2
  95. package/src/types/ui/Conversation/InputBox/InputBox.d.ts +0 -2
  96. package/src/types/ui/Conversation/InputBox/InputFilesBox.d.ts +0 -2
  97. package/src/types/ui/Conversation/InputBox/InputSubmitButtonBox.d.ts +0 -5
  98. package/src/types/ui/Conversation/InputBox/InputTextBox.d.ts +0 -8
  99. package/src/types/ui/Conversation/InputBox/index.d.ts +0 -14
  100. package/src/types/ui/Conversation/Message/MessageAuthor.d.ts +0 -3
  101. package/src/types/ui/Conversation/Message/MessageBox.d.ts +0 -6
  102. package/src/types/ui/Conversation/Message/MessageContent.d.ts +0 -3
  103. package/src/types/ui/Conversation/Message/MessageInfoBox.d.ts +0 -3
  104. package/src/types/ui/Conversation/Message/MessageRow.d.ts +0 -6
  105. package/src/types/ui/Conversation/Message/MessageText.d.ts +0 -3
  106. package/src/types/ui/Conversation/Message/index.d.ts +0 -20
  107. package/src/types/ui/Conversation/NextObserver/NextMessagesObserver.d.ts +0 -5
  108. package/src/types/ui/Conversation/NextObserver/index.d.ts +0 -1
  109. package/src/types/ui/Conversation/ReadObserver/MessageReadObserver.d.ts +0 -6
  110. package/src/types/ui/Conversation/ReadObserver/index.d.ts +0 -1
  111. package/src/types/ui/Conversation/SendingMessage/SendingMessageBox.d.ts +0 -7
  112. package/src/types/ui/Conversation/SendingMessage/SendingMessageContent.d.ts +0 -3
  113. package/src/types/ui/Conversation/SendingMessage/SendingMessageInfoBox.d.ts +0 -3
  114. package/src/types/ui/Conversation/SendingMessage/SendingMessageText.d.ts +0 -3
  115. package/src/types/ui/Conversation/SendingMessage/index.d.ts +0 -14
  116. package/src/types/ui/Conversation/SystemMessage/SystemMessageBox.d.ts +0 -6
  117. package/src/types/ui/Conversation/SystemMessage/SystemMessageRow.d.ts +0 -9
  118. package/src/types/ui/Conversation/SystemMessage/index.d.ts +0 -8
  119. package/src/types/ui/Conversation/index.d.ts +0 -12
  120. package/src/types/ui/Thread/ThreadActions.d.ts +0 -2
  121. package/src/types/ui/Thread/ThreadContext.d.ts +0 -8
  122. package/src/types/ui/Thread/ThreadPanel.d.ts +0 -14
  123. package/src/types/ui/Thread/index.d.ts +0 -3
  124. package/src/types/ui/color.d.ts +0 -6
  125. package/src/types/ui/common/Avatar.d.ts +0 -6
  126. package/src/types/ui/common/Icon.d.ts +0 -20
  127. package/src/types/ui/common/MessageSkeleton.d.ts +0 -5
  128. package/src/types/ui/common/index.d.ts +0 -11
  129. package/src/types/ui/customize.d.ts +0 -9
  130. package/src/types/ui/index.d.ts +0 -11
  131. package/src/types/ui/localizer.d.ts +0 -35
@@ -1,4 +1,4 @@
1
- import { BaseQueryFn, EndpointBuilder, createApi, fakeBaseQuery } from '@reduxjs/toolkit/query/react';
1
+ import { createApi, fakeBaseQuery } from '@reduxjs/toolkit/query/react';
2
2
  import { ChatifyClient } from '../client';
3
3
  import { Debounce } from '../debounce';
4
4
  import { chatId } from '../types';
@@ -22,17 +22,9 @@ export const __settings: ChatifyApiSettings = {
22
22
  client: null!,
23
23
  };
24
24
 
25
- const TAG_TYPES = ['chat', 'account', 'message', 'thread', 'thread-message'] as const;
26
-
27
- export interface BaseApiEndpointBuilder
28
- extends EndpointBuilder<BaseQueryFn, (typeof TAG_TYPES)[number], 'chatify'> {}
29
-
30
- const baseApi: ReturnType<typeof createApi<BaseQueryFn, {}, 'chatify', (typeof TAG_TYPES)[number]>> =
31
- createApi({
32
- baseQuery: fakeBaseQuery(),
33
- reducerPath: 'chatify',
34
- tagTypes: TAG_TYPES,
35
- endpoints: (_) => ({}),
36
- });
37
-
38
- export const __baseApi = baseApi;
25
+ export const __baseApi = createApi({
26
+ baseQuery: fakeBaseQuery(),
27
+ reducerPath: 'chatify',
28
+ tagTypes: ['chat', 'account', 'message', 'thread', 'thread-message'],
29
+ endpoints: (_) => ({}),
30
+ });
@@ -1,17 +1,16 @@
1
- import { BaseApiEndpointBuilder, __baseApi, __settings } from './baseApi';
1
+ import { __baseApi, __settings } from './baseApi';
2
2
  import { unsubscribe } from '../client';
3
3
  import { Chat } from '../types';
4
4
 
5
- export function chatEndpoints(builder: BaseApiEndpointBuilder) {
6
- return {};
7
- }
8
-
9
5
  const chatApi = __baseApi.injectEndpoints({
10
6
  endpoints: (builder) => ({
11
7
  getChat: builder.query<Chat, { id: string }>({
12
8
  queryFn: async ({ id }) => ({ data: await __settings.client.chat(id) }),
13
9
  providesTags: ['chat', 'account'],
14
- onCacheEntryAdded: async ({ id }, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
10
+ onCacheEntryAdded: async (
11
+ { id },
12
+ { dispatch, cacheEntryRemoved, cacheDataLoaded, updateCachedData },
13
+ ) => {
15
14
  await cacheDataLoaded;
16
15
 
17
16
  unsubscribe(
@@ -85,3 +84,6 @@ const chatApi = __baseApi.injectEndpoints({
85
84
  });
86
85
 
87
86
  export const __chatApi = chatApi;
87
+
88
+ export const { useGetChatQuery, useLazyGetChatQuery, useAddChatMutation, useUpdateChatNameMutation } =
89
+ chatApi;
@@ -1,13 +1,14 @@
1
- import { BaseApiEndpointBuilder, __settings } from './baseApi';
1
+ import { __baseApi, __settings } from './baseApi';
2
2
  import { unsubscribe } from '../client';
3
3
  import { ChatListItem } from '../types';
4
+ import { useSelector } from 'react-redux';
4
5
 
5
- export function chatListEndpoints(builder: BaseApiEndpointBuilder) {
6
- return {
6
+ const chatListApi = __baseApi.injectEndpoints({
7
+ endpoints: (builder) => ({
7
8
  getChatList: builder.query<ChatListItem[], void>({
8
9
  queryFn: async () => ({ data: await __settings.client.chats() }),
9
10
  providesTags: ['chat', 'account'],
10
- onCacheEntryAdded: async (_, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
11
+ onCacheEntryAdded: async (_, { dispatch, cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
11
12
  await cacheDataLoaded;
12
13
 
13
14
  unsubscribe(
@@ -130,5 +131,19 @@ export function chatListEndpoints(builder: BaseApiEndpointBuilder) {
130
131
  ).when(cacheEntryRemoved);
131
132
  },
132
133
  }),
133
- };
134
+ }),
135
+ });
136
+
137
+ export function useChatListItem(id: string | null) {
138
+ return useSelector((state: any) =>
139
+ id != null
140
+ ? chatListApi.endpoints.getChatList
141
+ .select()(state)
142
+ .data?.find((x) => x.id === id)
143
+ : null,
144
+ );
134
145
  }
146
+
147
+ export const __chatListApi = chatListApi;
148
+
149
+ export const { useGetChatListQuery, useLazyGetChatListQuery } = chatListApi;
@@ -1,8 +1,8 @@
1
1
  import { AddChatMemberRequest, RemoveChatMemberRequest } from '../types';
2
- import { BaseApiEndpointBuilder, __settings } from './baseApi';
2
+ import { __baseApi, __settings } from './baseApi';
3
3
 
4
- export function chatMemberEndpoints(builder: BaseApiEndpointBuilder) {
5
- return {
4
+ export const __chatMemberApi = __baseApi.injectEndpoints({
5
+ endpoints: (builder) => ({
6
6
  removeChatMember: builder.mutation<void, RemoveChatMemberRequest>({
7
7
  queryFn: async (args) => {
8
8
  await __settings.client.removeChatMember(args);
@@ -16,5 +16,7 @@ export function chatMemberEndpoints(builder: BaseApiEndpointBuilder) {
16
16
  return { data: null! };
17
17
  },
18
18
  }),
19
- };
20
- }
19
+ }),
20
+ });
21
+
22
+ export const { useAddChatMemberMutation, useRemoveChatMemberMutation } = __chatMemberApi;
@@ -1,12 +1,156 @@
1
1
  import { unsubscribe } from '../client';
2
2
  import { Debounce, DebounceValue } from '../debounce';
3
3
  import { ChatMessage, chatId } from '../types';
4
- import { BaseApiEndpointBuilder, DEFAULT_MESSAGE_LIST_TAKE, __baseApi, __settings } from './baseApi';
4
+ import { DEFAULT_MESSAGE_LIST_TAKE, __baseApi, __settings } from './baseApi';
5
5
 
6
- export function chatMessageEndpoints(builder: BaseApiEndpointBuilder) {
7
- return {};
6
+ export interface ChatMessageListResult {
7
+ messages: ChatMessage[];
8
+ hasMore: boolean;
8
9
  }
9
10
 
11
+ const CHAT_MESSAGE_READ_QUEUE = new Debounce<string>({
12
+ callback: (id) => __settings.client.readChatMessage({ id }),
13
+ compare: (a, b) => -a.localeCompare(b),
14
+ groupBy: (id) => chatId(id),
15
+ timeout: 500,
16
+ });
17
+
10
18
  export const __chatMessageApi = __baseApi.injectEndpoints({
11
- endpoints: (builder) => ({}),
19
+ endpoints: (builder) => ({
20
+ getChatMessageList: builder.query<ChatMessageListResult, { chatId: string }>({
21
+ queryFn: async ({ chatId }) => {
22
+ const messages = await __settings.client.chatMessages({
23
+ chatId,
24
+ skip: 0,
25
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
26
+ });
27
+ return {
28
+ data: {
29
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
30
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
31
+ },
32
+ };
33
+ },
34
+ providesTags: ['message', 'chat', 'account'],
35
+ onCacheEntryAdded: async ({ chatId }, { dispatch, cacheEntryRemoved, cacheDataLoaded }) => {
36
+ await cacheDataLoaded;
37
+
38
+ unsubscribe(
39
+ __settings.client.subscribe('chatify://chat-new-message', ([message]) => {
40
+ message.chatId === chatId &&
41
+ dispatch(
42
+ __chatMessageApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
43
+ draft.messages.unshift(message);
44
+ }),
45
+ );
46
+ }),
47
+
48
+ __settings.client.subscribe('chatify://chat-member-added', ([eventChatId, , , message]) => {
49
+ eventChatId === chatId &&
50
+ dispatch(
51
+ __chatMessageApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
52
+ draft.messages.unshift(message);
53
+ }),
54
+ );
55
+ }),
56
+
57
+ __settings.client.subscribe('chatify://chat-member-removed', ([eventChatId, , , message]) => {
58
+ eventChatId === chatId &&
59
+ dispatch(
60
+ __chatMessageApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
61
+ draft.messages.unshift(message);
62
+ }),
63
+ );
64
+ }),
65
+
66
+ __settings.client.subscribe('chatify://chat-name-changed', ([eventChatId, , message]) => {
67
+ eventChatId === chatId &&
68
+ dispatch(
69
+ __chatMessageApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
70
+ draft.messages.unshift(message);
71
+ }),
72
+ );
73
+ }),
74
+ ).when(cacheEntryRemoved);
75
+ },
76
+ }),
77
+
78
+ fetchNextChatMessageList: builder.mutation<void, { chatId: string }>({
79
+ queryFn: async ({ chatId }, { getState }) => {
80
+ const current = __chatMessageApi.endpoints.getChatMessageList.select({ chatId })(getState() as any);
81
+ const messages = await __settings.client.chatMessages({
82
+ chatId,
83
+ skip: current.data!.messages.length,
84
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
85
+ });
86
+ return {
87
+ data: null!,
88
+ meta: {
89
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
90
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
91
+ },
92
+ };
93
+ },
94
+ onQueryStarted: async ({ chatId }, { dispatch, queryFulfilled }) => {
95
+ const result = await queryFulfilled;
96
+ const meta: ChatMessageListResult = result.meta! as any;
97
+
98
+ dispatch(
99
+ __chatMessageApi.util.updateQueryData('getChatMessageList', { chatId }, (draft) => {
100
+ draft.hasMore = meta.hasMore;
101
+ draft.messages.push(...meta!.messages);
102
+ }),
103
+ );
104
+ },
105
+ }),
106
+
107
+ sendChatMessage: builder.mutation<void, { chatId: string; text: string }>({
108
+ queryFn: async ({ chatId, text }) => {
109
+ const requestId = new Date().getTime().toString();
110
+
111
+ await __settings.client.sendChatMessage({
112
+ chatId,
113
+ text,
114
+ files: [],
115
+ requestId,
116
+ });
117
+
118
+ return { data: null!, meta: { requestId } };
119
+ },
120
+ }),
121
+
122
+ readChatMessage: builder.mutation<void, { id: string }>({
123
+ queryFn: async () => {
124
+ return { data: null! };
125
+ },
126
+ onQueryStarted: async ({ id }, { dispatch }) => {
127
+ CHAT_MESSAGE_READ_QUEUE.push(id);
128
+ dispatch(
129
+ __chatMessageApi.endpoints.getChatMessageReadQueue.initiate(undefined, { forceRefetch: true }),
130
+ );
131
+ },
132
+ }),
133
+
134
+ getChatMessageReadQueue: builder.query<DebounceValue<string>[], void>({
135
+ queryFn: async () => ({ data: CHAT_MESSAGE_READ_QUEUE.units }),
136
+ onCacheEntryAdded: async (_, { dispatch, cacheEntryRemoved }) => {
137
+ unsubscribe(
138
+ CHAT_MESSAGE_READ_QUEUE.subscribe((value) => {
139
+ dispatch(
140
+ __chatMessageApi.util.updateQueryData('getChatMessageReadQueue', undefined, () => value.units),
141
+ );
142
+ }),
143
+ ).when(cacheEntryRemoved);
144
+ },
145
+ }),
146
+ }),
12
147
  });
148
+
149
+ export const {
150
+ useFetchNextChatMessageListMutation,
151
+ useGetChatMessageListQuery,
152
+ useReadChatMessageMutation,
153
+ useSendChatMessageMutation,
154
+ useGetChatMessageReadQueueQuery,
155
+ useLazyGetChatMessageListQuery,
156
+ } = __chatMessageApi;
@@ -1 +1,9 @@
1
+ export * from './baseApi';
2
+ export * from './accountApi';
3
+ export * from './chatApi';
4
+ export * from './chatListApi';
5
+ export * from './chatMemberApi';
6
+ export * from './chatMessageApi';
7
+ export * from './threadApi';
8
+ export * from './threadMessageApi';
1
9
  export * from './api';
@@ -4,5 +4,129 @@ import { unsubscribe } from '../client';
4
4
  import { useMemo } from 'react';
5
5
 
6
6
  export const __threadApi = __baseApi.injectEndpoints({
7
- endpoints: (builder) => ({}),
7
+ endpoints: (builder) => ({
8
+ getWatchThreadList: builder.query<ThreadWatchListItem[], void>({
9
+ queryFn: async () => {
10
+ const threads = await __settings.client.watchThreads();
11
+ return { data: threads };
12
+ },
13
+ providesTags: ['account', 'thread'],
14
+ onCacheEntryAdded: async (_, { dispatch, cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
15
+ await cacheDataLoaded;
16
+
17
+ unsubscribe(
18
+ __settings.client.subscribe(
19
+ 'chatify://thread-created',
20
+ ([thread, watch]) =>
21
+ watch &&
22
+ updateCachedData((threads) => {
23
+ threads.push(thread);
24
+ }),
25
+ ),
26
+ __settings.client.subscribe('chatify://thread-message-new', ([threadId, message, readForId]) =>
27
+ updateCachedData((threads) => {
28
+ const thread = threads.find((x) => x.id === threadId);
29
+ if (!thread) return;
30
+
31
+ thread.lastMessage = message;
32
+
33
+ if (readForId === __settings.me()) {
34
+ thread.lastReadMessageId = message.id;
35
+ }
36
+ }),
37
+ ),
38
+ __settings.client.subscribe('chatify://thread-message-read', ([threadId, messageId]) =>
39
+ updateCachedData((threads) => {
40
+ const thread = threads.find((x) => x.id === threadId);
41
+ if (!thread) return;
42
+
43
+ thread.lastReadMessageId = messageId;
44
+ }),
45
+ ),
46
+ __settings.client.subscribe('chatify://thread-watch-added', ([thread]) =>
47
+ updateCachedData((threads) => {
48
+ threads.push(thread);
49
+ }),
50
+ ),
51
+ __settings.client.subscribe('chatify://thread-watch-removed', ([id]) =>
52
+ updateCachedData((threads) => {
53
+ const index = threads.findIndex((x) => x.id === id);
54
+ if (index !== -1) threads.splice(index, 1);
55
+ }),
56
+ ),
57
+ ).when(cacheEntryRemoved);
58
+ },
59
+ }),
60
+
61
+ getThread: builder.query<Thread, { id: string }>({
62
+ queryFn: async ({ id }) => {
63
+ const threads = await __settings.client.thread(id);
64
+ return { data: threads };
65
+ },
66
+ providesTags: ['account', 'thread'],
67
+ onCacheEntryAdded: async ({ id }, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
68
+ await cacheDataLoaded;
69
+
70
+ unsubscribe(
71
+ __settings.client.subscribe(
72
+ 'chatify://thread-message-read',
73
+ ([threadId, messageId]) =>
74
+ id === threadId &&
75
+ updateCachedData((t) => {
76
+ t.lastReadMessageId = messageId;
77
+ }),
78
+ ),
79
+ __settings.client.subscribe(
80
+ 'chatify://thread-message-new',
81
+ ([threadId, message, readForId]) =>
82
+ id === threadId &&
83
+ updateCachedData((thread) => {
84
+ thread.lastMessageId = message.id;
85
+ thread.lastReadMessageId =
86
+ readForId === __settings.me() ? message.id : thread.lastReadMessageId;
87
+ }),
88
+ ),
89
+ __settings.client.subscribe(
90
+ 'chatify://thread-watch-added',
91
+ ([threadListItem]) =>
92
+ id === threadListItem.id &&
93
+ updateCachedData((thread) => {
94
+ thread.watch = true;
95
+ thread.lastMessageId = threadListItem.lastMessage?.id ?? null;
96
+ thread.lastReadMessageId = threadListItem.lastReadMessageId ?? null;
97
+ }),
98
+ ),
99
+ __settings.client.subscribe(
100
+ 'chatify://thread-watch-removed',
101
+ ([threadId]) =>
102
+ id === threadId &&
103
+ updateCachedData((thread) => {
104
+ thread.watch = false;
105
+ }),
106
+ ),
107
+ ).when(cacheEntryRemoved);
108
+ },
109
+ }),
110
+
111
+ watchThread: builder.mutation<void, { id: string; watch: boolean }>({
112
+ queryFn: async ({ id, watch }) => {
113
+ await __settings.client.watchThread(id, watch);
114
+ return { data: null! };
115
+ },
116
+ }),
117
+ }),
8
118
  });
119
+
120
+ export function useUnreadThreadMessageCount() {
121
+ const { data: threads, ...state } = useGetWatchThreadListQuery();
122
+ const count = useMemo(() => (threads != null ? calcThreadListUnreadCount(threads) : null), [threads]);
123
+ return [count, state] as const;
124
+ }
125
+
126
+ export const {
127
+ useGetWatchThreadListQuery,
128
+ useGetThreadQuery,
129
+ useLazyGetThreadQuery,
130
+ useLazyGetWatchThreadListQuery,
131
+ useWatchThreadMutation,
132
+ } = __threadApi;
@@ -16,5 +16,112 @@ const THREAD_MESSAGE_READ_QUEUE = new Debounce<string>({
16
16
  });
17
17
 
18
18
  export const __threadMessageApi = __baseApi.injectEndpoints({
19
- endpoints: (builder) => ({}),
19
+ endpoints: (builder) => ({
20
+ getThreadMessageList: builder.query<ThreadMessageList, { threadId: string }>({
21
+ queryFn: async (args) => {
22
+ const requestArgs = {
23
+ skip: 0,
24
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
25
+ ...args,
26
+ };
27
+ const messages = await __settings.client.threadMessages(requestArgs);
28
+ return {
29
+ data: {
30
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
31
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
32
+ },
33
+ };
34
+ },
35
+ providesTags: ['account', 'thread', 'thread-message'],
36
+ onCacheEntryAdded: async ({ threadId }, { cacheEntryRemoved, cacheDataLoaded, updateCachedData }) => {
37
+ await cacheDataLoaded;
38
+
39
+ unsubscribe(
40
+ __settings.client.subscribe('chatify://thread-message-new', ([_threadId, message]) => {
41
+ threadId === _threadId &&
42
+ updateCachedData((messageList) => {
43
+ !messageList.messages.some((x) => x.id === message.id) &&
44
+ messageList.messages.unshift(message);
45
+ });
46
+ }),
47
+ ).when(cacheEntryRemoved);
48
+ },
49
+ }),
50
+
51
+ fetchNextThreadMessageList: builder.mutation<void, { threadId: string }>({
52
+ queryFn: async ({ threadId }, { getState }) => {
53
+ const current = __threadMessageApi.endpoints.getThreadMessageList.select({ threadId })(
54
+ getState() as any,
55
+ );
56
+ const messages = await __settings.client.threadMessages({
57
+ threadId,
58
+ skip: current.data!.messages.length,
59
+ take: DEFAULT_MESSAGE_LIST_TAKE + 1,
60
+ });
61
+ return {
62
+ data: null!,
63
+ meta: {
64
+ messages: messages.slice(0, DEFAULT_MESSAGE_LIST_TAKE),
65
+ hasMore: messages.length === DEFAULT_MESSAGE_LIST_TAKE + 1,
66
+ },
67
+ };
68
+ },
69
+ onQueryStarted: async ({ threadId }, { dispatch, queryFulfilled }) => {
70
+ const result = await queryFulfilled;
71
+ const meta: ThreadMessageList = result.meta! as any;
72
+
73
+ dispatch(
74
+ __threadMessageApi.util.updateQueryData('getThreadMessageList', { threadId }, (draft) => {
75
+ draft.hasMore = meta.hasMore;
76
+ draft.messages.push(...meta!.messages);
77
+ }),
78
+ );
79
+ },
80
+ }),
81
+
82
+ sendThreadMessage: builder.mutation<string, SendThreadMessageRequest>({
83
+ queryFn: async (args) => {
84
+ const id = await __settings.client.sendThreadMessage(args);
85
+ return { data: id };
86
+ },
87
+ }),
88
+
89
+ readThreadMessage: builder.mutation<void, { id: string }>({
90
+ queryFn: async () => {
91
+ return { data: null! };
92
+ },
93
+ onQueryStarted: async ({ id }, { dispatch }) => {
94
+ THREAD_MESSAGE_READ_QUEUE.push(id);
95
+ dispatch(
96
+ __threadMessageApi.endpoints.getThreadMessageReadQueue.initiate(undefined, { forceRefetch: true }),
97
+ );
98
+ },
99
+ }),
100
+
101
+ getThreadMessageReadQueue: builder.query<DebounceValue<string>[], void>({
102
+ queryFn: async () => ({ data: THREAD_MESSAGE_READ_QUEUE.units }),
103
+ onCacheEntryAdded: async (_, { dispatch, cacheEntryRemoved }) => {
104
+ unsubscribe(
105
+ THREAD_MESSAGE_READ_QUEUE.subscribe((value) => {
106
+ dispatch(
107
+ __threadMessageApi.util.updateQueryData(
108
+ 'getThreadMessageReadQueue',
109
+ undefined,
110
+ () => value.units,
111
+ ),
112
+ );
113
+ }),
114
+ ).when(cacheEntryRemoved);
115
+ },
116
+ }),
117
+ }),
20
118
  });
119
+
120
+ export const {
121
+ useFetchNextThreadMessageListMutation,
122
+ useGetThreadMessageListQuery,
123
+ useLazyGetThreadMessageListQuery,
124
+ useReadThreadMessageMutation,
125
+ useSendThreadMessageMutation,
126
+ useGetThreadMessageReadQueueQuery,
127
+ } = __threadMessageApi;
@@ -1,5 +1,5 @@
1
1
  import { Input, Tooltip } from 'antd';
2
- import { chatifyApi } from '../../core';
2
+ import { useUpdateChatNameMutation } from '../../core';
3
3
  import { customize } from '../customize';
4
4
  import { useEffect, useState } from 'react';
5
5
  import isHotkey from 'is-hotkey';
@@ -9,7 +9,7 @@ const isEscape = isHotkey('escape');
9
9
  const isEnter = isHotkey('enter');
10
10
 
11
11
  function useUpdateChatName(id: string, value: string) {
12
- const [updateChatName] = chatifyApi.useUpdateChatNameMutation();
12
+ const [updateChatName] = useUpdateChatNameMutation();
13
13
  return () => updateChatName({ id, name: value });
14
14
  }
15
15
 
@@ -2,7 +2,7 @@ import { CSSProperties } from 'react';
2
2
  import { Localizer, LocalizerContext, defaultLocalizer } from '../localizer';
3
3
  import { CustomizeContext } from '../customize';
4
4
  import { ChatView, ChatViewCustomizeValue } from './ChatView';
5
- import { chatifyApi } from '../../core';
5
+ import { useGetChatQuery } from '../../core';
6
6
 
7
7
  export interface ChatPanelCustomizeValue extends ChatViewCustomizeValue {}
8
8
 
@@ -16,7 +16,7 @@ export interface ChatPanelProps {
16
16
 
17
17
  export function ChatPanel(props: ChatPanelProps) {
18
18
  const { id, className = '', customize, localizer = defaultLocalizer, style } = props;
19
- const { data: chat } = chatifyApi.useGetChatQuery({ id });
19
+ const { data: chat } = useGetChatQuery({ id });
20
20
 
21
21
  return (
22
22
  <div className={`wxchtf-chatify wxchtf-chat-panel ${className}`} style={style}>
@@ -1,5 +1,13 @@
1
1
  import { CustomizeContext, customize, useCustomizeContext } from '../customize';
2
- import { MessageBody, chatId, chatifyApi } from '../../core';
2
+ import {
3
+ MessageBody,
4
+ chatId,
5
+ useFetchNextChatMessageListMutation,
6
+ useGetChatMessageListQuery,
7
+ useGetChatMessageReadQueueQuery,
8
+ useReadChatMessageMutation,
9
+ useSendChatMessageMutation,
10
+ } from '../../core';
3
11
  import {
4
12
  Conversation,
5
13
  ConversationCustomizeValue,
@@ -33,19 +41,19 @@ function useConversation(props: ChatProps) {
33
41
  const { value: chat } = props;
34
42
  const { id, name, active, lastReadMessageId } = chat;
35
43
 
36
- const { data: messageList } = chatifyApi.useGetChatMessageListQuery({ chatId: id });
44
+ const { data: messageList } = useGetChatMessageListQuery({ chatId: id });
37
45
  const { messages, hasMore } = messageList ?? {};
38
46
 
39
- const [fetchNext, { isLoading: isFetchNextLoading }] = chatifyApi.useFetchNextChatMessageListMutation();
47
+ const [fetchNext, { isLoading: isFetchNextLoading }] = useFetchNextChatMessageListMutation();
40
48
  const onNext = useCallback(() => fetchNext({ chatId: id }), [fetchNext, id]);
41
49
 
42
- const [read] = chatifyApi.useReadChatMessageMutation();
50
+ const [read] = useReadChatMessageMutation();
43
51
  const onRead = useCallback((id: string) => read({ id }), [read]);
44
52
 
45
- const [send] = chatifyApi.useSendChatMessageMutation();
53
+ const [send] = useSendChatMessageMutation();
46
54
  const onSend = useCallback((message: MessageBody) => send({ ...message, chatId: id }).unwrap(), [id, send]);
47
55
 
48
- const { data: readQueue } = chatifyApi.useGetChatMessageReadQueueQuery();
56
+ const { data: readQueue } = useGetChatMessageReadQueueQuery();
49
57
 
50
58
  const conversation = useMemo(() => {
51
59
  if (!id) {
@@ -1,5 +1,10 @@
1
1
  import { Button, List, Space, Tooltip } from 'antd';
2
- import { chatifyApi } from '../../../core';
2
+ import {
3
+ useAddChatMemberMutation,
4
+ useGetAccountListQuery,
5
+ useGetChatQuery,
6
+ useRemoveChatMemberMutation,
7
+ } from '../../../core';
3
8
  import { customize } from '../../customize';
4
9
  import { useCallback, useMemo } from 'react';
5
10
  import { Icon } from '../../common/Icon';
@@ -12,7 +17,7 @@ interface ChatMemberListItemProps {
12
17
  }
13
18
 
14
19
  function useAdd(chatId: string, accountId: string, withHistory: boolean) {
15
- const [add, state] = chatifyApi.useAddChatMemberMutation();
20
+ const [add, state] = useAddChatMemberMutation();
16
21
  const onAdd = useCallback(
17
22
  () => add({ chatId, accountId, withHistory }),
18
23
  [add, chatId, accountId, withHistory],
@@ -21,7 +26,7 @@ function useAdd(chatId: string, accountId: string, withHistory: boolean) {
21
26
  }
22
27
 
23
28
  function useRemove(chatId: string, accountId: string, deleteHistory: boolean) {
24
- const [remove, state] = chatifyApi.useRemoveChatMemberMutation();
29
+ const [remove, state] = useRemoveChatMemberMutation();
25
30
  const onRemove = useCallback(
26
31
  () => remove({ chatId, accountId, deleteHistory }),
27
32
  [remove, chatId, accountId, deleteHistory],
@@ -31,8 +36,8 @@ function useRemove(chatId: string, accountId: string, deleteHistory: boolean) {
31
36
 
32
37
  const ChatMemberListItem = customize('ChatMemberListItem', (props: ChatMemberListItemProps) => {
33
38
  const { chatId } = props;
34
- const { data: chat } = chatifyApi.useGetChatQuery({ id: chatId });
35
- const { data: accounts } = chatifyApi.useGetAccountListQuery();
39
+ const { data: chat } = useGetChatQuery({ id: chatId });
40
+ const { data: accounts } = useGetAccountListQuery();
36
41
  const account = accounts!.find((x) => x.id === props.id)!;
37
42
  const isMember = chat!.members.some((x) => x.id === account.id);
38
43
  const [onAddWithoutHistory] = useAdd(chatId, account.id, false);
@@ -81,8 +86,8 @@ const ChatMemberListItem = customize('ChatMemberListItem', (props: ChatMemberLis
81
86
 
82
87
  export const ChatMembersPanel = customize('ChatMembersPanel', () => {
83
88
  const { id } = useChatContext();
84
- const { data: chat } = chatifyApi.useGetChatQuery({ id });
85
- const { data: accounts } = chatifyApi.useGetAccountListQuery();
89
+ const { data: chat } = useGetChatQuery({ id });
90
+ const { data: accounts } = useGetAccountListQuery();
86
91
  const ordered = useMemo(
87
92
  () =>
88
93
  accounts
@@ -1,4 +1,4 @@
1
- import { chatifyApi } from '../../../core';
1
+ import { useGetChatQuery } from '../../../core';
2
2
  import { Tooltip } from 'antd';
3
3
  import { customize } from '../../customize';
4
4
  import { Avatar as AntdAvatar } from 'antd';
@@ -7,7 +7,7 @@ import { useChatContext } from '../ChatContext';
7
7
 
8
8
  export const ChatMembersPreview = customize('ChatMembersPreview', () => {
9
9
  const { id } = useChatContext();
10
- const { data: chat } = chatifyApi.useGetChatQuery({ id });
10
+ const { data: chat } = useGetChatQuery({ id });
11
11
 
12
12
  if (!chat?.members) {
13
13
  return null;