@messenger-box/platform-browser 0.0.1-alpha.135 → 0.0.1-alpha.136

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.0.1-alpha.136](https://github.com/cdmbase/messenger-box/compare/v0.0.1-alpha.135...v0.0.1-alpha.136) (2022-02-14)
7
+
8
+
9
+ ### Features
10
+
11
+ * add support to upload files via signed url ([d83f7d3](https://github.com/cdmbase/messenger-box/commit/d83f7d360d5bab45a55f6ea45e44da670d8a6ace))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [0.0.1-alpha.135](https://github.com/cdmbase/messenger-box/compare/v0.0.1-alpha.134...v0.0.1-alpha.135) (2022-02-10)
7
18
 
8
19
  **Note:** Version bump only for package @messenger-box/platform-browser
@@ -0,0 +1,2 @@
1
+ export * from './use-upload-file.hook';
2
+ export * from './use-upload-files.hook';
@@ -0,0 +1,2 @@
1
+ import { useUploadFile as useBaseUploadFile } from '../temp';
2
+ export declare const useUploadFile: () => ReturnType<typeof useBaseUploadFile>;
@@ -0,0 +1,2 @@
1
+ import { useUploadFiles as useBaseUploadFiles } from '../temp';
2
+ export declare const useUploadFiles: () => ReturnType<typeof useBaseUploadFiles>;
package/lib/index.js CHANGED
@@ -1659,14 +1659,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
1659
1659
  exports.MessengerComponent = void 0;
1660
1660
  const react_1 = __importStar(__webpack_require__(/*! react */ "react"));
1661
1661
  const antd_1 = __webpack_require__(/*! antd */ "antd");
1662
- // import { MessagesList, ThreadMessagesList } from './messagesList';
1663
- const messagesList_1 = __webpack_require__(/*! ./messagesList */ "./src/components/messenger/messagesList/index.tsx");
1664
1662
  const core_1 = __webpack_require__(/*! @messenger-box/core */ "@messenger-box/core");
1665
1663
  const all_1 = __webpack_require__(/*! react-icons/all */ "react-icons/all");
1666
- const style_1 = __webpack_require__(/*! ./messagesList/style */ "./src/components/messenger/messagesList/style.ts");
1667
1664
  const react_fela_1 = __webpack_require__(/*! react-fela */ "react-fela");
1668
1665
  const emoji_mart_1 = __webpack_require__(/*! emoji-mart */ "emoji-mart");
1669
1666
  __webpack_require__(/*! emoji-mart/css/emoji-mart.css */ "emoji-mart/css/emoji-mart.css");
1667
+ const hooks_1 = __webpack_require__(/*! ../../hooks */ "./src/hooks/index.ts");
1668
+ const style_1 = __webpack_require__(/*! ./messagesList/style */ "./src/components/messenger/messagesList/style.ts");
1669
+ const messagesList_1 = __webpack_require__(/*! ./messagesList */ "./src/components/messenger/messagesList/index.tsx");
1670
1670
  const MessengerComponent = ({ channel, currentUsrId }) => {
1671
1671
  document.getElementsByClassName('demo')[0].setAttribute('style', `{{overflow: 'hidden'}}`);
1672
1672
  const { css } = react_fela_1.useFela();
@@ -1679,6 +1679,7 @@ const MessengerComponent = ({ channel, currentUsrId }) => {
1679
1679
  const [day, setDay] = react_1.useState('Today');
1680
1680
  const [threadEmoji, setThreadEmoji] = react_1.useState(false);
1681
1681
  const [files, setFiles] = react_1.useState([]);
1682
+ const { startUploads } = hooks_1.useUploadFiles();
1682
1683
  // let token;
1683
1684
  // useEffect(() => {
1684
1685
  // token = window.localStorage.getItem('access_token');
@@ -1687,7 +1688,7 @@ const MessengerComponent = ({ channel, currentUsrId }) => {
1687
1688
  variables: {
1688
1689
  channelId: channel.id,
1689
1690
  },
1690
- fetchPolicy: "cache-first"
1691
+ fetchPolicy: 'cache-first'
1691
1692
  });
1692
1693
  // useEffect(() => {
1693
1694
  // if (isRefetch) {
@@ -1717,20 +1718,34 @@ const MessengerComponent = ({ channel, currentUsrId }) => {
1717
1718
  // context: {headers: {Authorization: token}},
1718
1719
  // refetchQueries: ['GetMessages']
1719
1720
  });
1720
- const handleEnterButton = (e) => {
1721
- if (enterValue) {
1722
- sendMsg({
1723
- variables: {
1724
- channelId: channel.id,
1725
- content: enterValue,
1726
- files: files
1727
- },
1728
- });
1729
- setEnterValue('');
1730
- scrollToBottom();
1731
- setFlag(true);
1721
+ const handleEnterButton = react_1.useCallback(async (e) => {
1722
+ if (!enterValue) {
1723
+ return;
1732
1724
  }
1733
- };
1725
+ const { data } = await sendMsg({
1726
+ variables: {
1727
+ channelId: channel.id,
1728
+ content: enterValue,
1729
+ files: []
1730
+ },
1731
+ });
1732
+ const uploadedFiles = await startUploads({
1733
+ files,
1734
+ ref: data.sendMessage.id,
1735
+ refName: 'postId'
1736
+ });
1737
+ await editMessage({
1738
+ variables: {
1739
+ channelId: channel.id,
1740
+ messageId: data.sendMessage.id,
1741
+ files: uploadedFiles.map(i => i.data.id),
1742
+ content: enterValue
1743
+ }
1744
+ });
1745
+ setEnterValue('');
1746
+ scrollToBottom();
1747
+ setFlag(true);
1748
+ }, [channel, files, enterValue]);
1734
1749
  const [deleteMsg, {}] = core_1.useDeleteMessageMutation({
1735
1750
  // context: {headers: {Authorization: token}},
1736
1751
  // refetchQueries: ['GetMessages']
@@ -1795,17 +1810,21 @@ const MessengerComponent = ({ channel, currentUsrId }) => {
1795
1810
  const handleShowSideBar = () => {
1796
1811
  setShowThread(!showThread);
1797
1812
  };
1798
- const handleEditMessage = (id, msg) => {
1799
- editMessage({
1813
+ const handleEditMessage = react_1.useCallback(async (id, msg) => {
1814
+ const uploadedFiles = await startUploads({
1815
+ files,
1816
+ ref: id,
1817
+ refName: 'postId'
1818
+ });
1819
+ return editMessage({
1800
1820
  variables: {
1801
1821
  channelId: channel.id,
1802
1822
  messageId: id,
1803
1823
  content: msg,
1804
- files: files
1824
+ files: uploadedFiles.map(i => i.data.id),
1805
1825
  },
1806
1826
  });
1807
- };
1808
- const [fileUpload, uploadedRes] = core_1.useUploadImgMutation();
1827
+ }, [files, channel]);
1809
1828
  const handleFileChange = (e) => {
1810
1829
  let inputFiles = [];
1811
1830
  Object.keys(e.target.files).map((key) => inputFiles.push(e.target.files[key]));
@@ -1818,14 +1837,14 @@ const MessengerComponent = ({ channel, currentUsrId }) => {
1818
1837
  let imgs = document.getElementById('preview_img');
1819
1838
  imgs.innerHTML = '';
1820
1839
  files.forEach((f, index) => {
1821
- let childDiv = document.createElement("div");
1822
- childDiv.className = "child_div";
1823
- let childImg = document.createElement("img");
1840
+ let childDiv = document.createElement('div');
1841
+ childDiv.className = 'child_div';
1842
+ let childImg = document.createElement('img');
1824
1843
  childImg.src = URL.createObjectURL(f);
1825
- let childClose = document.createElement("i");
1826
- childClose.className = "fa fa-times";
1844
+ let childClose = document.createElement('i');
1845
+ childClose.className = 'fa fa-times';
1827
1846
  let overLayDiv = document.createElement('div');
1828
- overLayDiv.className = "overlay";
1847
+ overLayDiv.className = 'overlay';
1829
1848
  childClose.onclick = () => previewClose(f);
1830
1849
  childDiv.appendChild(overLayDiv);
1831
1850
  childDiv.appendChild(childImg);
@@ -24116,6 +24135,88 @@ exports.schema = schema;
24116
24135
  __webpack_require__.r(__webpack_exports__);
24117
24136
  /* harmony default export */ __webpack_exports__["default"] = ("\n\nextend type Post {\n isPinned: Boolean @client\n disableGroupHighlight: Boolean @client\n fromBot: Boolean @client\n\n \"\"\" Set to center the post \"\"\"\n center: Boolean @client\n\n \"\"\" Set to render post compactly \"\"\"\n compactDisplay: Boolean @client\n\n \"\"\" Set to render a preview of the parent post above this reply \"\"\"\n isFirstReply: Boolean @client\n\n \"\"\" Set to highlight the background of the post \"\"\"\n shouldHighlight: Boolean @client\n\n \"\"\" Set to render this post as if it was attached to the previous post \"\"\"\n consecutivePostByUser: Boolean @client\n\n \"\"\" Set if the previous post is a comment \"\"\"\n previousPostIsComment: Boolean @client\n\n togglePostMenu: Boolean @client\n\n \"\"\" Set to render this comment as a mention \"\"\"\n isCommentMention: Boolean @client\n\n \"\"\" If the post has replies \"\"\"\n hasReplies: Boolean @client\n\n \"\"\" To check if the current post is last in the list \"\"\"\n isLastPost: Boolean @client\n\n \"\"\" Whether or not the channel that contains this post is archived \"\"\"\n channelArchived: Boolean @client\n\n \"\"\" Set to mark the post as flagged \"\"\"\n isFlagged: Boolean @client\n\n isCollapsedThreadsEnabled: Boolean @client\n}");
24118
24137
 
24138
+ /***/ }),
24139
+
24140
+ /***/ "./src/hooks/index.ts":
24141
+ /*!****************************!*\
24142
+ !*** ./src/hooks/index.ts ***!
24143
+ \****************************/
24144
+ /*! no static exports found */
24145
+ /***/ (function(module, exports, __webpack_require__) {
24146
+
24147
+ "use strict";
24148
+
24149
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
24150
+ if (k2 === undefined) k2 = k;
24151
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
24152
+ }) : (function(o, m, k, k2) {
24153
+ if (k2 === undefined) k2 = k;
24154
+ o[k2] = m[k];
24155
+ }));
24156
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
24157
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
24158
+ };
24159
+ Object.defineProperty(exports, "__esModule", { value: true });
24160
+ __exportStar(__webpack_require__(/*! ./use-upload-file.hook */ "./src/hooks/use-upload-file.hook.ts"), exports);
24161
+ __exportStar(__webpack_require__(/*! ./use-upload-files.hook */ "./src/hooks/use-upload-files.hook.ts"), exports);
24162
+
24163
+
24164
+ /***/ }),
24165
+
24166
+ /***/ "./src/hooks/use-upload-file.hook.ts":
24167
+ /*!*******************************************!*\
24168
+ !*** ./src/hooks/use-upload-file.hook.ts ***!
24169
+ \*******************************************/
24170
+ /*! no static exports found */
24171
+ /***/ (function(module, exports, __webpack_require__) {
24172
+
24173
+ "use strict";
24174
+
24175
+ Object.defineProperty(exports, "__esModule", { value: true });
24176
+ exports.useUploadFile = void 0;
24177
+ const core_1 = __webpack_require__(/*! @messenger-box/core */ "@messenger-box/core");
24178
+ const temp_1 = __webpack_require__(/*! ../temp */ "./src/temp/index.ts");
24179
+ const useUploadFile = () => temp_1.useUploadFile({
24180
+ createUploadLink: {
24181
+ name: 'createMessageFileUploadLink',
24182
+ mutation: core_1.useCreateMessageFileUploadLinkMutation,
24183
+ },
24184
+ saveUploadedFile: {
24185
+ name: 'attachUploadedFileToMessage',
24186
+ mutation: core_1.useAttachUploadedFileToMessageMutation,
24187
+ },
24188
+ });
24189
+ exports.useUploadFile = useUploadFile;
24190
+
24191
+
24192
+ /***/ }),
24193
+
24194
+ /***/ "./src/hooks/use-upload-files.hook.ts":
24195
+ /*!********************************************!*\
24196
+ !*** ./src/hooks/use-upload-files.hook.ts ***!
24197
+ \********************************************/
24198
+ /*! no static exports found */
24199
+ /***/ (function(module, exports, __webpack_require__) {
24200
+
24201
+ "use strict";
24202
+
24203
+ Object.defineProperty(exports, "__esModule", { value: true });
24204
+ exports.useUploadFiles = void 0;
24205
+ const core_1 = __webpack_require__(/*! @messenger-box/core */ "@messenger-box/core");
24206
+ const temp_1 = __webpack_require__(/*! ../temp */ "./src/temp/index.ts");
24207
+ const useUploadFiles = () => temp_1.useUploadFiles({
24208
+ createUploadLink: {
24209
+ name: 'createMessageFileUploadLink',
24210
+ mutation: core_1.useCreateMessageFileUploadLinkMutation,
24211
+ },
24212
+ saveUploadedFile: {
24213
+ name: 'attachUploadedFileToMessage',
24214
+ mutation: core_1.useAttachUploadedFileToMessageMutation,
24215
+ },
24216
+ });
24217
+ exports.useUploadFiles = useUploadFiles;
24218
+
24219
+
24119
24220
  /***/ }),
24120
24221
 
24121
24222
  /***/ "./src/index.tsx":
@@ -24585,6 +24686,143 @@ exports.styleSheet = {
24585
24686
  };
24586
24687
 
24587
24688
 
24689
+ /***/ }),
24690
+
24691
+ /***/ "./src/temp/index.ts":
24692
+ /*!***************************!*\
24693
+ !*** ./src/temp/index.ts ***!
24694
+ \***************************/
24695
+ /*! no static exports found */
24696
+ /***/ (function(module, exports, __webpack_require__) {
24697
+
24698
+ "use strict";
24699
+
24700
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
24701
+ if (k2 === undefined) k2 = k;
24702
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
24703
+ }) : (function(o, m, k, k2) {
24704
+ if (k2 === undefined) k2 = k;
24705
+ o[k2] = m[k];
24706
+ }));
24707
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
24708
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
24709
+ };
24710
+ Object.defineProperty(exports, "__esModule", { value: true });
24711
+ __exportStar(__webpack_require__(/*! ./use-upload-file.hook */ "./src/temp/use-upload-file.hook.ts"), exports);
24712
+ __exportStar(__webpack_require__(/*! ./use-upload-files.hook */ "./src/temp/use-upload-files.hook.ts"), exports);
24713
+
24714
+
24715
+ /***/ }),
24716
+
24717
+ /***/ "./src/temp/use-upload-file.hook.ts":
24718
+ /*!******************************************!*\
24719
+ !*** ./src/temp/use-upload-file.hook.ts ***!
24720
+ \******************************************/
24721
+ /*! no static exports found */
24722
+ /***/ (function(module, exports, __webpack_require__) {
24723
+
24724
+ "use strict";
24725
+
24726
+ var __importDefault = (this && this.__importDefault) || function (mod) {
24727
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24728
+ };
24729
+ Object.defineProperty(exports, "__esModule", { value: true });
24730
+ exports.useUploadFile = void 0;
24731
+ const file_info_client_1 = __webpack_require__(/*! @container-stack/file-info-client */ "@container-stack/file-info-client");
24732
+ const axios_1 = __importDefault(__webpack_require__(/*! axios */ "axios"));
24733
+ const react_1 = __webpack_require__(/*! react */ "react");
24734
+ function isFileImage(file) {
24735
+ var _a;
24736
+ return file && ((_a = file.type) === null || _a === void 0 ? void 0 : _a.split('/')[0]) === 'image';
24737
+ }
24738
+ const useUploadFile = (options) => {
24739
+ const { createUploadLink: createLink, saveUploadedFile } = options;
24740
+ const [createUploadLink, { loading: linkLoading, error: linkError }] = createLink.mutation();
24741
+ const [attachFile, { loading: fileSaving, error: fileSaveError }] = saveUploadedFile.mutation();
24742
+ const startUpload = react_1.useCallback(async (options) => {
24743
+ try {
24744
+ const { file, ref, refName } = options;
24745
+ const { data: link, errors: linkErrors } = await createUploadLink({
24746
+ variables: {
24747
+ filename: file.name,
24748
+ [refName]: ref,
24749
+ },
24750
+ });
24751
+ if (linkErrors) {
24752
+ return { error: linkErrors[0] };
24753
+ }
24754
+ const { width, height } = isFileImage
24755
+ ? await file_info_client_1.imageSize(await file.arrayBuffer())
24756
+ : { width: null, height: null };
24757
+ const url = link[createLink.name];
24758
+ const { status } = await axios_1.default.put(url, file);
24759
+ if (status !== 200) {
24760
+ return;
24761
+ }
24762
+ const { data: attachedFile, errors: attachFileErrors } = await attachFile({
24763
+ variables: {
24764
+ [refName]: ref,
24765
+ file: {
24766
+ name: file.name,
24767
+ size: file.size,
24768
+ mimeType: file.type,
24769
+ url,
24770
+ width,
24771
+ height,
24772
+ },
24773
+ },
24774
+ });
24775
+ return {
24776
+ data: attachedFile[saveUploadedFile.name],
24777
+ error: attachFileErrors ? attachFileErrors[0] : null,
24778
+ };
24779
+ }
24780
+ catch (e) {
24781
+ console.error(e);
24782
+ }
24783
+ }, []);
24784
+ return {
24785
+ startUpload,
24786
+ loading: linkLoading || fileSaving,
24787
+ error: linkError || fileSaveError,
24788
+ };
24789
+ };
24790
+ exports.useUploadFile = useUploadFile;
24791
+
24792
+
24793
+ /***/ }),
24794
+
24795
+ /***/ "./src/temp/use-upload-files.hook.ts":
24796
+ /*!*******************************************!*\
24797
+ !*** ./src/temp/use-upload-files.hook.ts ***!
24798
+ \*******************************************/
24799
+ /*! no static exports found */
24800
+ /***/ (function(module, exports, __webpack_require__) {
24801
+
24802
+ "use strict";
24803
+
24804
+ var __rest = (this && this.__rest) || function (s, e) {
24805
+ var t = {};
24806
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
24807
+ t[p] = s[p];
24808
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
24809
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
24810
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
24811
+ t[p[i]] = s[p[i]];
24812
+ }
24813
+ return t;
24814
+ };
24815
+ Object.defineProperty(exports, "__esModule", { value: true });
24816
+ exports.useUploadFiles = void 0;
24817
+ const use_upload_file_hook_1 = __webpack_require__(/*! ./use-upload-file.hook */ "./src/temp/use-upload-file.hook.ts");
24818
+ const useUploadFiles = (options) => {
24819
+ const _a = use_upload_file_hook_1.useUploadFile(options), { startUpload: startUploadSingle } = _a, rest = __rest(_a, ["startUpload"]);
24820
+ const startUploads = async ({ files, ref, refName }) => Promise.all(files.map((file) => startUploadSingle({ file, ref, refName })));
24821
+ return Object.assign(Object.assign({}, rest), { startUploads });
24822
+ };
24823
+ exports.useUploadFiles = useUploadFiles;
24824
+
24825
+
24588
24826
  /***/ }),
24589
24827
 
24590
24828
  /***/ "./src/utils/index.ts":
@@ -24678,6 +24916,17 @@ module.exports = require("@common-stack/client-react");
24678
24916
 
24679
24917
  /***/ }),
24680
24918
 
24919
+ /***/ "@container-stack/file-info-client":
24920
+ /*!****************************************************!*\
24921
+ !*** external "@container-stack/file-info-client" ***!
24922
+ \****************************************************/
24923
+ /*! no static exports found */
24924
+ /***/ (function(module, exports) {
24925
+
24926
+ module.exports = require("@container-stack/file-info-client");
24927
+
24928
+ /***/ }),
24929
+
24681
24930
  /***/ "@messenger-box/core":
24682
24931
  /*!**************************************!*\
24683
24932
  !*** external "@messenger-box/core" ***!
@@ -24711,6 +24960,17 @@ module.exports = require("antd/dist/antd.css");
24711
24960
 
24712
24961
  /***/ }),
24713
24962
 
24963
+ /***/ "axios":
24964
+ /*!************************!*\
24965
+ !*** external "axios" ***!
24966
+ \************************/
24967
+ /*! no static exports found */
24968
+ /***/ (function(module, exports) {
24969
+
24970
+ module.exports = require("axios");
24971
+
24972
+ /***/ }),
24973
+
24714
24974
  /***/ "emoji-mart":
24715
24975
  /*!*****************************!*\
24716
24976
  !*** external "emoji-mart" ***!