cardoctor-call-chat 1.0.1 → 1.0.2

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/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # CarDoctor Call Chat
2
+
3
+ 🚀 A React SDK with hooks and utilities for Cardoctor Call Chat.
4
+
5
+ ## 📦 Installation
6
+
7
+ ```bash
8
+ npm install cardoctor-call-chat
9
+ # or
10
+ yarn add cardoctor-call-chat
11
+
12
+ ```
13
+
14
+ # Developed by Tuan Nguyen
package/dist/index.d.mts CHANGED
@@ -1,8 +1,12 @@
1
- import * as react from 'react';
1
+ interface ChatInitState {
2
+ user: any | null;
3
+ loading: boolean;
4
+ }
5
+ interface IPros {
6
+ appId: string;
7
+ region: string;
8
+ userId: string;
9
+ }
10
+ declare function useCallChatInit(props: IPros): ChatInitState;
2
11
 
3
- declare function useCounter(initial?: number): {
4
- count: number;
5
- setCount: react.Dispatch<react.SetStateAction<number>>;
6
- };
7
-
8
- export { useCounter };
12
+ export { useCallChatInit };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,12 @@
1
- import * as react from 'react';
1
+ interface ChatInitState {
2
+ user: any | null;
3
+ loading: boolean;
4
+ }
5
+ interface IPros {
6
+ appId: string;
7
+ region: string;
8
+ userId: string;
9
+ }
10
+ declare function useCallChatInit(props: IPros): ChatInitState;
2
11
 
3
- declare function useCounter(initial?: number): {
4
- count: number;
5
- setCount: react.Dispatch<react.SetStateAction<number>>;
6
- };
7
-
8
- export { useCounter };
12
+ export { useCallChatInit };
package/dist/index.js CHANGED
@@ -16,25 +16,112 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __async = (__this, __arguments, generator) => {
20
+ return new Promise((resolve, reject) => {
21
+ var fulfilled = (value) => {
22
+ try {
23
+ step(generator.next(value));
24
+ } catch (e) {
25
+ reject(e);
26
+ }
27
+ };
28
+ var rejected = (value) => {
29
+ try {
30
+ step(generator.throw(value));
31
+ } catch (e) {
32
+ reject(e);
33
+ }
34
+ };
35
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
+ step((generator = generator.apply(__this, __arguments)).next());
37
+ });
38
+ };
19
39
 
20
40
  // src/index.ts
21
41
  var index_exports = {};
22
42
  __export(index_exports, {
23
- useCounter: () => useCounter
43
+ useCallChatInit: () => useCallChatInit
24
44
  });
25
45
  module.exports = __toCommonJS(index_exports);
26
46
 
27
- // src/useCounter.ts
47
+ // src/cometchat/use-call-chat-init.tsx
28
48
  var import_react = require("react");
29
- function useCounter(initial = 0) {
30
- const [count, setCount] = (0, import_react.useState)(initial);
49
+ var import_chat_uikit_react2 = require("@cometchat/chat-uikit-react");
50
+
51
+ // src/cometchat/api/get-auth-token.ts
52
+ var myHeaders = new Headers();
53
+ myHeaders.append("accept", "application/json");
54
+ myHeaders.append("apikey", "07ffe79d29cf9a3596e5fe369125a48b829ea400");
55
+ myHeaders.append("content-type", "application/json");
56
+ var requestOptions = {
57
+ method: "POST",
58
+ headers: myHeaders
59
+ };
60
+ var getAuthToken = (userId) => __async(null, null, function* () {
61
+ const getToken = yield fetch(
62
+ `https://2772864c1a441d73.api-in.cometchat.io/v3/users/${userId}/auth_tokens`,
63
+ requestOptions
64
+ );
65
+ const response = yield getToken.json();
66
+ return response.data.authToken;
67
+ });
68
+
69
+ // src/cometchat/func/login-user.ts
70
+ var import_chat_uikit_react = require("@cometchat/chat-uikit-react");
71
+ var isLoggingIn = false;
72
+ var loginWithAuthToken = (authToken) => __async(null, null, function* () {
73
+ try {
74
+ if (isLoggingIn) {
75
+ console.log("\u26A0\uFE0F Login request already in progress...");
76
+ return;
77
+ }
78
+ const existingUser = yield import_chat_uikit_react.CometChatUIKit.getLoggedinUser();
79
+ if (existingUser) {
80
+ console.log("\u{1F539} Already logged in:", existingUser);
81
+ return existingUser;
82
+ }
83
+ isLoggingIn = true;
84
+ const user = yield import_chat_uikit_react.CometChatUIKit.loginWithAuthToken(authToken);
85
+ console.log("\u2705 Login successful:", user);
86
+ return user;
87
+ } catch (error) {
88
+ console.error("\u274C Login failed:", error);
89
+ } finally {
90
+ isLoggingIn = false;
91
+ }
92
+ });
93
+
94
+ // src/cometchat/use-call-chat-init.tsx
95
+ function useCallChatInit(props) {
96
+ const [user, setUser] = (0, import_react.useState)(null);
97
+ const [loading, setLoading] = (0, import_react.useState)(true);
31
98
  (0, import_react.useEffect)(() => {
32
- console.log("Counter mounted:", count);
33
- return () => console.log("Counter unmounted");
99
+ let mounted = true;
100
+ const init = () => __async(null, null, function* () {
101
+ try {
102
+ const UIKitSettings = new import_chat_uikit_react2.UIKitSettingsBuilder().setAppId(props.appId).setRegion(props.region).subscribePresenceForAllUsers().build();
103
+ yield import_chat_uikit_react2.CometChatUIKit.init(UIKitSettings);
104
+ console.log("\u2705 CometChat UI Kit initialized successfully.");
105
+ const token = yield getAuthToken(props.userId);
106
+ console.log("\u{1F680} ~ init ~ token:", token);
107
+ const loggedInUser = yield loginWithAuthToken(token);
108
+ if (mounted) {
109
+ setUser(loggedInUser);
110
+ setLoading(false);
111
+ }
112
+ } catch (error) {
113
+ console.error("\u274C CometChat init failed:", error);
114
+ if (mounted) setLoading(false);
115
+ }
116
+ });
117
+ init();
118
+ return () => {
119
+ mounted = false;
120
+ };
34
121
  }, []);
35
- return { count, setCount };
122
+ return { user, loading };
36
123
  }
37
124
  // Annotate the CommonJS export names for ESM import in node:
38
125
  0 && (module.exports = {
39
- useCounter
126
+ useCallChatInit
40
127
  });
package/dist/index.mjs CHANGED
@@ -1,13 +1,101 @@
1
- // src/useCounter.ts
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // src/cometchat/use-call-chat-init.tsx
2
23
  import { useState, useEffect } from "react";
3
- function useCounter(initial = 0) {
4
- const [count, setCount] = useState(initial);
24
+ import { UIKitSettingsBuilder, CometChatUIKit as CometChatUIKit2 } from "@cometchat/chat-uikit-react";
25
+
26
+ // src/cometchat/api/get-auth-token.ts
27
+ var myHeaders = new Headers();
28
+ myHeaders.append("accept", "application/json");
29
+ myHeaders.append("apikey", "07ffe79d29cf9a3596e5fe369125a48b829ea400");
30
+ myHeaders.append("content-type", "application/json");
31
+ var requestOptions = {
32
+ method: "POST",
33
+ headers: myHeaders
34
+ };
35
+ var getAuthToken = (userId) => __async(null, null, function* () {
36
+ const getToken = yield fetch(
37
+ `https://2772864c1a441d73.api-in.cometchat.io/v3/users/${userId}/auth_tokens`,
38
+ requestOptions
39
+ );
40
+ const response = yield getToken.json();
41
+ return response.data.authToken;
42
+ });
43
+
44
+ // src/cometchat/func/login-user.ts
45
+ import { CometChatUIKit } from "@cometchat/chat-uikit-react";
46
+ var isLoggingIn = false;
47
+ var loginWithAuthToken = (authToken) => __async(null, null, function* () {
48
+ try {
49
+ if (isLoggingIn) {
50
+ console.log("\u26A0\uFE0F Login request already in progress...");
51
+ return;
52
+ }
53
+ const existingUser = yield CometChatUIKit.getLoggedinUser();
54
+ if (existingUser) {
55
+ console.log("\u{1F539} Already logged in:", existingUser);
56
+ return existingUser;
57
+ }
58
+ isLoggingIn = true;
59
+ const user = yield CometChatUIKit.loginWithAuthToken(authToken);
60
+ console.log("\u2705 Login successful:", user);
61
+ return user;
62
+ } catch (error) {
63
+ console.error("\u274C Login failed:", error);
64
+ } finally {
65
+ isLoggingIn = false;
66
+ }
67
+ });
68
+
69
+ // src/cometchat/use-call-chat-init.tsx
70
+ function useCallChatInit(props) {
71
+ const [user, setUser] = useState(null);
72
+ const [loading, setLoading] = useState(true);
5
73
  useEffect(() => {
6
- console.log("Counter mounted:", count);
7
- return () => console.log("Counter unmounted");
74
+ let mounted = true;
75
+ const init = () => __async(null, null, function* () {
76
+ try {
77
+ const UIKitSettings = new UIKitSettingsBuilder().setAppId(props.appId).setRegion(props.region).subscribePresenceForAllUsers().build();
78
+ yield CometChatUIKit2.init(UIKitSettings);
79
+ console.log("\u2705 CometChat UI Kit initialized successfully.");
80
+ const token = yield getAuthToken(props.userId);
81
+ console.log("\u{1F680} ~ init ~ token:", token);
82
+ const loggedInUser = yield loginWithAuthToken(token);
83
+ if (mounted) {
84
+ setUser(loggedInUser);
85
+ setLoading(false);
86
+ }
87
+ } catch (error) {
88
+ console.error("\u274C CometChat init failed:", error);
89
+ if (mounted) setLoading(false);
90
+ }
91
+ });
92
+ init();
93
+ return () => {
94
+ mounted = false;
95
+ };
8
96
  }, []);
9
- return { count, setCount };
97
+ return { user, loading };
10
98
  }
11
99
  export {
12
- useCounter
100
+ useCallChatInit
13
101
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cardoctor-call-chat",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -0,0 +1,18 @@
1
+ const myHeaders = new Headers();
2
+ myHeaders.append("accept", "application/json");
3
+ myHeaders.append("apikey", "07ffe79d29cf9a3596e5fe369125a48b829ea400");
4
+ myHeaders.append("content-type", "application/json");
5
+
6
+ const requestOptions = {
7
+ method: "POST",
8
+ headers: myHeaders
9
+ };
10
+
11
+ export const getAuthToken = async (userId: string) => {
12
+ const getToken = await fetch(
13
+ `https://2772864c1a441d73.api-in.cometchat.io/v3/users/${userId}/auth_tokens`,
14
+ requestOptions
15
+ )
16
+ const response = await getToken.json()
17
+ return response.data.authToken
18
+ }
@@ -0,0 +1,25 @@
1
+ import { CometChatUIKit } from "@cometchat/chat-uikit-react";
2
+ let isLoggingIn = false;
3
+ export const loginWithAuthToken = async (authToken: string) => {
4
+ try {
5
+ if (isLoggingIn) {
6
+ console.log("⚠️ Login request already in progress...");
7
+ return;
8
+ }
9
+
10
+ const existingUser = await CometChatUIKit.getLoggedinUser();
11
+ if (existingUser) {
12
+ console.log("🔹 Already logged in:", existingUser);
13
+ return existingUser;
14
+ }
15
+
16
+ isLoggingIn = true;
17
+ const user = await CometChatUIKit.loginWithAuthToken(authToken);
18
+ console.log("✅ Login successful:", user);
19
+ return user;
20
+ } catch (error) {
21
+ console.error("❌ Login failed:", error);
22
+ } finally {
23
+ isLoggingIn = false;
24
+ }
25
+ };
@@ -0,0 +1,57 @@
1
+ import { useState, useEffect } from "react";
2
+ import { UIKitSettingsBuilder, CometChatUIKit } from "@cometchat/chat-uikit-react";
3
+ import { getAuthToken } from "./api/get-auth-token";
4
+ import { loginWithAuthToken } from "./func/login-user";
5
+
6
+ interface ChatInitState {
7
+ user: any | null;
8
+ loading: boolean;
9
+ }
10
+
11
+ interface IPros {
12
+ appId: string;
13
+ region: string;
14
+ userId: string
15
+ }
16
+
17
+ export function useCallChatInit(props: IPros): ChatInitState {
18
+ const [user, setUser] = useState<any | null>(null);
19
+ const [loading, setLoading] = useState(true);
20
+
21
+ useEffect(() => {
22
+ let mounted = true;
23
+
24
+ const init = async () => {
25
+ try {
26
+ const UIKitSettings = new UIKitSettingsBuilder()
27
+ .setAppId(props.appId)
28
+ .setRegion(props.region)
29
+ .subscribePresenceForAllUsers()
30
+ .build();
31
+
32
+ await CometChatUIKit.init(UIKitSettings);
33
+ console.log("✅ CometChat UI Kit initialized successfully.");
34
+
35
+ const token = await getAuthToken(props.userId);
36
+ console.log("🚀 ~ init ~ token:", token)
37
+ const loggedInUser = await loginWithAuthToken(token);
38
+
39
+ if (mounted) {
40
+ setUser(loggedInUser);
41
+ setLoading(false);
42
+ }
43
+ } catch (error) {
44
+ console.error("❌ CometChat init failed:", error);
45
+ if (mounted) setLoading(false);
46
+ }
47
+ };
48
+
49
+ init();
50
+
51
+ return () => {
52
+ mounted = false;
53
+ };
54
+ }, []);
55
+
56
+ return { user, loading };
57
+ }
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export * from "./useCounter";
1
+ export * from "./cometchat/use-call-chat-init";
File without changes
package/src/useCounter.ts DELETED
@@ -1,12 +0,0 @@
1
- import { useState, useEffect } from "react";
2
-
3
- export function useCounter(initial = 0) {
4
- const [count, setCount] = useState(initial);
5
-
6
- useEffect(() => {
7
- console.log("Counter mounted:", count);
8
- return () => console.log("Counter unmounted");
9
- }, []);
10
-
11
- return { count, setCount };
12
- }