@weavy/uikit-react 14.0.4 → 15.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 (360) hide show
  1. package/.vscode/settings.json +2 -0
  2. package/changelog.md +6 -0
  3. package/dist/cjs/client/WeavyClient.d.ts +1 -0
  4. package/dist/cjs/components/Attachment.d.ts +5 -5
  5. package/dist/cjs/components/Blob.d.ts +9 -0
  6. package/dist/cjs/components/Comment.d.ts +18 -0
  7. package/dist/cjs/components/CommentCount.d.ts +7 -0
  8. package/dist/cjs/components/CommentEdit.d.ts +16 -0
  9. package/dist/cjs/components/CommentPlaceholder.d.ts +8 -0
  10. package/dist/cjs/components/CommentTrashed.d.ts +15 -0
  11. package/dist/cjs/components/CommentView.d.ts +18 -0
  12. package/dist/cjs/components/Comments.d.ts +8 -0
  13. package/dist/cjs/components/ConversationListItem.d.ts +1 -1
  14. package/dist/cjs/components/Dropzone.d.ts +10 -0
  15. package/dist/cjs/components/Editor.d.ts +25 -0
  16. package/dist/cjs/components/Embed.d.ts +8 -0
  17. package/dist/cjs/components/FileItem.d.ts +41 -0
  18. package/dist/cjs/components/FileList.d.ts +11 -0
  19. package/dist/cjs/components/FileVersions.d.ts +9 -0
  20. package/dist/cjs/components/Files.d.ts +4 -0
  21. package/dist/cjs/components/Image.d.ts +3 -3
  22. package/dist/cjs/components/MeetingCard.d.ts +1 -1
  23. package/dist/cjs/components/Meetings.d.ts +2 -1
  24. package/dist/cjs/components/Poll.d.ts +8 -0
  25. package/dist/cjs/components/PollOption.d.ts +10 -0
  26. package/dist/cjs/components/Post.d.ts +21 -0
  27. package/dist/cjs/components/PostEdit.d.ts +17 -0
  28. package/dist/cjs/components/PostList.d.ts +6 -0
  29. package/dist/cjs/components/PostPlaceholder.d.ts +8 -0
  30. package/dist/cjs/components/PostTrashed.d.ts +14 -0
  31. package/dist/cjs/components/PostView.d.ts +21 -0
  32. package/dist/cjs/components/Posts.d.ts +4 -0
  33. package/dist/cjs/components/Preview.d.ts +1 -3
  34. package/dist/cjs/components/PreviewFiles.d.ts +10 -0
  35. package/dist/cjs/components/Reactions.d.ts +6 -2
  36. package/dist/cjs/components/SearchUsers.d.ts +2 -1
  37. package/dist/cjs/contexts/CloudFilesContext.d.ts +9 -0
  38. package/dist/cjs/hooks/useApps.d.ts +1 -0
  39. package/dist/cjs/hooks/useCloudFiles.d.ts +3 -0
  40. package/dist/cjs/hooks/useCommentList.d.ts +1 -0
  41. package/dist/cjs/hooks/useEmbeds.d.ts +5 -0
  42. package/dist/cjs/hooks/useFileList.d.ts +1 -0
  43. package/dist/cjs/hooks/useFileUploader.d.ts +8 -0
  44. package/dist/cjs/hooks/useFileVersions.d.ts +2 -0
  45. package/dist/cjs/hooks/useInfiniteScroll.d.ts +4 -0
  46. package/dist/cjs/hooks/useIsFirstRender.d.ts +2 -0
  47. package/dist/cjs/hooks/useMutateApps.d.ts +5 -0
  48. package/dist/cjs/hooks/useMutateComment.d.ts +10 -0
  49. package/dist/cjs/hooks/useMutateConversationName.d.ts +1 -1
  50. package/dist/cjs/hooks/useMutateDeleteReaction.d.ts +3 -1
  51. package/dist/cjs/hooks/useMutateEditComment.d.ts +10 -0
  52. package/dist/cjs/hooks/useMutateEditPost.d.ts +10 -0
  53. package/dist/cjs/hooks/useMutateExternalBlobs.d.ts +2 -2
  54. package/dist/cjs/hooks/useMutateFile.d.ts +26 -0
  55. package/dist/cjs/hooks/useMutateFileRename.d.ts +5 -0
  56. package/dist/cjs/hooks/useMutateFileSubscribe.d.ts +7 -0
  57. package/dist/cjs/hooks/useMutateFileTrash.d.ts +10 -0
  58. package/dist/cjs/hooks/useMutateFileVersion.d.ts +7 -0
  59. package/dist/cjs/hooks/useMutateFiles.d.ts +2 -0
  60. package/dist/cjs/hooks/useMutateLeaveConversation.d.ts +4 -0
  61. package/dist/cjs/hooks/useMutateMessage.d.ts +2 -2
  62. package/dist/cjs/hooks/useMutatePost.d.ts +10 -0
  63. package/dist/cjs/hooks/useMutateReaction.d.ts +3 -1
  64. package/dist/cjs/hooks/useMutateRead.d.ts +1 -1
  65. package/dist/cjs/hooks/useMutateReplaceReaction.d.ts +6 -0
  66. package/dist/cjs/hooks/useMutateRestoreComment.d.ts +5 -0
  67. package/dist/cjs/hooks/useMutateRestorePost.d.ts +4 -0
  68. package/dist/cjs/hooks/useMutateStarred.d.ts +4 -0
  69. package/dist/cjs/hooks/useMutateSubscribe.d.ts +4 -0
  70. package/dist/cjs/hooks/useMutateTrashComment.d.ts +5 -0
  71. package/dist/cjs/hooks/useMutateTrashPost.d.ts +4 -0
  72. package/dist/cjs/hooks/useMutateTyping.d.ts +2 -0
  73. package/dist/cjs/hooks/useMutateUnsubscribe.d.ts +4 -0
  74. package/dist/cjs/hooks/useMutateVote.d.ts +5 -0
  75. package/dist/cjs/hooks/usePost.d.ts +1 -0
  76. package/dist/cjs/hooks/usePosts.d.ts +1 -0
  77. package/dist/cjs/hooks/usePostsList.d.ts +1 -0
  78. package/dist/cjs/hooks/useReactionList.d.ts +1 -0
  79. package/dist/cjs/hooks/useSessionState.d.ts +2 -0
  80. package/dist/cjs/hooks/useUnload.d.ts +2 -0
  81. package/dist/cjs/hooks/useUpdateEffect.d.ts +3 -0
  82. package/dist/cjs/hooks/useVotes.d.ts +1 -0
  83. package/dist/cjs/index.d.ts +3 -1
  84. package/dist/cjs/index.js +28 -6
  85. package/dist/cjs/index.js.map +1 -1
  86. package/dist/cjs/types/ConversationListItem.d.ts +1 -0
  87. package/dist/cjs/types/Files.d.ts +7 -0
  88. package/dist/cjs/types/Message.d.ts +2 -2
  89. package/dist/cjs/types/Posts.d.ts +4 -0
  90. package/dist/cjs/types/interfaces.d.ts +9 -0
  91. package/dist/cjs/types/types.d.ts +138 -22
  92. package/dist/cjs/ui/Dropdown.d.ts +18 -2
  93. package/dist/cjs/ui/Icon.d.ts +10 -2
  94. package/dist/cjs/ui/Overlay.d.ts +3 -1
  95. package/dist/cjs/ui/Sheet.d.ts +14 -0
  96. package/dist/cjs/ui/Spinner.d.ts +2 -1
  97. package/dist/cjs/utils/cacheUtils.d.ts +14 -0
  98. package/dist/cjs/utils/fileUtilities.d.ts +10 -1
  99. package/dist/cjs/utils/mentions.d.ts +6 -0
  100. package/dist/cjs/utils/openUrl.d.ts +1 -0
  101. package/dist/css/weavy-chat.css +637 -218
  102. package/dist/css/weavy-files.css +3046 -0
  103. package/dist/css/weavy-messenger.css +643 -213
  104. package/dist/css/weavy-posts.css +2773 -0
  105. package/dist/css/weavy.css +1749 -308
  106. package/dist/esm/client/WeavyClient.d.ts +1 -0
  107. package/dist/esm/components/Attachment.d.ts +5 -5
  108. package/dist/esm/components/Blob.d.ts +9 -0
  109. package/dist/esm/components/Comment.d.ts +18 -0
  110. package/dist/esm/components/CommentCount.d.ts +7 -0
  111. package/dist/esm/components/CommentEdit.d.ts +16 -0
  112. package/dist/esm/components/CommentPlaceholder.d.ts +8 -0
  113. package/dist/esm/components/CommentTrashed.d.ts +15 -0
  114. package/dist/esm/components/CommentView.d.ts +18 -0
  115. package/dist/esm/components/Comments.d.ts +8 -0
  116. package/dist/esm/components/ConversationListItem.d.ts +1 -1
  117. package/dist/esm/components/Dropzone.d.ts +10 -0
  118. package/dist/esm/components/Editor.d.ts +25 -0
  119. package/dist/esm/components/Embed.d.ts +8 -0
  120. package/dist/esm/components/FileItem.d.ts +41 -0
  121. package/dist/esm/components/FileList.d.ts +11 -0
  122. package/dist/esm/components/FileVersions.d.ts +9 -0
  123. package/dist/esm/components/Files.d.ts +4 -0
  124. package/dist/esm/components/Image.d.ts +3 -3
  125. package/dist/esm/components/MeetingCard.d.ts +1 -1
  126. package/dist/esm/components/Meetings.d.ts +2 -1
  127. package/dist/esm/components/Poll.d.ts +8 -0
  128. package/dist/esm/components/PollOption.d.ts +10 -0
  129. package/dist/esm/components/Post.d.ts +21 -0
  130. package/dist/esm/components/PostEdit.d.ts +17 -0
  131. package/dist/esm/components/PostList.d.ts +6 -0
  132. package/dist/esm/components/PostPlaceholder.d.ts +8 -0
  133. package/dist/esm/components/PostTrashed.d.ts +14 -0
  134. package/dist/esm/components/PostView.d.ts +21 -0
  135. package/dist/esm/components/Posts.d.ts +4 -0
  136. package/dist/esm/components/Preview.d.ts +1 -3
  137. package/dist/esm/components/PreviewFiles.d.ts +10 -0
  138. package/dist/esm/components/Reactions.d.ts +6 -2
  139. package/dist/esm/components/SearchUsers.d.ts +2 -1
  140. package/dist/esm/contexts/CloudFilesContext.d.ts +9 -0
  141. package/dist/esm/hooks/useApps.d.ts +1 -0
  142. package/dist/esm/hooks/useCloudFiles.d.ts +3 -0
  143. package/dist/esm/hooks/useCommentList.d.ts +1 -0
  144. package/dist/esm/hooks/useEmbeds.d.ts +5 -0
  145. package/dist/esm/hooks/useFileList.d.ts +1 -0
  146. package/dist/esm/hooks/useFileUploader.d.ts +8 -0
  147. package/dist/esm/hooks/useFileVersions.d.ts +2 -0
  148. package/dist/esm/hooks/useInfiniteScroll.d.ts +4 -0
  149. package/dist/esm/hooks/useIsFirstRender.d.ts +2 -0
  150. package/dist/esm/hooks/useMutateApps.d.ts +5 -0
  151. package/dist/esm/hooks/useMutateComment.d.ts +10 -0
  152. package/dist/esm/hooks/useMutateConversationName.d.ts +1 -1
  153. package/dist/esm/hooks/useMutateDeleteReaction.d.ts +3 -1
  154. package/dist/esm/hooks/useMutateEditComment.d.ts +10 -0
  155. package/dist/esm/hooks/useMutateEditPost.d.ts +10 -0
  156. package/dist/esm/hooks/useMutateExternalBlobs.d.ts +2 -2
  157. package/dist/esm/hooks/useMutateFile.d.ts +26 -0
  158. package/dist/esm/hooks/useMutateFileRename.d.ts +5 -0
  159. package/dist/esm/hooks/useMutateFileSubscribe.d.ts +7 -0
  160. package/dist/esm/hooks/useMutateFileTrash.d.ts +10 -0
  161. package/dist/esm/hooks/useMutateFileVersion.d.ts +7 -0
  162. package/dist/esm/hooks/useMutateFiles.d.ts +2 -0
  163. package/dist/esm/hooks/useMutateLeaveConversation.d.ts +4 -0
  164. package/dist/esm/hooks/useMutateMessage.d.ts +2 -2
  165. package/dist/esm/hooks/useMutatePost.d.ts +10 -0
  166. package/dist/esm/hooks/useMutateReaction.d.ts +3 -1
  167. package/dist/esm/hooks/useMutateRead.d.ts +1 -1
  168. package/dist/esm/hooks/useMutateReplaceReaction.d.ts +6 -0
  169. package/dist/esm/hooks/useMutateRestoreComment.d.ts +5 -0
  170. package/dist/esm/hooks/useMutateRestorePost.d.ts +4 -0
  171. package/dist/esm/hooks/useMutateStarred.d.ts +4 -0
  172. package/dist/esm/hooks/useMutateSubscribe.d.ts +4 -0
  173. package/dist/esm/hooks/useMutateTrashComment.d.ts +5 -0
  174. package/dist/esm/hooks/useMutateTrashPost.d.ts +4 -0
  175. package/dist/esm/hooks/useMutateTyping.d.ts +2 -0
  176. package/dist/esm/hooks/useMutateUnsubscribe.d.ts +4 -0
  177. package/dist/esm/hooks/useMutateVote.d.ts +5 -0
  178. package/dist/esm/hooks/usePost.d.ts +1 -0
  179. package/dist/esm/hooks/usePosts.d.ts +1 -0
  180. package/dist/esm/hooks/usePostsList.d.ts +1 -0
  181. package/dist/esm/hooks/useReactionList.d.ts +1 -0
  182. package/dist/esm/hooks/useSessionState.d.ts +2 -0
  183. package/dist/esm/hooks/useUnload.d.ts +2 -0
  184. package/dist/esm/hooks/useUpdateEffect.d.ts +3 -0
  185. package/dist/esm/hooks/useVotes.d.ts +1 -0
  186. package/dist/esm/index.d.ts +3 -1
  187. package/dist/esm/index.js +28 -6
  188. package/dist/esm/index.js.map +1 -1
  189. package/dist/esm/types/ConversationListItem.d.ts +1 -0
  190. package/dist/esm/types/Files.d.ts +7 -0
  191. package/dist/esm/types/Message.d.ts +2 -2
  192. package/dist/esm/types/Posts.d.ts +4 -0
  193. package/dist/esm/types/interfaces.d.ts +9 -0
  194. package/dist/esm/types/types.d.ts +138 -22
  195. package/dist/esm/ui/Dropdown.d.ts +18 -2
  196. package/dist/esm/ui/Icon.d.ts +10 -2
  197. package/dist/esm/ui/Overlay.d.ts +3 -1
  198. package/dist/esm/ui/Sheet.d.ts +14 -0
  199. package/dist/esm/ui/Spinner.d.ts +2 -1
  200. package/dist/esm/utils/cacheUtils.d.ts +14 -0
  201. package/dist/esm/utils/fileUtilities.d.ts +10 -1
  202. package/dist/esm/utils/mentions.d.ts +6 -0
  203. package/dist/esm/utils/openUrl.d.ts +1 -0
  204. package/dist/index.d.ts +50 -6
  205. package/package.json +8 -2
  206. package/src/client/WeavyClient.ts +35 -1
  207. package/src/components/Attachment.tsx +20 -13
  208. package/src/components/Blob.tsx +28 -0
  209. package/src/components/Comment.tsx +43 -0
  210. package/src/components/CommentCount.tsx +15 -0
  211. package/src/components/CommentEdit.tsx +48 -0
  212. package/src/components/CommentPlaceholder.tsx +34 -0
  213. package/src/components/CommentTrashed.tsx +45 -0
  214. package/src/components/CommentView.tsx +142 -0
  215. package/src/components/Comments.tsx +78 -0
  216. package/src/components/Conversation.tsx +85 -31
  217. package/src/components/ConversationList.tsx +12 -41
  218. package/src/components/ConversationListItem.tsx +125 -74
  219. package/src/components/Dropzone.tsx +26 -0
  220. package/src/components/Editor.tsx +700 -0
  221. package/src/components/Embed.tsx +80 -0
  222. package/src/components/FileItem.tsx +391 -0
  223. package/src/components/FileList.tsx +166 -0
  224. package/src/components/FileVersions.tsx +100 -0
  225. package/src/components/Files.tsx +294 -0
  226. package/src/components/Image.tsx +11 -10
  227. package/src/components/Meeting.tsx +1 -2
  228. package/src/components/MeetingCard.tsx +1 -1
  229. package/src/components/Meetings.tsx +13 -5
  230. package/src/components/Message.tsx +14 -19
  231. package/src/components/Messages.tsx +38 -64
  232. package/src/components/NewConversation.tsx +8 -6
  233. package/src/components/PdfViewer.tsx +2 -2
  234. package/src/components/Poll.tsx +45 -0
  235. package/src/components/PollOption.tsx +65 -0
  236. package/src/components/Post.tsx +45 -0
  237. package/src/components/PostEdit.tsx +49 -0
  238. package/src/components/PostList.tsx +95 -0
  239. package/src/components/PostPlaceholder.tsx +32 -0
  240. package/src/components/PostTrashed.tsx +35 -0
  241. package/src/components/PostView.tsx +194 -0
  242. package/src/components/Posts.tsx +59 -0
  243. package/src/components/Preview.tsx +16 -23
  244. package/src/components/PreviewFiles.tsx +336 -0
  245. package/src/components/Reactions.tsx +142 -38
  246. package/src/components/SearchUsers.tsx +77 -37
  247. package/src/components/Typing.tsx +1 -1
  248. package/src/{components/FileBrowser.tsx → contexts/CloudFilesContext.tsx} +46 -30
  249. package/src/contexts/PreviewContext.tsx +102 -85
  250. package/src/contexts/WeavyContext.tsx +10 -6
  251. package/src/hooks/useApps.ts +23 -0
  252. package/src/hooks/useCloudFiles.ts +12 -0
  253. package/src/hooks/useCommentList.ts +30 -0
  254. package/src/hooks/useEmbeds.ts +126 -0
  255. package/src/hooks/useEvents.ts +3 -1
  256. package/src/hooks/useFileList.ts +84 -0
  257. package/src/hooks/useFileUploader.ts +38 -1
  258. package/src/hooks/useFileVersions.ts +20 -0
  259. package/src/hooks/useInfiniteScroll.ts +45 -0
  260. package/src/hooks/useIsFirstRender.ts +15 -0
  261. package/src/hooks/useMembers.ts +3 -3
  262. package/src/hooks/useMutateApps.ts +48 -0
  263. package/src/hooks/useMutateComment.ts +60 -0
  264. package/src/hooks/useMutateConversationName.ts +1 -1
  265. package/src/hooks/useMutateDeleteReaction.ts +17 -4
  266. package/src/hooks/useMutateEditComment.ts +63 -0
  267. package/src/hooks/useMutateEditPost.ts +64 -0
  268. package/src/hooks/useMutateExternalBlobs.ts +5 -9
  269. package/src/hooks/useMutateFile.ts +311 -0
  270. package/src/hooks/useMutateFileRename.ts +51 -0
  271. package/src/hooks/useMutateFileSubscribe.ts +80 -0
  272. package/src/hooks/useMutateFileTrash.ts +115 -0
  273. package/src/hooks/useMutateFileVersion.ts +85 -0
  274. package/src/hooks/useMutateFiles.ts +23 -0
  275. package/src/hooks/useMutateLeaveConversation.ts +38 -0
  276. package/src/hooks/useMutateMessage.ts +23 -62
  277. package/src/hooks/useMutatePost.ts +60 -0
  278. package/src/hooks/useMutateReaction.ts +21 -6
  279. package/src/hooks/useMutateRead.ts +8 -2
  280. package/src/hooks/useMutateRemoveMembers.ts +2 -9
  281. package/src/hooks/useMutateReplaceReaction.ts +59 -0
  282. package/src/hooks/useMutateRestoreComment.ts +37 -0
  283. package/src/hooks/useMutateRestorePost.ts +36 -0
  284. package/src/hooks/useMutateStarred.ts +35 -0
  285. package/src/hooks/useMutateSubscribe.ts +36 -0
  286. package/src/hooks/useMutateTrashComment.ts +37 -0
  287. package/src/hooks/useMutateTrashPost.ts +36 -0
  288. package/src/hooks/useMutateTyping.ts +5 -3
  289. package/src/hooks/useMutateUnsubscribe.ts +36 -0
  290. package/src/hooks/useMutateVote.ts +59 -0
  291. package/src/hooks/usePost.ts +20 -0
  292. package/src/hooks/usePosts.ts +21 -0
  293. package/src/hooks/usePostsList.ts +31 -0
  294. package/src/hooks/useReactionList.ts +21 -0
  295. package/src/hooks/useSearchUsers.ts +2 -2
  296. package/src/hooks/useSessionState.ts +23 -0
  297. package/src/hooks/useUnload.ts +19 -0
  298. package/src/hooks/useUpdateEffect.ts +16 -0
  299. package/src/hooks/useVotes.ts +21 -0
  300. package/src/index.ts +5 -1
  301. package/src/scss/theme/_appbar.scss +8 -4
  302. package/src/scss/theme/_card.scss +2 -0
  303. package/src/scss/theme/_comment-editor-cm.scss +5 -1
  304. package/src/scss/theme/_comments.scss +9 -8
  305. package/src/scss/theme/_conversations.scss +4 -0
  306. package/src/scss/theme/_files.scss +2 -81
  307. package/src/scss/theme/_icons.scss +21 -3
  308. package/src/scss/theme/_input.scss +13 -7
  309. package/src/scss/theme/_item.scss +23 -1
  310. package/src/scss/theme/_message-editor-cm.scss +5 -1
  311. package/src/scss/theme/_pager.scss +1 -1
  312. package/src/scss/theme/_post-editor-cm.scss +2 -2
  313. package/src/scss/theme/_post.scss +3 -10
  314. package/src/scss/theme/_preview-pdf-viewer.scss +996 -0
  315. package/src/scss/theme/_preview-pdf.scss +57 -783
  316. package/src/scss/theme/_sheet.scss +4 -1
  317. package/src/scss/theme/_spinner.scss +10 -1
  318. package/src/scss/theme/_tables.scss +2 -0
  319. package/src/scss/theme/base/_scroll.scss +3 -0
  320. package/src/scss/weavy-chat.scss +3 -1
  321. package/src/scss/weavy-files.scss +31 -0
  322. package/src/scss/weavy-messenger.scss +3 -1
  323. package/src/scss/weavy-posts.scss +35 -0
  324. package/src/scss/weavy.scss +2 -0
  325. package/src/types/ConversationListItem.ts +1 -0
  326. package/src/types/Files.ts +7 -0
  327. package/src/types/Message.ts +2 -2
  328. package/src/types/Posts.ts +4 -0
  329. package/src/types/interfaces.ts +13 -0
  330. package/src/types/types.ts +157 -28
  331. package/src/ui/Button.tsx +6 -5
  332. package/src/ui/Dropdown.tsx +67 -16
  333. package/src/ui/Icon.tsx +112 -15
  334. package/src/ui/Overlay.tsx +6 -2
  335. package/src/ui/Sheet.tsx +87 -0
  336. package/src/ui/Spinner.tsx +7 -4
  337. package/src/utils/cacheUtils.ts +246 -0
  338. package/src/utils/fileUtilities.ts +208 -24
  339. package/src/utils/infinite-scroll.js +103 -0
  340. package/src/utils/mentions.ts +50 -0
  341. package/src/utils/openUrl.ts +41 -0
  342. package/src/utils/{scrollToBottom.js → scroll-position.js} +50 -9
  343. package/src/utils/{scrollbarDetection.js → scrollbar-detection.js} +0 -0
  344. package/src/utils/utils.js +15 -1
  345. package/tsconfig.json +1 -1
  346. package/dist/cjs/components/ConversationForm.d.ts +0 -7
  347. package/dist/cjs/components/File.d.ts +0 -9
  348. package/dist/cjs/components/FileBrowser.d.ts +0 -6
  349. package/dist/cjs/hooks/usePreview.d.ts +0 -4
  350. package/dist/cjs/hooks/useReactions.d.ts +0 -3
  351. package/dist/esm/components/ConversationForm.d.ts +0 -7
  352. package/dist/esm/components/File.d.ts +0 -9
  353. package/dist/esm/components/FileBrowser.d.ts +0 -6
  354. package/dist/esm/hooks/usePreview.d.ts +0 -4
  355. package/dist/esm/hooks/useReactions.d.ts +0 -3
  356. package/src/components/ConversationForm.tsx +0 -210
  357. package/src/components/File.tsx +0 -21
  358. package/src/hooks/usePreview.ts +0 -21
  359. package/src/hooks/useReactions.ts +0 -51
  360. package/src/utils/infiniteScroll.js +0 -184
@@ -0,0 +1,64 @@
1
+ import { useContext } from "react";
2
+ import { useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+
5
+ /// PATCH to update a Post
6
+ export default function useMutateEditPost() {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateEditPost must be used within an WeavyProvider');
13
+ }
14
+
15
+ type MutateProps = {
16
+ id: number | null,
17
+ appId: number | null,
18
+ text: string,
19
+ blobs: BlobType[],
20
+ attachments: FileType[],
21
+ options: PollOptionType[],
22
+ meeting: number | null,
23
+ embed: number | null
24
+ }
25
+
26
+ const mutatePost = async ({ id, text, blobs, attachments, meeting, embed, options }: MutateProps) => {
27
+
28
+ const response = await client.post("/api/posts/" + id,
29
+ "PATCH",
30
+ JSON.stringify({
31
+ text: text,
32
+ blobs: blobs.map((b: BlobType) => { return b.id }),
33
+ attachments: attachments.map((a: FileType) => { return a.id }),
34
+ meeting_id: meeting,
35
+ embed_id: embed,
36
+ options: options.filter((o: PollOptionType) => o.text.trim() !== "").map((o: PollOptionType) => { return { id: o.id, text: o.text }; })
37
+ }));
38
+ return response.json();
39
+ };
40
+
41
+ return useMutation(mutatePost, {
42
+ onSuccess: (data: MessageType, variables: any, context: any) => {
43
+
44
+ const previousData = queryClient.getQueryData<any>(['posts', variables.appId]);
45
+
46
+ if (previousData && previousData.pages) {
47
+ // update cache
48
+ const newPagesArray = previousData.pages.map((page: any, i: number) => {
49
+ // update post
50
+ page.data = [...page.data.map((message: MessageType) => message.id === variables.id ? data : message)]
51
+ return page;
52
+ }) ?? [];
53
+
54
+ queryClient.setQueryData(["posts", variables.appId], (data: any) => ({
55
+ pages: newPagesArray,
56
+ pageParams: data.pageParams,
57
+ }));
58
+ }
59
+ },
60
+ onMutate: async (variables: any) => {
61
+
62
+ }
63
+ });
64
+ }
@@ -1,12 +1,9 @@
1
1
  import { useContext } from "react";
2
- import { useMutation, useQueryClient } from "react-query";
2
+ import { useMutation } from "react-query";
3
3
  import { WeavyContext } from "../contexts/WeavyContext";
4
4
 
5
5
  /// POST to upload an external blob
6
- export default function useMutateExternalBlobs() {
7
-
8
- const { client } = useContext(WeavyContext);
9
- const queryClient = useQueryClient();
6
+ export default function useMutateExternalBlobs(client: WeavyClient) {
10
7
 
11
8
  if (!client) {
12
9
  throw new Error('useMutateExternalBlobs must be used within an WeavyProvider');
@@ -14,14 +11,13 @@ export default function useMutateExternalBlobs() {
14
11
 
15
12
 
16
13
  type MutateProps = {
17
- blobs: []
14
+ blob: any
18
15
  }
19
16
 
20
- const mutateExternalBlobs = async ({ blobs }: MutateProps) => {
21
-
17
+ const mutateExternalBlobs = async ({ blob }: MutateProps) => {
22
18
  const response = await client.post("/api/blobs/external",
23
19
  "POST",
24
- JSON.stringify(blobs));
20
+ JSON.stringify(blob));
25
21
 
26
22
  return response.json();
27
23
  };
@@ -0,0 +1,311 @@
1
+ import { useContext, useEffect, useState } from "react";
2
+ import { InfiniteData, Mutation, MutationKey, QueryClient, QueryKey, useMutation, useQueryClient } from "react-query";
3
+ import { addCacheItem, findAnyExistingItem, removeCacheItem, updateCacheItem } from "../utils/cacheUtils";
4
+ import { UserContext } from "../contexts/UserContext";
5
+ import { getKind, getWebPreviewFormat } from "../utils/fileUtilities";
6
+ import { UploadProgressProps, useUploadFile } from "./useFileUploader";
7
+
8
+ export type CreateFileProps = {
9
+ blob: BlobType,
10
+ file?: FileType,
11
+ replace?: boolean
12
+ }
13
+
14
+ export type MutateFileProps = {
15
+ file: File,
16
+ onProgress?: (variables: UploadProgressProps) => void // Hack to be able to pass a progress function in the mutation.
17
+ }
18
+
19
+ export type MutationFileContext = {
20
+ file: FileType,
21
+ blob: BlobType
22
+ }
23
+
24
+ export type FileMutation = Mutation<FileType|BlobType, ServerErrorResponse, MutateFileProps, MutationFileContext | undefined>;
25
+
26
+ export function isFileMutation(mutation: unknown): mutation is FileMutation {
27
+ return (mutation as FileMutation).state !== undefined;
28
+ }
29
+
30
+ const getTempFile = (file: File | URL | undefined, name: string, size: number = 0, type: string = "application/octet-stream", refId: number, user: UserType) => {
31
+ const srcUrl = file && (file instanceof URL ? file.toString() : URL.createObjectURL(file));
32
+ const tempFile: FileType = {
33
+ id: refId,
34
+ refId: refId,
35
+ name: name,
36
+ kind: getKind(name),
37
+ size: size,
38
+ media_type: type,
39
+ embed_url: "",
40
+ preview_format: getWebPreviewFormat(name),
41
+ thumbnail_url: srcUrl,
42
+ preview_url: srcUrl,
43
+ download_url: srcUrl,
44
+ version: -1,
45
+ created_by: user,
46
+ created_by_id: user.id,
47
+ created_at: new Date().toUTCString(),
48
+ is_subscribed: false,
49
+ is_trashed: false,
50
+ status: "pending"
51
+ };
52
+ return tempFile;
53
+ }
54
+
55
+ const setMutationContext = (queryClient: QueryClient, mutationKey: MutationKey, variables: any, contextMutation: (context: any) => void) => {
56
+ // Needed to be able to use the blob on errors
57
+ let mutation = <FileMutation>queryClient.getMutationCache().find({ mutationKey: mutationKey, predicate: (mutation) => {
58
+ return (mutation as FileMutation).options.variables === variables;
59
+ }});
60
+
61
+ if (mutation && mutation.state.context) {
62
+ let newContext = {...mutation.state.context};
63
+ contextMutation(newContext);
64
+ mutation.setState({...mutation.state, context: newContext})
65
+ }
66
+ }
67
+
68
+ export function useMutatingFileUploads(filesKey: MutationKey) {
69
+
70
+ const queryClient = useQueryClient();
71
+
72
+ const [mutations, setMutations] = useState<FileMutation[]>([])
73
+ const [status, setStatus] = useState<string>();
74
+ const [progress, setProgress] = useState<number>();
75
+
76
+ const getProgress = (mutations: FileMutation[]) => mutations.reduce((combinedProgress, mutation) => {
77
+ let file = mutation.state.context?.file as FileType;
78
+ if (file && file.progress && file.size) {
79
+ return {
80
+ loaded: combinedProgress.loaded + file.progress * file.size,
81
+ total: combinedProgress.total + file.size
82
+ }
83
+ }
84
+ return combinedProgress;
85
+ }, { loaded: 0, total: 0 });
86
+
87
+ const getStatus = (mutations: FileMutation[]) =>
88
+ mutations.some((mutation) => mutation.state.error?.status === 409) ? "conflict" :
89
+ mutations.some((mutation) => mutation.state.status === "error") ? "error" :
90
+ mutations.every((mutation) => mutation.state.status === "success") ? "ok" :
91
+ "pending";
92
+
93
+ const updateMutations = () => {
94
+ let newMutations = <unknown>queryClient.getMutationCache().findAll({
95
+ mutationKey: filesKey
96
+ }) as FileMutation[];
97
+
98
+ setMutations(newMutations);
99
+ }
100
+
101
+ useEffect(() => {
102
+ updateMutations()
103
+ return queryClient.getMutationCache().subscribe(() => updateMutations())
104
+ }, [...filesKey])
105
+
106
+ useEffect(() => {
107
+ setStatus(getStatus(mutations));
108
+
109
+ let totalProgress = getProgress(mutations);
110
+
111
+ setProgress(totalProgress.total > 0 ? totalProgress.loaded / totalProgress.total : undefined)
112
+ }, [mutations])
113
+
114
+ return {
115
+ mutations,
116
+ status,
117
+ progress
118
+ };
119
+ }
120
+
121
+ export function useRemoveMutatingFileUpload(filesKey: MutationKey) {
122
+ const queryClient = useQueryClient();
123
+ const mutationKey: MutationKey = filesKey;
124
+
125
+ return (mutation: FileMutation) => {
126
+ if (queryClient.getMutationCache().find({ mutationKey: mutationKey, predicate: (m) => m === mutation })) {
127
+ updateCacheItem(queryClient, filesKey, mutation.state.context!.file?.refId || mutation.state.context!.file?.id, (file: FileType) => Object.assign(file, { status: "ok", statusText: undefined, progress: undefined }));
128
+ queryClient.getMutationCache().remove(mutation);
129
+ }
130
+ }
131
+ }
132
+
133
+ export function useClearMutatingFileUpload(filesKey: MutationKey) {
134
+ const queryClient = useQueryClient();
135
+ const mutationKey: MutationKey = filesKey;
136
+
137
+ return () => {
138
+ let mutations = queryClient.getMutationCache().findAll({ mutationKey: mutationKey });
139
+ mutations.forEach((mutation) => {
140
+ if (isFileMutation(mutation)) {
141
+ updateCacheItem(queryClient, filesKey, mutation.state.context?.file?.refId || mutation.state.context!.file?.id, (file: FileType) => Object.assign(file, { status: "ok", statusText: undefined, progress: undefined }));
142
+ }
143
+ queryClient.getMutationCache().remove(mutation);
144
+ });
145
+
146
+ }
147
+ }
148
+
149
+ /// Upload mutation to upload and add a new file for a <FileType> query
150
+ export function useMutateFileUpload(filesKey: QueryKey, createFile?: ({ blob, replace }: CreateFileProps) => Promise<FileType>) {
151
+ const queryClient = useQueryClient();
152
+ const { user } = useContext(UserContext);
153
+
154
+ const mutationKey: MutationKey = filesKey;
155
+
156
+ /// POST to upload a a file
157
+ const uploadFile = useUploadFile();
158
+
159
+ return useMutation(async (variables: MutateFileProps) => {
160
+ let uploadedFile = await uploadFile(variables.file, variables.onProgress);
161
+
162
+ // Needed to be able to use the blob on errors
163
+ setMutationContext(queryClient, mutationKey, variables, (context) => {
164
+ context.blob = uploadedFile;
165
+ })
166
+
167
+ return createFile ? await createFile({ blob: uploadedFile }) : uploadedFile;
168
+ }, {
169
+ mutationKey: mutationKey,
170
+ onMutate: async (variables: MutateFileProps) => {
171
+ await queryClient.cancelQueries({ queryKey: filesKey, exact: true});
172
+
173
+ let files = queryClient.getQueryData<InfiniteData<FilesResult>>(filesKey);
174
+ let file = findAnyExistingItem<FileType>(files, "name", variables.file.name, true);
175
+
176
+
177
+ if (!file) {
178
+ // If non existing add optimistic update
179
+ const meta = queryClient.getQueryCache().find<FilesResult>(filesKey)?.meta;
180
+ const order = meta?.order as FileOrder;
181
+
182
+ let refId = Math.random();
183
+
184
+ file = getTempFile(variables.file, variables.file.name, variables.file.size, variables.file.type, refId, user);
185
+ addCacheItem(queryClient, filesKey, file, file.id, order);
186
+ }
187
+
188
+ variables.onProgress = ({progress}) => {
189
+ if (file?.id) {
190
+ updateCacheItem(queryClient, filesKey, file.refId || file.id, (cacheFile: FileType) => Object.assign(cacheFile, { status: "pending", progress: progress }));
191
+ }
192
+
193
+ setMutationContext(queryClient, mutationKey, variables, (context) => {
194
+ context.file.progress = progress;
195
+ context.file.status = "pending";
196
+ })
197
+ }
198
+
199
+ return <MutationFileContext>{ file }
200
+ },
201
+ onSuccess: (data: FileType | BlobType, variables: MutateFileProps, context: MutationFileContext | undefined) => {
202
+ if (context?.file?.id) {
203
+ updateCacheItem(queryClient, filesKey, context.file.id, (file: FileType) => Object.assign(file, data as FileType || context?.file, { status: "ok", statusText: undefined, progress: undefined }));
204
+ }
205
+ setMutationContext(queryClient, mutationKey, variables, (context) => {
206
+ context.file.progress = undefined;
207
+ context.file.status = "ok";
208
+ context.file.statusText = undefined;
209
+ });
210
+ queryClient.invalidateQueries({queryKey: filesKey});
211
+ },
212
+ onError(error: ServerErrorResponse, variables: MutateFileProps, context: MutationFileContext | undefined) {
213
+ if (error && error.status === 409) {
214
+ if (context?.file?.id) {
215
+ updateCacheItem(queryClient, filesKey, context.file.id, (file: FileType) => Object.assign(file, { status: "conflict", statusText: error.detail || error.title, progress: undefined }));
216
+ }
217
+ setMutationContext(queryClient, mutationKey, variables, (context) => {
218
+ context.file.progress = undefined;
219
+ context.file.status = "conflict";
220
+ context.file.statusText = error.detail || error.title;
221
+ })
222
+ } else {
223
+ if(context?.file?.id) {
224
+ if (context.file.id >= 1) {
225
+ updateCacheItem(queryClient, filesKey, context.file.id, (file: FileType) => Object.assign(file, { status: "error", statusText: error.detail || error.title, progress: undefined }));
226
+ } else if(context.file.id >= 0 && context.file.id < 1) {
227
+ removeCacheItem(queryClient, filesKey, context.file.id);
228
+ }
229
+ }
230
+ setMutationContext(queryClient, mutationKey, variables, (context) => {
231
+ if (context) {
232
+ context.file.progress = undefined;
233
+ context.file.status = "error";
234
+ context.file.statusText = error.detail || error.title;
235
+ }
236
+ })
237
+ }
238
+ },
239
+ });
240
+ }
241
+
242
+ /// Mutation for adding a <BlobType> to a <FileType> query
243
+ export function useMutateFileCreate(filesKey: QueryKey, createFile: ({ blob, file, replace }: CreateFileProps) => Promise<FileType>) {
244
+ const queryClient = useQueryClient();
245
+ const { user } = useContext(UserContext);
246
+ const mutationKey: MutationKey = filesKey;
247
+
248
+ return useMutation(createFile, {
249
+ mutationKey: mutationKey,
250
+ onMutate: async (variables : CreateFileProps) => {
251
+ await queryClient.cancelQueries({queryKey: filesKey, exact: true});
252
+
253
+ let files = queryClient.getQueryData<InfiniteData<FilesResult>>(filesKey);
254
+ let file = findAnyExistingItem<FileType>(files, "name", variables.blob.name, true) || variables.file;
255
+
256
+ if (!file) {
257
+ // If non existing add optimistic update
258
+ const meta = queryClient.getQueryCache().find<FilesResult>(filesKey)!.meta;
259
+ const order = meta!.order as FileOrder;
260
+
261
+ let refId = Math.random();
262
+ var srcUrl: URL | undefined;
263
+ try {
264
+ srcUrl = variables.blob.thumbnail_url && new URL(variables.blob.thumbnail_url) || undefined;
265
+ } catch(e) { /* no worries */}
266
+
267
+ file = getTempFile(srcUrl, variables.blob.name, variables.blob.size, variables.blob.media_type, refId, user);
268
+ addCacheItem(queryClient, filesKey, file, file.id, order);
269
+ } else {
270
+ updateCacheItem(queryClient, filesKey, file?.refId || file?.id, (file: FileType) => Object.assign(file, { status: "pending" }));
271
+ }
272
+
273
+ return <MutationFileContext>{ file: file, blob: variables.blob }
274
+ },
275
+ onSuccess: (data: FileType, variables: CreateFileProps, context: MutationFileContext | undefined) => {
276
+ updateCacheItem(queryClient, filesKey, context!.file?.refId || context!.file?.id, (file: FileType) => Object.assign(file, data, { status: "ok", statusText: undefined, progress: undefined }));
277
+ setMutationContext(queryClient, mutationKey, variables, (context) => {
278
+ context.file.progress = undefined;
279
+ context.file.status = "ok";
280
+ context.file.statusText = undefined;
281
+ });
282
+ queryClient.invalidateQueries({queryKey: filesKey});
283
+ },
284
+ onError(error: ServerErrorResponse, variables: CreateFileProps, context: MutationFileContext | undefined) {
285
+ if (error && error.status === 409) {
286
+ updateCacheItem(queryClient, filesKey, context!.file?.refId || context!.file?.id, (file: FileType) => Object.assign(file, { status: "conflict", statusText: error.detail || error.title, progress: undefined }));
287
+ setMutationContext(queryClient, mutationKey, variables, (context) => {
288
+ context.file.progress = undefined;
289
+ context.file.status = "conflict";
290
+ context.file.statusText = error.detail || error.title;
291
+ })
292
+ } else {
293
+ const errorId = context!.file?.refId || context!.file?.id;
294
+ if (errorId) {
295
+ if (errorId >= 1) {
296
+ updateCacheItem(queryClient, filesKey, errorId, (file: FileType) => Object.assign(file, { status: "error", statusText: error.detail || error.title, progress: undefined }));
297
+ } else if (errorId > 0 && errorId < 1) {
298
+ removeCacheItem(queryClient, mutationKey, errorId);
299
+ }
300
+ }
301
+ setMutationContext(queryClient, mutationKey, variables, (context) => {
302
+ if (context) {
303
+ context.file.progress = undefined;
304
+ context.file.status = "error";
305
+ context.file.statusText = error.detail || error.title;
306
+ }
307
+ })
308
+ }
309
+ },
310
+ })
311
+ }
@@ -0,0 +1,51 @@
1
+ import { useContext } from "react";
2
+ import { MutationKey, useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+ import { updateCacheItem } from "../utils/cacheUtils";
5
+
6
+ /// PATCH to update a File
7
+ export default function useMutateFileRename(filesKey: MutationKey) {
8
+
9
+ const { client } = useContext(WeavyContext);
10
+ const queryClient = useQueryClient();
11
+
12
+ if (!client) {
13
+ throw new Error('useMutateFileRename must be used within an WeavyProvider');
14
+ }
15
+
16
+ type MutateProps = {
17
+ file: FileType,
18
+ name: string,
19
+ }
20
+
21
+ const mutateRename = async ({ file, name }: MutateProps) => {
22
+ if (file.id >=1) {
23
+ const response = await client.post("/api/files/" + file.id,
24
+ "PATCH",
25
+ JSON.stringify({
26
+ name: name
27
+ }));
28
+
29
+ if (!response.ok) {
30
+ throw <ServerErrorResponse> await response.json();
31
+ }
32
+
33
+ return response.json();
34
+ } else {
35
+ throw new Error(`Could not rename ${file.name}`);
36
+ }
37
+ };
38
+
39
+ return useMutation<FileType, ServerErrorResponse, MutateProps, void>(mutateRename, {
40
+ onMutate: async (variables: MutateProps) => {
41
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { name: variables.name }));
42
+ },
43
+ onSuccess: (data: FileType, variables: MutateProps, context: any) => {
44
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, data, { status: "ok" }));
45
+ },
46
+ onError(error, variables: MutateProps, context) {
47
+ // Show/update in mutation list also?
48
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { name: variables.file.name }));
49
+ },
50
+ });
51
+ }
@@ -0,0 +1,80 @@
1
+ import { useContext } from "react";
2
+ import { MutationKey, useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+ import { updateCacheItem } from "../utils/cacheUtils";
5
+
6
+ export function useMutateFileSubscribe(filesKey: MutationKey) {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateFileSubscribe must be used within an WeavyProvider');
13
+ }
14
+
15
+ type MutateProps = {
16
+ file: FileType
17
+ }
18
+
19
+ const subscribeMutation = useMutation(async ({ file }: MutateProps) => {
20
+ if (file.id >= 1) {
21
+ const response = await client.post("/api/files/" + file.id + "/subscribe", "POST", "");
22
+ if (!response.ok) {
23
+ throw await response.json();
24
+ }
25
+ return response.json();
26
+ } else {
27
+ throw new Error(`Could not subscribe to ${file.name}.`);
28
+ }
29
+ },{
30
+ onMutate: async (variables: MutateProps) => {
31
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { is_subscribed: true, status: "pending" }));
32
+ },
33
+ onSuccess: (data: FileType, variables: MutateProps, context: any) => {
34
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, data, { status: "ok" }));
35
+ },
36
+ onError(error: any, variables: MutateProps, context: any) {
37
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { is_subscribed: variables.file.is_subscribed, status: "error" }));
38
+ },
39
+ });
40
+
41
+ return subscribeMutation;
42
+ }
43
+
44
+ export function useMutateFileUnsubscribe(filesKey: MutationKey) {
45
+
46
+ const { client } = useContext(WeavyContext);
47
+ const queryClient = useQueryClient();
48
+
49
+ if (!client) {
50
+ throw new Error('useMutateFileUnsubscribe must be used within an WeavyProvider');
51
+ }
52
+
53
+ type MutateProps = {
54
+ file: FileType
55
+ }
56
+
57
+ const unsubscribeMutation = useMutation<FileType, ServerErrorResponse, MutateProps, any>(async ({ file }: MutateProps) => {
58
+ if (file.id >= 1) {
59
+ const response = await client.post("/api/files/" + file.id + "/unsubscribe", "POST", "");
60
+ if (!response.ok) {
61
+ throw <ServerErrorResponse> await response.json();
62
+ }
63
+ return response.json();
64
+ } else {
65
+ throw new Error(`Could not unsubscribe to ${file.name}.`);
66
+ }
67
+ },{
68
+ onMutate: async (variables: MutateProps) => {
69
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { is_subscribed: false }));
70
+ },
71
+ onSuccess: (data: FileType, variables: MutateProps, context: any) => {
72
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, data, { status: "ok" }));
73
+ },
74
+ onError(error: any, variables: MutateProps, context: any) {
75
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { is_subscribed: variables.file.is_subscribed }));
76
+ },
77
+ });
78
+
79
+ return unsubscribeMutation;
80
+ }
@@ -0,0 +1,115 @@
1
+ import { useContext } from "react";
2
+ import { MutationKey, useMutation, useQueryClient } from "react-query";
3
+ import { WeavyContext } from "../contexts/WeavyContext";
4
+ import { removeCacheItem, updateCacheItem } from "../utils/cacheUtils";
5
+
6
+ export function useMutateFileTrash(filesKey: MutationKey) {
7
+
8
+ const { client } = useContext(WeavyContext);
9
+ const queryClient = useQueryClient();
10
+
11
+ if (!client) {
12
+ throw new Error('useMutateFileTrash must be used within an WeavyProvider');
13
+ }
14
+
15
+ type MutateProps = {
16
+ file: FileType
17
+ }
18
+
19
+ const trashMutation = useMutation(async ({ file }: MutateProps) => {
20
+ if (file.id >= 1) {
21
+ const response = await client.post("/api/files/" + file.id + "/trash", "POST", "");
22
+ return response.json();
23
+ } else {
24
+ throw new Error(`Could not trash ${file.name}.`);
25
+ }
26
+ },{
27
+ onMutate: async (variables: MutateProps) => {
28
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { is_trashed: true, status: "pending" }));
29
+ },
30
+ onSuccess: (data: FileType, variables: MutateProps, context: any) => {
31
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, data, { status: "ok" }));
32
+ },
33
+ onError(error: any, variables: MutateProps, context: any) {
34
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { is_trashed: false, status: "error" }));
35
+ },
36
+ });
37
+
38
+ return trashMutation;
39
+ }
40
+
41
+ export function useMutateFileRestore(filesKey: MutationKey) {
42
+
43
+ const { client } = useContext(WeavyContext);
44
+ const queryClient = useQueryClient();
45
+
46
+ if (!client) {
47
+ throw new Error('useMutateFileRestore must be used within an WeavyProvider');
48
+ }
49
+
50
+ type MutateProps = {
51
+ file: FileType
52
+ }
53
+
54
+ const restoreMutation = useMutation(async ({ file }: MutateProps) => {
55
+ if (file.id >= 1) {
56
+ const response = await client.post("/api/files/" + file.id + "/restore", "POST","");
57
+ if (!response.ok) {
58
+ throw <ServerErrorResponse> await response.json();
59
+ }
60
+ return response.json();
61
+ } else {
62
+ throw <ServerErrorResponse> {status: 400, title: `Could not restore ${file.name}.`};
63
+ }
64
+ },{
65
+ onMutate: async (variables: MutateProps) => {
66
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { is_trashed: false }));
67
+ },
68
+ onSuccess: (data: FileType, variables: MutateProps, context: any) => {
69
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, data, { status: "ok" }));
70
+ },
71
+ onError(error: any, variables: MutateProps, context: any) {
72
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { is_trashed: true }));
73
+ },
74
+ });
75
+
76
+ return restoreMutation;
77
+ }
78
+
79
+ export function useMutateFileDeleteForever(filesKey: MutationKey) {
80
+
81
+ const { client } = useContext(WeavyContext);
82
+ const queryClient = useQueryClient();
83
+
84
+ if (!client) {
85
+ throw new Error('useMutateFileDeleteForever must be used within an WeavyProvider');
86
+ }
87
+
88
+ type MutateProps = {
89
+ file: FileType
90
+ }
91
+
92
+ const deleteForeverMutation = useMutation(async ({ file }: MutateProps) => {
93
+ if (file.id >= 1 && file.is_trashed) {
94
+ const response = await client.post("/api/files/" + file.id, "DELETE","");
95
+ if (!response.ok) {
96
+ throw <ServerErrorResponse> await response.json();
97
+ }
98
+ } else {
99
+ throw <ServerErrorResponse> {status: 400, title: `Could not delete ${file.name} forever.`};
100
+ }
101
+ },{
102
+ onMutate: async (variables: MutateProps) => {
103
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { status: "pending" }));
104
+ },
105
+ onSuccess: (data: any, variables: any, context: any) => {
106
+ removeCacheItem(queryClient, filesKey, variables.file.id);
107
+ },
108
+ onError(error, variables, context) {
109
+ // Show in error list instead?
110
+ updateCacheItem(queryClient, filesKey, variables.file.id, (existingFile: FileType) => Object.assign(existingFile, { status: undefined }));
111
+ },
112
+ });
113
+
114
+ return deleteForeverMutation;
115
+ }