@cstar.help/react 0.1.0 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +220 -0
- package/dist/index.cjs +1 -3
- package/dist/index.js +1 -3
- package/package.json +8 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 cStar
|
|
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,220 @@
|
|
|
1
|
+
# @cstar.help/react
|
|
2
|
+
|
|
3
|
+
React hooks for the [cStar](https://cstar.help) customer support platform. Build custom chat widgets and knowledge base UIs with type-safe, SSR-compatible hooks.
|
|
4
|
+
|
|
5
|
+
- **React 18+** — hooks with automatic loading states and error handling
|
|
6
|
+
- **Two modules** — Chat (real-time messaging) and Library (knowledge base)
|
|
7
|
+
- **TypeScript-first** — full type definitions included
|
|
8
|
+
- **Tiny** — ~18 KB, tree-shakeable, only peer deps
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @cstar.help/react @cstar.help/js
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Chat
|
|
17
|
+
|
|
18
|
+
Wrap your app in `<CStarChatProvider>` and use hooks anywhere inside.
|
|
19
|
+
|
|
20
|
+
### Setup
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
import { CStarChatProvider } from '@cstar.help/react';
|
|
24
|
+
|
|
25
|
+
function App() {
|
|
26
|
+
return (
|
|
27
|
+
<CStarChatProvider teamSlug="acme" supabaseUrl="..." supabaseAnonKey="...">
|
|
28
|
+
<ChatWidget />
|
|
29
|
+
</CStarChatProvider>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Identify the Customer
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { useChat } from '@cstar.help/react';
|
|
38
|
+
|
|
39
|
+
function ChatWidget() {
|
|
40
|
+
const { identify, isIdentified, isRealtimeReady, error } = useChat();
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
identify(
|
|
44
|
+
{ externalId: 'usr_123', email: 'jane@acme.com', timestamp: Math.floor(Date.now() / 1000) },
|
|
45
|
+
hmacSignature // computed server-side
|
|
46
|
+
);
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
if (!isIdentified) return <p>Connecting...</p>;
|
|
50
|
+
return <ConversationList />;
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Conversations
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { useConversations } from '@cstar.help/react';
|
|
58
|
+
|
|
59
|
+
function ConversationList() {
|
|
60
|
+
const { conversations, isLoading, create, refresh, hasMore } = useConversations();
|
|
61
|
+
|
|
62
|
+
if (isLoading) return <Spinner />;
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<ul>
|
|
66
|
+
{conversations.map((c) => (
|
|
67
|
+
<li key={c.id}>{c.subject}</li>
|
|
68
|
+
))}
|
|
69
|
+
</ul>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Messages + Typing Indicators
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
import { useMessages, useTyping } from '@cstar.help/react';
|
|
78
|
+
|
|
79
|
+
function ChatWindow({ ticketId }) {
|
|
80
|
+
const { messages, isLoading, send } = useMessages(ticketId);
|
|
81
|
+
const { typingAgents, sendTyping } = useTyping(ticketId);
|
|
82
|
+
const [text, setText] = useState('');
|
|
83
|
+
|
|
84
|
+
const handleSend = async () => {
|
|
85
|
+
await send(text); // optimistic — appears instantly
|
|
86
|
+
setText('');
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<>
|
|
91
|
+
{messages.map((msg) => (
|
|
92
|
+
<div key={msg.id}>{msg.content}</div>
|
|
93
|
+
))}
|
|
94
|
+
{typingAgents.map((a) => (
|
|
95
|
+
<p key={a.agentId}>{a.agentName} is typing...</p>
|
|
96
|
+
))}
|
|
97
|
+
<input
|
|
98
|
+
value={text}
|
|
99
|
+
onChange={(e) => {
|
|
100
|
+
setText(e.target.value);
|
|
101
|
+
sendTyping(e.target.value.length > 0);
|
|
102
|
+
}}
|
|
103
|
+
onKeyDown={(e) => e.key === 'Enter' && handleSend()}
|
|
104
|
+
/>
|
|
105
|
+
</>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Knowledge Base
|
|
111
|
+
|
|
112
|
+
Wrap in `<CStarLibraryProvider>` for public knowledge base access — no auth required.
|
|
113
|
+
|
|
114
|
+
### Setup
|
|
115
|
+
|
|
116
|
+
```tsx
|
|
117
|
+
import { CStarLibraryProvider } from '@cstar.help/react';
|
|
118
|
+
|
|
119
|
+
function App() {
|
|
120
|
+
return (
|
|
121
|
+
<CStarLibraryProvider teamSlug="acme">
|
|
122
|
+
<HelpCenter />
|
|
123
|
+
</CStarLibraryProvider>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Categories + Articles
|
|
129
|
+
|
|
130
|
+
```tsx
|
|
131
|
+
import { useCategories, useArticles, useArticle } from '@cstar.help/react';
|
|
132
|
+
|
|
133
|
+
function HelpCenter() {
|
|
134
|
+
const { categories, isLoading: catsLoading } = useCategories();
|
|
135
|
+
const { articles, isLoading: artsLoading } = useArticles({ categorySlug: 'getting-started' });
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<>
|
|
139
|
+
<nav>
|
|
140
|
+
{categories.map((cat) => (
|
|
141
|
+
<a key={cat.id}>{cat.name}</a>
|
|
142
|
+
))}
|
|
143
|
+
</nav>
|
|
144
|
+
<ul>
|
|
145
|
+
{articles.map((a) => (
|
|
146
|
+
<li key={a.id}>{a.title}</li>
|
|
147
|
+
))}
|
|
148
|
+
</ul>
|
|
149
|
+
</>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Single article by slug
|
|
154
|
+
function ArticlePage({ slug }) {
|
|
155
|
+
const { article, isLoading, error } = useArticle(slug);
|
|
156
|
+
if (isLoading) return <Spinner />;
|
|
157
|
+
if (!article) return <NotFound />;
|
|
158
|
+
return <h1>{article.title}</h1>;
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Search with Debouncing
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
import { useArticleSearch } from '@cstar.help/react';
|
|
166
|
+
|
|
167
|
+
function SearchBar() {
|
|
168
|
+
const [query, setQuery] = useState('');
|
|
169
|
+
const { results, totalCount, isLoading } = useArticleSearch(query); // 300ms debounce built-in
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<>
|
|
173
|
+
<input value={query} onChange={(e) => setQuery(e.target.value)} placeholder="Search..." />
|
|
174
|
+
{isLoading && <Spinner />}
|
|
175
|
+
{results.map((a) => (
|
|
176
|
+
<a key={a.id} href={`/articles/${a.slug}`}>
|
|
177
|
+
{a.title}
|
|
178
|
+
</a>
|
|
179
|
+
))}
|
|
180
|
+
</>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## API Reference
|
|
186
|
+
|
|
187
|
+
### Chat Hooks
|
|
188
|
+
|
|
189
|
+
| Hook | Returns | Description |
|
|
190
|
+
| ----------------------- | ---------------------------------------------------------------- | ---------------------------------------------------- |
|
|
191
|
+
| `useChat()` | `{ identify, disconnect, isIdentified, isRealtimeReady, error }` | Identity verification and connection state |
|
|
192
|
+
| `useConversations()` | `{ conversations, isLoading, error, hasMore, refresh, create }` | List and create conversations |
|
|
193
|
+
| `useMessages(ticketId)` | `{ messages, isLoading, error, send, refresh }` | Messages with real-time updates and optimistic send |
|
|
194
|
+
| `useTyping(ticketId)` | `{ typingAgents, sendTyping }` | Agent typing indicators with auto-clear (4s timeout) |
|
|
195
|
+
|
|
196
|
+
### Library Hooks
|
|
197
|
+
|
|
198
|
+
| Hook | Returns | Description |
|
|
199
|
+
| ------------------------- | ------------------------------------------- | ----------------------------------------- |
|
|
200
|
+
| `useCategories()` | `{ categories, isLoading, error, refresh }` | Knowledge base categories |
|
|
201
|
+
| `useArticles(params?)` | `{ articles, isLoading, error, refresh }` | Articles, optionally filtered by category |
|
|
202
|
+
| `useArticle(slug)` | `{ article, isLoading, error, refresh }` | Single article by slug |
|
|
203
|
+
| `useArticleSearch(query)` | `{ results, totalCount, isLoading, error }` | Search with 300ms debounce |
|
|
204
|
+
|
|
205
|
+
### Context Accessors
|
|
206
|
+
|
|
207
|
+
| Function | Returns | Description |
|
|
208
|
+
| -------------------- | --------------- | ---------------------------------------------- |
|
|
209
|
+
| `useChatClient()` | `ChatClient` | Raw client from nearest `CStarChatProvider` |
|
|
210
|
+
| `useLibraryClient()` | `LibraryClient` | Raw client from nearest `CStarLibraryProvider` |
|
|
211
|
+
|
|
212
|
+
## Requirements
|
|
213
|
+
|
|
214
|
+
- React 18+
|
|
215
|
+
- @cstar.help/js 0.1.0+
|
|
216
|
+
- Node.js 18+ (for SSR)
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -231,9 +231,7 @@ function useMessages(ticketId) {
|
|
|
231
231
|
try {
|
|
232
232
|
const msg = await client.messages.send(ticketId, content);
|
|
233
233
|
seenIdsRef.current.add(msg.id);
|
|
234
|
-
setMessages(
|
|
235
|
-
(prev) => prev.map((m) => m.id === optimistic.id ? msg : m)
|
|
236
|
-
);
|
|
234
|
+
setMessages((prev) => prev.map((m) => m.id === optimistic.id ? msg : m));
|
|
237
235
|
return msg;
|
|
238
236
|
} catch (err) {
|
|
239
237
|
setMessages((prev) => prev.filter((m) => m.id !== optimistic.id));
|
package/dist/index.js
CHANGED
|
@@ -192,9 +192,7 @@ function useMessages(ticketId) {
|
|
|
192
192
|
try {
|
|
193
193
|
const msg = await client.messages.send(ticketId, content);
|
|
194
194
|
seenIdsRef.current.add(msg.id);
|
|
195
|
-
setMessages(
|
|
196
|
-
(prev) => prev.map((m) => m.id === optimistic.id ? msg : m)
|
|
197
|
-
);
|
|
195
|
+
setMessages((prev) => prev.map((m) => m.id === optimistic.id ? msg : m));
|
|
198
196
|
return msg;
|
|
199
197
|
} catch (err) {
|
|
200
198
|
setMessages((prev) => prev.filter((m) => m.id !== optimistic.id));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cstar.help/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "React hooks for the cStar customer support platform",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"react": ">=18.0.0",
|
|
33
|
-
"@cstar.help/js": ">=0.
|
|
33
|
+
"@cstar.help/js": ">=0.2.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"react": "^18.3.1",
|
|
@@ -48,9 +48,14 @@
|
|
|
48
48
|
"engines": {
|
|
49
49
|
"node": ">=18.0.0"
|
|
50
50
|
},
|
|
51
|
+
"homepage": "https://cstar.help/developers/sdk/react",
|
|
51
52
|
"repository": {
|
|
52
53
|
"type": "git",
|
|
53
|
-
"url": "https://github.com/cstar-help/cstar"
|
|
54
|
+
"url": "https://github.com/cstar-help/cstar",
|
|
55
|
+
"directory": "packages/react"
|
|
56
|
+
},
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/cstar-help/cstar/issues"
|
|
54
59
|
},
|
|
55
60
|
"keywords": [
|
|
56
61
|
"cstar",
|