@viewberapp/chat 0.0.106 → 0.0.107

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 (66) hide show
  1. package/LICENSE +20 -20
  2. package/dist/API.d.ts +83 -83
  3. package/dist/API.js +216 -216
  4. package/dist/CachedChat.d.ts +59 -59
  5. package/dist/CachedChat.js +220 -220
  6. package/dist/Chat.d.ts +64 -64
  7. package/dist/Chat.js +267 -267
  8. package/dist/ChatError.d.ts +4 -4
  9. package/dist/ChatError.js +2 -2
  10. package/dist/ChatSubscription.d.ts +79 -79
  11. package/dist/ChatSubscription.js +119 -119
  12. package/dist/Message.d.ts +25 -25
  13. package/dist/Message.js +35 -35
  14. package/dist/OnlineUser.d.ts +8 -8
  15. package/dist/OnlineUser.js +9 -9
  16. package/dist/PendingMessage.d.ts +23 -23
  17. package/dist/PendingMessage.js +23 -23
  18. package/dist/index.d.ts +8 -8
  19. package/dist/index.js +27 -27
  20. package/dist/logger/ConsoleLogger.d.ts +8 -8
  21. package/dist/logger/ConsoleLogger.js +20 -20
  22. package/dist/logger/NullLogger.d.ts +8 -8
  23. package/dist/logger/NullLogger.js +10 -10
  24. package/dist/logger/RollbarLogger.d.ts +11 -11
  25. package/dist/logger/RollbarLogger.js +23 -23
  26. package/dist/logger/index.d.ts +12 -12
  27. package/dist/logger/index.js +12 -12
  28. package/dist/react/components/AdminMessage.d.ts +6 -6
  29. package/dist/react/components/AdminMessage.js +59 -59
  30. package/dist/react/components/ConnectionAndChannelStatusLabel.d.ts +7 -7
  31. package/dist/react/components/ConnectionAndChannelStatusLabel.js +59 -59
  32. package/dist/react/components/Message.d.ts +23 -23
  33. package/dist/react/components/Message.js +87 -87
  34. package/dist/react/components/MessageDateTime.d.ts +8 -8
  35. package/dist/react/components/MessageDateTime.js +31 -31
  36. package/dist/react/components/MessageList.d.ts +46 -46
  37. package/dist/react/components/MessageList.js +107 -107
  38. package/dist/react/components/MessageRow.d.ts +5 -5
  39. package/dist/react/components/MessageRow.js +34 -34
  40. package/dist/react/components/index.d.ts +6 -6
  41. package/dist/react/components/index.js +13 -13
  42. package/dist/react/index.d.ts +3 -3
  43. package/dist/react/index.js +16 -16
  44. package/dist/react/useAppointmentChat.d.ts +3 -3
  45. package/dist/react/useAppointmentChat.js +10 -10
  46. package/dist/react/useChat.d.ts +36 -36
  47. package/dist/react/useChat.js +123 -123
  48. package/dist/react/useRealtimeTotalUserUnreadChatMessages.d.ts +12 -12
  49. package/dist/react/useRealtimeTotalUserUnreadChatMessages.js +137 -139
  50. package/dist/react/useRealtimeTotalUserUnreadChatMessages.js.map +1 -1
  51. package/dist/util/getLatestMessage.d.ts +3 -3
  52. package/dist/util/getLatestMessage.js +17 -17
  53. package/dist/util/groupMessages.d.ts +8 -8
  54. package/dist/util/groupMessages.js +30 -30
  55. package/dist/util/index.d.ts +4 -4
  56. package/dist/util/index.js +9 -9
  57. package/dist/util/isNil.d.ts +1 -1
  58. package/dist/util/isNil.js +7 -7
  59. package/dist/util/jwtTokenChanged.d.ts +1 -1
  60. package/dist/util/jwtTokenChanged.js +10 -10
  61. package/dist/util/useIsMounted.d.ts +1 -1
  62. package/dist/util/useIsMounted.js +13 -13
  63. package/package.json +2 -2
  64. package/dist/util/getLatestMessageId.d.ts +0 -3
  65. package/dist/util/getLatestMessageId.js +0 -18
  66. package/dist/util/getLatestMessageId.js.map +0 -1
@@ -1,35 +1,35 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.MessageRow = void 0;
27
- const React = __importStar(require("react"));
28
- const MessageRow = ({ isCurrentUser, children }) => {
29
- const styles = {
30
- textAlign: isCurrentUser ? 'right' : undefined
31
- };
32
- return (React.createElement("div", { style: styles }, children));
33
- };
34
- exports.MessageRow = MessageRow;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.MessageRow = void 0;
27
+ const React = __importStar(require("react"));
28
+ const MessageRow = ({ isCurrentUser, children }) => {
29
+ const styles = {
30
+ textAlign: isCurrentUser ? 'right' : undefined
31
+ };
32
+ return (React.createElement("div", { style: styles }, children));
33
+ };
34
+ exports.MessageRow = MessageRow;
35
35
  //# sourceMappingURL=MessageRow.js.map
@@ -1,6 +1,6 @@
1
- import { Message } from './Message';
2
- import { MessageRow } from './MessageRow';
3
- import { MessageList } from './MessageList';
4
- import { AdminMessage } from "./AdminMessage";
5
- import { ConnectionAndChannelStatusLabel } from "./ConnectionAndChannelStatusLabel";
6
- export { Message, MessageRow, MessageList, AdminMessage, ConnectionAndChannelStatusLabel };
1
+ import { Message } from './Message';
2
+ import { MessageRow } from './MessageRow';
3
+ import { MessageList } from './MessageList';
4
+ import { AdminMessage } from "./AdminMessage";
5
+ import { ConnectionAndChannelStatusLabel } from "./ConnectionAndChannelStatusLabel";
6
+ export { Message, MessageRow, MessageList, AdminMessage, ConnectionAndChannelStatusLabel };
@@ -1,14 +1,14 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConnectionAndChannelStatusLabel = exports.AdminMessage = exports.MessageList = exports.MessageRow = exports.Message = void 0;
4
- const Message_1 = require("./Message");
5
- Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return Message_1.Message; } });
6
- const MessageRow_1 = require("./MessageRow");
7
- Object.defineProperty(exports, "MessageRow", { enumerable: true, get: function () { return MessageRow_1.MessageRow; } });
8
- const MessageList_1 = require("./MessageList");
9
- Object.defineProperty(exports, "MessageList", { enumerable: true, get: function () { return MessageList_1.MessageList; } });
10
- const AdminMessage_1 = require("./AdminMessage");
11
- Object.defineProperty(exports, "AdminMessage", { enumerable: true, get: function () { return AdminMessage_1.AdminMessage; } });
12
- const ConnectionAndChannelStatusLabel_1 = require("./ConnectionAndChannelStatusLabel");
13
- Object.defineProperty(exports, "ConnectionAndChannelStatusLabel", { enumerable: true, get: function () { return ConnectionAndChannelStatusLabel_1.ConnectionAndChannelStatusLabel; } });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConnectionAndChannelStatusLabel = exports.AdminMessage = exports.MessageList = exports.MessageRow = exports.Message = void 0;
4
+ const Message_1 = require("./Message");
5
+ Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return Message_1.Message; } });
6
+ const MessageRow_1 = require("./MessageRow");
7
+ Object.defineProperty(exports, "MessageRow", { enumerable: true, get: function () { return MessageRow_1.MessageRow; } });
8
+ const MessageList_1 = require("./MessageList");
9
+ Object.defineProperty(exports, "MessageList", { enumerable: true, get: function () { return MessageList_1.MessageList; } });
10
+ const AdminMessage_1 = require("./AdminMessage");
11
+ Object.defineProperty(exports, "AdminMessage", { enumerable: true, get: function () { return AdminMessage_1.AdminMessage; } });
12
+ const ConnectionAndChannelStatusLabel_1 = require("./ConnectionAndChannelStatusLabel");
13
+ Object.defineProperty(exports, "ConnectionAndChannelStatusLabel", { enumerable: true, get: function () { return ConnectionAndChannelStatusLabel_1.ConnectionAndChannelStatusLabel; } });
14
14
  //# sourceMappingURL=index.js.map
@@ -1,3 +1,3 @@
1
- export { default as useChat } from './useChat';
2
- export { default as useAppointmentChat } from './useAppointmentChat';
3
- export { Message, MessageRow, MessageList, AdminMessage, ConnectionAndChannelStatusLabel } from './components';
1
+ export { default as useChat } from './useChat';
2
+ export { default as useAppointmentChat } from './useAppointmentChat';
3
+ export { Message, MessageRow, MessageList, AdminMessage, ConnectionAndChannelStatusLabel } from './components';
@@ -1,17 +1,17 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ConnectionAndChannelStatusLabel = exports.AdminMessage = exports.MessageList = exports.MessageRow = exports.Message = exports.useAppointmentChat = exports.useChat = void 0;
7
- var useChat_1 = require("./useChat");
8
- Object.defineProperty(exports, "useChat", { enumerable: true, get: function () { return __importDefault(useChat_1).default; } });
9
- var useAppointmentChat_1 = require("./useAppointmentChat");
10
- Object.defineProperty(exports, "useAppointmentChat", { enumerable: true, get: function () { return __importDefault(useAppointmentChat_1).default; } });
11
- var components_1 = require("./components");
12
- Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return components_1.Message; } });
13
- Object.defineProperty(exports, "MessageRow", { enumerable: true, get: function () { return components_1.MessageRow; } });
14
- Object.defineProperty(exports, "MessageList", { enumerable: true, get: function () { return components_1.MessageList; } });
15
- Object.defineProperty(exports, "AdminMessage", { enumerable: true, get: function () { return components_1.AdminMessage; } });
16
- Object.defineProperty(exports, "ConnectionAndChannelStatusLabel", { enumerable: true, get: function () { return components_1.ConnectionAndChannelStatusLabel; } });
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ConnectionAndChannelStatusLabel = exports.AdminMessage = exports.MessageList = exports.MessageRow = exports.Message = exports.useAppointmentChat = exports.useChat = void 0;
7
+ var useChat_1 = require("./useChat");
8
+ Object.defineProperty(exports, "useChat", { enumerable: true, get: function () { return __importDefault(useChat_1).default; } });
9
+ var useAppointmentChat_1 = require("./useAppointmentChat");
10
+ Object.defineProperty(exports, "useAppointmentChat", { enumerable: true, get: function () { return __importDefault(useAppointmentChat_1).default; } });
11
+ var components_1 = require("./components");
12
+ Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return components_1.Message; } });
13
+ Object.defineProperty(exports, "MessageRow", { enumerable: true, get: function () { return components_1.MessageRow; } });
14
+ Object.defineProperty(exports, "MessageList", { enumerable: true, get: function () { return components_1.MessageList; } });
15
+ Object.defineProperty(exports, "AdminMessage", { enumerable: true, get: function () { return components_1.AdminMessage; } });
16
+ Object.defineProperty(exports, "ConnectionAndChannelStatusLabel", { enumerable: true, get: function () { return components_1.ConnectionAndChannelStatusLabel; } });
17
17
  //# sourceMappingURL=index.js.map
@@ -1,3 +1,3 @@
1
- import { UseChatOptions } from "./useChat";
2
- declare const useAppointmentChat: (id: number, options?: Omit<UseChatOptions, 'idType'>) => import("./useChat").UseChatReturnValue;
3
- export default useAppointmentChat;
1
+ import { UseChatOptions } from "./useChat";
2
+ declare const useAppointmentChat: (id: number, options?: Omit<UseChatOptions, 'idType'>) => import("./useChat").UseChatReturnValue;
3
+ export default useAppointmentChat;
@@ -1,11 +1,11 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const useChat_1 = __importDefault(require("./useChat"));
7
- const useAppointmentChat = (id, options = {}) => {
8
- return (0, useChat_1.default)(id, Object.assign(Object.assign({}, options), { idType: 'appointment' }));
9
- };
10
- exports.default = useAppointmentChat;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const useChat_1 = __importDefault(require("./useChat"));
7
+ const useAppointmentChat = (id, options = {}) => {
8
+ return (0, useChat_1.default)(id, Object.assign(Object.assign({}, options), { idType: 'appointment' }));
9
+ };
10
+ exports.default = useAppointmentChat;
11
11
  //# sourceMappingURL=useAppointmentChat.js.map
@@ -1,36 +1,36 @@
1
- import Message from '../Message';
2
- import ChatError from '../ChatError';
3
- import OnlineUser from '../OnlineUser';
4
- import { ConnectionStatus, ChannelStatus } from '../Chat';
5
- import { GetLatestMessagesStatus, Messages } from '../CachedChat';
6
- import { ChatSubscriptionStatus, ChatSubscriptionEventCallbackData } from '../ChatSubscription';
7
- import PendingMessage from '../PendingMessage';
8
- export interface UseChatOptions {
9
- idType?: 'appointment';
10
- onMessages?: OnMessagesCallback | undefined;
11
- }
12
- export declare type OnMessagesCallback = (data: ChatSubscriptionEventCallbackData['messages']) => void;
13
- export declare type UseChatStatus = {
14
- status: 'not_initialized' | 'initializing' | 'initialized';
15
- } | {
16
- status: 'error';
17
- error: ChatError | undefined;
18
- };
19
- export interface UseChatReturnValue {
20
- status: UseChatStatus;
21
- messages: Messages;
22
- onlineUsers: OnlineUser[];
23
- subscriptionStatus: ChatSubscriptionStatus | undefined;
24
- channelStatus: ChannelStatus | undefined;
25
- connectionStatus: ConnectionStatus | undefined;
26
- getLatestMessagesStatus: GetLatestMessagesStatus | undefined;
27
- initialize: () => void;
28
- sendMessage: (message: string) => Promise<any> | void;
29
- sendPendingMessage: (pendingMessage: PendingMessage) => Promise<any> | void;
30
- getLatestMessages: () => void;
31
- markMessagesReadUpTo: (message: Message) => Promise<{
32
- error?: ChatError | undefined;
33
- }>;
34
- }
35
- declare const useChat: (id: number, { idType, onMessages }?: UseChatOptions) => UseChatReturnValue;
36
- export default useChat;
1
+ import Message from '../Message';
2
+ import ChatError from '../ChatError';
3
+ import OnlineUser from '../OnlineUser';
4
+ import { ConnectionStatus, ChannelStatus } from '../Chat';
5
+ import { GetLatestMessagesStatus, Messages } from '../CachedChat';
6
+ import { ChatSubscriptionStatus, ChatSubscriptionEventCallbackData } from '../ChatSubscription';
7
+ import PendingMessage from '../PendingMessage';
8
+ export interface UseChatOptions {
9
+ idType?: 'appointment';
10
+ onMessages?: OnMessagesCallback | undefined;
11
+ }
12
+ export declare type OnMessagesCallback = (data: ChatSubscriptionEventCallbackData['messages']) => void;
13
+ export declare type UseChatStatus = {
14
+ status: 'not_initialized' | 'initializing' | 'initialized';
15
+ } | {
16
+ status: 'error';
17
+ error: ChatError | undefined;
18
+ };
19
+ export interface UseChatReturnValue {
20
+ status: UseChatStatus;
21
+ messages: Messages;
22
+ onlineUsers: OnlineUser[];
23
+ subscriptionStatus: ChatSubscriptionStatus | undefined;
24
+ channelStatus: ChannelStatus | undefined;
25
+ connectionStatus: ConnectionStatus | undefined;
26
+ getLatestMessagesStatus: GetLatestMessagesStatus | undefined;
27
+ initialize: () => void;
28
+ sendMessage: (message: string) => Promise<any> | void;
29
+ sendPendingMessage: (pendingMessage: PendingMessage) => Promise<any> | void;
30
+ getLatestMessages: () => void;
31
+ markMessagesReadUpTo: (message: Message) => Promise<{
32
+ error?: ChatError | undefined;
33
+ }>;
34
+ }
35
+ declare const useChat: (id: number, { idType, onMessages }?: UseChatOptions) => UseChatReturnValue;
36
+ export default useChat;
@@ -1,124 +1,124 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const react_1 = require("react");
7
- const isNil_1 = require("../util/isNil");
8
- const Chat_1 = __importDefault(require("../Chat"));
9
- const useIsMounted_1 = require("../util/useIsMounted");
10
- const useChat = (id, { idType, onMessages } = {}) => {
11
- const isMounted = (0, useIsMounted_1.useIsMounted)();
12
- const [subscription, setSubscription] = (0, react_1.useState)();
13
- const [status, setStatus] = (0, react_1.useState)({
14
- status: 'not_initialized'
15
- });
16
- const [messages, setMessages] = (0, react_1.useState)([]);
17
- const [onlineUsers, setOnlineUsers] = (0, react_1.useState)([]);
18
- const [subscriptionStatus, setSubscriptionStatus] = (0, react_1.useState)();
19
- const [channelStatus, setChannelStatus] = (0, react_1.useState)();
20
- const [connectionStatus, setConnectionStatus] = (0, react_1.useState)();
21
- const [getLatestMessagesStatus, setGetLatestMessagesStatus] = (0, react_1.useState)();
22
- const handleMessages = ({ messages }) => {
23
- setMessages(messages);
24
- if (onMessages)
25
- onMessages({ messages });
26
- };
27
- const handleHere = ({ users }) => {
28
- setOnlineUsers(users);
29
- };
30
- const handleUserJoining = ({ user, users }) => {
31
- setOnlineUsers(users);
32
- };
33
- const handleUserLeaving = ({ user, users }) => {
34
- setOnlineUsers(users);
35
- };
36
- const handleSubscriptionStatusChange = ({ status }) => {
37
- if (isMounted()) {
38
- setSubscriptionStatus(status);
39
- }
40
- };
41
- const handleChannelStatusChange = ({ status }) => {
42
- setChannelStatus(status);
43
- };
44
- const handleConnectionStatusChange = ({ status }) => {
45
- setConnectionStatus(status);
46
- };
47
- const handleGetLatestMessagesStatusChange = ({ status }) => {
48
- setGetLatestMessagesStatus(status);
49
- };
50
- const initializeChatSubscription = () => {
51
- if (status.status === 'initializing' || !(0, isNil_1.isNil)(subscription)) {
52
- if (!(0, isNil_1.isNil)(subscription) && status.status !== 'initialized') {
53
- setStatus({ status: 'initialized' });
54
- }
55
- ;
56
- return;
57
- }
58
- setStatus({ status: 'initializing' });
59
- Chat_1.default[idType === 'appointment' ? 'appointment' : 'get'](id).then(({ subscription, error }) => {
60
- if (error) {
61
- setStatus({ status: 'error', error });
62
- return;
63
- }
64
- setSubscription(subscription);
65
- }).catch((err) => {
66
- setStatus({
67
- status: 'error',
68
- error: {
69
- message: err.message || 'Error',
70
- code: 'error'
71
- }
72
- });
73
- });
74
- };
75
- (0, react_1.useEffect)(() => {
76
- initializeChatSubscription();
77
- }, []);
78
- (0, react_1.useEffect)(() => {
79
- if (subscription) {
80
- setStatus({ status: 'initialized' });
81
- setMessages(subscription.messages);
82
- setSubscriptionStatus(subscription.getStatus());
83
- setChannelStatus(subscription.getChannelStatus());
84
- setConnectionStatus(subscription.getConnectionStatus());
85
- setGetLatestMessagesStatus(subscription.getGetLatestMessagesStatus());
86
- subscription
87
- .on('here', handleHere)
88
- .on('userJoining', handleUserJoining)
89
- .on('userLeaving', handleUserLeaving)
90
- .on('messages', handleMessages)
91
- .on('statusChange', handleSubscriptionStatusChange)
92
- .on('channelStatusChange', handleChannelStatusChange)
93
- .on('connectionStatusChange', handleConnectionStatusChange)
94
- .on('getLatestMessagesStatusChange', handleGetLatestMessagesStatusChange);
95
- }
96
- return () => {
97
- subscription === null || subscription === void 0 ? void 0 : subscription.cancel();
98
- };
99
- }, [subscription]);
100
- return {
101
- status,
102
- messages,
103
- onlineUsers,
104
- subscriptionStatus,
105
- channelStatus,
106
- connectionStatus,
107
- getLatestMessagesStatus,
108
- initialize: initializeChatSubscription,
109
- sendMessage: (message) => subscription === null || subscription === void 0 ? void 0 : subscription.sendMessage(message),
110
- sendPendingMessage: (pendingMessage) => subscription === null || subscription === void 0 ? void 0 : subscription.sendPendingMessage(pendingMessage),
111
- getLatestMessages: () => subscription === null || subscription === void 0 ? void 0 : subscription.getLatestMessages(),
112
- markMessagesReadUpTo: (message) => {
113
- if (subscription) {
114
- return subscription.markMessagesReadUpTo(message);
115
- }
116
- return Promise.resolve({ error: {
117
- message: 'The chat is not initialized',
118
- code: 'no_chat_subscription'
119
- } });
120
- }
121
- };
122
- };
123
- exports.default = useChat;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = require("react");
7
+ const isNil_1 = require("../util/isNil");
8
+ const Chat_1 = __importDefault(require("../Chat"));
9
+ const useIsMounted_1 = require("../util/useIsMounted");
10
+ const useChat = (id, { idType, onMessages } = {}) => {
11
+ const isMounted = (0, useIsMounted_1.useIsMounted)();
12
+ const [subscription, setSubscription] = (0, react_1.useState)();
13
+ const [status, setStatus] = (0, react_1.useState)({
14
+ status: 'not_initialized'
15
+ });
16
+ const [messages, setMessages] = (0, react_1.useState)([]);
17
+ const [onlineUsers, setOnlineUsers] = (0, react_1.useState)([]);
18
+ const [subscriptionStatus, setSubscriptionStatus] = (0, react_1.useState)();
19
+ const [channelStatus, setChannelStatus] = (0, react_1.useState)();
20
+ const [connectionStatus, setConnectionStatus] = (0, react_1.useState)();
21
+ const [getLatestMessagesStatus, setGetLatestMessagesStatus] = (0, react_1.useState)();
22
+ const handleMessages = ({ messages }) => {
23
+ setMessages(messages);
24
+ if (onMessages)
25
+ onMessages({ messages });
26
+ };
27
+ const handleHere = ({ users }) => {
28
+ setOnlineUsers(users);
29
+ };
30
+ const handleUserJoining = ({ user, users }) => {
31
+ setOnlineUsers(users);
32
+ };
33
+ const handleUserLeaving = ({ user, users }) => {
34
+ setOnlineUsers(users);
35
+ };
36
+ const handleSubscriptionStatusChange = ({ status }) => {
37
+ if (isMounted()) {
38
+ setSubscriptionStatus(status);
39
+ }
40
+ };
41
+ const handleChannelStatusChange = ({ status }) => {
42
+ setChannelStatus(status);
43
+ };
44
+ const handleConnectionStatusChange = ({ status }) => {
45
+ setConnectionStatus(status);
46
+ };
47
+ const handleGetLatestMessagesStatusChange = ({ status }) => {
48
+ setGetLatestMessagesStatus(status);
49
+ };
50
+ const initializeChatSubscription = () => {
51
+ if (status.status === 'initializing' || !(0, isNil_1.isNil)(subscription)) {
52
+ if (!(0, isNil_1.isNil)(subscription) && status.status !== 'initialized') {
53
+ setStatus({ status: 'initialized' });
54
+ }
55
+ ;
56
+ return;
57
+ }
58
+ setStatus({ status: 'initializing' });
59
+ Chat_1.default[idType === 'appointment' ? 'appointment' : 'get'](id).then(({ subscription, error }) => {
60
+ if (error) {
61
+ setStatus({ status: 'error', error });
62
+ return;
63
+ }
64
+ setSubscription(subscription);
65
+ }).catch((err) => {
66
+ setStatus({
67
+ status: 'error',
68
+ error: {
69
+ message: err.message || 'Error',
70
+ code: 'error'
71
+ }
72
+ });
73
+ });
74
+ };
75
+ (0, react_1.useEffect)(() => {
76
+ initializeChatSubscription();
77
+ }, []);
78
+ (0, react_1.useEffect)(() => {
79
+ if (subscription) {
80
+ setStatus({ status: 'initialized' });
81
+ setMessages(subscription.messages);
82
+ setSubscriptionStatus(subscription.getStatus());
83
+ setChannelStatus(subscription.getChannelStatus());
84
+ setConnectionStatus(subscription.getConnectionStatus());
85
+ setGetLatestMessagesStatus(subscription.getGetLatestMessagesStatus());
86
+ subscription
87
+ .on('here', handleHere)
88
+ .on('userJoining', handleUserJoining)
89
+ .on('userLeaving', handleUserLeaving)
90
+ .on('messages', handleMessages)
91
+ .on('statusChange', handleSubscriptionStatusChange)
92
+ .on('channelStatusChange', handleChannelStatusChange)
93
+ .on('connectionStatusChange', handleConnectionStatusChange)
94
+ .on('getLatestMessagesStatusChange', handleGetLatestMessagesStatusChange);
95
+ }
96
+ return () => {
97
+ subscription === null || subscription === void 0 ? void 0 : subscription.cancel();
98
+ };
99
+ }, [subscription]);
100
+ return {
101
+ status,
102
+ messages,
103
+ onlineUsers,
104
+ subscriptionStatus,
105
+ channelStatus,
106
+ connectionStatus,
107
+ getLatestMessagesStatus,
108
+ initialize: initializeChatSubscription,
109
+ sendMessage: (message) => subscription === null || subscription === void 0 ? void 0 : subscription.sendMessage(message),
110
+ sendPendingMessage: (pendingMessage) => subscription === null || subscription === void 0 ? void 0 : subscription.sendPendingMessage(pendingMessage),
111
+ getLatestMessages: () => subscription === null || subscription === void 0 ? void 0 : subscription.getLatestMessages(),
112
+ markMessagesReadUpTo: (message) => {
113
+ if (subscription) {
114
+ return subscription.markMessagesReadUpTo(message);
115
+ }
116
+ return Promise.resolve({ error: {
117
+ message: 'The chat is not initialized',
118
+ code: 'no_chat_subscription'
119
+ } });
120
+ }
121
+ };
122
+ };
123
+ exports.default = useChat;
124
124
  //# sourceMappingURL=useChat.js.map
@@ -1,12 +1,12 @@
1
- import { ChannelStatus } from "../Chat";
2
- export interface UseRealtimeTotalUserUnreadChatMessagesOptions {
3
- appointmentId?: number;
4
- requireAppointmentId?: boolean;
5
- appointmentIsConfirmedForViewing?: boolean;
6
- __debug__?: boolean;
7
- }
8
- export interface UseRealtimeTotalUserUnreadChatMessagesReturnValue {
9
- channelStatus: ChannelStatus | undefined;
10
- totalUnreadMessages: number | undefined;
11
- }
12
- export declare const useRealtimeTotalUserUnreadChatMessages: (userId?: number | null, options?: UseRealtimeTotalUserUnreadChatMessagesOptions) => UseRealtimeTotalUserUnreadChatMessagesReturnValue;
1
+ import { ChannelStatus } from "../Chat";
2
+ export interface UseRealtimeTotalUserUnreadChatMessagesOptions {
3
+ appointmentId?: number;
4
+ requireAppointmentId?: boolean;
5
+ appointmentIsConfirmedForViewing?: boolean;
6
+ __debug__?: boolean;
7
+ }
8
+ export interface UseRealtimeTotalUserUnreadChatMessagesReturnValue {
9
+ channelStatus: ChannelStatus | undefined;
10
+ totalUnreadMessages: number | undefined;
11
+ }
12
+ export declare const useRealtimeTotalUserUnreadChatMessages: (userId?: number | null, options?: UseRealtimeTotalUserUnreadChatMessagesOptions) => UseRealtimeTotalUserUnreadChatMessagesReturnValue;