@weavy/uikit-react 11.0.0

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 (223) hide show
  1. package/.github/workflows/publish.yml +16 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +110 -0
  4. package/changelog.md +50 -0
  5. package/dist/cjs/index.js +39 -0
  6. package/dist/cjs/index.js.map +1 -0
  7. package/dist/cjs/types/client/WeavyClient.d.ts +16 -0
  8. package/dist/cjs/types/components/Attachment.d.ts +13 -0
  9. package/dist/cjs/types/components/Avatar.d.ts +11 -0
  10. package/dist/cjs/types/components/Chat.d.ts +4 -0
  11. package/dist/cjs/types/components/Conversation.d.ts +4 -0
  12. package/dist/cjs/types/components/ConversationBadge.d.ts +3 -0
  13. package/dist/cjs/types/components/ConversationForm.d.ts +7 -0
  14. package/dist/cjs/types/components/ConversationList.d.ts +3 -0
  15. package/dist/cjs/types/components/ConversationListItem.d.ts +4 -0
  16. package/dist/cjs/types/components/File.d.ts +9 -0
  17. package/dist/cjs/types/components/FileBrowser.d.ts +6 -0
  18. package/dist/cjs/types/components/Image.d.ts +16 -0
  19. package/dist/cjs/types/components/Meeting.d.ts +8 -0
  20. package/dist/cjs/types/components/MeetingCard.d.ts +6 -0
  21. package/dist/cjs/types/components/Meetings.d.ts +6 -0
  22. package/dist/cjs/types/components/Message.d.ts +4 -0
  23. package/dist/cjs/types/components/Messages.d.ts +9 -0
  24. package/dist/cjs/types/components/Messenger.d.ts +4 -0
  25. package/dist/cjs/types/components/NewConversation.d.ts +3 -0
  26. package/dist/cjs/types/components/Presence.d.ts +7 -0
  27. package/dist/cjs/types/components/Reactions.d.ts +13 -0
  28. package/dist/cjs/types/components/SearchUsers.d.ts +7 -0
  29. package/dist/cjs/types/components/SeenBy.d.ts +9 -0
  30. package/dist/cjs/types/components/Typing.d.ts +8 -0
  31. package/dist/cjs/types/contexts/MessengerContext.d.ts +8 -0
  32. package/dist/cjs/types/contexts/PreviewContext.d.ts +7 -0
  33. package/dist/cjs/types/contexts/UserContext.d.ts +8 -0
  34. package/dist/cjs/types/contexts/WeavyContext.d.ts +10 -0
  35. package/dist/cjs/types/hooks/useBadge.d.ts +1 -0
  36. package/dist/cjs/types/hooks/useChat.d.ts +1 -0
  37. package/dist/cjs/types/hooks/useConversation.d.ts +1 -0
  38. package/dist/cjs/types/hooks/useConversations.d.ts +1 -0
  39. package/dist/cjs/types/hooks/useDebounce.d.ts +2 -0
  40. package/dist/cjs/types/hooks/useEvents.d.ts +6 -0
  41. package/dist/cjs/types/hooks/useFileUploader.d.ts +1 -0
  42. package/dist/cjs/types/hooks/useMembers.d.ts +1 -0
  43. package/dist/cjs/types/hooks/useMessages.d.ts +1 -0
  44. package/dist/cjs/types/hooks/useMutateChat.d.ts +4 -0
  45. package/dist/cjs/types/hooks/useMutateConversation.d.ts +3 -0
  46. package/dist/cjs/types/hooks/useMutateConversationName.d.ts +4 -0
  47. package/dist/cjs/types/hooks/useMutateDeleteReaction.d.ts +4 -0
  48. package/dist/cjs/types/hooks/useMutateExternalBlobs.d.ts +3 -0
  49. package/dist/cjs/types/hooks/useMutateMeeting.d.ts +3 -0
  50. package/dist/cjs/types/hooks/useMutateMembers.d.ts +4 -0
  51. package/dist/cjs/types/hooks/useMutateMessage.d.ts +9 -0
  52. package/dist/cjs/types/hooks/useMutatePinned.d.ts +4 -0
  53. package/dist/cjs/types/hooks/useMutateReaction.d.ts +4 -0
  54. package/dist/cjs/types/hooks/useMutateRead.d.ts +4 -0
  55. package/dist/cjs/types/hooks/useMutateRemoveMembers.d.ts +4 -0
  56. package/dist/cjs/types/hooks/useMutateTyping.d.ts +3 -0
  57. package/dist/cjs/types/hooks/usePresence.d.ts +1 -0
  58. package/dist/cjs/types/hooks/usePreview.d.ts +4 -0
  59. package/dist/cjs/types/hooks/useReactions.d.ts +3 -0
  60. package/dist/cjs/types/hooks/useSearchUsers.d.ts +1 -0
  61. package/dist/cjs/types/hooks/useThrottle.d.ts +2 -0
  62. package/dist/cjs/types/hooks/useUser.d.ts +1 -0
  63. package/dist/cjs/types/index.d.ts +15 -0
  64. package/dist/cjs/types/types/Chat.d.ts +3 -0
  65. package/dist/cjs/types/types/Conversation.d.ts +4 -0
  66. package/dist/cjs/types/types/ConversationListItem.d.ts +4 -0
  67. package/dist/cjs/types/types/Message.d.ts +15 -0
  68. package/dist/cjs/types/types/Messenger.d.ts +3 -0
  69. package/dist/cjs/types/types/types.d.ts +150 -0
  70. package/dist/cjs/types/ui/Button.d.ts +4 -0
  71. package/dist/cjs/types/ui/Dropdown.d.ts +19 -0
  72. package/dist/cjs/types/ui/Icon.d.ts +10 -0
  73. package/dist/cjs/types/ui/Overlay.d.ts +12 -0
  74. package/dist/cjs/types/utils/fileUtilities.d.ts +5 -0
  75. package/dist/cjs/types/utils/styles.d.ts +17 -0
  76. package/dist/esm/index.js +39 -0
  77. package/dist/esm/index.js.map +1 -0
  78. package/dist/esm/types/client/WeavyClient.d.ts +16 -0
  79. package/dist/esm/types/components/Attachment.d.ts +13 -0
  80. package/dist/esm/types/components/Avatar.d.ts +11 -0
  81. package/dist/esm/types/components/Chat.d.ts +4 -0
  82. package/dist/esm/types/components/Conversation.d.ts +4 -0
  83. package/dist/esm/types/components/ConversationBadge.d.ts +3 -0
  84. package/dist/esm/types/components/ConversationForm.d.ts +7 -0
  85. package/dist/esm/types/components/ConversationList.d.ts +3 -0
  86. package/dist/esm/types/components/ConversationListItem.d.ts +4 -0
  87. package/dist/esm/types/components/File.d.ts +9 -0
  88. package/dist/esm/types/components/FileBrowser.d.ts +6 -0
  89. package/dist/esm/types/components/Image.d.ts +16 -0
  90. package/dist/esm/types/components/Meeting.d.ts +8 -0
  91. package/dist/esm/types/components/MeetingCard.d.ts +6 -0
  92. package/dist/esm/types/components/Meetings.d.ts +6 -0
  93. package/dist/esm/types/components/Message.d.ts +4 -0
  94. package/dist/esm/types/components/Messages.d.ts +9 -0
  95. package/dist/esm/types/components/Messenger.d.ts +4 -0
  96. package/dist/esm/types/components/NewConversation.d.ts +3 -0
  97. package/dist/esm/types/components/Presence.d.ts +7 -0
  98. package/dist/esm/types/components/Reactions.d.ts +13 -0
  99. package/dist/esm/types/components/SearchUsers.d.ts +7 -0
  100. package/dist/esm/types/components/SeenBy.d.ts +9 -0
  101. package/dist/esm/types/components/Typing.d.ts +8 -0
  102. package/dist/esm/types/contexts/MessengerContext.d.ts +8 -0
  103. package/dist/esm/types/contexts/PreviewContext.d.ts +7 -0
  104. package/dist/esm/types/contexts/UserContext.d.ts +8 -0
  105. package/dist/esm/types/contexts/WeavyContext.d.ts +10 -0
  106. package/dist/esm/types/hooks/useBadge.d.ts +1 -0
  107. package/dist/esm/types/hooks/useChat.d.ts +1 -0
  108. package/dist/esm/types/hooks/useConversation.d.ts +1 -0
  109. package/dist/esm/types/hooks/useConversations.d.ts +1 -0
  110. package/dist/esm/types/hooks/useDebounce.d.ts +2 -0
  111. package/dist/esm/types/hooks/useEvents.d.ts +6 -0
  112. package/dist/esm/types/hooks/useFileUploader.d.ts +1 -0
  113. package/dist/esm/types/hooks/useMembers.d.ts +1 -0
  114. package/dist/esm/types/hooks/useMessages.d.ts +1 -0
  115. package/dist/esm/types/hooks/useMutateChat.d.ts +4 -0
  116. package/dist/esm/types/hooks/useMutateConversation.d.ts +3 -0
  117. package/dist/esm/types/hooks/useMutateConversationName.d.ts +4 -0
  118. package/dist/esm/types/hooks/useMutateDeleteReaction.d.ts +4 -0
  119. package/dist/esm/types/hooks/useMutateExternalBlobs.d.ts +3 -0
  120. package/dist/esm/types/hooks/useMutateMeeting.d.ts +3 -0
  121. package/dist/esm/types/hooks/useMutateMembers.d.ts +4 -0
  122. package/dist/esm/types/hooks/useMutateMessage.d.ts +9 -0
  123. package/dist/esm/types/hooks/useMutatePinned.d.ts +4 -0
  124. package/dist/esm/types/hooks/useMutateReaction.d.ts +4 -0
  125. package/dist/esm/types/hooks/useMutateRead.d.ts +4 -0
  126. package/dist/esm/types/hooks/useMutateRemoveMembers.d.ts +4 -0
  127. package/dist/esm/types/hooks/useMutateTyping.d.ts +3 -0
  128. package/dist/esm/types/hooks/usePresence.d.ts +1 -0
  129. package/dist/esm/types/hooks/usePreview.d.ts +4 -0
  130. package/dist/esm/types/hooks/useReactions.d.ts +3 -0
  131. package/dist/esm/types/hooks/useSearchUsers.d.ts +1 -0
  132. package/dist/esm/types/hooks/useThrottle.d.ts +2 -0
  133. package/dist/esm/types/hooks/useUser.d.ts +1 -0
  134. package/dist/esm/types/index.d.ts +15 -0
  135. package/dist/esm/types/types/Chat.d.ts +3 -0
  136. package/dist/esm/types/types/Conversation.d.ts +4 -0
  137. package/dist/esm/types/types/ConversationListItem.d.ts +4 -0
  138. package/dist/esm/types/types/Message.d.ts +15 -0
  139. package/dist/esm/types/types/Messenger.d.ts +3 -0
  140. package/dist/esm/types/types/types.d.ts +150 -0
  141. package/dist/esm/types/ui/Button.d.ts +4 -0
  142. package/dist/esm/types/ui/Dropdown.d.ts +19 -0
  143. package/dist/esm/types/ui/Icon.d.ts +10 -0
  144. package/dist/esm/types/ui/Overlay.d.ts +12 -0
  145. package/dist/esm/types/utils/fileUtilities.d.ts +5 -0
  146. package/dist/esm/types/utils/styles.d.ts +17 -0
  147. package/dist/index.d.ts +98 -0
  148. package/package.json +47 -0
  149. package/rollup.config.js +41 -0
  150. package/src/client/WeavyClient.ts +95 -0
  151. package/src/components/Attachment.tsx +33 -0
  152. package/src/components/Avatar.tsx +26 -0
  153. package/src/components/Chat.tsx +68 -0
  154. package/src/components/Conversation.tsx +220 -0
  155. package/src/components/ConversationBadge.tsx +44 -0
  156. package/src/components/ConversationForm.tsx +217 -0
  157. package/src/components/ConversationList.tsx +61 -0
  158. package/src/components/ConversationListItem.tsx +155 -0
  159. package/src/components/File.tsx +21 -0
  160. package/src/components/FileBrowser.tsx +86 -0
  161. package/src/components/Image.tsx +66 -0
  162. package/src/components/Meeting.tsx +21 -0
  163. package/src/components/MeetingCard.tsx +31 -0
  164. package/src/components/Meetings.tsx +58 -0
  165. package/src/components/Message.tsx +90 -0
  166. package/src/components/Messages.tsx +271 -0
  167. package/src/components/Messenger.tsx +34 -0
  168. package/src/components/NewConversation.tsx +50 -0
  169. package/src/components/Presence.tsx +15 -0
  170. package/src/components/Reactions.tsx +95 -0
  171. package/src/components/SearchUsers.tsx +90 -0
  172. package/src/components/SeenBy.tsx +26 -0
  173. package/src/components/Typing.tsx +131 -0
  174. package/src/contexts/MessengerContext.tsx +44 -0
  175. package/src/contexts/PreviewContext.tsx +105 -0
  176. package/src/contexts/UserContext.tsx +31 -0
  177. package/src/contexts/WeavyContext.tsx +66 -0
  178. package/src/hooks/useBadge.ts +32 -0
  179. package/src/hooks/useChat.ts +32 -0
  180. package/src/hooks/useConversation.ts +28 -0
  181. package/src/hooks/useConversations.ts +27 -0
  182. package/src/hooks/useDebounce.ts +22 -0
  183. package/src/hooks/useEvents.ts +43 -0
  184. package/src/hooks/useFileUploader.ts +35 -0
  185. package/src/hooks/useMembers.ts +27 -0
  186. package/src/hooks/useMessages.ts +42 -0
  187. package/src/hooks/useMessenger.ts +51 -0
  188. package/src/hooks/useMutateChat.ts +44 -0
  189. package/src/hooks/useMutateConversation.ts +40 -0
  190. package/src/hooks/useMutateConversationName.ts +41 -0
  191. package/src/hooks/useMutateDeleteReaction.ts +38 -0
  192. package/src/hooks/useMutateExternalBlobs.ts +39 -0
  193. package/src/hooks/useMutateMeeting.ts +39 -0
  194. package/src/hooks/useMutateMembers.ts +43 -0
  195. package/src/hooks/useMutateMessage.ts +116 -0
  196. package/src/hooks/useMutatePinned.ts +40 -0
  197. package/src/hooks/useMutateReaction.ts +38 -0
  198. package/src/hooks/useMutateRead.ts +40 -0
  199. package/src/hooks/useMutateRemoveMembers.ts +43 -0
  200. package/src/hooks/useMutateTyping.ts +34 -0
  201. package/src/hooks/usePresence.ts +32 -0
  202. package/src/hooks/usePreview.ts +21 -0
  203. package/src/hooks/useReactions.ts +53 -0
  204. package/src/hooks/useSearchUsers.ts +26 -0
  205. package/src/hooks/useThrottle.ts +13 -0
  206. package/src/hooks/useUser.ts +38 -0
  207. package/src/index.ts +33 -0
  208. package/src/types/Chat.ts +3 -0
  209. package/src/types/Conversation.ts +4 -0
  210. package/src/types/ConversationListItem.ts +4 -0
  211. package/src/types/Message.ts +16 -0
  212. package/src/types/Messenger.ts +3 -0
  213. package/src/types/emoji-toolkit.d.ts +1 -0
  214. package/src/types/types.ts +175 -0
  215. package/src/ui/Button.tsx +32 -0
  216. package/src/ui/Dropdown.tsx +58 -0
  217. package/src/ui/Icon.tsx +79 -0
  218. package/src/ui/Overlay.tsx +41 -0
  219. package/src/utils/fileUtilities.ts +230 -0
  220. package/src/utils/infiniteScroll.js +175 -0
  221. package/src/utils/scrollToBottom.js +75 -0
  222. package/src/utils/styles.ts +42 -0
  223. package/tsconfig.json +108 -0
@@ -0,0 +1,35 @@
1
+ import { useContext } from "react";
2
+ import { useMutation } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// POST to upload a a file
6
+ export default function useFileUploader(callback: Function) {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+
10
+ return useMutation(
11
+ async ({ request }: any) => {
12
+
13
+ const formData = new FormData();
14
+ formData.append('blob', request.file);
15
+
16
+ var response = await fetch(client.uri + "/api/blobs", {
17
+ method: 'POST',
18
+ body: formData,
19
+ headers: {
20
+ "Authorization": "Bearer " + await client.tokenFactory()
21
+ }
22
+ });
23
+
24
+ var uploaded = await response.json();
25
+ callback(uploaded);
26
+ },
27
+ {
28
+ onError: (e: any) => {
29
+
30
+ },
31
+ onSuccess: (data: any, variables: any) => {
32
+ },
33
+ }
34
+ );
35
+ }
@@ -0,0 +1,27 @@
1
+ import { useContext } from "react";
2
+ import { useQuery } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// GET all members in an app
6
+ export default function useMembers(id: number | null, options: any) {
7
+ const { client } = useContext(WeavyContext);
8
+
9
+ if (!client) {
10
+ throw new Error('useMembers must be used within an WeavyProvider');
11
+ }
12
+
13
+ const getConversationMembers = async () => {
14
+
15
+ const response = await fetch(client.uri + "/api/apps/" + id + "/members", {
16
+ headers: {
17
+ "content-type": "application/json",
18
+ "Authorization": "Bearer " + await client.tokenFactory()
19
+ }
20
+ });
21
+ const data = await response.json();
22
+ return data;
23
+ };
24
+
25
+
26
+ return useQuery<MembersResult>(["members", id], getConversationMembers, options);
27
+ }
@@ -0,0 +1,42 @@
1
+ import { useContext } from "react";
2
+ import { useInfiniteQuery, useQuery } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// GET all messages in an app
6
+ export default function useMessages(id: number | null, options: any) {
7
+ const PAGE_SIZE = 25;
8
+ const { client } = useContext(WeavyContext);
9
+
10
+ if (!client) {
11
+ throw new Error('useMessages must be used within an WeavyProvider');
12
+ }
13
+
14
+ const getMessages = async (opt: any) => {
15
+ let skip = opt.pageParam || 0;
16
+
17
+ const response = await fetch(client.uri + "/api/apps/" + id + "/messages?orderby=createdat+desc&skip=" + skip + "&top=" + PAGE_SIZE, {
18
+ headers: {
19
+ "content-type": "application/json",
20
+ "Authorization": "Bearer " + await client.tokenFactory()
21
+ }
22
+ });
23
+ let result = await response.json();
24
+ result.data = result.data?.reverse() || [];
25
+ return result;
26
+ };
27
+ var opts = {
28
+ ...options,
29
+ getNextPageParam: (lastPage:any, pages:any) => {
30
+ if (lastPage?.end < lastPage?.count) {
31
+ return pages.length * PAGE_SIZE;
32
+ }
33
+ },
34
+ select: (data: any) => ({
35
+ // reverse scroll
36
+ pages: [...data.pages].reverse(),
37
+ pageParams: [...data.pageParams].reverse(),
38
+ }),
39
+ };
40
+
41
+ return useInfiniteQuery<MessagesResult>(['messages', id], getMessages, opts)
42
+ }
@@ -0,0 +1,51 @@
1
+ // import { useContext, useEffect, useState } from "react";
2
+ // import { WeavyContext } from "../contexts/WeavyContext";
3
+
4
+
5
+ // export const useMessenger = (id: number) => {
6
+ // //const { client, state, dispatch } = useContext<WeavyContextProps>(WeavyContext);
7
+ // const [loading, setLoading] = useState<boolean>(true);
8
+
9
+ // // if (!client) {
10
+ // // throw new Error('useMessenger must be used within an WeavyProvider');
11
+ // // }
12
+
13
+
14
+ // useEffect(() => {
15
+ // let mocked = [
16
+ // { id: 11, title: "Chat 1", text: "Quisque cursus, metus vitae pharetra" },
17
+ // { id: 22, title: "Chat 2", text: "Quisque cursus, metus vitae pharetra" }
18
+ // ];
19
+
20
+ // setTimeout(() => {
21
+ // init(mocked);
22
+ // setLoading(false);
23
+ // }, 2000);
24
+
25
+
26
+ // }, [])
27
+
28
+ // const init = (data: Conversation[]) => {
29
+ // //dispatch({ app: "messenger", type: "init", payload: data })
30
+ // }
31
+
32
+ // const insert = ({ title, text }: InsertConversationProps) => {
33
+
34
+ // //dispatch({ app: "messenger", type: "insert", payload: { id: Math.random(), title, text } })
35
+ // }
36
+
37
+ // const select = (id: number) => {
38
+ // //dispatch({ app: "messenger", type: "select", payload: id })
39
+ // }
40
+
41
+
42
+ // return {
43
+ // //data: state.messenger.conversations,
44
+ // //selected: state.messenger.selected,
45
+ // loading,
46
+ // init,
47
+ // select,
48
+ // insert
49
+ // };
50
+
51
+ // }
@@ -0,0 +1,44 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// POST to create a new conversation
6
+ export default function useMutateChat() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateChat must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ identifier: string,
18
+ members: number[]
19
+ }
20
+
21
+ // create new conversation
22
+ const mutateChat = async ({ identifier, members }: MutateProps) => {
23
+
24
+ const response = await fetch(client.uri + "/api/apps/", {
25
+ method: "POST",
26
+ body: JSON.stringify({
27
+ identifier: identifier,
28
+ members: members
29
+ }),
30
+ headers: {
31
+ "content-type": "application/json",
32
+ "Authorization": "Bearer " + await client.tokenFactory()
33
+ }
34
+ });
35
+
36
+ return response.json();
37
+ };
38
+
39
+ return useMutation(mutateChat, {
40
+ onSuccess: () => {
41
+ //queryClient.invalidateQueries("conversations");
42
+ }
43
+ });
44
+ }
@@ -0,0 +1,40 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// POST to create a new conversation
6
+ export default function useMutateConversation() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateConversation must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ members: number[]
18
+ }
19
+
20
+ // create new conversation
21
+ const mutateConversation = async ({ members }: MutateProps) => {
22
+
23
+ const response = await fetch(client.uri + "/api/conversations/", {
24
+ method: "POST",
25
+ body: JSON.stringify({ members: members }),
26
+ headers: {
27
+ "content-type": "application/json",
28
+ "Authorization": "Bearer " + await client.tokenFactory()
29
+ }
30
+ });
31
+
32
+ return response.json();
33
+ };
34
+
35
+ return useMutation(mutateConversation, {
36
+ onSuccess: () => {
37
+ queryClient.invalidateQueries("conversations");
38
+ }
39
+ });
40
+ }
@@ -0,0 +1,41 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// PUT to update an app name
6
+ export default function useMutateConversationName() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateConversationName must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ id: number | null,
18
+ name: string
19
+ }
20
+
21
+ const mutateConversationName = async ({ id, name }: MutateProps) => {
22
+
23
+ const response = await fetch(client.uri + "/api/apps/" + id, {
24
+ method: "PATCH",
25
+ body: JSON.stringify({ name: name }),
26
+ headers: {
27
+ "content-type": "application/json",
28
+ "Authorization": "Bearer " + await client.tokenFactory()
29
+ }
30
+ });
31
+
32
+ return response.json();
33
+ };
34
+
35
+ return useMutation(mutateConversationName, {
36
+ onSuccess: (data: any, variables: any, context: any) => {
37
+ queryClient.invalidateQueries("conversations");
38
+ queryClient.invalidateQueries(["conversation", variables.id]);
39
+ }
40
+ });
41
+ }
@@ -0,0 +1,38 @@
1
+ import { useContext } from "react";
2
+ import { useMutation } from "react-query";
3
+ import { UserContext } from "../contexts/UserContext";
4
+ import { WeavyContext } from "../contexts/WeavyContext";
5
+
6
+ /// DELETE to remove a reaction from a message
7
+ export default function useMutateDeleteReaction() {
8
+
9
+ const { client } = useContext(WeavyContext);
10
+ const { user } = useContext(UserContext);
11
+
12
+ if (!client) {
13
+ throw new Error('useMutateDeleteReaction must be used within an WeavyProvider');
14
+ }
15
+
16
+
17
+ type MutateProps = {
18
+ id: number | null,
19
+ reaction: string
20
+ }
21
+
22
+ const mutateDeleteReaction = async ({ id, reaction }: MutateProps) => {
23
+
24
+ // remove the existing reaction
25
+ const response = await fetch(client.uri + "/api/messages/" + id + "/reactions/", {
26
+ method: "DELETE",
27
+ headers: {
28
+ "content-type": "application/json",
29
+ "Authorization": "Bearer " + await client.tokenFactory()
30
+ }
31
+ });
32
+
33
+
34
+ return response;
35
+ };
36
+
37
+ return useMutation(mutateDeleteReaction);
38
+ }
@@ -0,0 +1,39 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// POST to upload an external blob
6
+ export default function useMutateExternalBlobs() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateExternalBlobs must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ blobs: []
18
+ }
19
+
20
+ const mutateExternalBlobs = async ({ blobs }: MutateProps) => {
21
+
22
+ const response = await fetch(client.uri + "/api/blobs/external", {
23
+ method: "POST",
24
+ body: JSON.stringify(blobs),
25
+ headers: {
26
+ "content-type": "application/json",
27
+ "Authorization": "Bearer " + await client.tokenFactory()
28
+ }
29
+ });
30
+
31
+ return response.json();
32
+ };
33
+
34
+ return useMutation(mutateExternalBlobs, {
35
+ onSuccess: (variables: any) => {
36
+
37
+ }
38
+ });
39
+ }
@@ -0,0 +1,39 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// POST to create a new meeting
6
+ export default function useMutateMeeting() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateMeeting must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ provider: string
18
+ }
19
+
20
+ const mutateMeeting = async ({ provider }: MutateProps) => {
21
+
22
+ const response = await fetch(client.uri + "/api/meetings", {
23
+ method: "POST",
24
+ body: JSON.stringify({provider: provider}),
25
+ headers: {
26
+ "content-type": "application/json",
27
+ "Authorization": "Bearer " + await client.tokenFactory()
28
+ }
29
+ });
30
+
31
+ return response.json();
32
+ };
33
+
34
+ return useMutation(mutateMeeting, {
35
+ onSuccess: (variables: any) => {
36
+
37
+ }
38
+ });
39
+ }
@@ -0,0 +1,43 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// PUT to update members in an app
6
+ export default function useMutateMembers() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateMembers must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ id: number | null,
18
+ members: number[]
19
+ }
20
+
21
+ // add members to conversation
22
+ const mutateMembers = async ({ id, members }: MutateProps) => {
23
+
24
+ const response = await fetch(client.uri + "/api/apps/" + id + "/members/", {
25
+ method: "POST",
26
+ body: JSON.stringify(members),
27
+ headers: {
28
+ "content-type": "application/json",
29
+ "Authorization": "Bearer " + await client.tokenFactory()
30
+ }
31
+ });
32
+
33
+ return response;
34
+ };
35
+
36
+ return useMutation(mutateMembers, {
37
+ onSuccess: (data: any, variables: any, context: any) => {
38
+ queryClient.invalidateQueries("conversations");
39
+ queryClient.invalidateQueries(["conversation", variables.id]);
40
+ queryClient.invalidateQueries(["members", variables.id]);
41
+ }
42
+ });
43
+ }
@@ -0,0 +1,116 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// PSOT to add a new message to an app
6
+ export default function useMutateMessage() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateMessage must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ id: number | null,
18
+ text: string,
19
+ userId: number,
20
+ attachments: [],
21
+ meetings: MeetingType[]
22
+ }
23
+
24
+ const mutateMessage = async ({ id, text, userId, attachments, meetings }: MutateProps) => {
25
+ const response = await fetch(client.uri + "/api/apps/" + id + "/messages", {
26
+ method: "POST",
27
+ body: JSON.stringify({
28
+ text: text,
29
+ blobs: attachments.map((a: FileType) => { return a.id }),
30
+ meeting_id: meetings.length > 0 ? meetings[0].id : null
31
+ }),
32
+ headers: {
33
+ "content-type": "application/json",
34
+ "Authorization": "Bearer " + await client.tokenFactory()
35
+ }
36
+ });
37
+
38
+ return response.json();
39
+ };
40
+
41
+ return useMutation(mutateMessage, {
42
+ onSuccess: (data: any, variables: any, context: any) => {
43
+
44
+ const previousData = queryClient.getQueryData<any>(['messages', variables.id]);
45
+ if (previousData && previousData.pages) {
46
+ // update cache
47
+ const newPagesArray = previousData.pages.map((page: any, i: number) => {
48
+
49
+
50
+
51
+ // remove temp message
52
+ if (i === 0) {
53
+ page.data = [
54
+ ...page.data.filter((message: MessageType) => message.id !== context.tempId),
55
+ data
56
+ ]
57
+
58
+ }
59
+
60
+ return page;
61
+
62
+ }) ?? [];
63
+
64
+ queryClient.setQueryData(["messages", variables.id], (data: any) => ({
65
+ pages: newPagesArray,
66
+ pageParams: data.pageParams,
67
+ }));
68
+ }
69
+
70
+ // refetch conversations list
71
+ queryClient.invalidateQueries("conversations");
72
+
73
+ },
74
+ onMutate: async (variables: any) => {
75
+ await queryClient.cancelQueries(['messages', variables.id]);
76
+
77
+ const previousData = queryClient.getQueryData<any>(['messages', variables.id]);
78
+ const tempId = Math.random();
79
+
80
+ if (previousData && previousData.pages.length > 0) {
81
+
82
+ var lastPage = previousData.pages[0]; // in reverse order
83
+
84
+ // add temp message
85
+ var data = lastPage.data || [];
86
+ let pageMessages = [...data, {
87
+ id: tempId,
88
+ text: variables.text,
89
+ display_name: "",
90
+ temp: true,
91
+ parent_id: null,
92
+ created_by: { id: variables.userId },
93
+ created_at: new Date().toUTCString(),
94
+ attachments: [],
95
+ reactions: [],
96
+ reactions_count: 0
97
+ }];
98
+ lastPage.data = pageMessages;
99
+
100
+ // update cache
101
+ queryClient.setQueryData(["messages", variables.id], (data: any) => {
102
+ let updatedPages = [lastPage];
103
+ if (data?.pages.length > 1) {
104
+ updatedPages = [lastPage, ...data?.pages.slice(1)];
105
+ }
106
+ return {
107
+ pages: updatedPages,
108
+ pageParams: data?.pageParams,
109
+ }
110
+ });
111
+ }
112
+
113
+ return { tempId }
114
+ }
115
+ });
116
+ }
@@ -0,0 +1,40 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// PUT or DELETE to update pinned state on a conversation
6
+ export default function useMutatePinned() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutatePinned must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ id: number | null,
18
+ pin: boolean
19
+ }
20
+
21
+ const mutatePinned = async ({ id, pin }: MutateProps) => {
22
+
23
+ const response = await fetch(client.uri + "/api/conversations/" + id + "/pin", {
24
+ method: + !pin ? "DELETE": "PUT",
25
+ body: "",
26
+ headers: {
27
+ "content-type": "application/json",
28
+ "Authorization": "Bearer " + await client.tokenFactory()
29
+ }
30
+ });
31
+
32
+ return response.json();
33
+ };
34
+
35
+ return useMutation(mutatePinned, {
36
+ onSuccess: () => {
37
+ queryClient.invalidateQueries("conversations");
38
+ }
39
+ });
40
+ }
@@ -0,0 +1,38 @@
1
+ import { useContext } from "react";
2
+ import { useMutation } from "react-query";
3
+ import { UserContext } from "../contexts/UserContext";
4
+ import { WeavyContext } from "../contexts/WeavyContext";
5
+
6
+ /// POST to add a reaction to a message
7
+ export default function useMutateReaction() {
8
+
9
+ const { client } = useContext(WeavyContext);
10
+ const { user } = useContext(UserContext);
11
+
12
+ if (!client) {
13
+ throw new Error('useMutateReaction must be used within an WeavyProvider');
14
+ }
15
+
16
+
17
+ type MutateProps = {
18
+ id: number | null,
19
+ reaction: string
20
+ }
21
+
22
+ const mutateReaction = async ({id, reaction }: MutateProps) => {
23
+
24
+
25
+ const response = await fetch(client.uri + "/api/messages/" + id + "/reactions/", {
26
+ method: "POST",
27
+ body: JSON.stringify({ content: reaction}),
28
+ headers: {
29
+ "content-type": "application/json",
30
+ "Authorization": "Bearer " + await client.tokenFactory()
31
+ }
32
+ });
33
+
34
+ return response;
35
+ };
36
+
37
+ return useMutation(mutateReaction);
38
+ }
@@ -0,0 +1,40 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// PUT or DELETE to update read state on a conversation
6
+ export default function useMutateRead() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateUnread must be used within an WeavyProvider');
13
+ }
14
+
15
+
16
+ type MutateProps = {
17
+ id: number | null,
18
+ read: boolean
19
+ }
20
+
21
+ const mutateRead = async ({ id, read }: MutateProps) => {
22
+
23
+ const response = await fetch(client.uri + "/api/conversations/" + id + "/read", {
24
+ method: !read ? "DELETE": "PUT",
25
+ body: "",
26
+ headers: {
27
+ "content-type": "application/json",
28
+ "Authorization": "Bearer " + await client.tokenFactory()
29
+ }
30
+ });
31
+
32
+ return response.json();
33
+ };
34
+
35
+ return useMutation(mutateRead, {
36
+ onSuccess: () => {
37
+ queryClient.invalidateQueries("conversations");
38
+ }
39
+ });
40
+ }