@traiyani/chatsdk-react 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 +57 -279
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,303 +2,81 @@
|
|
|
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
|
+
## Features
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
- Real-time messaging via Socket.IO
|
|
8
|
+
- Product-based conversations with metadata
|
|
9
|
+
- File/image/video uploads and sharing
|
|
10
|
+
- Block/unblock users
|
|
11
|
+
- Internationalization (English + Arabic with RTL)
|
|
12
|
+
- Dark mode support (light / dark / auto)
|
|
13
|
+
- Typing indicators
|
|
14
|
+
- Read receipts and message status
|
|
15
|
+
- Unread message counts
|
|
16
|
+
- Ready-made UI components (Conversation List + Chat Window)
|
|
17
|
+
- Pre-built — works with any bundler, zero config
|
|
10
18
|
|
|
11
|
-
|
|
19
|
+
## Requirements
|
|
12
20
|
|
|
13
|
-
|
|
21
|
+
- **React**: >= 16.8.0
|
|
22
|
+
- **Node.js**: >= 16.0.0
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
14
25
|
|
|
15
26
|
```bash
|
|
16
|
-
npm install react
|
|
27
|
+
npm install @traiyani/chatsdk-react axios socket.io-client
|
|
17
28
|
```
|
|
18
29
|
|
|
19
|
-
|
|
20
|
-
|---|---|
|
|
21
|
-
| `react` | >= 16.8.0 |
|
|
22
|
-
| `react-dom` | >= 16.8.0 |
|
|
23
|
-
| `axios` | >= 0.21.0 |
|
|
24
|
-
| `socket.io-client` | >= 4.0.0 |
|
|
25
|
-
|
|
26
|
-
## Quick Start
|
|
30
|
+
Import the stylesheet in your app's entry point:
|
|
27
31
|
|
|
28
32
|
```tsx
|
|
29
|
-
import React, { useState, useEffect } from 'react';
|
|
30
|
-
import { ChatSDK, ChatWindow, ConversationList } from '@traiyani/chatsdk-react';
|
|
31
33
|
import '@traiyani/chatsdk-react/dist/chatsdk.css';
|
|
32
|
-
|
|
33
|
-
function App() {
|
|
34
|
-
const [currentUser, setCurrentUser] = useState(null);
|
|
35
|
-
const [selectedConversation, setSelectedConversation] = useState(null);
|
|
36
|
-
|
|
37
|
-
useEffect(() => {
|
|
38
|
-
const init = async () => {
|
|
39
|
-
const sdk = ChatSDK.getInstance();
|
|
40
|
-
|
|
41
|
-
await sdk.init({
|
|
42
|
-
apiBaseUrl: 'https://your-chat-api.com',
|
|
43
|
-
appId: 'your-app-id',
|
|
44
|
-
environment: 'production',
|
|
45
|
-
enableLogging: false,
|
|
46
|
-
autoConnect: false,
|
|
47
|
-
});
|
|
48
|
-
|
|
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);
|
|
57
|
-
setCurrentUser(user);
|
|
58
|
-
};
|
|
59
|
-
init();
|
|
60
|
-
}, []);
|
|
61
|
-
|
|
62
|
-
if (!currentUser) return <div>Loading...</div>;
|
|
63
|
-
|
|
64
|
-
return (
|
|
65
|
-
<div style={{ display: 'flex', height: '100vh' }}>
|
|
66
|
-
<ConversationList
|
|
67
|
-
currentUser={currentUser}
|
|
68
|
-
onSelectConversation={setSelectedConversation}
|
|
69
|
-
selectedConversationId={selectedConversation?.id}
|
|
70
|
-
/>
|
|
71
|
-
{selectedConversation && (
|
|
72
|
-
<ChatWindow
|
|
73
|
-
conversation={selectedConversation}
|
|
74
|
-
currentUser={currentUser}
|
|
75
|
-
onClose={() => setSelectedConversation(null)}
|
|
76
|
-
onBack={() => setSelectedConversation(null)}
|
|
77
|
-
/>
|
|
78
|
-
)}
|
|
79
|
-
</div>
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
**Zero build configuration changes required. Works with Vite, webpack, Next.js, Create React App, Parcel, and any React bundler.**
|
|
85
|
-
|
|
86
|
-
## Components
|
|
87
|
-
|
|
88
|
-
### ChatWindow
|
|
89
|
-
|
|
90
|
-
Displays the chat interface with messages, file uploads, block/unblock, and real-time updates.
|
|
91
|
-
|
|
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
|
|
109
|
-
|
|
110
|
-
Displays the list of conversations with search, unread counts, and real-time updates.
|
|
111
|
-
|
|
112
|
-
```tsx
|
|
113
|
-
<ConversationList
|
|
114
|
-
currentUser={currentUser}
|
|
115
|
-
onSelectConversation={handleSelect}
|
|
116
|
-
selectedConversationId={activeConversationId}
|
|
117
|
-
/>
|
|
118
|
-
```
|
|
119
|
-
|
|
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 |
|
|
125
|
-
|
|
126
|
-
### ThemeToggle
|
|
127
|
-
|
|
128
|
-
A button that cycles through light, dark, and auto themes. Must be wrapped in `<ThemeProvider>`.
|
|
129
|
-
|
|
130
|
-
```tsx
|
|
131
|
-
<ThemeProvider>
|
|
132
|
-
<ThemeToggle showLabel />
|
|
133
|
-
</ThemeProvider>
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
| Prop | Type | Required | Description |
|
|
137
|
-
|---|---|---|---|
|
|
138
|
-
| `className` | `string` | No | Additional CSS class name |
|
|
139
|
-
| `showLabel` | `boolean` | No | Show current theme label text |
|
|
140
|
-
|
|
141
|
-
## Hooks
|
|
142
|
-
|
|
143
|
-
### useTheme
|
|
144
|
-
|
|
145
|
-
Theme management (light, dark, auto).
|
|
146
|
-
|
|
147
|
-
```tsx
|
|
148
|
-
import { useTheme } from '@traiyani/chatsdk-react';
|
|
149
|
-
|
|
150
|
-
function MyComponent() {
|
|
151
|
-
const { theme, actualTheme, setTheme, toggleTheme } = useTheme();
|
|
152
|
-
return <button onClick={toggleTheme}>Current: {actualTheme}</button>;
|
|
153
|
-
}
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
### useThemeContext
|
|
157
|
-
|
|
158
|
-
Same API as `useTheme`, but reads from the nearest `<ThemeProvider>` so all descendants share state.
|
|
159
|
-
|
|
160
|
-
```tsx
|
|
161
|
-
import { useThemeContext } from '@traiyani/chatsdk-react';
|
|
162
|
-
|
|
163
|
-
const { theme, actualTheme, setTheme, toggleTheme } = useThemeContext();
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
## Configuration
|
|
167
|
-
|
|
168
|
-
Config values can come from any source — environment variables, server-rendered props, a config file, or hardcoded values:
|
|
169
|
-
|
|
170
|
-
```tsx
|
|
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, ... })
|
|
179
|
-
|
|
180
|
-
// Hardcoded / server-rendered
|
|
181
|
-
await sdk.init({ apiBaseUrl: 'https://chat-api.example.com', ... })
|
|
182
|
-
```
|
|
183
|
-
|
|
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
|
|
200
|
-
|
|
201
|
-
```tsx
|
|
202
|
-
const sdk = ChatSDK.getInstance();
|
|
203
|
-
|
|
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
|
-
);
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
## Manager Classes (Advanced Usage)
|
|
219
|
-
|
|
220
|
-
Access individual managers through the SDK instance for fine-grained control:
|
|
221
|
-
|
|
222
|
-
```tsx
|
|
223
|
-
const sdk = ChatSDK.getInstance();
|
|
224
|
-
|
|
225
|
-
// Users
|
|
226
|
-
const users = await sdk.users.searchUsers({ query: 'john', limit: 10 });
|
|
227
|
-
|
|
228
|
-
// Conversations
|
|
229
|
-
const convos = await sdk.conversations.getConversations({ limit: 20 });
|
|
230
|
-
|
|
231
|
-
// Messages
|
|
232
|
-
const msgs = await sdk.messages.getMessages({ conversationId: 'abc', limit: 50 });
|
|
233
|
-
|
|
234
|
-
// Media
|
|
235
|
-
const result = await sdk.media.uploadMedia({ file, type: 'image', conversationId: 'abc' });
|
|
236
|
-
|
|
237
|
-
// Real-time events
|
|
238
|
-
sdk.socket.on('message_received', (data) => console.log('New message:', data));
|
|
239
34
|
```
|
|
240
35
|
|
|
241
|
-
##
|
|
242
|
-
|
|
243
|
-
```tsx
|
|
244
|
-
import { t, changeLanguage, getCurrentLanguage, isRTL } from '@traiyani/chatsdk-react';
|
|
36
|
+
## Quick Start
|
|
245
37
|
|
|
246
|
-
|
|
247
|
-
|
|
38
|
+
See [DOCUMENTATION.md](./DOCUMENTATION.md) for the complete integration guide covering:
|
|
39
|
+
|
|
40
|
+
1. SDK Initialization
|
|
41
|
+
2. User Authentication (login with `isLoggedinUser = true`)
|
|
42
|
+
3. Verify Other User (with `isLoggedinUser = false`)
|
|
43
|
+
4. Generate `externalGroupId` (GUID for fast room lookup)
|
|
44
|
+
5. Start Conversations (direct + product-based with metadata)
|
|
45
|
+
6. Using Ready-Made UI Components (ConversationList + ChatWindow)
|
|
46
|
+
7. Sending Messages
|
|
47
|
+
8. File Uploads
|
|
48
|
+
9. Unread Message Counts
|
|
49
|
+
10. Block & Unblock Users
|
|
50
|
+
11. Real-Time Events
|
|
51
|
+
12. Theme Support
|
|
52
|
+
13. Internationalization (i18n)
|
|
53
|
+
14. Logout & Cleanup
|
|
54
|
+
15. Troubleshooting
|
|
55
|
+
|
|
56
|
+
## Peer Dependencies
|
|
57
|
+
|
|
58
|
+
| Package | Version |
|
|
59
|
+
|---------|---------|
|
|
60
|
+
| `react` | >= 16.8.0 |
|
|
61
|
+
| `react-dom` | >= 16.8.0 |
|
|
62
|
+
| `axios` | >= 0.21.0 |
|
|
63
|
+
| `socket.io-client` | >= 4.0.0 |
|
|
248
64
|
|
|
249
|
-
|
|
250
|
-
changeLanguage('ar'); // or 'en'
|
|
65
|
+
## What's Inside the Package
|
|
251
66
|
|
|
252
|
-
// Check if RTL
|
|
253
|
-
if (isRTL()) {
|
|
254
|
-
// Apply RTL styles
|
|
255
|
-
}
|
|
256
67
|
```
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
```tsx
|
|
265
|
-
import type {
|
|
266
|
-
ChatSDKUser,
|
|
267
|
-
ChatSDKMessage,
|
|
268
|
-
ChatSDKConversation,
|
|
269
|
-
ChatSDKConfig,
|
|
270
|
-
ProductContext,
|
|
271
|
-
ChatWindowProps,
|
|
272
|
-
ConversationListProps,
|
|
273
|
-
} from '@traiyani/chatsdk-react';
|
|
68
|
+
dist/
|
|
69
|
+
chatsdk-react.mjs – ES module build
|
|
70
|
+
chatsdk-react.cjs – CommonJS build
|
|
71
|
+
chatsdk.css – single merged stylesheet
|
|
72
|
+
index.d.ts – TypeScript declarations
|
|
73
|
+
*.map – source maps
|
|
274
74
|
```
|
|
275
75
|
|
|
276
|
-
## Styling
|
|
277
|
-
|
|
278
|
-
Import the CSS file once in your app entry point:
|
|
279
|
-
|
|
280
|
-
```tsx
|
|
281
|
-
import '@traiyani/chatsdk-react/dist/chatsdk.css';
|
|
282
|
-
```
|
|
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
|
-
|
|
302
76
|
## License
|
|
303
77
|
|
|
304
78
|
MIT
|
|
79
|
+
|
|
80
|
+
## Support
|
|
81
|
+
|
|
82
|
+
- Documentation: [DOCUMENTATION.md](./DOCUMENTATION.md)
|
package/package.json
CHANGED