deskcord 0.11.0 → 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 +66 -1
- package/dist/deskcord.min.js +2 -146
- package/package.json +7 -23
package/README.md
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
1
|
# Deskcord widget
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The chat widget customers embed on their site. Preact + plain CSS in a shadow root, one IIFE file (`dist/deskcord.min.js`, ~51 KB gzipped) published to npm as `deskcord` and served from cdn.deskcord.chat.
|
|
4
|
+
|
|
5
|
+
## Embed
|
|
6
|
+
|
|
7
|
+
```html
|
|
8
|
+
<script>
|
|
9
|
+
window.deskcord = {
|
|
10
|
+
settings: {
|
|
11
|
+
app_id: "YOUR_DISCORD_SERVER_ID",
|
|
12
|
+
// theme: "auto", // "light" (default), "dark", or "auto" (follows the visitor's system)
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
</script>
|
|
16
|
+
<script async src="https://cdn.deskcord.chat/deskcord.min.js"></script>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Colors, position, titles and the offline message come from the server's `/customize-widget` settings.
|
|
20
|
+
|
|
21
|
+
`window.deskcord` also exposes `openChatbox()`, `closeChatbox()`, `identify(user)` and `logout()` once the script has loaded.
|
|
22
|
+
|
|
23
|
+
## Identify logged-in users
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
window.deskcord.identify({
|
|
27
|
+
name: "Ada",
|
|
28
|
+
email: "ada@example.com",
|
|
29
|
+
avatarUrl: "https://example.com/ada.png", // optional, https only, shown in Discord
|
|
30
|
+
userId: "42", // optional, with userHash: same conversations on every device
|
|
31
|
+
userHash: "…", // HMAC-SHA256(identity secret, userId), computed on the site's server
|
|
32
|
+
});
|
|
33
|
+
window.deskcord.logout(); // on logout: forgets the user on this browser
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- Skips the "who should we reply to?" step. Without `userId`/`userHash`, conversations stay tied to the browser.
|
|
37
|
+
- The secret comes from `/identity-verification enable` in Discord. It must never reach the browser.
|
|
38
|
+
- To identify before the first render, pass the same object as `window.deskcord.settings.user` in the embed snippet.
|
|
39
|
+
- Server-rendered sites should print `user: null` when nobody is logged in: it ends a verified session left over from a logout that didn't call `logout()`.
|
|
40
|
+
- Call it on every page load: verified sessions last 24h, and one without a token is dropped.
|
|
41
|
+
- Conversations started anonymously in the browser are carried over to the user. Calling `identify()` with another user (or `logout()`) wipes the previous user's conversations from the browser.
|
|
42
|
+
|
|
43
|
+
## Develop
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install
|
|
47
|
+
npm run dev:mock # mock Deskcord API on :3999 (no Discord needed)
|
|
48
|
+
npm run dev # then open http://localhost:5173/?api=mock (&theme=dark|auto)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Without `?api=mock`, the dev page talks to a local API on `http://localhost:10001`.
|
|
52
|
+
|
|
53
|
+
The mock plays the Discord side through `POST localhost:3999/__mock/<action>`: `reply` (`{"text": "…"}`), `typing`, `image`, `edit`, `delete`, `handoff`, `close`, `reopen`, `offline`, `online`, `drop` (cuts the live connection), `auto-reply` (`{"on": false}`), `reset`.
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npm test # unit tests (sync model, persistence, rich text, colors)
|
|
57
|
+
npm run build
|
|
58
|
+
npm run e2e # drives the built widget in headless Chrome against the mock (CHROME=/path/to/chrome)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## How it works
|
|
62
|
+
|
|
63
|
+
- `src/state/session.ts` owns everything that talks to the server: boot, the socket, the outbox, uploads, identify/logout, read state, persistence. The UI only reads the store and calls its actions.
|
|
64
|
+
- The server is the source of truth. A fetched conversation replaces the cached one; messages are matched by Discord id (`src/state/model.ts`), never by guessing from content.
|
|
65
|
+
- Messages being sent live in an outbox, sent one at a time so they reach Discord in order, and survive a reload. Server data that may contain them waits until they're confirmed, so nothing shows twice.
|
|
66
|
+
- After every reconnect (and when the tab comes back after a while) the current conversation is fetched again: events missed while offline aren't lost.
|
|
67
|
+
- Unread state is a single date on the server's clock; the badge is derived from it.
|
|
68
|
+
- Data lives in `localStorage` under `deskcord:<app_id>`. Visitors from widgets before 1.0 (cookie `discord-widget`) are migrated on their first visit.
|