@waysdrop/chat 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.
package/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # ways-chat
2
+
3
+ Internal chat widget package for Waysdrop. Provides a floating support chat UI backed by Socket.IO — drop it into any React app with a single component.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ This is a private GitHub package. Your machine needs access to the `WaysdropHQ` GitHub org.
10
+
11
+ ```bash
12
+ npm install github:WaysdropHQ/ways-chat#v1.0.0
13
+ ```
14
+
15
+ Pin to a specific tag always. Avoid `#main` in production.
16
+
17
+ **Peer dependencies** — install these if not already in your project:
18
+
19
+ ```bash
20
+ npm install react react-dom
21
+ ```
22
+
23
+ ---
24
+
25
+ ## Usage
26
+
27
+ ```tsx
28
+ import { ChatWidget } from 'ways-chat'
29
+
30
+ export default function App() {
31
+ return (
32
+ <ChatWidget
33
+ config={{
34
+ serverUrl: 'https://socket.waysdrop.com',
35
+ apiUrl: 'https://api.waysdrop.com',
36
+ }}
37
+ />
38
+ )
39
+ }
40
+ ```
41
+
42
+ Mount `<ChatWidget />` once at the root of your app. It renders a floating button and manages its own panel state internally.
43
+
44
+ ---
45
+
46
+ ## Config
47
+
48
+ ```ts
49
+ type ChatConfig = {
50
+ serverUrl: string // Socket.IO server URL
51
+ apiUrl: string // REST base URL (used for file uploads)
52
+ token?: string // JWT for authenticated users — omit for visitor flow
53
+ visitorId?: string // Pass a returning visitor's ID to restore chat history
54
+ }
55
+ ```
56
+
57
+ **Visitor flow** — when `token` is omitted, the widget assigns the user a `visitorId` on first connect and persists it to `localStorage` automatically. On subsequent loads, it reads it back. You can also manage this yourself:
58
+
59
+ ```ts
60
+ import { loadVisitorId, saveVisitorId, clearVisitorId } from 'ways-chat'
61
+
62
+ loadVisitorId() // reads from localStorage
63
+ saveVisitorId(id) // writes to localStorage
64
+ clearVisitorId() // clears — use on logout
65
+ ```
66
+
67
+ **Authenticated flow** — pass a `token` (JWT). The socket server resolves the user from it. No `visitorId` needed.
68
+
69
+ ---
70
+
71
+ ## Hooks
72
+
73
+ ### `useChat(config)`
74
+
75
+ The underlying hook `ChatWidget` uses internally. Expose it if you want to build a custom UI on top of the same socket logic.
76
+
77
+ ```ts
78
+ const {
79
+ status, // 'idle' | 'connecting' | 'connected' | 'error'
80
+ role, // 'VISITOR' | 'USER' | 'ADMIN'
81
+ messages, // ChatMessage[]
82
+ error, // SocketError | null
83
+ visitorId, // string | null
84
+ chatId, // string | null
85
+ visitorInfo, // VisitorInfo | null
86
+ setVisitorInfo,
87
+ sendMessage, // (content: string, info?: VisitorInfo) => void
88
+ sendFile, // (file: File, info?: VisitorInfo) => Promise<void>
89
+ } = useChat(config)
90
+ ```
91
+
92
+ `sendMessage` and `sendFile` accept an optional `VisitorInfo` argument for the first message in a visitor session (name, email, phone). After the first message, `visitorInfo` is stored in the Zustand store and reused automatically.
93
+
94
+ ---
95
+
96
+ ## Types
97
+
98
+ ```ts
99
+ type ChatMessage = {
100
+ id: string
101
+ content?: string | null
102
+ file?: string | null
103
+ externalId?: string | null
104
+ direction: 'INBOUND' | 'OUTBOUND'
105
+ senderRole: 'VISITOR' | 'USER' | 'MEMBER' | 'BOT'
106
+ type: string
107
+ createdAt: string
108
+ }
109
+
110
+ type VisitorInfo = {
111
+ email: string // required
112
+ name?: string
113
+ phone?: string
114
+ }
115
+
116
+ type SocketError = {
117
+ code: number
118
+ message: string
119
+ }
120
+ ```
121
+
122
+ ---
123
+
124
+ ## Versioning
125
+
126
+ After changes, bump the version and push a new tag:
127
+
128
+ ```bash
129
+ npm version patch # bug fix
130
+ npm version minor # new feature
131
+ npm version major # breaking change
132
+
133
+ git push origin main --tags
134
+ ```
135
+
136
+ Consuming projects update their reference:
137
+
138
+ ```json
139
+ "ways-chat": "github:WaysdropHQ/ways-chat#v1.0.1"
140
+ ```
141
+
142
+ ---
143
+
144
+ ## Socket Events
145
+
146
+ For reference — what the widget sends and listens to.
147
+
148
+ | Direction | Event | Payload |
149
+ |------------------|-------------------------|-----------------------------|
150
+ | Client → Server | `support-send-message` | `SupportSendMessageDTO` |
151
+ | Server → Client | `connected` | `ConnectedPayload` |
152
+ | Server → Client | `support-message-sent` | `SupportMessageSentPayload` |
153
+ | Server → Client | `support-new-message` | `SupportNewMessagePayload` |
154
+ | Server → Client | `error` | `SocketError` |