@traiyani/chatsdk-react 1.0.0 → 1.0.1
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 +168 -114
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ChatSDK React SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React SDK for ChatSDK - Real-time chat solution with product-based conversations, Socket.io, instant messaging, and file sharing.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,40 +10,56 @@ npm install @traiyani/chatsdk-react axios socket.io-client
|
|
|
10
10
|
|
|
11
11
|
### Peer Dependencies
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
Ensure your project has these installed:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install react react-dom axios socket.io-client
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
| Dependency | Minimum Version |
|
|
20
|
+
|---|---|
|
|
21
|
+
| `react` | >= 16.8.0 |
|
|
22
|
+
| `react-dom` | >= 16.8.0 |
|
|
23
|
+
| `axios` | >= 0.21.0 |
|
|
24
|
+
| `socket.io-client` | >= 4.0.0 |
|
|
19
25
|
|
|
20
26
|
## Quick Start
|
|
21
27
|
|
|
22
28
|
```tsx
|
|
23
29
|
import React, { useState, useEffect } from 'react';
|
|
24
|
-
import { ChatSDK,
|
|
30
|
+
import { ChatSDK, ChatWindow, ConversationList } from '@traiyani/chatsdk-react';
|
|
25
31
|
import '@traiyani/chatsdk-react/dist/chatsdk.css';
|
|
26
32
|
|
|
27
33
|
function App() {
|
|
28
|
-
const [sdk] = useState(() => ChatSDK.getInstance());
|
|
29
34
|
const [currentUser, setCurrentUser] = useState(null);
|
|
30
35
|
const [selectedConversation, setSelectedConversation] = useState(null);
|
|
31
36
|
|
|
32
37
|
useEffect(() => {
|
|
33
|
-
async
|
|
38
|
+
const init = async () => {
|
|
39
|
+
const sdk = ChatSDK.getInstance();
|
|
40
|
+
|
|
34
41
|
await sdk.init({
|
|
35
|
-
apiBaseUrl: 'https://your-api.
|
|
42
|
+
apiBaseUrl: 'https://your-chat-api.com',
|
|
36
43
|
appId: 'your-app-id',
|
|
37
44
|
environment: 'production',
|
|
45
|
+
enableLogging: false,
|
|
46
|
+
autoConnect: false,
|
|
38
47
|
});
|
|
39
48
|
|
|
40
|
-
const user = await sdk.
|
|
49
|
+
const user = await sdk.chatUsers.authenticate(
|
|
50
|
+
'user-123', // your app's user ID
|
|
51
|
+
'your-app-id', // your ChatSDK app ID
|
|
52
|
+
'John Doe', // display name
|
|
53
|
+
'john@example.com' // email
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
await sdk.connect(user.id);
|
|
41
57
|
setCurrentUser(user);
|
|
42
|
-
}
|
|
58
|
+
};
|
|
43
59
|
init();
|
|
44
60
|
}, []);
|
|
45
61
|
|
|
46
|
-
if (!currentUser) return <div>Loading
|
|
62
|
+
if (!currentUser) return <div>Loading...</div>;
|
|
47
63
|
|
|
48
64
|
return (
|
|
49
65
|
<div style={{ display: 'flex', height: '100vh' }}>
|
|
@@ -52,58 +68,81 @@ function App() {
|
|
|
52
68
|
onSelectConversation={setSelectedConversation}
|
|
53
69
|
selectedConversationId={selectedConversation?.id}
|
|
54
70
|
/>
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
{selectedConversation && (
|
|
72
|
+
<ChatWindow
|
|
73
|
+
conversation={selectedConversation}
|
|
74
|
+
currentUser={currentUser}
|
|
75
|
+
onClose={() => setSelectedConversation(null)}
|
|
76
|
+
onBack={() => setSelectedConversation(null)}
|
|
77
|
+
/>
|
|
78
|
+
)}
|
|
61
79
|
</div>
|
|
62
80
|
);
|
|
63
81
|
}
|
|
64
|
-
|
|
65
|
-
export default App;
|
|
66
82
|
```
|
|
67
83
|
|
|
84
|
+
**Zero build configuration changes required. Works with Vite, webpack, Next.js, Create React App, Parcel, and any React bundler.**
|
|
85
|
+
|
|
68
86
|
## Components
|
|
69
87
|
|
|
70
|
-
###
|
|
88
|
+
### ChatWindow
|
|
71
89
|
|
|
72
|
-
Displays the
|
|
90
|
+
Displays the chat interface with messages, file uploads, block/unblock, and real-time updates.
|
|
73
91
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
92
|
+
```tsx
|
|
93
|
+
<ChatWindow
|
|
94
|
+
conversation={selectedConversation}
|
|
95
|
+
currentUser={currentUser}
|
|
96
|
+
onClose={handleClose}
|
|
97
|
+
onBack={handleBack}
|
|
98
|
+
/>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
| Prop | Type | Required | Description |
|
|
102
|
+
|---|---|---|---|
|
|
103
|
+
| `conversation` | `ChatSDKConversation \| null` | Yes | Active conversation object |
|
|
104
|
+
| `currentUser` | `ChatSDKUser` | Yes | Authenticated user object |
|
|
105
|
+
| `onClose` | `() => void` | No | Called when user clicks close |
|
|
106
|
+
| `onBack` | `() => void` | No | Called when user clicks back (mobile) |
|
|
107
|
+
|
|
108
|
+
### ConversationList
|
|
79
109
|
|
|
80
|
-
|
|
110
|
+
Displays the list of conversations with search, unread counts, and real-time updates.
|
|
81
111
|
|
|
82
|
-
|
|
112
|
+
```tsx
|
|
113
|
+
<ConversationList
|
|
114
|
+
currentUser={currentUser}
|
|
115
|
+
onSelectConversation={handleSelect}
|
|
116
|
+
selectedConversationId={activeConversationId}
|
|
117
|
+
/>
|
|
118
|
+
```
|
|
83
119
|
|
|
84
|
-
| Prop
|
|
85
|
-
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
89
|
-
| `onBack` | `() => void` | No | Called when back button pressed |
|
|
120
|
+
| Prop | Type | Required | Description |
|
|
121
|
+
|---|---|---|---|
|
|
122
|
+
| `currentUser` | `ChatSDKUser` | Yes | Authenticated user object |
|
|
123
|
+
| `onSelectConversation` | `(conv: ChatSDKConversation) => void` | Yes | Called when user selects a conversation |
|
|
124
|
+
| `selectedConversationId` | `string` | No | ID of currently selected conversation |
|
|
90
125
|
|
|
91
|
-
###
|
|
126
|
+
### ThemeToggle
|
|
92
127
|
|
|
93
|
-
A button that cycles through light, dark, and auto themes.
|
|
128
|
+
A button that cycles through light, dark, and auto themes. Must be wrapped in `<ThemeProvider>`.
|
|
94
129
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
130
|
+
```tsx
|
|
131
|
+
<ThemeProvider>
|
|
132
|
+
<ThemeToggle showLabel />
|
|
133
|
+
</ThemeProvider>
|
|
134
|
+
```
|
|
99
135
|
|
|
100
|
-
|
|
136
|
+
| Prop | Type | Required | Description |
|
|
137
|
+
|---|---|---|---|
|
|
138
|
+
| `className` | `string` | No | Additional CSS class name |
|
|
139
|
+
| `showLabel` | `boolean` | No | Show current theme label text |
|
|
101
140
|
|
|
102
141
|
## Hooks
|
|
103
142
|
|
|
104
|
-
###
|
|
143
|
+
### useTheme
|
|
105
144
|
|
|
106
|
-
|
|
145
|
+
Theme management (light, dark, auto).
|
|
107
146
|
|
|
108
147
|
```tsx
|
|
109
148
|
import { useTheme } from '@traiyani/chatsdk-react';
|
|
@@ -114,53 +153,71 @@ function MyComponent() {
|
|
|
114
153
|
}
|
|
115
154
|
```
|
|
116
155
|
|
|
117
|
-
###
|
|
118
|
-
|
|
119
|
-
Same API as `useTheme()`, but reads from the nearest `<ThemeProvider>` so all descendants share state.
|
|
156
|
+
### useThemeContext
|
|
120
157
|
|
|
121
|
-
|
|
158
|
+
Same API as `useTheme`, but reads from the nearest `<ThemeProvider>` so all descendants share state.
|
|
122
159
|
|
|
123
160
|
```tsx
|
|
124
|
-
import {
|
|
161
|
+
import { useThemeContext } from '@traiyani/chatsdk-react';
|
|
125
162
|
|
|
126
|
-
|
|
127
|
-
return (
|
|
128
|
-
<ThemeProvider>
|
|
129
|
-
<ThemeToggle showLabel />
|
|
130
|
-
{/* chat components */}
|
|
131
|
-
</ThemeProvider>
|
|
132
|
-
);
|
|
133
|
-
}
|
|
163
|
+
const { theme, actualTheme, setTheme, toggleTheme } = useThemeContext();
|
|
134
164
|
```
|
|
135
165
|
|
|
136
|
-
|
|
166
|
+
## Configuration
|
|
167
|
+
|
|
168
|
+
Config values can come from any source — environment variables, server-rendered props, a config file, or hardcoded values:
|
|
137
169
|
|
|
138
170
|
```tsx
|
|
139
|
-
|
|
171
|
+
// Create React App
|
|
172
|
+
await sdk.init({ apiBaseUrl: process.env.REACT_APP_API_URL, ... })
|
|
173
|
+
|
|
174
|
+
// Vite
|
|
175
|
+
await sdk.init({ apiBaseUrl: import.meta.env.VITE_API_URL, ... })
|
|
176
|
+
|
|
177
|
+
// Next.js
|
|
178
|
+
await sdk.init({ apiBaseUrl: process.env.NEXT_PUBLIC_API_URL, ... })
|
|
140
179
|
|
|
141
|
-
|
|
180
|
+
// Hardcoded / server-rendered
|
|
181
|
+
await sdk.init({ apiBaseUrl: 'https://chat-api.example.com', ... })
|
|
142
182
|
```
|
|
143
183
|
|
|
144
|
-
|
|
184
|
+
### Full Configuration
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
interface ChatSDKConfig {
|
|
188
|
+
apiBaseUrl: string;
|
|
189
|
+
wsUrl?: string;
|
|
190
|
+
appId: string;
|
|
191
|
+
apiKey?: string;
|
|
192
|
+
environment: 'development' | 'staging' | 'production';
|
|
193
|
+
enableLogging?: boolean;
|
|
194
|
+
autoConnect?: boolean;
|
|
195
|
+
timeout?: number;
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Starting a Product-Based Chat
|
|
145
200
|
|
|
146
201
|
```tsx
|
|
147
202
|
const sdk = ChatSDK.getInstance();
|
|
148
203
|
|
|
149
|
-
await sdk.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
204
|
+
const conversation = await sdk.chatUsers.startChatWithProduct(
|
|
205
|
+
'unique-group-id', // externalGroupId for deduplication
|
|
206
|
+
'seller-user-id', // the other participant's chat user ID
|
|
207
|
+
{
|
|
208
|
+
productId: 'prod-123',
|
|
209
|
+
productName: 'iPhone 15 Pro',
|
|
210
|
+
productImage: 'https://example.com/iphone.jpg',
|
|
211
|
+
price: 4999,
|
|
212
|
+
currency: 'QAR',
|
|
213
|
+
category: 'Electronics',
|
|
214
|
+
}
|
|
215
|
+
);
|
|
159
216
|
```
|
|
160
217
|
|
|
161
|
-
## Manager Classes (Advanced)
|
|
218
|
+
## Manager Classes (Advanced Usage)
|
|
162
219
|
|
|
163
|
-
|
|
220
|
+
Access individual managers through the SDK instance for fine-grained control:
|
|
164
221
|
|
|
165
222
|
```tsx
|
|
166
223
|
const sdk = ChatSDK.getInstance();
|
|
@@ -181,70 +238,67 @@ const result = await sdk.media.uploadMedia({ file, type: 'image', conversationId
|
|
|
181
238
|
sdk.socket.on('message_received', (data) => console.log('New message:', data));
|
|
182
239
|
```
|
|
183
240
|
|
|
184
|
-
|
|
241
|
+
## Internationalization
|
|
185
242
|
|
|
186
243
|
```tsx
|
|
187
|
-
import {
|
|
188
|
-
AuthManager,
|
|
189
|
-
UserManager,
|
|
190
|
-
ConversationManager,
|
|
191
|
-
MessageManager,
|
|
192
|
-
MediaManager,
|
|
193
|
-
EventManager,
|
|
194
|
-
SocketManager,
|
|
195
|
-
} from '@traiyani/chatsdk-react';
|
|
196
|
-
```
|
|
244
|
+
import { t, changeLanguage, getCurrentLanguage, isRTL } from '@traiyani/chatsdk-react';
|
|
197
245
|
|
|
198
|
-
|
|
246
|
+
// Get translated string
|
|
247
|
+
const message = t('welcome_message');
|
|
199
248
|
|
|
200
|
-
|
|
201
|
-
|
|
249
|
+
// Change language
|
|
250
|
+
changeLanguage('ar'); // or 'en'
|
|
202
251
|
|
|
203
|
-
|
|
204
|
-
|
|
252
|
+
// Check if RTL
|
|
253
|
+
if (isRTL()) {
|
|
254
|
+
// Apply RTL styles
|
|
255
|
+
}
|
|
205
256
|
```
|
|
206
257
|
|
|
207
258
|
Supported locales: `en`, `ar`.
|
|
208
259
|
|
|
209
|
-
## TypeScript
|
|
260
|
+
## TypeScript Support
|
|
210
261
|
|
|
211
|
-
Full type
|
|
262
|
+
Full type definitions are included. Import types directly:
|
|
212
263
|
|
|
213
264
|
```tsx
|
|
214
265
|
import type {
|
|
215
|
-
ChatSDKConfig,
|
|
216
266
|
ChatSDKUser,
|
|
217
267
|
ChatSDKMessage,
|
|
218
268
|
ChatSDKConversation,
|
|
269
|
+
ChatSDKConfig,
|
|
219
270
|
ProductContext,
|
|
220
271
|
ChatWindowProps,
|
|
221
272
|
ConversationListProps,
|
|
222
273
|
} from '@traiyani/chatsdk-react';
|
|
223
274
|
```
|
|
224
275
|
|
|
225
|
-
##
|
|
226
|
-
|
|
227
|
-
| Bundler | Works? |
|
|
228
|
-
| ----------------- | ------ |
|
|
229
|
-
| Vite | Yes |
|
|
230
|
-
| webpack / CRA | Yes |
|
|
231
|
-
| Next.js | Yes |
|
|
232
|
-
| Parcel | Yes |
|
|
233
|
-
| Rollup | Yes |
|
|
276
|
+
## Styling
|
|
234
277
|
|
|
235
|
-
|
|
278
|
+
Import the CSS file once in your app entry point:
|
|
236
279
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
```
|
|
240
|
-
dist/
|
|
241
|
-
chatsdk-react.mjs – ES module build
|
|
242
|
-
chatsdk-react.cjs – CommonJS build
|
|
243
|
-
chatsdk.css – single merged stylesheet
|
|
244
|
-
index.d.ts – TypeScript declarations
|
|
245
|
-
*.map – source maps
|
|
280
|
+
```tsx
|
|
281
|
+
import '@traiyani/chatsdk-react/dist/chatsdk.css';
|
|
246
282
|
```
|
|
247
283
|
|
|
284
|
+
All theme colors, conversation styles, and utility classes are bundled into this single file.
|
|
285
|
+
|
|
286
|
+
## Features
|
|
287
|
+
|
|
288
|
+
- React 16.8+ with TypeScript
|
|
289
|
+
- Real-time messaging via Socket.io
|
|
290
|
+
- File/image uploads with progress
|
|
291
|
+
- Product-based conversations
|
|
292
|
+
- Block/unblock users
|
|
293
|
+
- Theme support (light / dark / auto)
|
|
294
|
+
- Internationalization (EN / AR with RTL)
|
|
295
|
+
- Responsive design (desktop + mobile)
|
|
296
|
+
- Message status (sending, sent, delivered, read)
|
|
297
|
+
- Typing indicators
|
|
298
|
+
- Unread message counts with badges
|
|
299
|
+
- Search conversations
|
|
300
|
+
- Pre-built — works with any bundler, no build config needed
|
|
301
|
+
|
|
248
302
|
## License
|
|
249
303
|
|
|
250
304
|
MIT
|
package/package.json
CHANGED