jibtalk-react 1.0.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.
@@ -0,0 +1,25 @@
1
+ import React from 'react';
2
+ import type { JibTalkUser, JibTalkOptions } from 'jibtalk-core';
3
+ export declare function useJibTalk(): {
4
+ isOpen: boolean;
5
+ isConnected: boolean;
6
+ unreadCount: number;
7
+ currentConversation: any;
8
+ messages: any[];
9
+ open: () => void | undefined;
10
+ close: () => void | undefined;
11
+ toggle: () => void | undefined;
12
+ destroy: () => void | undefined;
13
+ identify: (user: JibTalkUser) => void | undefined;
14
+ setUser: (user: JibTalkUser) => void | undefined;
15
+ logout: () => void | undefined;
16
+ sendMessage: (content: string) => void | undefined;
17
+ startTyping: () => void | undefined;
18
+ stopTyping: () => void | undefined;
19
+ on: (event: any, cb: Function) => void | undefined;
20
+ off: (event: any, cb: Function) => void | undefined;
21
+ };
22
+ export interface JibTalkProps extends JibTalkOptions {
23
+ user?: JibTalkUser;
24
+ }
25
+ export declare const JibTalk: React.FC<JibTalkProps>;
@@ -0,0 +1,148 @@
1
+ import { useEffect as e, useRef as t, useState as n } from "react";
2
+ import { getJibTalkInstance as r, injectStyles as i } from "jibtalk-core";
3
+ import { jsx as a, jsxs as o } from "react/jsx-runtime";
4
+ //#region src/index.tsx
5
+ function s() {
6
+ let [t, i] = n(() => {
7
+ let e = r();
8
+ return {
9
+ isOpen: e?.isOpen || !1,
10
+ isConnected: e?.isConnected || !1,
11
+ unreadCount: e?.unreadCount || 0,
12
+ currentConversation: e?.currentConversation || null,
13
+ messages: e?.messages || []
14
+ };
15
+ });
16
+ e(() => {
17
+ let e = r();
18
+ if (!e) return;
19
+ let t = (e) => {
20
+ i({
21
+ isOpen: e.isOpen,
22
+ isConnected: e.isConnected,
23
+ unreadCount: e.unreadCount,
24
+ currentConversation: e.currentConversation,
25
+ messages: [...e.messages]
26
+ });
27
+ };
28
+ return e.on("stateChange", t), t(e), () => {
29
+ e.off("stateChange", t);
30
+ };
31
+ }, []);
32
+ let a = r();
33
+ return {
34
+ isOpen: t.isOpen,
35
+ isConnected: t.isConnected,
36
+ unreadCount: t.unreadCount,
37
+ currentConversation: t.currentConversation,
38
+ messages: t.messages,
39
+ open: () => a?.open(),
40
+ close: () => a?.close(),
41
+ toggle: () => a?.toggle(),
42
+ destroy: () => a?.destroy(),
43
+ identify: (e) => a?.identify(e),
44
+ setUser: (e) => a?.setUser(e),
45
+ logout: () => a?.logout(),
46
+ sendMessage: (e) => a?.sendMessage(e),
47
+ startTyping: () => a?.startTyping(),
48
+ stopTyping: () => a?.stopTyping(),
49
+ on: (e, t) => a?.on(e, t),
50
+ off: (e, t) => a?.off(e, t)
51
+ };
52
+ }
53
+ var c = (c) => {
54
+ let [l, u] = n(""), d = t(null), f = t(null), p = t(!1);
55
+ e(() => {
56
+ i();
57
+ let e = r(c);
58
+ e && c.user && e.identify(c.user, c.timestamp, c.signature);
59
+ }, [
60
+ c.apiKey,
61
+ c.theme,
62
+ c.position,
63
+ c.primaryColor,
64
+ c.locale,
65
+ c.user,
66
+ c.timestamp,
67
+ c.signature
68
+ ]), e(() => () => {
69
+ r()?.destroy();
70
+ }, []);
71
+ let { isOpen: m, unreadCount: h, messages: g, toggle: _, sendMessage: v, startTyping: y, stopTyping: b } = s();
72
+ e(() => {
73
+ m && d.current?.scrollIntoView({ behavior: "smooth" });
74
+ }, [g, m]);
75
+ let x = () => {
76
+ l.trim() && (v(l), u(""), p.current &&= (b(), !1), f.current && clearTimeout(f.current));
77
+ };
78
+ return e(() => () => {
79
+ f.current && clearTimeout(f.current);
80
+ }, []), /* @__PURE__ */ a("div", {
81
+ id: "jibtalk-sdk-root",
82
+ className: `position-${c.position || "bottom-right"}`,
83
+ "data-theme": c.theme || "dark",
84
+ style: { "--primary": c.primaryColor || "#00AC78" },
85
+ children: m ? /* @__PURE__ */ o("div", {
86
+ className: "jibtalk-sdk-window",
87
+ children: [
88
+ /* @__PURE__ */ o("div", {
89
+ className: "jibtalk-sdk-header",
90
+ children: [/* @__PURE__ */ a("h4", { children: c.locale === "fa" ? "پشتیبانی" : "Support" }), /* @__PURE__ */ a("button", {
91
+ className: "jibtalk-sdk-close-btn",
92
+ onClick: _,
93
+ "aria-label": "Close support chat",
94
+ children: "×"
95
+ })]
96
+ }),
97
+ /* @__PURE__ */ o("div", {
98
+ className: "jibtalk-sdk-messages",
99
+ children: [g.map((e, t) => /* @__PURE__ */ a("div", {
100
+ className: `jibtalk-sdk-message ${e.sender === "visitor" ? "jibtalk-sdk-message-self" : "jibtalk-sdk-message-other"}`,
101
+ children: e.content
102
+ }, e.id || t)), /* @__PURE__ */ a("div", { ref: d })]
103
+ }),
104
+ /* @__PURE__ */ o("div", {
105
+ className: "jibtalk-sdk-input-area",
106
+ children: [/* @__PURE__ */ a("input", {
107
+ type: "text",
108
+ className: "jibtalk-sdk-input",
109
+ placeholder: c.locale === "fa" ? "پیام خود را بنویسید..." : "Type your message...",
110
+ value: l,
111
+ onChange: (e) => {
112
+ u(e.target.value), p.current ||= (y(), !0), f.current && clearTimeout(f.current), f.current = setTimeout(() => {
113
+ b(), p.current = !1;
114
+ }, 2e3);
115
+ },
116
+ onKeyPress: (e) => {
117
+ e.key === "Enter" && x();
118
+ }
119
+ }), /* @__PURE__ */ a("button", {
120
+ className: "jibtalk-sdk-send-btn",
121
+ onClick: x,
122
+ children: c.locale === "fa" ? "ارسال" : "Send"
123
+ })]
124
+ })
125
+ ]
126
+ }) : /* @__PURE__ */ o("button", {
127
+ className: "jibtalk-sdk-btn",
128
+ onClick: _,
129
+ "aria-label": "Open support chat",
130
+ children: [/* @__PURE__ */ a("svg", {
131
+ width: "24",
132
+ height: "24",
133
+ viewBox: "0 0 24 24",
134
+ fill: "none",
135
+ stroke: "currentColor",
136
+ strokeWidth: "2",
137
+ strokeLinecap: "round",
138
+ strokeLinejoin: "round",
139
+ children: /* @__PURE__ */ a("path", { d: "M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" })
140
+ }), h > 0 && /* @__PURE__ */ a("div", {
141
+ className: "jibtalk-sdk-unread-badge",
142
+ children: h
143
+ })]
144
+ })
145
+ });
146
+ };
147
+ //#endregion
148
+ export { c as JibTalk, s as useJibTalk };
@@ -0,0 +1 @@
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("react"),require("jibtalk-core"),require("react/jsx-runtime")):typeof define==`function`&&define.amd?define([`exports`,`react`,`jibtalk-core`,`react/jsx-runtime`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.JibTalkReact={},e.React,e.JibTalkCore,e.JSX))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var i=Object.create,a=Object.defineProperty,o=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,c=Object.getPrototypeOf,l=Object.prototype.hasOwnProperty,u=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=s(t),c=0,u=i.length,d;c<u;c++)d=i[c],!l.call(e,d)&&d!==n&&a(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=o(t,d))||r.enumerable});return e};t=((e,t,n)=>(n=e==null?{}:i(c(e)),u(t||!e||!e.__esModule?a(n,`default`,{value:e,enumerable:!0}):n,e)))(t,1);function d(){let[e,r]=(0,t.useState)(()=>{let e=(0,n.getJibTalkInstance)();return{isOpen:e?.isOpen||!1,isConnected:e?.isConnected||!1,unreadCount:e?.unreadCount||0,currentConversation:e?.currentConversation||null,messages:e?.messages||[]}});(0,t.useEffect)(()=>{let e=(0,n.getJibTalkInstance)();if(!e)return;let t=e=>{r({isOpen:e.isOpen,isConnected:e.isConnected,unreadCount:e.unreadCount,currentConversation:e.currentConversation,messages:[...e.messages]})};return e.on(`stateChange`,t),t(e),()=>{e.off(`stateChange`,t)}},[]);let i=(0,n.getJibTalkInstance)();return{isOpen:e.isOpen,isConnected:e.isConnected,unreadCount:e.unreadCount,currentConversation:e.currentConversation,messages:e.messages,open:()=>i?.open(),close:()=>i?.close(),toggle:()=>i?.toggle(),destroy:()=>i?.destroy(),identify:e=>i?.identify(e),setUser:e=>i?.setUser(e),logout:()=>i?.logout(),sendMessage:e=>i?.sendMessage(e),startTyping:()=>i?.startTyping(),stopTyping:()=>i?.stopTyping(),on:(e,t)=>i?.on(e,t),off:(e,t)=>i?.off(e,t)}}e.JibTalk=e=>{let[i,a]=(0,t.useState)(``),o=(0,t.useRef)(null),s=(0,t.useRef)(null),c=(0,t.useRef)(!1);(0,t.useEffect)(()=>{(0,n.injectStyles)();let t=(0,n.getJibTalkInstance)(e);t&&e.user&&t.identify(e.user,e.timestamp,e.signature)},[e.apiKey,e.theme,e.position,e.primaryColor,e.locale,e.user,e.timestamp,e.signature]),(0,t.useEffect)(()=>()=>{(0,n.getJibTalkInstance)()?.destroy()},[]);let{isOpen:l,unreadCount:u,messages:f,toggle:p,sendMessage:m,startTyping:h,stopTyping:g}=d();(0,t.useEffect)(()=>{l&&o.current?.scrollIntoView({behavior:`smooth`})},[f,l]);let _=()=>{i.trim()&&(m(i),a(``),c.current&&=(g(),!1),s.current&&clearTimeout(s.current))};return(0,t.useEffect)(()=>()=>{s.current&&clearTimeout(s.current)},[]),(0,r.jsx)(`div`,{id:`jibtalk-sdk-root`,className:`position-${e.position||`bottom-right`}`,"data-theme":e.theme||`dark`,style:{"--primary":e.primaryColor||`#00AC78`},children:l?(0,r.jsxs)(`div`,{className:`jibtalk-sdk-window`,children:[(0,r.jsxs)(`div`,{className:`jibtalk-sdk-header`,children:[(0,r.jsx)(`h4`,{children:e.locale===`fa`?`پشتیبانی`:`Support`}),(0,r.jsx)(`button`,{className:`jibtalk-sdk-close-btn`,onClick:p,"aria-label":`Close support chat`,children:`×`})]}),(0,r.jsxs)(`div`,{className:`jibtalk-sdk-messages`,children:[f.map((e,t)=>(0,r.jsx)(`div`,{className:`jibtalk-sdk-message ${e.sender===`visitor`?`jibtalk-sdk-message-self`:`jibtalk-sdk-message-other`}`,children:e.content},e.id||t)),(0,r.jsx)(`div`,{ref:o})]}),(0,r.jsxs)(`div`,{className:`jibtalk-sdk-input-area`,children:[(0,r.jsx)(`input`,{type:`text`,className:`jibtalk-sdk-input`,placeholder:e.locale===`fa`?`پیام خود را بنویسید...`:`Type your message...`,value:i,onChange:e=>{a(e.target.value),c.current||=(h(),!0),s.current&&clearTimeout(s.current),s.current=setTimeout(()=>{g(),c.current=!1},2e3)},onKeyPress:e=>{e.key===`Enter`&&_()}}),(0,r.jsx)(`button`,{className:`jibtalk-sdk-send-btn`,onClick:_,children:e.locale===`fa`?`ارسال`:`Send`})]})]}):(0,r.jsxs)(`button`,{className:`jibtalk-sdk-btn`,onClick:p,"aria-label":`Open support chat`,children:[(0,r.jsx)(`svg`,{width:`24`,height:`24`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,strokeLinecap:`round`,strokeLinejoin:`round`,children:(0,r.jsx)(`path`,{d:`M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z`})}),u>0&&(0,r.jsx)(`div`,{className:`jibtalk-sdk-unread-badge`,children:u})]})})},e.useJibTalk=d});
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "jibtalk-react",
3
+ "version": "1.0.0",
4
+ "sideEffects": false,
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "type": "module",
9
+ "main": "./dist/jibtalk-react.umd.cjs",
10
+ "module": "./dist/jibtalk-react.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/jibtalk-react.js",
16
+ "require": "./dist/jibtalk-react.umd.cjs"
17
+ }
18
+ },
19
+ "scripts": {
20
+ "build": "vite build && tsc --declaration --emitDeclarationOnly --noEmit false --outDir dist --rootDir src"
21
+ },
22
+ "peerDependencies": {
23
+ "react": ">=18.0.0",
24
+ "react-dom": ">=18.0.0"
25
+ },
26
+ "dependencies": {
27
+ "jibtalk-core": "^0.1.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/react": "^19.0.0",
31
+ "@types/react-dom": "^19.0.0",
32
+ "typescript": "~6.0.2",
33
+ "vite": "^8.0.12"
34
+ }
35
+ }