@sts-ai/sdk 0.0.0-stage → 1.0.1
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 +195 -2
- package/dist/controllers.d.ts +79 -0
- package/dist/controllers.js +204 -0
- package/dist/core.d.ts +38 -0
- package/dist/core.js +126 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4 -0
- package/dist/react.d.ts +113 -0
- package/dist/react.js +440 -0
- package/dist/types.d.ts +275 -0
- package/dist/types.js +9 -0
- package/dist/vue.d.ts +110 -0
- package/dist/vue.js +211 -0
- package/dist/wizardPlayer.d.ts +38 -0
- package/dist/wizardPlayer.js +242 -0
- package/package.json +70 -4
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type StsClient } from './core.js';
|
|
3
|
+
import { type StsAnswer, type StsReview, type StsBusiness, type StsBusinessService, type StsChatLinks, type StsContactCard, type StsForm, type StsFormAnswer, type StsLiveSession, type StsOffer, type StsDoc, type StsSessionInfo, type StsWizard, type StsWizardStep } from './types.js';
|
|
4
|
+
/** Wrap your site once. Every hook below talks to your STS-AI business through this client. */
|
|
5
|
+
export declare function StsProvider({ apiBase, siteKey, children }: {
|
|
6
|
+
apiBase: string;
|
|
7
|
+
siteKey: string;
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}): import("react").JSX.Element;
|
|
10
|
+
export declare function useSts(): StsClient;
|
|
11
|
+
/** Business name + which services are turned on. */
|
|
12
|
+
export declare function useStsInfo(): {
|
|
13
|
+
info: StsSessionInfo | null;
|
|
14
|
+
error: string | null;
|
|
15
|
+
};
|
|
16
|
+
export type StsChatMessage = {
|
|
17
|
+
id: string;
|
|
18
|
+
role: 'user' | 'assistant';
|
|
19
|
+
content: string;
|
|
20
|
+
links?: StsChatLinks;
|
|
21
|
+
};
|
|
22
|
+
export declare function useStsChat(): {
|
|
23
|
+
/** false when Chat AI is off in the dashboard. */
|
|
24
|
+
available: boolean;
|
|
25
|
+
greeting: string;
|
|
26
|
+
suggestions: string[];
|
|
27
|
+
maxMessageLength: number;
|
|
28
|
+
/** Documents the assistant may ask for; show your attach button when this is not empty. */
|
|
29
|
+
documentRequests: import("./types.js").StsDocumentRequest[];
|
|
30
|
+
messages: StsChatMessage[];
|
|
31
|
+
send: (text: string) => Promise<void>;
|
|
32
|
+
attach: (file: File, documentId?: string) => Promise<void>;
|
|
33
|
+
reset: () => void;
|
|
34
|
+
status: "idle" | "thinking" | "error";
|
|
35
|
+
error: string | null;
|
|
36
|
+
};
|
|
37
|
+
type SpeakState = 'idle' | 'connecting' | 'live' | 'ended' | 'error';
|
|
38
|
+
/** One line of live captions. `final` is false while the words are still coming in. */
|
|
39
|
+
export type StsCaption = {
|
|
40
|
+
id: string;
|
|
41
|
+
speaker: 'agent' | 'visitor';
|
|
42
|
+
text: string;
|
|
43
|
+
final: boolean;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Voice call over WebRTC (LiveKit is included in this package). Headless like the
|
|
47
|
+
* chat: your designer builds the call button, mute button, captions and any animation.
|
|
48
|
+
*/
|
|
49
|
+
export declare function useStsSpeak(): {
|
|
50
|
+
state: SpeakState;
|
|
51
|
+
start: (participantName?: string) => Promise<void>;
|
|
52
|
+
stop: () => Promise<void>;
|
|
53
|
+
isAgentSpeaking: boolean;
|
|
54
|
+
muted: boolean;
|
|
55
|
+
setMuted: (value: boolean) => Promise<void>;
|
|
56
|
+
captions: StsCaption[];
|
|
57
|
+
agentLevel: number;
|
|
58
|
+
error: string | null;
|
|
59
|
+
};
|
|
60
|
+
export declare function useStsForm(): {
|
|
61
|
+
form: StsForm | null;
|
|
62
|
+
values: Record<string, StsFormAnswer>;
|
|
63
|
+
setValue: (fieldId: string, value: StsFormAnswer) => void;
|
|
64
|
+
consent: boolean;
|
|
65
|
+
setConsent: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
66
|
+
submit: () => Promise<void>;
|
|
67
|
+
status: "error" | "loading" | "ready" | "submitting" | "success" | "unavailable";
|
|
68
|
+
message: string | null;
|
|
69
|
+
fieldError: string | null;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Voice tour across your own page. Tag sections with `data-sts-step="…"`; style `[data-sts-active]`.
|
|
73
|
+
* Each business has one wizard, so `slug` is optional.
|
|
74
|
+
*/
|
|
75
|
+
export declare function useStsWizard(slug?: string): {
|
|
76
|
+
wizard: StsWizard | null;
|
|
77
|
+
ready: boolean;
|
|
78
|
+
start: () => void;
|
|
79
|
+
follow: () => void;
|
|
80
|
+
savedSound: () => "on" | "off" | null;
|
|
81
|
+
rememberSound: (choice: "on" | "off") => void;
|
|
82
|
+
stop: () => void;
|
|
83
|
+
play: (stepName: string) => boolean;
|
|
84
|
+
unlock: () => void;
|
|
85
|
+
playing: boolean;
|
|
86
|
+
speaking: boolean;
|
|
87
|
+
step: StsWizardStep | null;
|
|
88
|
+
stepIndex: number;
|
|
89
|
+
caption: string;
|
|
90
|
+
muted: boolean;
|
|
91
|
+
setMuted: (value: boolean) => void;
|
|
92
|
+
error: string | null;
|
|
93
|
+
};
|
|
94
|
+
export declare function useStsBusiness(): {
|
|
95
|
+
business: StsBusiness | null;
|
|
96
|
+
error: string | null;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* The business's own answers to the questions customers ask most, for an FAQ block your
|
|
100
|
+
* designer styles. Pair each with `useStsChat().send(question)` for an "Ask more" button.
|
|
101
|
+
*/
|
|
102
|
+
export declare function useStsAnswers(): {
|
|
103
|
+
answers: StsAnswer[];
|
|
104
|
+
ready: boolean;
|
|
105
|
+
error: string | null;
|
|
106
|
+
};
|
|
107
|
+
/** Reviews the business chose to show, for a reviews block your designer styles. */
|
|
108
|
+
export declare function useStsReviews(): {
|
|
109
|
+
reviews: StsReview[];
|
|
110
|
+
ready: boolean;
|
|
111
|
+
error: string | null;
|
|
112
|
+
};
|
|
113
|
+
export type { StsChatLinks, StsForm, StsSessionInfo, StsWizard, StsWizardStep, StsBusiness, StsBusinessService, StsDoc, StsOffer, StsContactCard, StsLiveSession, };
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { createSts } from './core.js';
|
|
5
|
+
import { createWizardPlayer } from './wizardPlayer.js';
|
|
6
|
+
import { StsError, } from './types.js';
|
|
7
|
+
const StsContext = createContext(null);
|
|
8
|
+
/** Wrap your site once. Every hook below talks to your STS-AI business through this client. */
|
|
9
|
+
export function StsProvider({ apiBase, siteKey, children }) {
|
|
10
|
+
const client = useMemo(() => createSts({ apiBase, siteKey }), [apiBase, siteKey]);
|
|
11
|
+
return _jsx(StsContext.Provider, { value: client, children: children });
|
|
12
|
+
}
|
|
13
|
+
export function useSts() {
|
|
14
|
+
const client = useContext(StsContext);
|
|
15
|
+
if (!client)
|
|
16
|
+
throw new Error('Wrap your app in <StsProvider apiBase siteKey>');
|
|
17
|
+
return client;
|
|
18
|
+
}
|
|
19
|
+
function errorMessage(error) {
|
|
20
|
+
return error instanceof Error ? error.message : 'Something went wrong';
|
|
21
|
+
}
|
|
22
|
+
/** Business name + which services are turned on. */
|
|
23
|
+
export function useStsInfo() {
|
|
24
|
+
const client = useSts();
|
|
25
|
+
const [info, setInfo] = useState(null);
|
|
26
|
+
const [error, setError] = useState(null);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
let active = true;
|
|
29
|
+
client
|
|
30
|
+
.info()
|
|
31
|
+
.then((value) => active && setInfo(value))
|
|
32
|
+
.catch((err) => active && setError(errorMessage(err)));
|
|
33
|
+
return () => {
|
|
34
|
+
active = false;
|
|
35
|
+
};
|
|
36
|
+
}, [client]);
|
|
37
|
+
return { info, error };
|
|
38
|
+
}
|
|
39
|
+
export function useStsChat() {
|
|
40
|
+
const client = useSts();
|
|
41
|
+
const { info, error: infoError } = useStsInfo();
|
|
42
|
+
const [messages, setMessages] = useState([]);
|
|
43
|
+
const [status, setStatus] = useState('idle');
|
|
44
|
+
const [error, setError] = useState(null);
|
|
45
|
+
const counter = useRef(0);
|
|
46
|
+
const nextId = () => `m${(counter.current += 1)}`;
|
|
47
|
+
const send = useCallback(async (text) => {
|
|
48
|
+
const message = text.trim();
|
|
49
|
+
if (!message || status === 'thinking')
|
|
50
|
+
return;
|
|
51
|
+
setMessages((current) => [...current, { id: nextId(), role: 'user', content: message }]);
|
|
52
|
+
setStatus('thinking');
|
|
53
|
+
setError(null);
|
|
54
|
+
try {
|
|
55
|
+
const result = await client.chat.send(message);
|
|
56
|
+
setMessages((current) => [...current, { id: nextId(), role: 'assistant', content: result.reply, links: result.links }]);
|
|
57
|
+
setStatus('idle');
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
setError(errorMessage(err));
|
|
61
|
+
setStatus('error');
|
|
62
|
+
}
|
|
63
|
+
}, [client, status]);
|
|
64
|
+
/**
|
|
65
|
+
* Sends a file the assistant asked for to the business, then tells the assistant it arrived.
|
|
66
|
+
* Wire it to your attach button's file input (accept="application/pdf,image/*").
|
|
67
|
+
*/
|
|
68
|
+
const attach = useCallback(async (file, documentId) => {
|
|
69
|
+
if (status === 'thinking')
|
|
70
|
+
return;
|
|
71
|
+
setStatus('thinking');
|
|
72
|
+
setError(null);
|
|
73
|
+
try {
|
|
74
|
+
const uploaded = await client.chat.attach(file, documentId);
|
|
75
|
+
setStatus('idle');
|
|
76
|
+
await send(uploaded.message);
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
setError(errorMessage(err));
|
|
80
|
+
setStatus('error');
|
|
81
|
+
}
|
|
82
|
+
}, [client, send, status]);
|
|
83
|
+
const chat = info?.services.chat ?? null;
|
|
84
|
+
return {
|
|
85
|
+
/** false when Chat AI is off in the dashboard. */
|
|
86
|
+
available: Boolean(chat),
|
|
87
|
+
greeting: chat?.greeting ?? '',
|
|
88
|
+
suggestions: chat?.suggestions ?? [],
|
|
89
|
+
maxMessageLength: chat?.maxMessageLength ?? 1000,
|
|
90
|
+
/** Documents the assistant may ask for; show your attach button when this is not empty. */
|
|
91
|
+
documentRequests: chat?.documentRequests ?? [],
|
|
92
|
+
messages,
|
|
93
|
+
send,
|
|
94
|
+
attach,
|
|
95
|
+
reset: () => setMessages([]),
|
|
96
|
+
status,
|
|
97
|
+
error: error ?? infoError,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const MAX_CAPTIONS = 20;
|
|
101
|
+
const LEVEL_INTERVAL_MS = 100;
|
|
102
|
+
/**
|
|
103
|
+
* Voice call over WebRTC (LiveKit is included in this package). Headless like the
|
|
104
|
+
* chat: your designer builds the call button, mute button, captions and any animation.
|
|
105
|
+
*/
|
|
106
|
+
export function useStsSpeak() {
|
|
107
|
+
const client = useSts();
|
|
108
|
+
const [state, setState] = useState('idle');
|
|
109
|
+
const [isAgentSpeaking, setAgentSpeaking] = useState(false);
|
|
110
|
+
const [muted, setMutedState] = useState(false);
|
|
111
|
+
const [captions, setCaptions] = useState([]);
|
|
112
|
+
/** The AI's voice loudness, 0 to 1, about ten times a second: drive an orb or bars with it. */
|
|
113
|
+
const [agentLevel, setAgentLevel] = useState(0);
|
|
114
|
+
const [error, setError] = useState(null);
|
|
115
|
+
const roomRef = useRef(null);
|
|
116
|
+
const timerRef = useRef(null);
|
|
117
|
+
const levelTimerRef = useRef(null);
|
|
118
|
+
const audioEls = useRef([]);
|
|
119
|
+
const stop = useCallback(async () => {
|
|
120
|
+
if (timerRef.current)
|
|
121
|
+
clearTimeout(timerRef.current);
|
|
122
|
+
if (levelTimerRef.current)
|
|
123
|
+
clearInterval(levelTimerRef.current);
|
|
124
|
+
timerRef.current = null;
|
|
125
|
+
levelTimerRef.current = null;
|
|
126
|
+
const room = roomRef.current;
|
|
127
|
+
roomRef.current = null;
|
|
128
|
+
audioEls.current.forEach((el) => el.remove());
|
|
129
|
+
audioEls.current = [];
|
|
130
|
+
setAgentSpeaking(false);
|
|
131
|
+
setAgentLevel(0);
|
|
132
|
+
setMutedState(false);
|
|
133
|
+
if (room)
|
|
134
|
+
await room.disconnect();
|
|
135
|
+
setState((current) => (current === 'error' ? current : 'ended'));
|
|
136
|
+
}, []);
|
|
137
|
+
/** Mutes or unmutes the visitor's microphone during a call. */
|
|
138
|
+
const setMuted = useCallback(async (value) => {
|
|
139
|
+
const room = roomRef.current;
|
|
140
|
+
if (!room)
|
|
141
|
+
return;
|
|
142
|
+
await room.localParticipant.setMicrophoneEnabled(!value);
|
|
143
|
+
setMutedState(value);
|
|
144
|
+
}, []);
|
|
145
|
+
const upsertCaption = useCallback((caption) => {
|
|
146
|
+
setCaptions((current) => {
|
|
147
|
+
const index = current.findIndex((item) => item.id === caption.id);
|
|
148
|
+
const next = index >= 0 ? current.map((item, i) => (i === index ? caption : item)) : [...current, caption];
|
|
149
|
+
return next.slice(-MAX_CAPTIONS);
|
|
150
|
+
});
|
|
151
|
+
}, []);
|
|
152
|
+
const start = useCallback(async (participantName) => {
|
|
153
|
+
if (roomRef.current)
|
|
154
|
+
return;
|
|
155
|
+
setState('connecting');
|
|
156
|
+
setError(null);
|
|
157
|
+
setCaptions([]);
|
|
158
|
+
try {
|
|
159
|
+
const [{ Room, RoomEvent, Track }, details] = await Promise.all([
|
|
160
|
+
import('livekit-client'),
|
|
161
|
+
client.speak.connect(participantName),
|
|
162
|
+
]);
|
|
163
|
+
const room = new Room();
|
|
164
|
+
room.on(RoomEvent.TrackSubscribed, (track) => {
|
|
165
|
+
if (track.kind !== Track.Kind.Audio)
|
|
166
|
+
return;
|
|
167
|
+
const element = track.attach();
|
|
168
|
+
element.style.display = 'none';
|
|
169
|
+
document.body.appendChild(element);
|
|
170
|
+
audioEls.current.push(element);
|
|
171
|
+
});
|
|
172
|
+
room.on(RoomEvent.ActiveSpeakersChanged, (speakers) => {
|
|
173
|
+
setAgentSpeaking(speakers.some((speaker) => speaker.identity !== room.localParticipant.identity));
|
|
174
|
+
});
|
|
175
|
+
room.on(RoomEvent.Disconnected, () => void stop());
|
|
176
|
+
// The voice agent publishes what both sides say on the "lk.transcription" text stream.
|
|
177
|
+
room.registerTextStreamHandler('lk.transcription', async (reader, participant) => {
|
|
178
|
+
const attributes = reader.info.attributes ?? {};
|
|
179
|
+
const id = attributes['lk.segment_id'] || reader.info.id;
|
|
180
|
+
const speaker = participant.identity === room.localParticipant.identity ? 'visitor' : 'agent';
|
|
181
|
+
let text = '';
|
|
182
|
+
for await (const chunk of reader) {
|
|
183
|
+
text += chunk;
|
|
184
|
+
upsertCaption({ id, speaker, text, final: false });
|
|
185
|
+
}
|
|
186
|
+
upsertCaption({ id, speaker, text, final: true });
|
|
187
|
+
});
|
|
188
|
+
await room.connect(details.serverUrl, details.participantToken);
|
|
189
|
+
await room.localParticipant.setMicrophoneEnabled(true);
|
|
190
|
+
roomRef.current = room;
|
|
191
|
+
levelTimerRef.current = setInterval(() => {
|
|
192
|
+
const agent = [...room.remoteParticipants.values()][0];
|
|
193
|
+
setAgentLevel(agent ? Math.min(1, agent.audioLevel) : 0);
|
|
194
|
+
}, LEVEL_INTERVAL_MS);
|
|
195
|
+
setState('live');
|
|
196
|
+
timerRef.current = setTimeout(() => void stop(), details.maxCallMinutes * 60000);
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
setError(errorMessage(err));
|
|
200
|
+
setState('error');
|
|
201
|
+
await stop();
|
|
202
|
+
}
|
|
203
|
+
}, [client, stop, upsertCaption]);
|
|
204
|
+
useEffect(() => () => void stop(), [stop]);
|
|
205
|
+
return { state, start, stop, isAgentSpeaking, muted, setMuted, captions, agentLevel, error };
|
|
206
|
+
}
|
|
207
|
+
// ── Form ───────────────────────────────────────────────────────────────────
|
|
208
|
+
export function useStsForm() {
|
|
209
|
+
const client = useSts();
|
|
210
|
+
const [form, setForm] = useState(null);
|
|
211
|
+
const [values, setValues] = useState({});
|
|
212
|
+
const [consent, setConsent] = useState(false);
|
|
213
|
+
const [status, setStatus] = useState('loading');
|
|
214
|
+
const [message, setMessage] = useState(null);
|
|
215
|
+
const [fieldError, setFieldError] = useState(null);
|
|
216
|
+
useEffect(() => {
|
|
217
|
+
let active = true;
|
|
218
|
+
client.form
|
|
219
|
+
.schema()
|
|
220
|
+
.then((schema) => {
|
|
221
|
+
if (!active)
|
|
222
|
+
return;
|
|
223
|
+
setForm(schema);
|
|
224
|
+
setStatus('ready');
|
|
225
|
+
})
|
|
226
|
+
.catch((err) => {
|
|
227
|
+
if (!active)
|
|
228
|
+
return;
|
|
229
|
+
setStatus(err instanceof StsError && err.code === 'SERVICE_DISABLED' ? 'unavailable' : 'error');
|
|
230
|
+
setMessage(errorMessage(err));
|
|
231
|
+
});
|
|
232
|
+
return () => {
|
|
233
|
+
active = false;
|
|
234
|
+
};
|
|
235
|
+
}, [client]);
|
|
236
|
+
const setValue = useCallback((fieldId, value) => {
|
|
237
|
+
setValues((current) => ({ ...current, [fieldId]: value }));
|
|
238
|
+
}, []);
|
|
239
|
+
const submit = useCallback(async () => {
|
|
240
|
+
setStatus('submitting');
|
|
241
|
+
setFieldError(null);
|
|
242
|
+
try {
|
|
243
|
+
setMessage(await client.form.submit(values, consent));
|
|
244
|
+
setStatus('success');
|
|
245
|
+
setValues({});
|
|
246
|
+
setConsent(false);
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
setMessage(errorMessage(err));
|
|
250
|
+
setFieldError(err instanceof StsError ? err.fieldId : null);
|
|
251
|
+
setStatus('error');
|
|
252
|
+
}
|
|
253
|
+
}, [client, consent, values]);
|
|
254
|
+
return { form, values, setValue, consent, setConsent, submit, status, message, fieldError };
|
|
255
|
+
}
|
|
256
|
+
// ── Wizard ─────────────────────────────────────────────────────────────────
|
|
257
|
+
/**
|
|
258
|
+
* Voice tour across your own page. Tag sections with `data-sts-step="…"`; style `[data-sts-active]`.
|
|
259
|
+
* Each business has one wizard, so `slug` is optional.
|
|
260
|
+
*/
|
|
261
|
+
export function useStsWizard(slug) {
|
|
262
|
+
const client = useSts();
|
|
263
|
+
const [wizard, setWizard] = useState(null);
|
|
264
|
+
const [error, setError] = useState(null);
|
|
265
|
+
const [playing, setPlaying] = useState(false);
|
|
266
|
+
const [speaking, setSpeaking] = useState(false);
|
|
267
|
+
const [step, setStep] = useState(null);
|
|
268
|
+
const [stepIndex, setStepIndex] = useState(-1);
|
|
269
|
+
const [muted, setMutedState] = useState(false);
|
|
270
|
+
const playerRef = useRef(null);
|
|
271
|
+
useEffect(() => {
|
|
272
|
+
let active = true;
|
|
273
|
+
const resolveSlug = slug
|
|
274
|
+
? Promise.resolve(slug)
|
|
275
|
+
: client.info().then((info) => {
|
|
276
|
+
const first = info.services.wizards[0]?.slug;
|
|
277
|
+
if (!first)
|
|
278
|
+
throw new Error('No published wizard for this business');
|
|
279
|
+
return first;
|
|
280
|
+
});
|
|
281
|
+
resolveSlug
|
|
282
|
+
.then((wizardSlug) => client.wizard.load(wizardSlug))
|
|
283
|
+
.then((value) => active && setWizard(value))
|
|
284
|
+
.catch((err) => active && setError(errorMessage(err)));
|
|
285
|
+
return () => {
|
|
286
|
+
active = false;
|
|
287
|
+
playerRef.current?.stop();
|
|
288
|
+
};
|
|
289
|
+
}, [client, slug]);
|
|
290
|
+
const makePlayer = useCallback(() => {
|
|
291
|
+
if (!wizard)
|
|
292
|
+
return null;
|
|
293
|
+
playerRef.current?.stop();
|
|
294
|
+
const player = createWizardPlayer(wizard, {
|
|
295
|
+
onStep: (current, index) => {
|
|
296
|
+
setStep(current);
|
|
297
|
+
setStepIndex(index);
|
|
298
|
+
},
|
|
299
|
+
onSpeakingChange: setSpeaking,
|
|
300
|
+
onEnd: () => setPlaying(false),
|
|
301
|
+
});
|
|
302
|
+
player.setMuted(muted);
|
|
303
|
+
playerRef.current = player;
|
|
304
|
+
setPlaying(true);
|
|
305
|
+
return player;
|
|
306
|
+
}, [muted, wizard]);
|
|
307
|
+
/** The tour leads: scrolls to each section and narrates it. Call from a click handler. */
|
|
308
|
+
const start = useCallback(() => {
|
|
309
|
+
makePlayer()?.start();
|
|
310
|
+
}, [makePlayer]);
|
|
311
|
+
/**
|
|
312
|
+
* The visitor leads: each section speaks when they scroll to it. Call from your "Start" or
|
|
313
|
+
* "Sound on" button the first time. Call again after a client-side page change.
|
|
314
|
+
*/
|
|
315
|
+
const follow = useCallback(() => {
|
|
316
|
+
const player = playerRef.current ?? makePlayer();
|
|
317
|
+
player?.follow();
|
|
318
|
+
}, [makePlayer]);
|
|
319
|
+
/** The visitor's last answer to "sound on or off", remembered per tour in this browser. */
|
|
320
|
+
const savedSound = useCallback(() => {
|
|
321
|
+
if (!wizard || typeof localStorage === 'undefined')
|
|
322
|
+
return null;
|
|
323
|
+
try {
|
|
324
|
+
const value = localStorage.getItem(`sts-tour-sound:${wizard.slug}`);
|
|
325
|
+
return value === 'on' || value === 'off' ? value : null;
|
|
326
|
+
}
|
|
327
|
+
catch {
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
}, [wizard]);
|
|
331
|
+
const rememberSound = useCallback((choice) => {
|
|
332
|
+
if (!wizard || typeof localStorage === 'undefined')
|
|
333
|
+
return;
|
|
334
|
+
try {
|
|
335
|
+
localStorage.setItem(`sts-tour-sound:${wizard.slug}`, choice);
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
// Private windows can refuse storage; the visitor is simply asked again next time.
|
|
339
|
+
}
|
|
340
|
+
}, [wizard]);
|
|
341
|
+
const stop = useCallback(() => {
|
|
342
|
+
playerRef.current?.stop();
|
|
343
|
+
setPlaying(false);
|
|
344
|
+
}, []);
|
|
345
|
+
/**
|
|
346
|
+
* Plays one step from your own event (by id or `data-sts-step` name), e.g.
|
|
347
|
+
* `window.addEventListener('pricing-open', () => tour.play('pricing'))`. Needs one click on the
|
|
348
|
+
* page first for sound (any start/follow, or `unlock()`).
|
|
349
|
+
*/
|
|
350
|
+
const play = useCallback((stepName) => {
|
|
351
|
+
const player = playerRef.current ?? makePlayer();
|
|
352
|
+
return player?.play(stepName) ?? false;
|
|
353
|
+
}, [makePlayer]);
|
|
354
|
+
/** Call from any click so later `play()` calls can make sound. */
|
|
355
|
+
const unlock = useCallback(() => {
|
|
356
|
+
const player = playerRef.current ?? makePlayer();
|
|
357
|
+
player?.unlock();
|
|
358
|
+
}, [makePlayer]);
|
|
359
|
+
const setMuted = useCallback((value) => {
|
|
360
|
+
setMutedState(value);
|
|
361
|
+
playerRef.current?.setMuted(value);
|
|
362
|
+
}, []);
|
|
363
|
+
return {
|
|
364
|
+
wizard,
|
|
365
|
+
ready: Boolean(wizard),
|
|
366
|
+
start,
|
|
367
|
+
follow,
|
|
368
|
+
savedSound,
|
|
369
|
+
rememberSound,
|
|
370
|
+
stop,
|
|
371
|
+
play,
|
|
372
|
+
unlock,
|
|
373
|
+
playing,
|
|
374
|
+
speaking,
|
|
375
|
+
step,
|
|
376
|
+
stepIndex,
|
|
377
|
+
caption: step?.narration ?? '',
|
|
378
|
+
muted,
|
|
379
|
+
setMuted,
|
|
380
|
+
error,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
// ── Business details ───────────────────────────────────────────────────────
|
|
384
|
+
export function useStsBusiness() {
|
|
385
|
+
const client = useSts();
|
|
386
|
+
const [business, setBusiness] = useState(null);
|
|
387
|
+
const [error, setError] = useState(null);
|
|
388
|
+
useEffect(() => {
|
|
389
|
+
let active = true;
|
|
390
|
+
client.business
|
|
391
|
+
.get()
|
|
392
|
+
.then((value) => active && setBusiness(value))
|
|
393
|
+
.catch((err) => active && setError(errorMessage(err)));
|
|
394
|
+
return () => {
|
|
395
|
+
active = false;
|
|
396
|
+
};
|
|
397
|
+
}, [client]);
|
|
398
|
+
return { business, error };
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* The business's own answers to the questions customers ask most, for an FAQ block your
|
|
402
|
+
* designer styles. Pair each with `useStsChat().send(question)` for an "Ask more" button.
|
|
403
|
+
*/
|
|
404
|
+
export function useStsAnswers() {
|
|
405
|
+
const client = useSts();
|
|
406
|
+
const [answers, setAnswers] = useState([]);
|
|
407
|
+
const [ready, setReady] = useState(false);
|
|
408
|
+
const [error, setError] = useState(null);
|
|
409
|
+
useEffect(() => {
|
|
410
|
+
let active = true;
|
|
411
|
+
client.business
|
|
412
|
+
.answers()
|
|
413
|
+
.then((value) => active && setAnswers(value))
|
|
414
|
+
.catch((err) => active && setError(errorMessage(err)))
|
|
415
|
+
.finally(() => active && setReady(true));
|
|
416
|
+
return () => {
|
|
417
|
+
active = false;
|
|
418
|
+
};
|
|
419
|
+
}, [client]);
|
|
420
|
+
return { answers, ready, error };
|
|
421
|
+
}
|
|
422
|
+
/** Reviews the business chose to show, for a reviews block your designer styles. */
|
|
423
|
+
export function useStsReviews() {
|
|
424
|
+
const client = useSts();
|
|
425
|
+
const [reviews, setReviews] = useState([]);
|
|
426
|
+
const [ready, setReady] = useState(false);
|
|
427
|
+
const [error, setError] = useState(null);
|
|
428
|
+
useEffect(() => {
|
|
429
|
+
let active = true;
|
|
430
|
+
client.business
|
|
431
|
+
.reviews()
|
|
432
|
+
.then((value) => active && setReviews(value))
|
|
433
|
+
.catch((err) => active && setError(errorMessage(err)))
|
|
434
|
+
.finally(() => active && setReady(true));
|
|
435
|
+
return () => {
|
|
436
|
+
active = false;
|
|
437
|
+
};
|
|
438
|
+
}, [client]);
|
|
439
|
+
return { reviews, ready, error };
|
|
440
|
+
}
|