powr-sdk-web 2.3.1 → 2.4.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.
- package/dist/chat/index.js +892 -0
- package/dist/index.js +7 -0
- package/package.json +1 -1
|
@@ -0,0 +1,892 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
9
|
+
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
11
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t6 in e) "default" !== _t6 && {}.hasOwnProperty.call(e, _t6) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t6)) && (i.get || i.set) ? o(f, _t6, i) : f[_t6] = e[_t6]); return f; })(e, t); }
|
|
12
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
13
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
14
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
15
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
16
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
|
17
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
|
|
18
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
19
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
20
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
21
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
22
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
23
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
24
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
25
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
26
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
27
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
28
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
29
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
30
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
31
|
+
var PowrBaseChat = function PowrBaseChat(_ref) {
|
|
32
|
+
var _selectedConversation;
|
|
33
|
+
var projectId = _ref.projectId,
|
|
34
|
+
_ref$apiUrl = _ref.apiUrl,
|
|
35
|
+
apiUrl = _ref$apiUrl === void 0 ? 'https://api.powrbase.cloud/chat' : _ref$apiUrl,
|
|
36
|
+
onMessageSent = _ref.onMessageSent,
|
|
37
|
+
onConversationCreated = _ref.onConversationCreated,
|
|
38
|
+
onError = _ref.onError,
|
|
39
|
+
_ref$className = _ref.className,
|
|
40
|
+
className = _ref$className === void 0 ? '' : _ref$className,
|
|
41
|
+
_ref$style = _ref.style,
|
|
42
|
+
style = _ref$style === void 0 ? {} : _ref$style,
|
|
43
|
+
_ref$theme = _ref.theme,
|
|
44
|
+
theme = _ref$theme === void 0 ? 'default' : _ref$theme;
|
|
45
|
+
var _useState = (0, _react.useState)(null),
|
|
46
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
47
|
+
selectedChat = _useState2[0],
|
|
48
|
+
setSelectedChat = _useState2[1];
|
|
49
|
+
var _useState3 = (0, _react.useState)(''),
|
|
50
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
51
|
+
message = _useState4[0],
|
|
52
|
+
setMessage = _useState4[1];
|
|
53
|
+
var _useState5 = (0, _react.useState)(''),
|
|
54
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
55
|
+
searchQuery = _useState6[0],
|
|
56
|
+
setSearchQuery = _useState6[1];
|
|
57
|
+
var _useState7 = (0, _react.useState)([]),
|
|
58
|
+
_useState8 = _slicedToArray(_useState7, 2),
|
|
59
|
+
conversations = _useState8[0],
|
|
60
|
+
setConversations = _useState8[1];
|
|
61
|
+
var _useState9 = (0, _react.useState)([]),
|
|
62
|
+
_useState0 = _slicedToArray(_useState9, 2),
|
|
63
|
+
messages = _useState0[0],
|
|
64
|
+
setMessages = _useState0[1];
|
|
65
|
+
var _useState1 = (0, _react.useState)(false),
|
|
66
|
+
_useState10 = _slicedToArray(_useState1, 2),
|
|
67
|
+
loading = _useState10[0],
|
|
68
|
+
setLoading = _useState10[1];
|
|
69
|
+
var _useState11 = (0, _react.useState)(false),
|
|
70
|
+
_useState12 = _slicedToArray(_useState11, 2),
|
|
71
|
+
sending = _useState12[0],
|
|
72
|
+
setSending = _useState12[1];
|
|
73
|
+
var _useState13 = (0, _react.useState)(false),
|
|
74
|
+
_useState14 = _slicedToArray(_useState13, 2),
|
|
75
|
+
showNewChat = _useState14[0],
|
|
76
|
+
setShowNewChat = _useState14[1];
|
|
77
|
+
var _useState15 = (0, _react.useState)([]),
|
|
78
|
+
_useState16 = _slicedToArray(_useState15, 2),
|
|
79
|
+
users = _useState16[0],
|
|
80
|
+
setUsers = _useState16[1];
|
|
81
|
+
var messagesEndRef = (0, _react.useRef)(null);
|
|
82
|
+
var pollingInterval = (0, _react.useRef)(undefined);
|
|
83
|
+
|
|
84
|
+
// Get auth token from localStorage or other auth mechanism
|
|
85
|
+
var getAuthToken = function getAuthToken() {
|
|
86
|
+
if (typeof window !== 'undefined') {
|
|
87
|
+
var authStore = localStorage.getItem('auth-store');
|
|
88
|
+
if (authStore) {
|
|
89
|
+
var _auth$state;
|
|
90
|
+
var auth = JSON.parse(authStore);
|
|
91
|
+
return (_auth$state = auth.state) === null || _auth$state === void 0 ? void 0 : _auth$state.token;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// API helper function
|
|
98
|
+
var apiCall = /*#__PURE__*/function () {
|
|
99
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(endpoint) {
|
|
100
|
+
var options,
|
|
101
|
+
token,
|
|
102
|
+
url,
|
|
103
|
+
config,
|
|
104
|
+
response,
|
|
105
|
+
data,
|
|
106
|
+
_args = arguments;
|
|
107
|
+
return _regenerator().w(function (_context) {
|
|
108
|
+
while (1) switch (_context.n) {
|
|
109
|
+
case 0:
|
|
110
|
+
options = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
|
|
111
|
+
token = getAuthToken();
|
|
112
|
+
url = "".concat(apiUrl).concat(endpoint);
|
|
113
|
+
config = _objectSpread({
|
|
114
|
+
headers: _objectSpread(_objectSpread({
|
|
115
|
+
'Content-Type': 'application/json'
|
|
116
|
+
}, token && {
|
|
117
|
+
'Authorization': "Bearer ".concat(token)
|
|
118
|
+
}), options.headers)
|
|
119
|
+
}, options);
|
|
120
|
+
if (options.body) {
|
|
121
|
+
config.body = JSON.stringify(options.body);
|
|
122
|
+
}
|
|
123
|
+
_context.n = 1;
|
|
124
|
+
return fetch(url, config);
|
|
125
|
+
case 1:
|
|
126
|
+
response = _context.v;
|
|
127
|
+
_context.n = 2;
|
|
128
|
+
return response.json();
|
|
129
|
+
case 2:
|
|
130
|
+
data = _context.v;
|
|
131
|
+
return _context.a(2, {
|
|
132
|
+
response: response,
|
|
133
|
+
data: data,
|
|
134
|
+
ok: response.ok
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}, _callee);
|
|
138
|
+
}));
|
|
139
|
+
return function apiCall(_x) {
|
|
140
|
+
return _ref2.apply(this, arguments);
|
|
141
|
+
};
|
|
142
|
+
}();
|
|
143
|
+
|
|
144
|
+
// Scroll to bottom of messages
|
|
145
|
+
var scrollToBottom = function scrollToBottom() {
|
|
146
|
+
var _messagesEndRef$curre;
|
|
147
|
+
(_messagesEndRef$curre = messagesEndRef.current) === null || _messagesEndRef$curre === void 0 || _messagesEndRef$curre.scrollIntoView({
|
|
148
|
+
behavior: 'smooth'
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// Fetch conversations
|
|
153
|
+
var fetchConversations = /*#__PURE__*/function () {
|
|
154
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
|
|
155
|
+
var _yield$apiCall, data, ok, _t;
|
|
156
|
+
return _regenerator().w(function (_context2) {
|
|
157
|
+
while (1) switch (_context2.n) {
|
|
158
|
+
case 0:
|
|
159
|
+
_context2.p = 0;
|
|
160
|
+
console.log('Fetching conversations...');
|
|
161
|
+
_context2.n = 1;
|
|
162
|
+
return apiCall('/conversations');
|
|
163
|
+
case 1:
|
|
164
|
+
_yield$apiCall = _context2.v;
|
|
165
|
+
data = _yield$apiCall.data;
|
|
166
|
+
ok = _yield$apiCall.ok;
|
|
167
|
+
if (ok && data.success && Array.isArray(data.data)) {
|
|
168
|
+
setConversations(data.data);
|
|
169
|
+
} else {
|
|
170
|
+
console.error('Invalid conversations data:', data);
|
|
171
|
+
setConversations([]);
|
|
172
|
+
}
|
|
173
|
+
_context2.n = 3;
|
|
174
|
+
break;
|
|
175
|
+
case 2:
|
|
176
|
+
_context2.p = 2;
|
|
177
|
+
_t = _context2.v;
|
|
178
|
+
console.error('Error fetching conversations:', _t);
|
|
179
|
+
setConversations([]);
|
|
180
|
+
onError === null || onError === void 0 || onError(_t);
|
|
181
|
+
case 3:
|
|
182
|
+
return _context2.a(2);
|
|
183
|
+
}
|
|
184
|
+
}, _callee2, null, [[0, 2]]);
|
|
185
|
+
}));
|
|
186
|
+
return function fetchConversations() {
|
|
187
|
+
return _ref3.apply(this, arguments);
|
|
188
|
+
};
|
|
189
|
+
}();
|
|
190
|
+
|
|
191
|
+
// Fetch users for new chat
|
|
192
|
+
var fetchUsers = /*#__PURE__*/function () {
|
|
193
|
+
var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
|
|
194
|
+
var _yield$apiCall2, data, ok, _t2;
|
|
195
|
+
return _regenerator().w(function (_context3) {
|
|
196
|
+
while (1) switch (_context3.n) {
|
|
197
|
+
case 0:
|
|
198
|
+
_context3.p = 0;
|
|
199
|
+
console.log('Fetching users...');
|
|
200
|
+
_context3.n = 1;
|
|
201
|
+
return apiCall('/users');
|
|
202
|
+
case 1:
|
|
203
|
+
_yield$apiCall2 = _context3.v;
|
|
204
|
+
data = _yield$apiCall2.data;
|
|
205
|
+
ok = _yield$apiCall2.ok;
|
|
206
|
+
if (ok && data.success && Array.isArray(data.data)) {
|
|
207
|
+
console.log('Setting users:', data.data);
|
|
208
|
+
setUsers(data.data);
|
|
209
|
+
} else {
|
|
210
|
+
console.error('Invalid users data:', data);
|
|
211
|
+
setUsers([]);
|
|
212
|
+
}
|
|
213
|
+
_context3.n = 3;
|
|
214
|
+
break;
|
|
215
|
+
case 2:
|
|
216
|
+
_context3.p = 2;
|
|
217
|
+
_t2 = _context3.v;
|
|
218
|
+
console.error('Error fetching users:', _t2);
|
|
219
|
+
setUsers([]);
|
|
220
|
+
onError === null || onError === void 0 || onError(_t2);
|
|
221
|
+
case 3:
|
|
222
|
+
return _context3.a(2);
|
|
223
|
+
}
|
|
224
|
+
}, _callee3, null, [[0, 2]]);
|
|
225
|
+
}));
|
|
226
|
+
return function fetchUsers() {
|
|
227
|
+
return _ref4.apply(this, arguments);
|
|
228
|
+
};
|
|
229
|
+
}();
|
|
230
|
+
|
|
231
|
+
// Create new conversation
|
|
232
|
+
var createConversation = /*#__PURE__*/function () {
|
|
233
|
+
var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(userId) {
|
|
234
|
+
var _yield$apiCall3, data, ok, _t3;
|
|
235
|
+
return _regenerator().w(function (_context4) {
|
|
236
|
+
while (1) switch (_context4.n) {
|
|
237
|
+
case 0:
|
|
238
|
+
_context4.p = 0;
|
|
239
|
+
_context4.n = 1;
|
|
240
|
+
return apiCall('/conversations', {
|
|
241
|
+
method: 'POST',
|
|
242
|
+
body: {
|
|
243
|
+
participantId: userId
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
case 1:
|
|
247
|
+
_yield$apiCall3 = _context4.v;
|
|
248
|
+
data = _yield$apiCall3.data;
|
|
249
|
+
ok = _yield$apiCall3.ok;
|
|
250
|
+
if (ok && data.success && data.data) {
|
|
251
|
+
setSelectedChat(data.data.id);
|
|
252
|
+
setShowNewChat(false);
|
|
253
|
+
fetchConversations(); // Refresh conversations list
|
|
254
|
+
onConversationCreated === null || onConversationCreated === void 0 || onConversationCreated(data.data);
|
|
255
|
+
}
|
|
256
|
+
_context4.n = 3;
|
|
257
|
+
break;
|
|
258
|
+
case 2:
|
|
259
|
+
_context4.p = 2;
|
|
260
|
+
_t3 = _context4.v;
|
|
261
|
+
console.error('Error creating conversation:', _t3);
|
|
262
|
+
onError === null || onError === void 0 || onError(_t3);
|
|
263
|
+
case 3:
|
|
264
|
+
return _context4.a(2);
|
|
265
|
+
}
|
|
266
|
+
}, _callee4, null, [[0, 2]]);
|
|
267
|
+
}));
|
|
268
|
+
return function createConversation(_x2) {
|
|
269
|
+
return _ref5.apply(this, arguments);
|
|
270
|
+
};
|
|
271
|
+
}();
|
|
272
|
+
|
|
273
|
+
// Fetch messages for a conversation
|
|
274
|
+
var fetchMessages = /*#__PURE__*/function () {
|
|
275
|
+
var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(conversationId) {
|
|
276
|
+
var _yield$apiCall4, data, ok, _t4;
|
|
277
|
+
return _regenerator().w(function (_context5) {
|
|
278
|
+
while (1) switch (_context5.n) {
|
|
279
|
+
case 0:
|
|
280
|
+
_context5.p = 0;
|
|
281
|
+
setLoading(true);
|
|
282
|
+
_context5.n = 1;
|
|
283
|
+
return apiCall("/conversations/".concat(conversationId, "/messages"));
|
|
284
|
+
case 1:
|
|
285
|
+
_yield$apiCall4 = _context5.v;
|
|
286
|
+
data = _yield$apiCall4.data;
|
|
287
|
+
ok = _yield$apiCall4.ok;
|
|
288
|
+
if (ok && data.success && Array.isArray(data.data)) {
|
|
289
|
+
setMessages(data.data);
|
|
290
|
+
setTimeout(scrollToBottom, 100);
|
|
291
|
+
} else {
|
|
292
|
+
console.error('Invalid messages data:', data);
|
|
293
|
+
setMessages([]);
|
|
294
|
+
}
|
|
295
|
+
_context5.n = 3;
|
|
296
|
+
break;
|
|
297
|
+
case 2:
|
|
298
|
+
_context5.p = 2;
|
|
299
|
+
_t4 = _context5.v;
|
|
300
|
+
console.error('Error fetching messages:', _t4);
|
|
301
|
+
setMessages([]);
|
|
302
|
+
onError === null || onError === void 0 || onError(_t4);
|
|
303
|
+
case 3:
|
|
304
|
+
_context5.p = 3;
|
|
305
|
+
setLoading(false);
|
|
306
|
+
return _context5.f(3);
|
|
307
|
+
case 4:
|
|
308
|
+
return _context5.a(2);
|
|
309
|
+
}
|
|
310
|
+
}, _callee5, null, [[0, 2, 3, 4]]);
|
|
311
|
+
}));
|
|
312
|
+
return function fetchMessages(_x3) {
|
|
313
|
+
return _ref6.apply(this, arguments);
|
|
314
|
+
};
|
|
315
|
+
}();
|
|
316
|
+
|
|
317
|
+
// Send message
|
|
318
|
+
var handleSendMessage = /*#__PURE__*/function () {
|
|
319
|
+
var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
|
|
320
|
+
var _yield$apiCall5, data, ok, _t5;
|
|
321
|
+
return _regenerator().w(function (_context6) {
|
|
322
|
+
while (1) switch (_context6.n) {
|
|
323
|
+
case 0:
|
|
324
|
+
if (!(!message.trim() || !selectedChat || sending)) {
|
|
325
|
+
_context6.n = 1;
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
return _context6.a(2);
|
|
329
|
+
case 1:
|
|
330
|
+
_context6.p = 1;
|
|
331
|
+
setSending(true);
|
|
332
|
+
_context6.n = 2;
|
|
333
|
+
return apiCall("/conversations/".concat(selectedChat, "/messages"), {
|
|
334
|
+
method: 'POST',
|
|
335
|
+
body: {
|
|
336
|
+
content: message.trim()
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
case 2:
|
|
340
|
+
_yield$apiCall5 = _context6.v;
|
|
341
|
+
data = _yield$apiCall5.data;
|
|
342
|
+
ok = _yield$apiCall5.ok;
|
|
343
|
+
if (ok && data.success && data.data) {
|
|
344
|
+
setMessages(function (prev) {
|
|
345
|
+
return [].concat(_toConsumableArray(prev), [data.data]);
|
|
346
|
+
});
|
|
347
|
+
setMessage('');
|
|
348
|
+
setTimeout(scrollToBottom, 100);
|
|
349
|
+
// Refresh conversations to update last message
|
|
350
|
+
fetchConversations();
|
|
351
|
+
onMessageSent === null || onMessageSent === void 0 || onMessageSent(data.data);
|
|
352
|
+
}
|
|
353
|
+
_context6.n = 4;
|
|
354
|
+
break;
|
|
355
|
+
case 3:
|
|
356
|
+
_context6.p = 3;
|
|
357
|
+
_t5 = _context6.v;
|
|
358
|
+
console.error('Error sending message:', _t5);
|
|
359
|
+
onError === null || onError === void 0 || onError(_t5);
|
|
360
|
+
case 4:
|
|
361
|
+
_context6.p = 4;
|
|
362
|
+
setSending(false);
|
|
363
|
+
return _context6.f(4);
|
|
364
|
+
case 5:
|
|
365
|
+
return _context6.a(2);
|
|
366
|
+
}
|
|
367
|
+
}, _callee6, null, [[1, 3, 4, 5]]);
|
|
368
|
+
}));
|
|
369
|
+
return function handleSendMessage() {
|
|
370
|
+
return _ref7.apply(this, arguments);
|
|
371
|
+
};
|
|
372
|
+
}();
|
|
373
|
+
|
|
374
|
+
// Poll for new messages when a conversation is selected
|
|
375
|
+
(0, _react.useEffect)(function () {
|
|
376
|
+
if (selectedChat) {
|
|
377
|
+
// Initial fetch
|
|
378
|
+
fetchMessages(selectedChat);
|
|
379
|
+
|
|
380
|
+
// Set up polling every 3 seconds
|
|
381
|
+
pollingInterval.current = setInterval(function () {
|
|
382
|
+
fetchMessages(selectedChat);
|
|
383
|
+
}, 3000);
|
|
384
|
+
return function () {
|
|
385
|
+
if (pollingInterval.current) {
|
|
386
|
+
clearInterval(pollingInterval.current);
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
}, [selectedChat]);
|
|
391
|
+
|
|
392
|
+
// Initial load of conversations and users
|
|
393
|
+
(0, _react.useEffect)(function () {
|
|
394
|
+
if (projectId) {
|
|
395
|
+
fetchConversations();
|
|
396
|
+
fetchUsers();
|
|
397
|
+
}
|
|
398
|
+
}, [projectId]);
|
|
399
|
+
|
|
400
|
+
// Poll for new conversations every 10 seconds
|
|
401
|
+
(0, _react.useEffect)(function () {
|
|
402
|
+
if (projectId) {
|
|
403
|
+
var conversationPolling = setInterval(function () {
|
|
404
|
+
fetchConversations();
|
|
405
|
+
}, 10000);
|
|
406
|
+
return function () {
|
|
407
|
+
return clearInterval(conversationPolling);
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
}, [projectId]);
|
|
411
|
+
var filteredConversations = Array.isArray(conversations) ? conversations.filter(function (conv) {
|
|
412
|
+
return conv.name.toLowerCase().includes(searchQuery.toLowerCase());
|
|
413
|
+
}) : [];
|
|
414
|
+
var selectedConversation = Array.isArray(conversations) ? conversations.find(function (conv) {
|
|
415
|
+
return conv.id === selectedChat;
|
|
416
|
+
}) : null;
|
|
417
|
+
var formatTime = function formatTime(date) {
|
|
418
|
+
var dateObj = typeof date === 'string' ? new Date(date) : date;
|
|
419
|
+
var now = new Date();
|
|
420
|
+
var diffInHours = (now.getTime() - dateObj.getTime()) / (1000 * 60 * 60);
|
|
421
|
+
if (diffInHours < 1) {
|
|
422
|
+
return 'Just now';
|
|
423
|
+
} else if (diffInHours < 24) {
|
|
424
|
+
return "".concat(Math.floor(diffInHours), "h ago");
|
|
425
|
+
} else {
|
|
426
|
+
return dateObj.toLocaleDateString();
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
var getThemeStyles = function getThemeStyles() {
|
|
430
|
+
var baseStyles = {
|
|
431
|
+
container: {
|
|
432
|
+
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif',
|
|
433
|
+
display: 'flex',
|
|
434
|
+
height: '100%',
|
|
435
|
+
backgroundColor: '#ffffff',
|
|
436
|
+
borderRadius: '12px',
|
|
437
|
+
overflow: 'hidden',
|
|
438
|
+
boxShadow: '0 4px 12px rgba(0, 0, 0, 0.05)',
|
|
439
|
+
border: '1px solid #e5e7eb'
|
|
440
|
+
},
|
|
441
|
+
sidebar: {
|
|
442
|
+
width: '320px',
|
|
443
|
+
borderRight: '1px solid #e5e7eb',
|
|
444
|
+
display: 'flex',
|
|
445
|
+
flexDirection: 'column',
|
|
446
|
+
backgroundColor: '#f9fafb'
|
|
447
|
+
},
|
|
448
|
+
header: {
|
|
449
|
+
padding: '20px',
|
|
450
|
+
borderBottom: '1px solid #e5e7eb',
|
|
451
|
+
backgroundColor: '#ffffff'
|
|
452
|
+
},
|
|
453
|
+
title: {
|
|
454
|
+
fontSize: '20px',
|
|
455
|
+
fontWeight: '600',
|
|
456
|
+
color: '#111827',
|
|
457
|
+
margin: '0 0 16px 0'
|
|
458
|
+
},
|
|
459
|
+
searchInput: {
|
|
460
|
+
width: '100%',
|
|
461
|
+
padding: '10px 12px',
|
|
462
|
+
border: '1px solid #d1d5db',
|
|
463
|
+
borderRadius: '8px',
|
|
464
|
+
fontSize: '14px',
|
|
465
|
+
outline: 'none',
|
|
466
|
+
backgroundColor: '#ffffff'
|
|
467
|
+
},
|
|
468
|
+
conversationList: {
|
|
469
|
+
flex: 1,
|
|
470
|
+
overflowY: 'auto',
|
|
471
|
+
padding: '8px'
|
|
472
|
+
},
|
|
473
|
+
conversationItem: {
|
|
474
|
+
padding: '12px',
|
|
475
|
+
margin: '4px 0',
|
|
476
|
+
borderRadius: '8px',
|
|
477
|
+
cursor: 'pointer',
|
|
478
|
+
transition: 'background-color 0.2s',
|
|
479
|
+
backgroundColor: '#ffffff',
|
|
480
|
+
border: '1px solid #e5e7eb'
|
|
481
|
+
},
|
|
482
|
+
conversationItemSelected: {
|
|
483
|
+
backgroundColor: '#eff6ff',
|
|
484
|
+
borderColor: '#3b82f6'
|
|
485
|
+
},
|
|
486
|
+
conversationItemHover: {
|
|
487
|
+
backgroundColor: '#f3f4f6'
|
|
488
|
+
},
|
|
489
|
+
chatArea: {
|
|
490
|
+
flex: 1,
|
|
491
|
+
display: 'flex',
|
|
492
|
+
flexDirection: 'column',
|
|
493
|
+
backgroundColor: '#ffffff'
|
|
494
|
+
},
|
|
495
|
+
chatHeader: {
|
|
496
|
+
padding: '20px',
|
|
497
|
+
borderBottom: '1px solid #e5e7eb',
|
|
498
|
+
display: 'flex',
|
|
499
|
+
alignItems: 'center',
|
|
500
|
+
justifyContent: 'space-between'
|
|
501
|
+
},
|
|
502
|
+
messagesArea: {
|
|
503
|
+
flex: 1,
|
|
504
|
+
overflowY: 'auto',
|
|
505
|
+
padding: '20px',
|
|
506
|
+
display: 'flex',
|
|
507
|
+
flexDirection: 'column',
|
|
508
|
+
gap: '12px'
|
|
509
|
+
},
|
|
510
|
+
messageInput: {
|
|
511
|
+
padding: '20px',
|
|
512
|
+
borderTop: '1px solid #e5e7eb',
|
|
513
|
+
display: 'flex',
|
|
514
|
+
gap: '12px',
|
|
515
|
+
alignItems: 'center'
|
|
516
|
+
},
|
|
517
|
+
input: {
|
|
518
|
+
flex: 1,
|
|
519
|
+
padding: '12px 16px',
|
|
520
|
+
border: '1px solid #d1d5db',
|
|
521
|
+
borderRadius: '8px',
|
|
522
|
+
fontSize: '14px',
|
|
523
|
+
outline: 'none',
|
|
524
|
+
resize: 'none'
|
|
525
|
+
},
|
|
526
|
+
sendButton: {
|
|
527
|
+
padding: '12px 20px',
|
|
528
|
+
backgroundColor: '#3b82f6',
|
|
529
|
+
color: '#ffffff',
|
|
530
|
+
border: 'none',
|
|
531
|
+
borderRadius: '8px',
|
|
532
|
+
fontSize: '14px',
|
|
533
|
+
fontWeight: '500',
|
|
534
|
+
cursor: 'pointer',
|
|
535
|
+
transition: 'background-color 0.2s'
|
|
536
|
+
},
|
|
537
|
+
sendButtonDisabled: {
|
|
538
|
+
backgroundColor: '#9ca3af',
|
|
539
|
+
cursor: 'not-allowed'
|
|
540
|
+
},
|
|
541
|
+
message: {
|
|
542
|
+
maxWidth: '70%',
|
|
543
|
+
padding: '12px 16px',
|
|
544
|
+
borderRadius: '12px',
|
|
545
|
+
fontSize: '14px',
|
|
546
|
+
lineHeight: '1.4'
|
|
547
|
+
},
|
|
548
|
+
messageOwn: {
|
|
549
|
+
backgroundColor: '#3b82f6',
|
|
550
|
+
color: '#ffffff',
|
|
551
|
+
alignSelf: 'flex-end'
|
|
552
|
+
},
|
|
553
|
+
messageOther: {
|
|
554
|
+
backgroundColor: '#f3f4f6',
|
|
555
|
+
color: '#111827',
|
|
556
|
+
alignSelf: 'flex-start'
|
|
557
|
+
},
|
|
558
|
+
emptyState: {
|
|
559
|
+
flex: 1,
|
|
560
|
+
display: 'flex',
|
|
561
|
+
alignItems: 'center',
|
|
562
|
+
justifyContent: 'center',
|
|
563
|
+
color: '#6b7280',
|
|
564
|
+
fontSize: '16px'
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// Theme variations
|
|
569
|
+
if (theme === 'dark') {
|
|
570
|
+
baseStyles.container.backgroundColor = '#1f2937';
|
|
571
|
+
baseStyles.sidebar.backgroundColor = '#111827';
|
|
572
|
+
baseStyles.header.backgroundColor = '#1f2937';
|
|
573
|
+
baseStyles.title.color = '#f9fafb';
|
|
574
|
+
baseStyles.searchInput.backgroundColor = '#374151';
|
|
575
|
+
baseStyles.searchInput.borderColor = '#4b5563';
|
|
576
|
+
baseStyles.searchInput.color = '#f9fafb';
|
|
577
|
+
baseStyles.conversationItem.backgroundColor = '#374151';
|
|
578
|
+
baseStyles.conversationItem.borderColor = '#4b5563';
|
|
579
|
+
baseStyles.conversationItemSelected.backgroundColor = '#1e40af';
|
|
580
|
+
baseStyles.conversationItemHover.backgroundColor = '#4b5563';
|
|
581
|
+
baseStyles.chatArea.backgroundColor = '#1f2937';
|
|
582
|
+
baseStyles.chatHeader.borderBottomColor = '#4b5563';
|
|
583
|
+
baseStyles.input.backgroundColor = '#374151';
|
|
584
|
+
baseStyles.input.borderColor = '#4b5563';
|
|
585
|
+
baseStyles.input.color = '#f9fafb';
|
|
586
|
+
baseStyles.messageOther.backgroundColor = '#374151';
|
|
587
|
+
baseStyles.messageOther.color = '#f9fafb';
|
|
588
|
+
}
|
|
589
|
+
return baseStyles;
|
|
590
|
+
};
|
|
591
|
+
var styles = getThemeStyles();
|
|
592
|
+
if (!projectId) {
|
|
593
|
+
return /*#__PURE__*/_react["default"].createElement("div", {
|
|
594
|
+
style: _objectSpread(_objectSpread({}, styles.emptyState), {}, {
|
|
595
|
+
padding: '40px'
|
|
596
|
+
})
|
|
597
|
+
}, "Project ID is required for chat functionality");
|
|
598
|
+
}
|
|
599
|
+
return /*#__PURE__*/_react["default"].createElement("div", {
|
|
600
|
+
className: className,
|
|
601
|
+
style: _objectSpread(_objectSpread({}, styles.container), style)
|
|
602
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
603
|
+
style: styles.sidebar
|
|
604
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
605
|
+
style: styles.header
|
|
606
|
+
}, /*#__PURE__*/_react["default"].createElement("h2", {
|
|
607
|
+
style: styles.title
|
|
608
|
+
}, "Messages"), /*#__PURE__*/_react["default"].createElement("input", {
|
|
609
|
+
type: "text",
|
|
610
|
+
placeholder: "Search conversations...",
|
|
611
|
+
value: searchQuery,
|
|
612
|
+
onChange: function onChange(e) {
|
|
613
|
+
return setSearchQuery(e.target.value);
|
|
614
|
+
},
|
|
615
|
+
style: styles.searchInput
|
|
616
|
+
}), /*#__PURE__*/_react["default"].createElement("button", {
|
|
617
|
+
onClick: function onClick() {
|
|
618
|
+
return setShowNewChat(!showNewChat);
|
|
619
|
+
},
|
|
620
|
+
style: _objectSpread(_objectSpread({}, styles.sendButton), {}, {
|
|
621
|
+
marginTop: '12px',
|
|
622
|
+
width: '100%'
|
|
623
|
+
})
|
|
624
|
+
}, showNewChat ? 'Cancel' : 'New Chat')), /*#__PURE__*/_react["default"].createElement("div", {
|
|
625
|
+
style: styles.conversationList
|
|
626
|
+
}, showNewChat && /*#__PURE__*/_react["default"].createElement("div", {
|
|
627
|
+
style: {
|
|
628
|
+
padding: '16px',
|
|
629
|
+
borderBottom: '1px solid #e5e7eb'
|
|
630
|
+
}
|
|
631
|
+
}, /*#__PURE__*/_react["default"].createElement("h3", {
|
|
632
|
+
style: {
|
|
633
|
+
fontSize: '16px',
|
|
634
|
+
fontWeight: '600',
|
|
635
|
+
margin: '0 0 12px 0'
|
|
636
|
+
}
|
|
637
|
+
}, "Start a new conversation"), /*#__PURE__*/_react["default"].createElement("div", {
|
|
638
|
+
style: {
|
|
639
|
+
display: 'flex',
|
|
640
|
+
flexDirection: 'column',
|
|
641
|
+
gap: '8px'
|
|
642
|
+
}
|
|
643
|
+
}, users.length === 0 ? /*#__PURE__*/_react["default"].createElement("div", {
|
|
644
|
+
style: {
|
|
645
|
+
textAlign: 'center',
|
|
646
|
+
color: '#6b7280',
|
|
647
|
+
padding: '20px'
|
|
648
|
+
}
|
|
649
|
+
}, /*#__PURE__*/_react["default"].createElement("p", null, "No users available"), /*#__PURE__*/_react["default"].createElement("p", {
|
|
650
|
+
style: {
|
|
651
|
+
fontSize: '12px'
|
|
652
|
+
}
|
|
653
|
+
}, "Make sure you have other users registered in the system")) : users.map(function (user) {
|
|
654
|
+
var _user$name;
|
|
655
|
+
return /*#__PURE__*/_react["default"].createElement("div", {
|
|
656
|
+
key: user.id,
|
|
657
|
+
style: _objectSpread(_objectSpread({}, styles.conversationItem), styles.conversationItemHover),
|
|
658
|
+
onClick: function onClick() {
|
|
659
|
+
return createConversation(user.id);
|
|
660
|
+
}
|
|
661
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
662
|
+
style: {
|
|
663
|
+
display: 'flex',
|
|
664
|
+
alignItems: 'center',
|
|
665
|
+
gap: '12px'
|
|
666
|
+
}
|
|
667
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
668
|
+
style: {
|
|
669
|
+
width: '40px',
|
|
670
|
+
height: '40px',
|
|
671
|
+
borderRadius: '50%',
|
|
672
|
+
backgroundColor: '#3b82f6',
|
|
673
|
+
display: 'flex',
|
|
674
|
+
alignItems: 'center',
|
|
675
|
+
justifyContent: 'center',
|
|
676
|
+
color: '#ffffff',
|
|
677
|
+
fontWeight: '600',
|
|
678
|
+
fontSize: '14px'
|
|
679
|
+
}
|
|
680
|
+
}, (_user$name = user.name) === null || _user$name === void 0 ? void 0 : _user$name.split(' ').map(function (n) {
|
|
681
|
+
return n[0];
|
|
682
|
+
}).join('')), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("p", {
|
|
683
|
+
style: {
|
|
684
|
+
margin: '0',
|
|
685
|
+
fontWeight: '500',
|
|
686
|
+
fontSize: '14px'
|
|
687
|
+
}
|
|
688
|
+
}, user.name), /*#__PURE__*/_react["default"].createElement("p", {
|
|
689
|
+
style: {
|
|
690
|
+
margin: '0',
|
|
691
|
+
fontSize: '12px',
|
|
692
|
+
color: '#6b7280'
|
|
693
|
+
}
|
|
694
|
+
}, user.username))));
|
|
695
|
+
}))), filteredConversations.map(function (conversation) {
|
|
696
|
+
var _conversation$name;
|
|
697
|
+
return /*#__PURE__*/_react["default"].createElement("div", {
|
|
698
|
+
key: conversation.id,
|
|
699
|
+
style: _objectSpread(_objectSpread(_objectSpread({}, styles.conversationItem), selectedChat === conversation.id ? styles.conversationItemSelected : {}), styles.conversationItemHover),
|
|
700
|
+
onClick: function onClick() {
|
|
701
|
+
return setSelectedChat(conversation.id);
|
|
702
|
+
}
|
|
703
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
704
|
+
style: {
|
|
705
|
+
display: 'flex',
|
|
706
|
+
alignItems: 'center',
|
|
707
|
+
gap: '12px'
|
|
708
|
+
}
|
|
709
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
710
|
+
style: {
|
|
711
|
+
width: '40px',
|
|
712
|
+
height: '40px',
|
|
713
|
+
borderRadius: '50%',
|
|
714
|
+
backgroundColor: '#3b82f6',
|
|
715
|
+
display: 'flex',
|
|
716
|
+
alignItems: 'center',
|
|
717
|
+
justifyContent: 'center',
|
|
718
|
+
color: '#ffffff',
|
|
719
|
+
fontWeight: '600',
|
|
720
|
+
fontSize: '14px'
|
|
721
|
+
}
|
|
722
|
+
}, (_conversation$name = conversation.name) === null || _conversation$name === void 0 ? void 0 : _conversation$name.split(' ').map(function (n) {
|
|
723
|
+
return n[0];
|
|
724
|
+
}).join('')), /*#__PURE__*/_react["default"].createElement("div", {
|
|
725
|
+
style: {
|
|
726
|
+
flex: 1,
|
|
727
|
+
minWidth: 0
|
|
728
|
+
}
|
|
729
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
730
|
+
style: {
|
|
731
|
+
display: 'flex',
|
|
732
|
+
alignItems: 'center',
|
|
733
|
+
justifyContent: 'space-between'
|
|
734
|
+
}
|
|
735
|
+
}, /*#__PURE__*/_react["default"].createElement("p", {
|
|
736
|
+
style: {
|
|
737
|
+
margin: '0',
|
|
738
|
+
fontWeight: '500',
|
|
739
|
+
fontSize: '14px'
|
|
740
|
+
}
|
|
741
|
+
}, conversation.name), /*#__PURE__*/_react["default"].createElement("span", {
|
|
742
|
+
style: {
|
|
743
|
+
fontSize: '12px',
|
|
744
|
+
color: '#6b7280'
|
|
745
|
+
}
|
|
746
|
+
}, conversation.lastMessageTime ? formatTime(conversation.lastMessageTime) : 'No messages')), /*#__PURE__*/_react["default"].createElement("p", {
|
|
747
|
+
style: {
|
|
748
|
+
margin: '4px 0 0 0',
|
|
749
|
+
fontSize: '12px',
|
|
750
|
+
color: '#6b7280',
|
|
751
|
+
overflow: 'hidden',
|
|
752
|
+
textOverflow: 'ellipsis',
|
|
753
|
+
whiteSpace: 'nowrap'
|
|
754
|
+
}
|
|
755
|
+
}, conversation.lastMessage)), conversation.unreadCount > 0 && /*#__PURE__*/_react["default"].createElement("div", {
|
|
756
|
+
style: {
|
|
757
|
+
backgroundColor: '#ef4444',
|
|
758
|
+
color: '#ffffff',
|
|
759
|
+
borderRadius: '50%',
|
|
760
|
+
width: '20px',
|
|
761
|
+
height: '20px',
|
|
762
|
+
display: 'flex',
|
|
763
|
+
alignItems: 'center',
|
|
764
|
+
justifyContent: 'center',
|
|
765
|
+
fontSize: '12px',
|
|
766
|
+
fontWeight: '600'
|
|
767
|
+
}
|
|
768
|
+
}, conversation.unreadCount)));
|
|
769
|
+
}))), /*#__PURE__*/_react["default"].createElement("div", {
|
|
770
|
+
style: styles.chatArea
|
|
771
|
+
}, selectedChat ? /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
|
|
772
|
+
style: styles.chatHeader
|
|
773
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
774
|
+
style: {
|
|
775
|
+
display: 'flex',
|
|
776
|
+
alignItems: 'center',
|
|
777
|
+
gap: '12px'
|
|
778
|
+
}
|
|
779
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
780
|
+
style: {
|
|
781
|
+
width: '40px',
|
|
782
|
+
height: '40px',
|
|
783
|
+
borderRadius: '50%',
|
|
784
|
+
backgroundColor: '#3b82f6',
|
|
785
|
+
display: 'flex',
|
|
786
|
+
alignItems: 'center',
|
|
787
|
+
justifyContent: 'center',
|
|
788
|
+
color: '#ffffff',
|
|
789
|
+
fontWeight: '600',
|
|
790
|
+
fontSize: '14px'
|
|
791
|
+
}
|
|
792
|
+
}, selectedConversation === null || selectedConversation === void 0 || (_selectedConversation = selectedConversation.name) === null || _selectedConversation === void 0 ? void 0 : _selectedConversation.split(' ').map(function (n) {
|
|
793
|
+
return n[0];
|
|
794
|
+
}).join('')), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("h3", {
|
|
795
|
+
style: {
|
|
796
|
+
margin: '0',
|
|
797
|
+
fontWeight: '600',
|
|
798
|
+
fontSize: '16px'
|
|
799
|
+
}
|
|
800
|
+
}, selectedConversation === null || selectedConversation === void 0 ? void 0 : selectedConversation.name), /*#__PURE__*/_react["default"].createElement("p", {
|
|
801
|
+
style: {
|
|
802
|
+
margin: '0',
|
|
803
|
+
fontSize: '12px',
|
|
804
|
+
color: '#6b7280'
|
|
805
|
+
}
|
|
806
|
+
}, selectedConversation !== null && selectedConversation !== void 0 && selectedConversation.isOnline ? 'Online' : 'Offline')))), /*#__PURE__*/_react["default"].createElement("div", {
|
|
807
|
+
style: styles.messagesArea
|
|
808
|
+
}, loading && /*#__PURE__*/_react["default"].createElement("div", {
|
|
809
|
+
style: {
|
|
810
|
+
textAlign: 'center',
|
|
811
|
+
color: '#6b7280'
|
|
812
|
+
}
|
|
813
|
+
}, "Loading messages..."), !loading && messages.map(function (msg) {
|
|
814
|
+
return /*#__PURE__*/_react["default"].createElement("div", {
|
|
815
|
+
key: msg.id,
|
|
816
|
+
style: _objectSpread(_objectSpread({}, styles.message), msg.isOwn ? styles.messageOwn : styles.messageOther)
|
|
817
|
+
}, /*#__PURE__*/_react["default"].createElement("p", {
|
|
818
|
+
style: {
|
|
819
|
+
margin: '0 0 4px 0'
|
|
820
|
+
}
|
|
821
|
+
}, msg.message), /*#__PURE__*/_react["default"].createElement("p", {
|
|
822
|
+
style: {
|
|
823
|
+
margin: '0',
|
|
824
|
+
fontSize: '11px',
|
|
825
|
+
opacity: '0.7',
|
|
826
|
+
textAlign: msg.isOwn ? 'right' : 'left'
|
|
827
|
+
}
|
|
828
|
+
}, new Date(msg.timestamp).toLocaleTimeString([], {
|
|
829
|
+
hour: '2-digit',
|
|
830
|
+
minute: '2-digit'
|
|
831
|
+
})));
|
|
832
|
+
}), /*#__PURE__*/_react["default"].createElement("div", {
|
|
833
|
+
ref: messagesEndRef
|
|
834
|
+
})), /*#__PURE__*/_react["default"].createElement("div", {
|
|
835
|
+
style: styles.messageInput
|
|
836
|
+
}, /*#__PURE__*/_react["default"].createElement("textarea", {
|
|
837
|
+
placeholder: "Type a message...",
|
|
838
|
+
value: message,
|
|
839
|
+
onChange: function onChange(e) {
|
|
840
|
+
return setMessage(e.target.value);
|
|
841
|
+
},
|
|
842
|
+
onKeyPress: function onKeyPress(e) {
|
|
843
|
+
return e.key === 'Enter' && !e.shiftKey && handleSendMessage();
|
|
844
|
+
},
|
|
845
|
+
style: _objectSpread(_objectSpread({}, styles.input), {}, {
|
|
846
|
+
minHeight: '44px',
|
|
847
|
+
maxHeight: '120px',
|
|
848
|
+
resize: 'none'
|
|
849
|
+
}),
|
|
850
|
+
disabled: sending
|
|
851
|
+
}), /*#__PURE__*/_react["default"].createElement("button", {
|
|
852
|
+
onClick: handleSendMessage,
|
|
853
|
+
disabled: sending || !message.trim(),
|
|
854
|
+
style: _objectSpread(_objectSpread({}, styles.sendButton), sending || !message.trim() ? styles.sendButtonDisabled : {})
|
|
855
|
+
}, sending ? 'Sending...' : 'Send'))) :
|
|
856
|
+
/*#__PURE__*/
|
|
857
|
+
/* Empty State */
|
|
858
|
+
_react["default"].createElement("div", {
|
|
859
|
+
style: styles.emptyState
|
|
860
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
861
|
+
style: {
|
|
862
|
+
textAlign: 'center'
|
|
863
|
+
}
|
|
864
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
865
|
+
style: {
|
|
866
|
+
fontSize: '48px',
|
|
867
|
+
marginBottom: '16px'
|
|
868
|
+
}
|
|
869
|
+
}, "\uD83D\uDCAC"), /*#__PURE__*/_react["default"].createElement("h3", {
|
|
870
|
+
style: {
|
|
871
|
+
margin: '0 0 8px 0',
|
|
872
|
+
fontSize: '18px',
|
|
873
|
+
fontWeight: '600'
|
|
874
|
+
}
|
|
875
|
+
}, "Select a conversation"), /*#__PURE__*/_react["default"].createElement("p", {
|
|
876
|
+
style: {
|
|
877
|
+
margin: '0',
|
|
878
|
+
fontSize: '14px'
|
|
879
|
+
}
|
|
880
|
+
}, "Choose a conversation from the list to start messaging")))));
|
|
881
|
+
};
|
|
882
|
+
PowrBaseChat.propTypes = {
|
|
883
|
+
projectId: _propTypes["default"].string.isRequired,
|
|
884
|
+
apiUrl: _propTypes["default"].string,
|
|
885
|
+
onMessageSent: _propTypes["default"].func,
|
|
886
|
+
onConversationCreated: _propTypes["default"].func,
|
|
887
|
+
onError: _propTypes["default"].func,
|
|
888
|
+
className: _propTypes["default"].string,
|
|
889
|
+
style: _propTypes["default"].object,
|
|
890
|
+
theme: _propTypes["default"].oneOf(['default', 'dark'])
|
|
891
|
+
};
|
|
892
|
+
var _default = exports["default"] = PowrBaseChat;
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,12 @@ Object.defineProperty(exports, "PowrBaseBlogs", {
|
|
|
15
15
|
return _blogs["default"];
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
+
Object.defineProperty(exports, "PowrBaseChat", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function get() {
|
|
21
|
+
return _chat["default"];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
18
24
|
Object.defineProperty(exports, "PowrBaseComments", {
|
|
19
25
|
enumerable: true,
|
|
20
26
|
get: function get() {
|
|
@@ -129,4 +135,5 @@ var _admin4 = _interopRequireDefault(require("./invoices/admin"));
|
|
|
129
135
|
var _admin5 = _interopRequireDefault(require("./notifications/admin"));
|
|
130
136
|
var _admin6 = _interopRequireDefault(require("./users/admin"));
|
|
131
137
|
var _table = _interopRequireDefault(require("./table"));
|
|
138
|
+
var _chat = _interopRequireDefault(require("./chat"));
|
|
132
139
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|