@viewberapp/chat 0.0.4
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/LICENSE +21 -0
- package/README.md +11 -0
- package/dist/CachedChat.d.ts +21 -0
- package/dist/CachedChat.js +36 -0
- package/dist/CachedChat.js.map +1 -0
- package/dist/Chat.d.ts +41 -0
- package/dist/Chat.js +110 -0
- package/dist/Chat.js.map +1 -0
- package/dist/ChatSubscription.d.ts +20 -0
- package/dist/ChatSubscription.js +33 -0
- package/dist/ChatSubscription.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/react/useAppointmentChat.d.ts +5 -0
- package/dist/react/useAppointmentChat.js +11 -0
- package/dist/react/useAppointmentChat.js.map +1 -0
- package/dist/react/useChat.d.ts +6 -0
- package/dist/react/useChat.js +43 -0
- package/dist/react/useChat.js.map +1 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Viewber
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
|
+
import { PresenceChannel } from "laravel-echo";
|
|
3
|
+
import ChatSubscription from "./ChatSubscription";
|
|
4
|
+
export declare type CachedChatGetLatestMessagesReturnValue = {
|
|
5
|
+
messages: [];
|
|
6
|
+
} | {
|
|
7
|
+
error: {};
|
|
8
|
+
};
|
|
9
|
+
export default class CachedChat {
|
|
10
|
+
private axios;
|
|
11
|
+
private messages;
|
|
12
|
+
private subscriptions;
|
|
13
|
+
private channel;
|
|
14
|
+
constructor(axios: AxiosInstance, channel: PresenceChannel);
|
|
15
|
+
addSubscription(subscription: ChatSubscription): void;
|
|
16
|
+
protected handleChannelHere(users: any): void;
|
|
17
|
+
protected handleChannelJoining(user: any): void;
|
|
18
|
+
protected handleChannelLeaving(user: any): void;
|
|
19
|
+
protected handleChannelChatMessageCreated(event: any): void;
|
|
20
|
+
protected handleChannelError(error: any): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class CachedChat {
|
|
4
|
+
constructor(axios, channel) {
|
|
5
|
+
this.axios = axios;
|
|
6
|
+
this.messages = [];
|
|
7
|
+
this.subscriptions = [];
|
|
8
|
+
this.channel = channel;
|
|
9
|
+
channel
|
|
10
|
+
.here(this.handleChannelHere)
|
|
11
|
+
.joining(this.handleChannelJoining)
|
|
12
|
+
.leaving(this.handleChannelLeaving)
|
|
13
|
+
.listen('.chat.message.created', this.handleChannelChatMessageCreated)
|
|
14
|
+
.error(this.handleChannelError);
|
|
15
|
+
}
|
|
16
|
+
addSubscription(subscription) {
|
|
17
|
+
this.subscriptions.push(subscription);
|
|
18
|
+
}
|
|
19
|
+
handleChannelHere(users) {
|
|
20
|
+
this.subscriptions.forEach((subscription) => subscription.emit('onHere', users));
|
|
21
|
+
}
|
|
22
|
+
handleChannelJoining(user) {
|
|
23
|
+
this.subscriptions.forEach((subscription) => subscription.emit('onUserJoining', user));
|
|
24
|
+
}
|
|
25
|
+
handleChannelLeaving(user) {
|
|
26
|
+
this.subscriptions.forEach((subscription) => subscription.emit('onUserLeaving', user));
|
|
27
|
+
}
|
|
28
|
+
handleChannelChatMessageCreated(event) {
|
|
29
|
+
this.subscriptions.forEach((subscription) => subscription.emit('onMessage', event));
|
|
30
|
+
}
|
|
31
|
+
handleChannelError(error) {
|
|
32
|
+
console.error(error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.default = CachedChat;
|
|
36
|
+
//# sourceMappingURL=CachedChat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CachedChat.js","sourceRoot":"","sources":["../src/CachedChat.ts"],"names":[],"mappings":";;AAQA,MAAqB,UAAU;IAM3B,YAAY,KAAoB,EAAE,OAAwB;QACtD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,aAAa,GAAG,EAAE,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,OAAO;aACF,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC;aAC5B,OAAO,CAAC,IAAI,CAAC,oBAAoB,CAAC;aAClC,OAAO,CAAC,IAAI,CAAC,oBAAoB,CAAC;aAClC,MAAM,CAAC,uBAAuB,EAAE,IAAI,CAAC,+BAA+B,CAAC;aACrE,KAAK,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;IACxC,CAAC;IAED,eAAe,CAAC,YAA8B;QAC1C,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC1C,CAAC;IAES,iBAAiB,CAAC,KAAU;QAClC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,YAA8B,EAAE,EAAE,CAC1D,YAAY,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CACrC,CAAC;IACN,CAAC;IAES,oBAAoB,CAAC,IAAS;QACpC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,YAA8B,EAAE,EAAE,CAC1D,YAAY,CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,CAC3C,CAAC;IACN,CAAC;IAES,oBAAoB,CAAC,IAAS;QACpC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,YAA8B,EAAE,EAAE,CAC1D,YAAY,CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,CAC3C,CAAC;IACN,CAAC;IAES,+BAA+B,CAAC,KAAU;QAChD,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,YAA8B,EAAE,EAAE,CAC1D,YAAY,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,CACxC,CAAC;IACN,CAAC;IAES,kBAAkB,CAAC,KAAU;QACnC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;CACJ;AAlDD,6BAkDC"}
|
package/dist/Chat.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import Echo from "laravel-echo";
|
|
2
|
+
import { AxiosInstance } from 'axios';
|
|
3
|
+
import ChatSubscription from "./ChatSubscription";
|
|
4
|
+
export interface ChatOptions {
|
|
5
|
+
echo: Echo;
|
|
6
|
+
axios?: AxiosInstance;
|
|
7
|
+
getChatForAppointmentRoute: (appointmentId: number) => string;
|
|
8
|
+
getChatMessagesRoute: (chatId: number) => string;
|
|
9
|
+
}
|
|
10
|
+
export declare type GetReturnValue = {
|
|
11
|
+
subscription: ChatSubscription;
|
|
12
|
+
error?: undefined;
|
|
13
|
+
} | {
|
|
14
|
+
subscription?: undefined;
|
|
15
|
+
error: ChatError;
|
|
16
|
+
};
|
|
17
|
+
export declare type GetLatestChatMessagesReturnValue = {
|
|
18
|
+
messages: any[];
|
|
19
|
+
error?: undefined;
|
|
20
|
+
} | {
|
|
21
|
+
messages?: undefined;
|
|
22
|
+
error: ChatError;
|
|
23
|
+
};
|
|
24
|
+
export declare type GetChatIdFromAppointmentIdReturnValue = {
|
|
25
|
+
chatId: number;
|
|
26
|
+
error?: undefined;
|
|
27
|
+
} | {
|
|
28
|
+
chatId?: undefined;
|
|
29
|
+
error: ChatError;
|
|
30
|
+
};
|
|
31
|
+
export interface ChatError {
|
|
32
|
+
code: ChatErrorCode;
|
|
33
|
+
}
|
|
34
|
+
export declare type ChatErrorCode = 'error' | 'unauthorized' | 'chat_not_initialized';
|
|
35
|
+
declare const Chat: {
|
|
36
|
+
init: (options: ChatOptions) => void;
|
|
37
|
+
isInitialized: () => boolean;
|
|
38
|
+
get: (id: number) => Promise<GetReturnValue>;
|
|
39
|
+
appointment: (appointmentId: number) => Promise<GetReturnValue>;
|
|
40
|
+
};
|
|
41
|
+
export default Chat;
|
package/dist/Chat.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const axios_1 = __importDefault(require("axios"));
|
|
16
|
+
const ChatSubscription_1 = __importDefault(require("./ChatSubscription"));
|
|
17
|
+
const CachedChat_1 = __importDefault(require("./CachedChat"));
|
|
18
|
+
const Chat = (function () {
|
|
19
|
+
let initialized = false;
|
|
20
|
+
let echo;
|
|
21
|
+
let axios;
|
|
22
|
+
let options;
|
|
23
|
+
const chats = {};
|
|
24
|
+
const init = (options) => {
|
|
25
|
+
if (initialized) {
|
|
26
|
+
console.warn('Chat.init() was called more than once. Ignoring.');
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
initialized = true;
|
|
30
|
+
echo = options.echo;
|
|
31
|
+
axios = options.axios || axios_1.default.create();
|
|
32
|
+
options = options;
|
|
33
|
+
};
|
|
34
|
+
const isInitialized = () => {
|
|
35
|
+
return initialized;
|
|
36
|
+
};
|
|
37
|
+
const get = (id) => __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const chat = getChat(id);
|
|
39
|
+
let { messages, error } = yield getLatestChatMessages(id);
|
|
40
|
+
console.log('MESSAGES', messages);
|
|
41
|
+
if (error) {
|
|
42
|
+
return { error };
|
|
43
|
+
}
|
|
44
|
+
const subscription = new ChatSubscription_1.default({
|
|
45
|
+
destroy: destroyChatSubscription
|
|
46
|
+
});
|
|
47
|
+
chat.addSubscription(subscription);
|
|
48
|
+
return { subscription };
|
|
49
|
+
});
|
|
50
|
+
const appointment = (appointmentId) => __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
var _a;
|
|
52
|
+
let chatId;
|
|
53
|
+
try {
|
|
54
|
+
const result = yield getChatIdFromAppointmentId(appointmentId);
|
|
55
|
+
if (result.error) {
|
|
56
|
+
return { error: result.error };
|
|
57
|
+
}
|
|
58
|
+
chatId = result.chatId;
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (axios_1.default.isAxiosError(error) && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 403) {
|
|
62
|
+
return { error: { code: 'unauthorized' } };
|
|
63
|
+
}
|
|
64
|
+
return { error: { code: 'error' } };
|
|
65
|
+
}
|
|
66
|
+
return yield get(chatId);
|
|
67
|
+
});
|
|
68
|
+
const getLatestChatMessages = (id) => __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
var _b;
|
|
70
|
+
try {
|
|
71
|
+
const response = yield axios.get(options.getChatMessagesRoute(id));
|
|
72
|
+
return { messages: response.data.data };
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (axios_1.default.isAxiosError(error) && ((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) === 403) {
|
|
76
|
+
return { error: { code: 'unauthorized' } };
|
|
77
|
+
}
|
|
78
|
+
return { error: { code: 'error' } };
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
const getChatIdFromAppointmentId = (appointmentId) => __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
var _c;
|
|
83
|
+
try {
|
|
84
|
+
const response = yield axios.get(options.getChatForAppointmentRoute(appointmentId));
|
|
85
|
+
return { chatId: response.data.data.id };
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (axios_1.default.isAxiosError(error) && ((_c = error.response) === null || _c === void 0 ? void 0 : _c.status) === 403) {
|
|
89
|
+
return { error: { code: 'unauthorized' } };
|
|
90
|
+
}
|
|
91
|
+
return { error: { code: 'error' } };
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
const getChat = (chatId) => {
|
|
95
|
+
if (!(chatId.toString() in chats)) {
|
|
96
|
+
chats[chatId] = new CachedChat_1.default(axios, echo.join(`chat.${chatId}`));
|
|
97
|
+
}
|
|
98
|
+
return chats[chatId];
|
|
99
|
+
};
|
|
100
|
+
const destroyChatSubscription = (subscription) => {
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
init,
|
|
104
|
+
isInitialized,
|
|
105
|
+
get,
|
|
106
|
+
appointment
|
|
107
|
+
};
|
|
108
|
+
})();
|
|
109
|
+
exports.default = Chat;
|
|
110
|
+
//# sourceMappingURL=Chat.js.map
|
package/dist/Chat.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Chat.js","sourceRoot":"","sources":["../src/Chat.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,kDAAwC;AACxC,0EAAkD;AAClD,8DAAsC;AA8BtC,MAAM,IAAI,GAAG,CAAC;IACV,IAAI,WAAW,GAAY,KAAK,CAAC;IACjC,IAAI,IAAU,CAAC;IACf,IAAI,KAAoB,CAAC;IACzB,IAAI,OAAoB,CAAC;IAEzB,MAAM,KAAK,GAAkC,EAAE,CAAC;IAQhD,MAAM,IAAI,GAAG,CAAC,OAAoB,EAAE,EAAE;QAClC,IAAI,WAAW,EAAE;YACb,OAAO,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAC;YACjE,OAAO;SACV;QACD,WAAW,GAAG,IAAI,CAAC;QACnB,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACpB,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,eAAE,CAAC,MAAM,EAAE,CAAC;QACrC,OAAO,GAAG,OAAO,CAAC;IACtB,CAAC,CAAA;IAOD,MAAM,aAAa,GAAG,GAAG,EAAE;QACvB,OAAO,WAAW,CAAC;IACvB,CAAC,CAAA;IAQD,MAAM,GAAG,GAAG,CAAO,EAAU,EAA2B,EAAE;QACtD,MAAM,IAAI,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC;QAEzB,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,MAAM,qBAAqB,CAAC,EAAE,CAAC,CAAC;QAE1D,OAAO,CAAC,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MAAM,uBAAuB,GAAG,CAAC,YAA8B,EAAE,EAAE;IAEnE,CAAC,CAAA;IAED,OAAO;QACH,IAAI;QACJ,aAAa;QACb,GAAG;QACH,WAAW;KACd,CAAA;AACL,CAAC,CAAC,EAAE,CAAC;AAML,kBAAe,IAAI,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ChatSubscriptionOptions {
|
|
2
|
+
destroy: (subscription: ChatSubscription) => void;
|
|
3
|
+
}
|
|
4
|
+
export interface ChatSubscriptionEventHandlers {
|
|
5
|
+
onMessage?: (event: any) => void;
|
|
6
|
+
onHere?: (event: any) => void;
|
|
7
|
+
onUserJoining?: (event: any) => void;
|
|
8
|
+
onUserLeaving?: (event: any) => void;
|
|
9
|
+
}
|
|
10
|
+
export default class ChatSubscription {
|
|
11
|
+
private options;
|
|
12
|
+
private eventHandlers;
|
|
13
|
+
constructor(options: ChatSubscriptionOptions);
|
|
14
|
+
onMessage(handler: ChatSubscriptionEventHandlers['onMessage']): this;
|
|
15
|
+
onHere(handler: ChatSubscriptionEventHandlers['onHere']): this;
|
|
16
|
+
onUserJoining(handler: ChatSubscriptionEventHandlers['onUserJoining']): this;
|
|
17
|
+
onUserLeaving(handler: ChatSubscriptionEventHandlers['onUserLeaving']): this;
|
|
18
|
+
emit(name: keyof ChatSubscriptionEventHandlers, data: any): void;
|
|
19
|
+
destroy(): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ChatSubscription {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.eventHandlers = {};
|
|
6
|
+
this.options = options;
|
|
7
|
+
}
|
|
8
|
+
onMessage(handler) {
|
|
9
|
+
this.eventHandlers.onMessage = handler;
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
onHere(handler) {
|
|
13
|
+
this.eventHandlers.onHere = handler;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
onUserJoining(handler) {
|
|
17
|
+
this.eventHandlers.onUserJoining = handler;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
onUserLeaving(handler) {
|
|
21
|
+
this.eventHandlers.onUserLeaving = handler;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
emit(name, data) {
|
|
25
|
+
var _a, _b;
|
|
26
|
+
(_b = (_a = this.eventHandlers)[name]) === null || _b === void 0 ? void 0 : _b.call(_a, data);
|
|
27
|
+
}
|
|
28
|
+
destroy() {
|
|
29
|
+
this.options.destroy(this);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.default = ChatSubscription;
|
|
33
|
+
//# sourceMappingURL=ChatSubscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatSubscription.js","sourceRoot":"","sources":["../src/ChatSubscription.ts"],"names":[],"mappings":";;AAWA,MAAqB,gBAAgB;IAIjC,YAAY,OAAgC;QACxC,IAAI,CAAC,aAAa,GAAG,EAAE,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IAC3B,CAAC;IAED,SAAS,CAAC,OAAmD;QACzD,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,OAAO,CAAC;QACvC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,CAAC,OAAgD;QACnD,IAAI,CAAC,aAAa,CAAC,MAAM,GAAG,OAAO,CAAC;QACpC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,aAAa,CAAC,OAAuD;QACjE,IAAI,CAAC,aAAa,CAAC,aAAa,GAAG,OAAO,CAAC;QAC3C,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,aAAa,CAAC,OAAuD;QACjE,IAAI,CAAC,aAAa,CAAC,aAAa,GAAG,OAAO,CAAC;QAC3C,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,IAAI,CAAC,IAAyC,EAAE,IAAS;;QACrD,MAAA,MAAA,IAAI,CAAC,aAAa,EAAC,IAAI,CAAC,mDAAG,IAAI,CAAC,CAAC;IACrC,CAAC;IAED,OAAO;QACH,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;CACJ;AApCD,mCAoCC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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.Chat = void 0;
|
|
7
|
+
const Chat_1 = __importDefault(require("./Chat"));
|
|
8
|
+
exports.Chat = Chat_1.default;
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;AACb,kDAA0B;AAElB,eAFD,cAAI,CAEC"}
|
|
@@ -0,0 +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 }) => {
|
|
8
|
+
return (0, useChat_1.default)({ id, type: 'appointment' });
|
|
9
|
+
};
|
|
10
|
+
exports.default = useAppointmentChat;
|
|
11
|
+
//# sourceMappingURL=useAppointmentChat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAppointmentChat.js","sourceRoot":"","sources":["../../src/react/useAppointmentChat.ts"],"names":[],"mappings":";;;;;AAAA,wDAAgC;AAMhC,MAAM,kBAAkB,GAAG,CAAC,EAAC,EAAE,EAA4B,EAAE,EAAE;IAC3D,OAAO,IAAA,iBAAO,EAAC,EAAC,EAAE,EAAE,IAAI,EAAE,aAAa,EAAC,CAAC,CAAC;AAC9C,CAAC,CAAA;AAED,kBAAe,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
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 Chat_1 = __importDefault(require("../Chat"));
|
|
7
|
+
const react_1 = require("react");
|
|
8
|
+
const useChat = ({ id, type }) => {
|
|
9
|
+
const [subscription, setSubscription] = (0, react_1.useState)();
|
|
10
|
+
const handleChatHere = (users) => {
|
|
11
|
+
console.log('handleChatHere', users);
|
|
12
|
+
};
|
|
13
|
+
const handleChatUserJoining = (user) => {
|
|
14
|
+
console.log('handleChatUserJoining', user);
|
|
15
|
+
};
|
|
16
|
+
const handleChatUserLeaving = (user) => {
|
|
17
|
+
console.log('handleChatUserLeaving', user);
|
|
18
|
+
};
|
|
19
|
+
const handleChatMessage = (event) => {
|
|
20
|
+
console.log('handleChatMessage', event);
|
|
21
|
+
};
|
|
22
|
+
(0, react_1.useEffect)(() => {
|
|
23
|
+
const method = type === 'appointment' ? 'appointment' : 'get';
|
|
24
|
+
Chat_1.default[method](id).then(({ subscription, error }) => {
|
|
25
|
+
if (error) {
|
|
26
|
+
console.log('ERROR', error);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
setSubscription(subscription);
|
|
30
|
+
}).catch((err) => {
|
|
31
|
+
console.log('Chat.get');
|
|
32
|
+
});
|
|
33
|
+
}, []);
|
|
34
|
+
(0, react_1.useEffect)(() => {
|
|
35
|
+
var _a, _b, _c;
|
|
36
|
+
(_c = (_b = (_a = subscription === null || subscription === void 0 ? void 0 : subscription.onHere(handleChatHere)) === null || _a === void 0 ? void 0 : _a.onUserJoining(handleChatUserJoining)) === null || _b === void 0 ? void 0 : _b.onUserLeaving(handleChatUserLeaving)) === null || _c === void 0 ? void 0 : _c.onMessage(handleChatMessage);
|
|
37
|
+
return () => {
|
|
38
|
+
subscription === null || subscription === void 0 ? void 0 : subscription.destroy();
|
|
39
|
+
};
|
|
40
|
+
}, [subscription]);
|
|
41
|
+
};
|
|
42
|
+
exports.default = useChat;
|
|
43
|
+
//# sourceMappingURL=useChat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useChat.js","sourceRoot":"","sources":["../../src/react/useChat.ts"],"names":[],"mappings":";;;;;AAAA,mDAA2B;AAC3B,iCAA0C;AAQ1C,MAAM,OAAO,GAAG,CAAC,EAAC,EAAE,EAAE,IAAI,EAAgB,EAAE,EAAE;IAC1C,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,IAAA,gBAAQ,GAA8B,CAAC;IAE/E,MAAM,cAAc,GAAG,CAAC,KAAU,EAAE,EAAE;QAClC,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,KAAK,CAAC,CAAC;IACzC,CAAC,CAAA;IAED,MAAM,qBAAqB,GAAG,CAAC,IAAS,EAAE,EAAE;QACxC,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC,CAAA;IAED,MAAM,qBAAqB,GAAG,CAAC,IAAS,EAAE,EAAE;QACxC,OAAO,CAAC,GAAG,CAAC,uBAAuB,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC,CAAA;IAED,MAAM,iBAAiB,GAAG,CAAC,KAAU,EAAE,EAAE;QACrC,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC,CAAA;IAED,IAAA,iBAAS,EAAC,GAAG,EAAE;QACX,MAAM,MAAM,GAAG,IAAI,KAAK,aAAa,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC;QAC9D,cAAI,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,EAAC,YAAY,EAAE,KAAK,EAAC,EAAE,EAAE;YAC5C,IAAI,KAAK,EAAE;gBAEP,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;gBAC5B,OAAO;aACV;YACD,eAAe,CAAC,YAAY,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAQ,EAAE,EAAE;YAClB,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;QAC3B,CAAC,CAAC,CAAC;IACP,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAA,iBAAS,EAAC,GAAG,EAAE;;QACX,MAAA,MAAA,MAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CACN,MAAM,CAAC,cAAc,CAAC,0CACtB,aAAa,CAAC,qBAAqB,CAAC,0CACpC,aAAa,CAAC,qBAAqB,CAAC,0CACpC,SAAS,CAAC,iBAAiB,CAAC,CAAA;QAElC,OAAO,GAAG,EAAE;YACR,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,EAAE,CAAC;QAC5B,CAAC,CAAA;IACL,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;AACvB,CAAC,CAAA;AAED,kBAAe,OAAO,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@viewberapp/chat",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Viewber chat",
|
|
5
|
+
"author": "Jacob <jacob.chen@viewber.co.uk>",
|
|
6
|
+
"homepage": "https://bitbucket.org/ezadr/viewberjs#readme",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"main": "dist/chat.cjs.js",
|
|
9
|
+
"module": "dist/chat.esm.js",
|
|
10
|
+
"typings": "dist/chat.cjs.d.ts",
|
|
11
|
+
"directories": {
|
|
12
|
+
"dist": "dist",
|
|
13
|
+
"test": "__tests__"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+ssh://git@bitbucket.org/ezadr/viewberjs.git"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"tsc": "tsc",
|
|
27
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://bitbucket.org/ezadr/viewberjs/issues"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"laravel-echo": "^1.12.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"axios": "^0.21.1",
|
|
37
|
+
"react": "^16.14.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/react": "^16.14.0"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "ebd79a0dc507f66a8c759459ee180b1ed1f8e624"
|
|
43
|
+
}
|