@callembed/react 0.0.0-stage → 0.1.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 +92 -2
- package/dist/index.cjs +371 -0
- package/dist/index.d.cts +86 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.js +350 -0
- package/dist/styles.css +89 -0
- package/package.json +85 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sealby
|
|
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
CHANGED
|
@@ -1,3 +1,93 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@callembed/react`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React components and hooks for [CallEmbed](https://callembed.sealby.me) — a **thin wrapper** over [`@callembed/sdk`](../sdk).
|
|
4
|
+
|
|
5
|
+
**Product model:** customer-service queue only. An end user on a web page clicks “talk to a human” → rings a project queue → an agent answers. This package does **not** implement person-to-person calling.
|
|
6
|
+
|
|
7
|
+
Call / auth / LiveKit connection logic stays in `@callembed/sdk`. Remote audio is already handled by the headless SDK (`hidePanel: true`), so this package does not pull in `@livekit/components-react` by default.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @callembed/react @callembed/sdk livekit-client react react-dom
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Peer dependencies: `react`, `react-dom`, `@callembed/sdk`, and `livekit-client` (optional peer; required transitively by the SDK).
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
"use client"; // Next.js App Router
|
|
21
|
+
|
|
22
|
+
import { CallEmbedProvider, CallEmbedButton } from "@callembed/react";
|
|
23
|
+
import "@callembed/react/styles.css";
|
|
24
|
+
|
|
25
|
+
export function SupportCall() {
|
|
26
|
+
return (
|
|
27
|
+
<CallEmbedProvider
|
|
28
|
+
publishableKey="pk_…"
|
|
29
|
+
apiBaseUrl=""
|
|
30
|
+
locale="en"
|
|
31
|
+
queue="onboarding"
|
|
32
|
+
>
|
|
33
|
+
<CallEmbedButton />
|
|
34
|
+
</CallEmbedProvider>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Hook
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { useCallEmbed } from "@callembed/react";
|
|
43
|
+
|
|
44
|
+
function Controls() {
|
|
45
|
+
const { uiState, status, muted, start, end, setMuted } = useCallEmbed();
|
|
46
|
+
// uiState: idle | ringing | connected | ended | error
|
|
47
|
+
// status: idle | connecting | ringing | answered | ended | error (SDK)
|
|
48
|
+
return (
|
|
49
|
+
<>
|
|
50
|
+
<button type="button" onClick={() => void start({ queue: "support" })}>
|
|
51
|
+
Talk
|
|
52
|
+
</button>
|
|
53
|
+
<button type="button" onClick={() => void end()}>
|
|
54
|
+
Hang up
|
|
55
|
+
</button>
|
|
56
|
+
<button type="button" onClick={() => void setMuted(!muted)}>
|
|
57
|
+
{muted ? "Unmute" : "Mute"}
|
|
58
|
+
</button>
|
|
59
|
+
<span>{uiState}</span>
|
|
60
|
+
</>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
| Export | Role |
|
|
68
|
+
|--------|------|
|
|
69
|
+
| `CallEmbedProvider` | Calls `CallEmbed.init` in `useEffect` (SSR-safe); provides context |
|
|
70
|
+
| `CallEmbedButton` | Start / end button + optional mute + status line (en / zh-TW) |
|
|
71
|
+
| `useCallEmbed()` | `{ status, uiState, callId, muted, error, start, end, setMuted, … }` |
|
|
72
|
+
|
|
73
|
+
`uiState` maps SDK `answered` → `connected`, and folds `connecting` into `ringing` for button UX. Locale strings match `@callembed/sdk/ui` (`en` + `zh-TW`).
|
|
74
|
+
|
|
75
|
+
Theme: same `--ce-*` CSS variables as the vanilla SDK UI.
|
|
76
|
+
|
|
77
|
+
## SSR / Next.js
|
|
78
|
+
|
|
79
|
+
- Entry is marked `"use client"`.
|
|
80
|
+
- No `window` / `document` access at import time; init runs in `useEffect`.
|
|
81
|
+
- Import as a Client Component from Server Components.
|
|
82
|
+
|
|
83
|
+
## Builds
|
|
84
|
+
|
|
85
|
+
Emits ESM (`dist/index.js`), CJS (`dist/index.cjs`), and TypeScript declarations (`dist/index.d.ts`), plus `dist/styles.css`.
|
|
86
|
+
|
|
87
|
+
## Minimal example
|
|
88
|
+
|
|
89
|
+
See [`examples/react-vite`](../../examples/react-vite) in this repo.
|
|
90
|
+
|
|
91
|
+
## Publishing
|
|
92
|
+
|
|
93
|
+
Published via the multi-package tag workflow (see [`docs/RELEASING.md`](../../docs/RELEASING.md)). Local allowlist check: `npm run pack:verify -w @callembed/react`.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
"use client";
|
|
3
|
+
|
|
4
|
+
var sdk = require('@callembed/sdk');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
7
|
+
var ui = require('@callembed/sdk/ui');
|
|
8
|
+
|
|
9
|
+
// src/context.tsx
|
|
10
|
+
|
|
11
|
+
// src/types.ts
|
|
12
|
+
function toUiState(status) {
|
|
13
|
+
switch (status) {
|
|
14
|
+
case "connecting":
|
|
15
|
+
case "ringing":
|
|
16
|
+
return "ringing";
|
|
17
|
+
case "answered":
|
|
18
|
+
return "connected";
|
|
19
|
+
case "ended":
|
|
20
|
+
return "ended";
|
|
21
|
+
case "error":
|
|
22
|
+
return "error";
|
|
23
|
+
case "idle":
|
|
24
|
+
default:
|
|
25
|
+
return "idle";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
var CallEmbedReactContext = react.createContext(null);
|
|
29
|
+
function toErrorState(err) {
|
|
30
|
+
if (err instanceof sdk.CallEmbedError) {
|
|
31
|
+
return { code: err.code, message: err.message };
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
code: "call_failed",
|
|
35
|
+
message: err instanceof Error ? err.message : String(err)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function isActiveStatus(status) {
|
|
39
|
+
return status === "connecting" || status === "ringing" || status === "answered";
|
|
40
|
+
}
|
|
41
|
+
function bindCall(call, handlers) {
|
|
42
|
+
const offs = [
|
|
43
|
+
call.on("statusChange", ({ status }) => {
|
|
44
|
+
handlers.setStatus(status);
|
|
45
|
+
if (call.callId && call.callId !== "pending") {
|
|
46
|
+
handlers.setCallId(call.callId);
|
|
47
|
+
}
|
|
48
|
+
}),
|
|
49
|
+
call.on("muteChange", ({ muted }) => {
|
|
50
|
+
handlers.setMuted(muted);
|
|
51
|
+
}),
|
|
52
|
+
call.on("error", ({ code, message, callId }) => {
|
|
53
|
+
handlers.setError({ code, message });
|
|
54
|
+
handlers.setStatus("error");
|
|
55
|
+
if (callId && callId !== "pending") handlers.setCallId(callId);
|
|
56
|
+
}),
|
|
57
|
+
call.on("ended", ({ callId }) => {
|
|
58
|
+
handlers.setStatus("ended");
|
|
59
|
+
if (callId && callId !== "pending") handlers.setCallId(callId);
|
|
60
|
+
})
|
|
61
|
+
];
|
|
62
|
+
return () => {
|
|
63
|
+
offs.forEach((off) => off());
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function CallEmbedProvider({
|
|
67
|
+
children,
|
|
68
|
+
publishableKey,
|
|
69
|
+
apiBaseUrl,
|
|
70
|
+
locale,
|
|
71
|
+
copy,
|
|
72
|
+
branding,
|
|
73
|
+
debug,
|
|
74
|
+
queue
|
|
75
|
+
}) {
|
|
76
|
+
const [status, setStatus] = react.useState("idle");
|
|
77
|
+
const [callId, setCallId] = react.useState(null);
|
|
78
|
+
const [muted, setMutedState] = react.useState(false);
|
|
79
|
+
const [error, setError] = react.useState(null);
|
|
80
|
+
const [resolvedLocale, setResolvedLocale] = react.useState(locale ?? "en");
|
|
81
|
+
const callRef = react.useRef(null);
|
|
82
|
+
const unbindRef = react.useRef(null);
|
|
83
|
+
const copyKey = JSON.stringify(copy ?? {});
|
|
84
|
+
const hangUpActive = react.useCallback(() => {
|
|
85
|
+
unbindRef.current?.();
|
|
86
|
+
unbindRef.current = null;
|
|
87
|
+
const call = callRef.current ?? sdk.CallEmbed.getActiveCall();
|
|
88
|
+
callRef.current = null;
|
|
89
|
+
if (call && isActiveStatus(call.status)) {
|
|
90
|
+
void call.hangUp();
|
|
91
|
+
}
|
|
92
|
+
}, []);
|
|
93
|
+
react.useEffect(() => {
|
|
94
|
+
try {
|
|
95
|
+
sdk.CallEmbed.init({
|
|
96
|
+
publishableKey,
|
|
97
|
+
apiBaseUrl,
|
|
98
|
+
locale,
|
|
99
|
+
copy,
|
|
100
|
+
branding,
|
|
101
|
+
debug
|
|
102
|
+
});
|
|
103
|
+
const runtime = sdk.CallEmbed._getRuntime();
|
|
104
|
+
if (runtime) setResolvedLocale(runtime.locale);
|
|
105
|
+
} catch (err) {
|
|
106
|
+
const state = toErrorState(err);
|
|
107
|
+
setError(state);
|
|
108
|
+
setStatus("error");
|
|
109
|
+
}
|
|
110
|
+
}, [publishableKey, apiBaseUrl, locale, copyKey, branding, debug]);
|
|
111
|
+
react.useEffect(() => {
|
|
112
|
+
return () => {
|
|
113
|
+
hangUpActive();
|
|
114
|
+
};
|
|
115
|
+
}, [publishableKey, apiBaseUrl, hangUpActive]);
|
|
116
|
+
const start = react.useCallback(
|
|
117
|
+
async (options = {}) => {
|
|
118
|
+
try {
|
|
119
|
+
const q = options.queue ?? queue;
|
|
120
|
+
if (!q?.trim()) {
|
|
121
|
+
throw new sdk.CallEmbedError(
|
|
122
|
+
"invalid_queue",
|
|
123
|
+
"queue is required (pass queue on the provider, button, or start())."
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
setError(null);
|
|
127
|
+
setMutedState(false);
|
|
128
|
+
const userOn = options.on;
|
|
129
|
+
const call = await sdk.CallEmbed.startCall({
|
|
130
|
+
...options,
|
|
131
|
+
queue: q,
|
|
132
|
+
hidePanel: true,
|
|
133
|
+
on: {
|
|
134
|
+
...userOn,
|
|
135
|
+
statusChange: (payload) => {
|
|
136
|
+
setStatus(payload.status);
|
|
137
|
+
userOn?.statusChange?.(payload);
|
|
138
|
+
},
|
|
139
|
+
muteChange: (payload) => {
|
|
140
|
+
setMutedState(payload.muted);
|
|
141
|
+
userOn?.muteChange?.(payload);
|
|
142
|
+
},
|
|
143
|
+
error: (payload) => {
|
|
144
|
+
setError({ code: payload.code, message: payload.message });
|
|
145
|
+
setStatus("error");
|
|
146
|
+
if (payload.callId && payload.callId !== "pending") {
|
|
147
|
+
setCallId(payload.callId);
|
|
148
|
+
}
|
|
149
|
+
userOn?.error?.(payload);
|
|
150
|
+
},
|
|
151
|
+
ended: (payload) => {
|
|
152
|
+
setStatus("ended");
|
|
153
|
+
if (payload.callId && payload.callId !== "pending") {
|
|
154
|
+
setCallId(payload.callId);
|
|
155
|
+
}
|
|
156
|
+
userOn?.ended?.(payload);
|
|
157
|
+
},
|
|
158
|
+
ringing: (payload) => {
|
|
159
|
+
setCallId(payload.callId);
|
|
160
|
+
userOn?.ringing?.(payload);
|
|
161
|
+
},
|
|
162
|
+
answered: (payload) => {
|
|
163
|
+
setCallId(payload.callId);
|
|
164
|
+
userOn?.answered?.(payload);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
unbindRef.current?.();
|
|
169
|
+
callRef.current = call;
|
|
170
|
+
setStatus(call.status);
|
|
171
|
+
if (call.callId !== "pending") setCallId(call.callId);
|
|
172
|
+
unbindRef.current = bindCall(call, {
|
|
173
|
+
setStatus,
|
|
174
|
+
setCallId,
|
|
175
|
+
setMuted: setMutedState,
|
|
176
|
+
setError
|
|
177
|
+
});
|
|
178
|
+
return call;
|
|
179
|
+
} catch (err) {
|
|
180
|
+
const state = toErrorState(err);
|
|
181
|
+
setError(state);
|
|
182
|
+
setStatus("error");
|
|
183
|
+
if (err instanceof sdk.CallEmbedError) throw err;
|
|
184
|
+
throw new sdk.CallEmbedError("call_failed", state.message);
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
[queue]
|
|
188
|
+
);
|
|
189
|
+
const end = react.useCallback(async () => {
|
|
190
|
+
const call = callRef.current ?? sdk.CallEmbed.getActiveCall();
|
|
191
|
+
if (!call) {
|
|
192
|
+
setStatus("idle");
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
await call.hangUp();
|
|
196
|
+
}, []);
|
|
197
|
+
const setMuted = react.useCallback(async (next) => {
|
|
198
|
+
const call = callRef.current ?? sdk.CallEmbed.getActiveCall();
|
|
199
|
+
if (!call) {
|
|
200
|
+
setMutedState(next);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
await call.setMuted(next);
|
|
204
|
+
}, []);
|
|
205
|
+
const value = react.useMemo(
|
|
206
|
+
() => ({
|
|
207
|
+
status,
|
|
208
|
+
uiState: toUiState(status),
|
|
209
|
+
callId,
|
|
210
|
+
muted,
|
|
211
|
+
error,
|
|
212
|
+
locale: resolvedLocale,
|
|
213
|
+
start,
|
|
214
|
+
end,
|
|
215
|
+
setMuted,
|
|
216
|
+
defaultQueue: queue
|
|
217
|
+
}),
|
|
218
|
+
[status, callId, muted, error, resolvedLocale, start, end, setMuted, queue]
|
|
219
|
+
);
|
|
220
|
+
return /* @__PURE__ */ jsxRuntime.jsx(CallEmbedReactContext.Provider, { value, children });
|
|
221
|
+
}
|
|
222
|
+
function useCallEmbed() {
|
|
223
|
+
const ctx = react.useContext(CallEmbedReactContext);
|
|
224
|
+
if (!ctx) {
|
|
225
|
+
throw new Error("useCallEmbed() must be used within <CallEmbedProvider>.");
|
|
226
|
+
}
|
|
227
|
+
return ctx;
|
|
228
|
+
}
|
|
229
|
+
function statusLabel(copy, uiState, sdkStatus) {
|
|
230
|
+
switch (uiState) {
|
|
231
|
+
case "idle":
|
|
232
|
+
return copy.ready;
|
|
233
|
+
case "ringing":
|
|
234
|
+
return sdkStatus === "connecting" ? copy.connecting : copy.ringing;
|
|
235
|
+
case "connected":
|
|
236
|
+
return copy.answered;
|
|
237
|
+
case "ended":
|
|
238
|
+
return copy.ended;
|
|
239
|
+
case "error":
|
|
240
|
+
return copy.error;
|
|
241
|
+
default:
|
|
242
|
+
return copy.ready;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function runtimeForCopy(locale, copyOverrides = {}) {
|
|
246
|
+
return {
|
|
247
|
+
publishableKey: "",
|
|
248
|
+
apiBaseUrl: "",
|
|
249
|
+
locale,
|
|
250
|
+
copyOverrides,
|
|
251
|
+
branding: true,
|
|
252
|
+
debug: false
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function CallEmbedButton({
|
|
256
|
+
queue,
|
|
257
|
+
label,
|
|
258
|
+
metadata,
|
|
259
|
+
displayName,
|
|
260
|
+
showStatus = true,
|
|
261
|
+
showMute = true,
|
|
262
|
+
onError,
|
|
263
|
+
className,
|
|
264
|
+
disabled,
|
|
265
|
+
...rest
|
|
266
|
+
}) {
|
|
267
|
+
const embed = useCallEmbed();
|
|
268
|
+
const [busy, setBusy] = react.useState(false);
|
|
269
|
+
const copy = react.useMemo(
|
|
270
|
+
() => ui.resolveCopy(runtimeForCopy(embed.locale)),
|
|
271
|
+
[embed.locale]
|
|
272
|
+
);
|
|
273
|
+
const inCall = embed.uiState === "ringing" || embed.uiState === "connected";
|
|
274
|
+
const buttonLabel = inCall ? copy.hangup : label ?? copy.launchDefault;
|
|
275
|
+
const statusText = react.useMemo(() => {
|
|
276
|
+
if (embed.uiState === "error" && embed.error) {
|
|
277
|
+
return ui.friendlyErrorMessage(copy, embed.error.code, embed.error.message);
|
|
278
|
+
}
|
|
279
|
+
return statusLabel(copy, embed.uiState, embed.status);
|
|
280
|
+
}, [copy, embed.error, embed.status, embed.uiState]);
|
|
281
|
+
const tone = embed.uiState === "error" ? "error" : embed.uiState === "connected" ? "ok" : void 0;
|
|
282
|
+
const onClick = react.useCallback(async () => {
|
|
283
|
+
setBusy(true);
|
|
284
|
+
try {
|
|
285
|
+
if (inCall) {
|
|
286
|
+
await embed.end();
|
|
287
|
+
} else {
|
|
288
|
+
await embed.start({
|
|
289
|
+
queue: queue ?? embed.defaultQueue,
|
|
290
|
+
metadata,
|
|
291
|
+
displayName
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
} catch (err) {
|
|
295
|
+
const ce = err instanceof sdk.CallEmbedError ? err : new sdk.CallEmbedError(
|
|
296
|
+
"call_failed",
|
|
297
|
+
err instanceof Error ? err.message : String(err)
|
|
298
|
+
);
|
|
299
|
+
onError?.(ce);
|
|
300
|
+
} finally {
|
|
301
|
+
setBusy(false);
|
|
302
|
+
}
|
|
303
|
+
}, [displayName, embed, inCall, metadata, onError, queue]);
|
|
304
|
+
const onMuteClick = react.useCallback(async () => {
|
|
305
|
+
setBusy(true);
|
|
306
|
+
try {
|
|
307
|
+
await embed.setMuted(!embed.muted);
|
|
308
|
+
} catch (err) {
|
|
309
|
+
const ce = err instanceof sdk.CallEmbedError ? err : new sdk.CallEmbedError(
|
|
310
|
+
"call_failed",
|
|
311
|
+
err instanceof Error ? err.message : String(err)
|
|
312
|
+
);
|
|
313
|
+
onError?.(ce);
|
|
314
|
+
} finally {
|
|
315
|
+
setBusy(false);
|
|
316
|
+
}
|
|
317
|
+
}, [embed, onError]);
|
|
318
|
+
const rootClass = ["ce-react-root", className].filter(Boolean).join(" ");
|
|
319
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: rootClass, "data-ce-react": "", children: [
|
|
320
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
321
|
+
"button",
|
|
322
|
+
{
|
|
323
|
+
type: "button",
|
|
324
|
+
className: "ce-react-button",
|
|
325
|
+
"data-state": embed.uiState,
|
|
326
|
+
...rest,
|
|
327
|
+
disabled: disabled || busy,
|
|
328
|
+
"aria-busy": busy || void 0,
|
|
329
|
+
onClick: () => {
|
|
330
|
+
void onClick();
|
|
331
|
+
},
|
|
332
|
+
children: buttonLabel
|
|
333
|
+
}
|
|
334
|
+
),
|
|
335
|
+
showMute && inCall ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
336
|
+
"button",
|
|
337
|
+
{
|
|
338
|
+
type: "button",
|
|
339
|
+
className: "ce-react-mute",
|
|
340
|
+
disabled: busy,
|
|
341
|
+
onClick: () => {
|
|
342
|
+
void onMuteClick();
|
|
343
|
+
},
|
|
344
|
+
children: embed.muted ? copy.unmute : copy.mute
|
|
345
|
+
}
|
|
346
|
+
) : null,
|
|
347
|
+
showStatus ? /* @__PURE__ */ jsxRuntime.jsx("p", { className: "ce-react-status", "data-tone": tone, "data-state": embed.uiState, children: statusText }) : null
|
|
348
|
+
] });
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
Object.defineProperty(exports, "enCopy", {
|
|
352
|
+
enumerable: true,
|
|
353
|
+
get: function () { return ui.enCopy; }
|
|
354
|
+
});
|
|
355
|
+
Object.defineProperty(exports, "friendlyErrorMessage", {
|
|
356
|
+
enumerable: true,
|
|
357
|
+
get: function () { return ui.friendlyErrorMessage; }
|
|
358
|
+
});
|
|
359
|
+
Object.defineProperty(exports, "resolveCopy", {
|
|
360
|
+
enumerable: true,
|
|
361
|
+
get: function () { return ui.resolveCopy; }
|
|
362
|
+
});
|
|
363
|
+
Object.defineProperty(exports, "zhTwCopy", {
|
|
364
|
+
enumerable: true,
|
|
365
|
+
get: function () { return ui.zhTwCopy; }
|
|
366
|
+
});
|
|
367
|
+
exports.CallEmbedButton = CallEmbedButton;
|
|
368
|
+
exports.CallEmbedProvider = CallEmbedProvider;
|
|
369
|
+
exports.statusLabel = statusLabel;
|
|
370
|
+
exports.toUiState = toUiState;
|
|
371
|
+
exports.useCallEmbed = useCallEmbed;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ReactNode, ButtonHTMLAttributes } from 'react';
|
|
2
|
+
import { CallStatus, Locale, StartCallOptions, ActiveCall, InitOptions, CallEmbedError } from '@callembed/sdk';
|
|
3
|
+
export { ActiveCall, CallStatus, InitOptions, Locale, StartCallOptions } from '@callembed/sdk';
|
|
4
|
+
import { PanelCopy } from '@callembed/sdk/ui';
|
|
5
|
+
export { PanelCopy, enCopy, friendlyErrorMessage, resolveCopy, zhTwCopy } from '@callembed/sdk/ui';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Simplified UI lifecycle for `<CallEmbedButton />`.
|
|
9
|
+
* Maps onto SDK {@link CallStatus}:
|
|
10
|
+
* - `connecting` | `ringing` → `ringing`
|
|
11
|
+
* - `answered` → `connected`
|
|
12
|
+
*/
|
|
13
|
+
type CallEmbedUiState = "idle" | "ringing" | "connected" | "ended" | "error";
|
|
14
|
+
declare function toUiState(status: CallStatus | "idle"): CallEmbedUiState;
|
|
15
|
+
interface CallEmbedErrorState {
|
|
16
|
+
code: string;
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
interface CallEmbedContextValue {
|
|
20
|
+
/** SDK call status (includes `connecting` / `answered`). */
|
|
21
|
+
status: CallStatus | "idle";
|
|
22
|
+
/** Button-oriented status (`answered` → `connected`). */
|
|
23
|
+
uiState: CallEmbedUiState;
|
|
24
|
+
callId: string | null;
|
|
25
|
+
muted: boolean;
|
|
26
|
+
error: CallEmbedErrorState | null;
|
|
27
|
+
locale: Locale;
|
|
28
|
+
/** Start a queue call (customer-service ring → agent answers). */
|
|
29
|
+
start: (options?: Partial<StartCallOptions> & {
|
|
30
|
+
queue?: string;
|
|
31
|
+
}) => Promise<ActiveCall>;
|
|
32
|
+
/** Hang up the active call (idempotent). */
|
|
33
|
+
end: () => Promise<void>;
|
|
34
|
+
setMuted: (muted: boolean) => Promise<void>;
|
|
35
|
+
/** Default queue from the provider when button/hook omit `queue`. */
|
|
36
|
+
defaultQueue: string | undefined;
|
|
37
|
+
}
|
|
38
|
+
type CallEmbedProviderProps = InitOptions & {
|
|
39
|
+
children: ReactNode;
|
|
40
|
+
/**
|
|
41
|
+
* Default queue name for `<CallEmbedButton />` / `useCallEmbed().start()`
|
|
42
|
+
* when callers omit `queue`.
|
|
43
|
+
*/
|
|
44
|
+
queue?: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Initializes `@callembed/sdk` once for the subtree and exposes call state.
|
|
49
|
+
* Safe for Next.js App Router: no `window` access at module evaluate time;
|
|
50
|
+
* `CallEmbed.init` runs in `useEffect`.
|
|
51
|
+
*
|
|
52
|
+
* Unmount or `publishableKey` / `apiBaseUrl` change hangs up any active call
|
|
53
|
+
* so an SPA route change cannot leave a billed call running.
|
|
54
|
+
*/
|
|
55
|
+
declare function CallEmbedProvider({ children, publishableKey, apiBaseUrl, locale, copy, branding, debug, queue, }: CallEmbedProviderProps): ReactNode;
|
|
56
|
+
/**
|
|
57
|
+
* Access CallEmbed state and controls. Must be used under {@link CallEmbedProvider}.
|
|
58
|
+
*/
|
|
59
|
+
declare function useCallEmbed(): CallEmbedContextValue;
|
|
60
|
+
|
|
61
|
+
interface CallEmbedButtonProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "onClick" | "children" | "onError"> {
|
|
62
|
+
/** Queue to ring. Falls back to `CallEmbedProvider` `queue`. */
|
|
63
|
+
queue?: string;
|
|
64
|
+
/** Idle-state label; defaults to locale `launchDefault`. */
|
|
65
|
+
label?: string;
|
|
66
|
+
/** Optional metadata forwarded to `startCall`. */
|
|
67
|
+
metadata?: Record<string, unknown>;
|
|
68
|
+
/** Optional caller display name. */
|
|
69
|
+
displayName?: string;
|
|
70
|
+
/** Show a status line under the button. Default: `true`. */
|
|
71
|
+
showStatus?: boolean;
|
|
72
|
+
/** Show mute control while ringing/connected. Default: `true`. */
|
|
73
|
+
showMute?: boolean;
|
|
74
|
+
/** Called when start/end rejects. */
|
|
75
|
+
onError?: (error: CallEmbedError) => void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Start / end call button for a customer-service queue.
|
|
79
|
+
* States: `idle` → `ringing` → `connected` → `ended` | `error`.
|
|
80
|
+
*/
|
|
81
|
+
declare function CallEmbedButton({ queue, label, metadata, displayName, showStatus, showMute, onError, className, disabled, ...rest }: CallEmbedButtonProps): ReactNode;
|
|
82
|
+
|
|
83
|
+
/** Status line for the React button / hook consumers. */
|
|
84
|
+
declare function statusLabel(copy: PanelCopy, uiState: CallEmbedUiState, sdkStatus: string): string;
|
|
85
|
+
|
|
86
|
+
export { CallEmbedButton, type CallEmbedButtonProps, type CallEmbedContextValue, type CallEmbedErrorState, CallEmbedProvider, type CallEmbedProviderProps, type CallEmbedUiState, statusLabel, toUiState, useCallEmbed };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ReactNode, ButtonHTMLAttributes } from 'react';
|
|
2
|
+
import { CallStatus, Locale, StartCallOptions, ActiveCall, InitOptions, CallEmbedError } from '@callembed/sdk';
|
|
3
|
+
export { ActiveCall, CallStatus, InitOptions, Locale, StartCallOptions } from '@callembed/sdk';
|
|
4
|
+
import { PanelCopy } from '@callembed/sdk/ui';
|
|
5
|
+
export { PanelCopy, enCopy, friendlyErrorMessage, resolveCopy, zhTwCopy } from '@callembed/sdk/ui';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Simplified UI lifecycle for `<CallEmbedButton />`.
|
|
9
|
+
* Maps onto SDK {@link CallStatus}:
|
|
10
|
+
* - `connecting` | `ringing` → `ringing`
|
|
11
|
+
* - `answered` → `connected`
|
|
12
|
+
*/
|
|
13
|
+
type CallEmbedUiState = "idle" | "ringing" | "connected" | "ended" | "error";
|
|
14
|
+
declare function toUiState(status: CallStatus | "idle"): CallEmbedUiState;
|
|
15
|
+
interface CallEmbedErrorState {
|
|
16
|
+
code: string;
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
interface CallEmbedContextValue {
|
|
20
|
+
/** SDK call status (includes `connecting` / `answered`). */
|
|
21
|
+
status: CallStatus | "idle";
|
|
22
|
+
/** Button-oriented status (`answered` → `connected`). */
|
|
23
|
+
uiState: CallEmbedUiState;
|
|
24
|
+
callId: string | null;
|
|
25
|
+
muted: boolean;
|
|
26
|
+
error: CallEmbedErrorState | null;
|
|
27
|
+
locale: Locale;
|
|
28
|
+
/** Start a queue call (customer-service ring → agent answers). */
|
|
29
|
+
start: (options?: Partial<StartCallOptions> & {
|
|
30
|
+
queue?: string;
|
|
31
|
+
}) => Promise<ActiveCall>;
|
|
32
|
+
/** Hang up the active call (idempotent). */
|
|
33
|
+
end: () => Promise<void>;
|
|
34
|
+
setMuted: (muted: boolean) => Promise<void>;
|
|
35
|
+
/** Default queue from the provider when button/hook omit `queue`. */
|
|
36
|
+
defaultQueue: string | undefined;
|
|
37
|
+
}
|
|
38
|
+
type CallEmbedProviderProps = InitOptions & {
|
|
39
|
+
children: ReactNode;
|
|
40
|
+
/**
|
|
41
|
+
* Default queue name for `<CallEmbedButton />` / `useCallEmbed().start()`
|
|
42
|
+
* when callers omit `queue`.
|
|
43
|
+
*/
|
|
44
|
+
queue?: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Initializes `@callembed/sdk` once for the subtree and exposes call state.
|
|
49
|
+
* Safe for Next.js App Router: no `window` access at module evaluate time;
|
|
50
|
+
* `CallEmbed.init` runs in `useEffect`.
|
|
51
|
+
*
|
|
52
|
+
* Unmount or `publishableKey` / `apiBaseUrl` change hangs up any active call
|
|
53
|
+
* so an SPA route change cannot leave a billed call running.
|
|
54
|
+
*/
|
|
55
|
+
declare function CallEmbedProvider({ children, publishableKey, apiBaseUrl, locale, copy, branding, debug, queue, }: CallEmbedProviderProps): ReactNode;
|
|
56
|
+
/**
|
|
57
|
+
* Access CallEmbed state and controls. Must be used under {@link CallEmbedProvider}.
|
|
58
|
+
*/
|
|
59
|
+
declare function useCallEmbed(): CallEmbedContextValue;
|
|
60
|
+
|
|
61
|
+
interface CallEmbedButtonProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "onClick" | "children" | "onError"> {
|
|
62
|
+
/** Queue to ring. Falls back to `CallEmbedProvider` `queue`. */
|
|
63
|
+
queue?: string;
|
|
64
|
+
/** Idle-state label; defaults to locale `launchDefault`. */
|
|
65
|
+
label?: string;
|
|
66
|
+
/** Optional metadata forwarded to `startCall`. */
|
|
67
|
+
metadata?: Record<string, unknown>;
|
|
68
|
+
/** Optional caller display name. */
|
|
69
|
+
displayName?: string;
|
|
70
|
+
/** Show a status line under the button. Default: `true`. */
|
|
71
|
+
showStatus?: boolean;
|
|
72
|
+
/** Show mute control while ringing/connected. Default: `true`. */
|
|
73
|
+
showMute?: boolean;
|
|
74
|
+
/** Called when start/end rejects. */
|
|
75
|
+
onError?: (error: CallEmbedError) => void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Start / end call button for a customer-service queue.
|
|
79
|
+
* States: `idle` → `ringing` → `connected` → `ended` | `error`.
|
|
80
|
+
*/
|
|
81
|
+
declare function CallEmbedButton({ queue, label, metadata, displayName, showStatus, showMute, onError, className, disabled, ...rest }: CallEmbedButtonProps): ReactNode;
|
|
82
|
+
|
|
83
|
+
/** Status line for the React button / hook consumers. */
|
|
84
|
+
declare function statusLabel(copy: PanelCopy, uiState: CallEmbedUiState, sdkStatus: string): string;
|
|
85
|
+
|
|
86
|
+
export { CallEmbedButton, type CallEmbedButtonProps, type CallEmbedContextValue, type CallEmbedErrorState, CallEmbedProvider, type CallEmbedProviderProps, type CallEmbedUiState, statusLabel, toUiState, useCallEmbed };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { CallEmbed, CallEmbedError } from '@callembed/sdk';
|
|
3
|
+
import { createContext, useState, useRef, useCallback, useEffect, useMemo, useContext } from 'react';
|
|
4
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
5
|
+
import { resolveCopy, friendlyErrorMessage } from '@callembed/sdk/ui';
|
|
6
|
+
export { enCopy, friendlyErrorMessage, resolveCopy, zhTwCopy } from '@callembed/sdk/ui';
|
|
7
|
+
|
|
8
|
+
// src/context.tsx
|
|
9
|
+
|
|
10
|
+
// src/types.ts
|
|
11
|
+
function toUiState(status) {
|
|
12
|
+
switch (status) {
|
|
13
|
+
case "connecting":
|
|
14
|
+
case "ringing":
|
|
15
|
+
return "ringing";
|
|
16
|
+
case "answered":
|
|
17
|
+
return "connected";
|
|
18
|
+
case "ended":
|
|
19
|
+
return "ended";
|
|
20
|
+
case "error":
|
|
21
|
+
return "error";
|
|
22
|
+
case "idle":
|
|
23
|
+
default:
|
|
24
|
+
return "idle";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
var CallEmbedReactContext = createContext(null);
|
|
28
|
+
function toErrorState(err) {
|
|
29
|
+
if (err instanceof CallEmbedError) {
|
|
30
|
+
return { code: err.code, message: err.message };
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
code: "call_failed",
|
|
34
|
+
message: err instanceof Error ? err.message : String(err)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function isActiveStatus(status) {
|
|
38
|
+
return status === "connecting" || status === "ringing" || status === "answered";
|
|
39
|
+
}
|
|
40
|
+
function bindCall(call, handlers) {
|
|
41
|
+
const offs = [
|
|
42
|
+
call.on("statusChange", ({ status }) => {
|
|
43
|
+
handlers.setStatus(status);
|
|
44
|
+
if (call.callId && call.callId !== "pending") {
|
|
45
|
+
handlers.setCallId(call.callId);
|
|
46
|
+
}
|
|
47
|
+
}),
|
|
48
|
+
call.on("muteChange", ({ muted }) => {
|
|
49
|
+
handlers.setMuted(muted);
|
|
50
|
+
}),
|
|
51
|
+
call.on("error", ({ code, message, callId }) => {
|
|
52
|
+
handlers.setError({ code, message });
|
|
53
|
+
handlers.setStatus("error");
|
|
54
|
+
if (callId && callId !== "pending") handlers.setCallId(callId);
|
|
55
|
+
}),
|
|
56
|
+
call.on("ended", ({ callId }) => {
|
|
57
|
+
handlers.setStatus("ended");
|
|
58
|
+
if (callId && callId !== "pending") handlers.setCallId(callId);
|
|
59
|
+
})
|
|
60
|
+
];
|
|
61
|
+
return () => {
|
|
62
|
+
offs.forEach((off) => off());
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function CallEmbedProvider({
|
|
66
|
+
children,
|
|
67
|
+
publishableKey,
|
|
68
|
+
apiBaseUrl,
|
|
69
|
+
locale,
|
|
70
|
+
copy,
|
|
71
|
+
branding,
|
|
72
|
+
debug,
|
|
73
|
+
queue
|
|
74
|
+
}) {
|
|
75
|
+
const [status, setStatus] = useState("idle");
|
|
76
|
+
const [callId, setCallId] = useState(null);
|
|
77
|
+
const [muted, setMutedState] = useState(false);
|
|
78
|
+
const [error, setError] = useState(null);
|
|
79
|
+
const [resolvedLocale, setResolvedLocale] = useState(locale ?? "en");
|
|
80
|
+
const callRef = useRef(null);
|
|
81
|
+
const unbindRef = useRef(null);
|
|
82
|
+
const copyKey = JSON.stringify(copy ?? {});
|
|
83
|
+
const hangUpActive = useCallback(() => {
|
|
84
|
+
unbindRef.current?.();
|
|
85
|
+
unbindRef.current = null;
|
|
86
|
+
const call = callRef.current ?? CallEmbed.getActiveCall();
|
|
87
|
+
callRef.current = null;
|
|
88
|
+
if (call && isActiveStatus(call.status)) {
|
|
89
|
+
void call.hangUp();
|
|
90
|
+
}
|
|
91
|
+
}, []);
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
try {
|
|
94
|
+
CallEmbed.init({
|
|
95
|
+
publishableKey,
|
|
96
|
+
apiBaseUrl,
|
|
97
|
+
locale,
|
|
98
|
+
copy,
|
|
99
|
+
branding,
|
|
100
|
+
debug
|
|
101
|
+
});
|
|
102
|
+
const runtime = CallEmbed._getRuntime();
|
|
103
|
+
if (runtime) setResolvedLocale(runtime.locale);
|
|
104
|
+
} catch (err) {
|
|
105
|
+
const state = toErrorState(err);
|
|
106
|
+
setError(state);
|
|
107
|
+
setStatus("error");
|
|
108
|
+
}
|
|
109
|
+
}, [publishableKey, apiBaseUrl, locale, copyKey, branding, debug]);
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
return () => {
|
|
112
|
+
hangUpActive();
|
|
113
|
+
};
|
|
114
|
+
}, [publishableKey, apiBaseUrl, hangUpActive]);
|
|
115
|
+
const start = useCallback(
|
|
116
|
+
async (options = {}) => {
|
|
117
|
+
try {
|
|
118
|
+
const q = options.queue ?? queue;
|
|
119
|
+
if (!q?.trim()) {
|
|
120
|
+
throw new CallEmbedError(
|
|
121
|
+
"invalid_queue",
|
|
122
|
+
"queue is required (pass queue on the provider, button, or start())."
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
setError(null);
|
|
126
|
+
setMutedState(false);
|
|
127
|
+
const userOn = options.on;
|
|
128
|
+
const call = await CallEmbed.startCall({
|
|
129
|
+
...options,
|
|
130
|
+
queue: q,
|
|
131
|
+
hidePanel: true,
|
|
132
|
+
on: {
|
|
133
|
+
...userOn,
|
|
134
|
+
statusChange: (payload) => {
|
|
135
|
+
setStatus(payload.status);
|
|
136
|
+
userOn?.statusChange?.(payload);
|
|
137
|
+
},
|
|
138
|
+
muteChange: (payload) => {
|
|
139
|
+
setMutedState(payload.muted);
|
|
140
|
+
userOn?.muteChange?.(payload);
|
|
141
|
+
},
|
|
142
|
+
error: (payload) => {
|
|
143
|
+
setError({ code: payload.code, message: payload.message });
|
|
144
|
+
setStatus("error");
|
|
145
|
+
if (payload.callId && payload.callId !== "pending") {
|
|
146
|
+
setCallId(payload.callId);
|
|
147
|
+
}
|
|
148
|
+
userOn?.error?.(payload);
|
|
149
|
+
},
|
|
150
|
+
ended: (payload) => {
|
|
151
|
+
setStatus("ended");
|
|
152
|
+
if (payload.callId && payload.callId !== "pending") {
|
|
153
|
+
setCallId(payload.callId);
|
|
154
|
+
}
|
|
155
|
+
userOn?.ended?.(payload);
|
|
156
|
+
},
|
|
157
|
+
ringing: (payload) => {
|
|
158
|
+
setCallId(payload.callId);
|
|
159
|
+
userOn?.ringing?.(payload);
|
|
160
|
+
},
|
|
161
|
+
answered: (payload) => {
|
|
162
|
+
setCallId(payload.callId);
|
|
163
|
+
userOn?.answered?.(payload);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
unbindRef.current?.();
|
|
168
|
+
callRef.current = call;
|
|
169
|
+
setStatus(call.status);
|
|
170
|
+
if (call.callId !== "pending") setCallId(call.callId);
|
|
171
|
+
unbindRef.current = bindCall(call, {
|
|
172
|
+
setStatus,
|
|
173
|
+
setCallId,
|
|
174
|
+
setMuted: setMutedState,
|
|
175
|
+
setError
|
|
176
|
+
});
|
|
177
|
+
return call;
|
|
178
|
+
} catch (err) {
|
|
179
|
+
const state = toErrorState(err);
|
|
180
|
+
setError(state);
|
|
181
|
+
setStatus("error");
|
|
182
|
+
if (err instanceof CallEmbedError) throw err;
|
|
183
|
+
throw new CallEmbedError("call_failed", state.message);
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
[queue]
|
|
187
|
+
);
|
|
188
|
+
const end = useCallback(async () => {
|
|
189
|
+
const call = callRef.current ?? CallEmbed.getActiveCall();
|
|
190
|
+
if (!call) {
|
|
191
|
+
setStatus("idle");
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
await call.hangUp();
|
|
195
|
+
}, []);
|
|
196
|
+
const setMuted = useCallback(async (next) => {
|
|
197
|
+
const call = callRef.current ?? CallEmbed.getActiveCall();
|
|
198
|
+
if (!call) {
|
|
199
|
+
setMutedState(next);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
await call.setMuted(next);
|
|
203
|
+
}, []);
|
|
204
|
+
const value = useMemo(
|
|
205
|
+
() => ({
|
|
206
|
+
status,
|
|
207
|
+
uiState: toUiState(status),
|
|
208
|
+
callId,
|
|
209
|
+
muted,
|
|
210
|
+
error,
|
|
211
|
+
locale: resolvedLocale,
|
|
212
|
+
start,
|
|
213
|
+
end,
|
|
214
|
+
setMuted,
|
|
215
|
+
defaultQueue: queue
|
|
216
|
+
}),
|
|
217
|
+
[status, callId, muted, error, resolvedLocale, start, end, setMuted, queue]
|
|
218
|
+
);
|
|
219
|
+
return /* @__PURE__ */ jsx(CallEmbedReactContext.Provider, { value, children });
|
|
220
|
+
}
|
|
221
|
+
function useCallEmbed() {
|
|
222
|
+
const ctx = useContext(CallEmbedReactContext);
|
|
223
|
+
if (!ctx) {
|
|
224
|
+
throw new Error("useCallEmbed() must be used within <CallEmbedProvider>.");
|
|
225
|
+
}
|
|
226
|
+
return ctx;
|
|
227
|
+
}
|
|
228
|
+
function statusLabel(copy, uiState, sdkStatus) {
|
|
229
|
+
switch (uiState) {
|
|
230
|
+
case "idle":
|
|
231
|
+
return copy.ready;
|
|
232
|
+
case "ringing":
|
|
233
|
+
return sdkStatus === "connecting" ? copy.connecting : copy.ringing;
|
|
234
|
+
case "connected":
|
|
235
|
+
return copy.answered;
|
|
236
|
+
case "ended":
|
|
237
|
+
return copy.ended;
|
|
238
|
+
case "error":
|
|
239
|
+
return copy.error;
|
|
240
|
+
default:
|
|
241
|
+
return copy.ready;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function runtimeForCopy(locale, copyOverrides = {}) {
|
|
245
|
+
return {
|
|
246
|
+
publishableKey: "",
|
|
247
|
+
apiBaseUrl: "",
|
|
248
|
+
locale,
|
|
249
|
+
copyOverrides,
|
|
250
|
+
branding: true,
|
|
251
|
+
debug: false
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function CallEmbedButton({
|
|
255
|
+
queue,
|
|
256
|
+
label,
|
|
257
|
+
metadata,
|
|
258
|
+
displayName,
|
|
259
|
+
showStatus = true,
|
|
260
|
+
showMute = true,
|
|
261
|
+
onError,
|
|
262
|
+
className,
|
|
263
|
+
disabled,
|
|
264
|
+
...rest
|
|
265
|
+
}) {
|
|
266
|
+
const embed = useCallEmbed();
|
|
267
|
+
const [busy, setBusy] = useState(false);
|
|
268
|
+
const copy = useMemo(
|
|
269
|
+
() => resolveCopy(runtimeForCopy(embed.locale)),
|
|
270
|
+
[embed.locale]
|
|
271
|
+
);
|
|
272
|
+
const inCall = embed.uiState === "ringing" || embed.uiState === "connected";
|
|
273
|
+
const buttonLabel = inCall ? copy.hangup : label ?? copy.launchDefault;
|
|
274
|
+
const statusText = useMemo(() => {
|
|
275
|
+
if (embed.uiState === "error" && embed.error) {
|
|
276
|
+
return friendlyErrorMessage(copy, embed.error.code, embed.error.message);
|
|
277
|
+
}
|
|
278
|
+
return statusLabel(copy, embed.uiState, embed.status);
|
|
279
|
+
}, [copy, embed.error, embed.status, embed.uiState]);
|
|
280
|
+
const tone = embed.uiState === "error" ? "error" : embed.uiState === "connected" ? "ok" : void 0;
|
|
281
|
+
const onClick = useCallback(async () => {
|
|
282
|
+
setBusy(true);
|
|
283
|
+
try {
|
|
284
|
+
if (inCall) {
|
|
285
|
+
await embed.end();
|
|
286
|
+
} else {
|
|
287
|
+
await embed.start({
|
|
288
|
+
queue: queue ?? embed.defaultQueue,
|
|
289
|
+
metadata,
|
|
290
|
+
displayName
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
} catch (err) {
|
|
294
|
+
const ce = err instanceof CallEmbedError ? err : new CallEmbedError(
|
|
295
|
+
"call_failed",
|
|
296
|
+
err instanceof Error ? err.message : String(err)
|
|
297
|
+
);
|
|
298
|
+
onError?.(ce);
|
|
299
|
+
} finally {
|
|
300
|
+
setBusy(false);
|
|
301
|
+
}
|
|
302
|
+
}, [displayName, embed, inCall, metadata, onError, queue]);
|
|
303
|
+
const onMuteClick = useCallback(async () => {
|
|
304
|
+
setBusy(true);
|
|
305
|
+
try {
|
|
306
|
+
await embed.setMuted(!embed.muted);
|
|
307
|
+
} catch (err) {
|
|
308
|
+
const ce = err instanceof CallEmbedError ? err : new CallEmbedError(
|
|
309
|
+
"call_failed",
|
|
310
|
+
err instanceof Error ? err.message : String(err)
|
|
311
|
+
);
|
|
312
|
+
onError?.(ce);
|
|
313
|
+
} finally {
|
|
314
|
+
setBusy(false);
|
|
315
|
+
}
|
|
316
|
+
}, [embed, onError]);
|
|
317
|
+
const rootClass = ["ce-react-root", className].filter(Boolean).join(" ");
|
|
318
|
+
return /* @__PURE__ */ jsxs("div", { className: rootClass, "data-ce-react": "", children: [
|
|
319
|
+
/* @__PURE__ */ jsx(
|
|
320
|
+
"button",
|
|
321
|
+
{
|
|
322
|
+
type: "button",
|
|
323
|
+
className: "ce-react-button",
|
|
324
|
+
"data-state": embed.uiState,
|
|
325
|
+
...rest,
|
|
326
|
+
disabled: disabled || busy,
|
|
327
|
+
"aria-busy": busy || void 0,
|
|
328
|
+
onClick: () => {
|
|
329
|
+
void onClick();
|
|
330
|
+
},
|
|
331
|
+
children: buttonLabel
|
|
332
|
+
}
|
|
333
|
+
),
|
|
334
|
+
showMute && inCall ? /* @__PURE__ */ jsx(
|
|
335
|
+
"button",
|
|
336
|
+
{
|
|
337
|
+
type: "button",
|
|
338
|
+
className: "ce-react-mute",
|
|
339
|
+
disabled: busy,
|
|
340
|
+
onClick: () => {
|
|
341
|
+
void onMuteClick();
|
|
342
|
+
},
|
|
343
|
+
children: embed.muted ? copy.unmute : copy.mute
|
|
344
|
+
}
|
|
345
|
+
) : null,
|
|
346
|
+
showStatus ? /* @__PURE__ */ jsx("p", { className: "ce-react-status", "data-tone": tone, "data-state": embed.uiState, children: statusText }) : null
|
|
347
|
+
] });
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export { CallEmbedButton, CallEmbedProvider, statusLabel, toUiState, useCallEmbed };
|
package/dist/styles.css
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default styles for `@callembed/react`.
|
|
3
|
+
* Theme via `--ce-*` (same public tokens as `@callembed/sdk/ui`).
|
|
4
|
+
*/
|
|
5
|
+
:where(.ce-react-button, .ce-react-status, .ce-react-mute) {
|
|
6
|
+
font-family: var(
|
|
7
|
+
--ce-font,
|
|
8
|
+
"Noto Sans TC",
|
|
9
|
+
"IBM Plex Sans",
|
|
10
|
+
"Segoe UI",
|
|
11
|
+
sans-serif
|
|
12
|
+
);
|
|
13
|
+
color: var(--ce-fg, #e8eef4);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.ce-react-root {
|
|
17
|
+
display: inline-flex;
|
|
18
|
+
flex-direction: column;
|
|
19
|
+
align-items: flex-start;
|
|
20
|
+
gap: 0.5rem;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.ce-react-button {
|
|
24
|
+
appearance: none;
|
|
25
|
+
display: inline-block;
|
|
26
|
+
border: 1px solid var(--ce-border, #2a3a46);
|
|
27
|
+
border-radius: var(--ce-radius, 0);
|
|
28
|
+
background: color-mix(in srgb, var(--ce-accent, #5eb0d8) 25%, transparent);
|
|
29
|
+
color: var(--ce-fg, #e8eef4);
|
|
30
|
+
padding: 0.7rem 1.1rem;
|
|
31
|
+
font-size: inherit;
|
|
32
|
+
cursor: pointer;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.ce-react-button:hover {
|
|
36
|
+
border-color: var(--ce-accent, #5eb0d8);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.ce-react-button:disabled {
|
|
40
|
+
opacity: 0.5;
|
|
41
|
+
cursor: not-allowed;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.ce-react-button[data-state="ringing"],
|
|
45
|
+
.ce-react-button[data-state="connected"] {
|
|
46
|
+
border-color: color-mix(
|
|
47
|
+
in srgb,
|
|
48
|
+
var(--ce-danger, #c44b4b) 55%,
|
|
49
|
+
var(--ce-border, #2a3a46)
|
|
50
|
+
);
|
|
51
|
+
color: var(--ce-error, #f0c4c4);
|
|
52
|
+
background: transparent;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.ce-react-status {
|
|
56
|
+
margin: 0;
|
|
57
|
+
color: var(--ce-muted, #9aadb8);
|
|
58
|
+
font-size: 0.9rem;
|
|
59
|
+
line-height: 1.4;
|
|
60
|
+
min-height: 1.25em;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.ce-react-status[data-tone="error"] {
|
|
64
|
+
color: var(--ce-error, #f0c4c4);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.ce-react-status[data-tone="ok"] {
|
|
68
|
+
color: var(--ce-ok, #9fd6c0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.ce-react-mute {
|
|
72
|
+
appearance: none;
|
|
73
|
+
border: 1px solid var(--ce-border, #2a3a46);
|
|
74
|
+
border-radius: var(--ce-radius, 0);
|
|
75
|
+
background: transparent;
|
|
76
|
+
color: inherit;
|
|
77
|
+
padding: 0.45rem 0.75rem;
|
|
78
|
+
font: inherit;
|
|
79
|
+
cursor: pointer;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.ce-react-mute:hover {
|
|
83
|
+
border-color: var(--ce-accent, #5eb0d8);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.ce-react-mute:disabled {
|
|
87
|
+
opacity: 0.5;
|
|
88
|
+
cursor: not-allowed;
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,87 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@callembed/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React components and hooks for CallEmbed (thin wrapper over @callembed/sdk)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Sealby",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.cjs",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./styles.css": "./dist/styles.css",
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"sideEffects": [
|
|
22
|
+
"./dist/styles.css"
|
|
23
|
+
],
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsup && node ./scripts/copy-css.mjs",
|
|
31
|
+
"prepack": "npm run build",
|
|
32
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
33
|
+
"dev": "tsup --watch",
|
|
34
|
+
"test": "vitest run",
|
|
35
|
+
"test:watch": "vitest",
|
|
36
|
+
"pack:verify": "node ./scripts/pack-verify.mjs"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://callembed.sealby.me",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/TonyFeng1203/CallEmbed.git",
|
|
42
|
+
"directory": "packages/react"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/TonyFeng1203/CallEmbed/issues"
|
|
46
|
+
},
|
|
47
|
+
"keywords": [
|
|
48
|
+
"callembed",
|
|
49
|
+
"react",
|
|
50
|
+
"voice",
|
|
51
|
+
"webrtc",
|
|
52
|
+
"livekit",
|
|
53
|
+
"customer-support"
|
|
54
|
+
],
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=20"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"@callembed/sdk": "^0.1.0",
|
|
63
|
+
"livekit-client": "^2.13.0",
|
|
64
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
65
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
66
|
+
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"livekit-client": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@callembed/sdk": "^0.1.0",
|
|
74
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
75
|
+
"@testing-library/react": "^16.3.0",
|
|
76
|
+
"@testing-library/user-event": "^14.6.1",
|
|
77
|
+
"@types/react": "^18.3.20",
|
|
78
|
+
"@types/react-dom": "^18.3.6",
|
|
79
|
+
"jsdom": "26.1.0",
|
|
80
|
+
"livekit-client": "^2.13.0",
|
|
81
|
+
"react": "^18.3.1",
|
|
82
|
+
"react-dom": "^18.3.1",
|
|
83
|
+
"tsup": "^8.4.0",
|
|
84
|
+
"typescript": "^5.8.3",
|
|
85
|
+
"vitest": "3.2.7"
|
|
86
|
+
}
|
|
87
|
+
}
|