@superbased/feedback-widget 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 +202 -0
- package/NOTICE +4 -0
- package/README.md +267 -2
- package/dist/define.d.ts +2 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +8 -0
- package/dist/define.js.map +1 -0
- package/dist/index.d.ts +138 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +457 -0
- package/dist/index.js.map +1 -0
- package/dist/react.d.ts +64 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +89 -0
- package/dist/react.js.map +1 -0
- package/package.json +77 -4
- package/src/define.ts +9 -0
- package/src/index.ts +490 -0
- package/src/react.ts +132 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<superbased-feedback>`: an end user's thumbs up / down, and an optional
|
|
3
|
+
* comment, on one AI answer (plan W5-6). Framework-agnostic, no runtime
|
|
4
|
+
* dependencies, keyboard and screen-reader accessible, themeable with CSS
|
|
5
|
+
* variables.
|
|
6
|
+
*
|
|
7
|
+
* Security: the browser never holds a superbased SDK key. The widget only
|
|
8
|
+
* dispatches a `superbased-feedback` event and, when `endpoint` is set,
|
|
9
|
+
* POSTs to YOUR backend (same origin by default). Your backend calls the
|
|
10
|
+
* SDK's `feedback()` with the trace and span id it rendered the answer
|
|
11
|
+
* with. See the README for that server half in each SDK.
|
|
12
|
+
*
|
|
13
|
+
* ```html
|
|
14
|
+
* <superbased-feedback trace-id="4bf9..." span-id="00f0..." endpoint="/api/feedback" comment="optional"></superbased-feedback>
|
|
15
|
+
* <script type="module">import { defineFeedbackElement } from "@superbased/feedback-widget"; defineFeedbackElement();</script>
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
/** The custom element's default tag name. */
|
|
19
|
+
export const TAG = "superbased-feedback";
|
|
20
|
+
/** The event the widget dispatches (bubbles, composed, cancelable: `preventDefault()` skips the POST). */
|
|
21
|
+
export const FEEDBACK_EVENT = "superbased-feedback";
|
|
22
|
+
/** Dispatched after the POST answered 2xx (`detail`: the payload and the status). */
|
|
23
|
+
export const SENT_EVENT = "superbased-feedback-sent";
|
|
24
|
+
/** Dispatched when the POST failed (`detail`: the payload, the status (0 = no answer) and the error). */
|
|
25
|
+
export const ERROR_EVENT = "superbased-feedback-error";
|
|
26
|
+
/** The score name the SDK's `feedback()` uses by default. */
|
|
27
|
+
export const DEFAULT_NAME = "user_feedback";
|
|
28
|
+
export const DEFAULT_TEXTS = {
|
|
29
|
+
question: "Was this answer helpful?",
|
|
30
|
+
labelUp: "Helpful",
|
|
31
|
+
labelDown: "Not helpful",
|
|
32
|
+
commentLabel: "Tell us more (optional)",
|
|
33
|
+
commentPlaceholder: "",
|
|
34
|
+
submit: "Send",
|
|
35
|
+
skip: "Skip",
|
|
36
|
+
thanks: "Thanks for your feedback.",
|
|
37
|
+
error: "Your feedback could not be sent.",
|
|
38
|
+
commentRequired: "Please add a comment.",
|
|
39
|
+
commentTooLong: "Your comment is too long.",
|
|
40
|
+
};
|
|
41
|
+
const TEXT_ATTRS = {
|
|
42
|
+
question: "question",
|
|
43
|
+
labelUp: "label-up",
|
|
44
|
+
labelDown: "label-down",
|
|
45
|
+
commentLabel: "comment-label",
|
|
46
|
+
commentPlaceholder: "comment-placeholder",
|
|
47
|
+
submit: "submit-label",
|
|
48
|
+
skip: "skip-label",
|
|
49
|
+
thanks: "thanks",
|
|
50
|
+
error: "error-text",
|
|
51
|
+
commentRequired: "comment-required-text",
|
|
52
|
+
commentTooLong: "comment-too-long-text",
|
|
53
|
+
};
|
|
54
|
+
const TRACE = /^[0-9a-f]{32}$/;
|
|
55
|
+
const SPAN = /^[0-9a-f]{16}$/;
|
|
56
|
+
const NAME = /^[a-z0-9_.-]{1,64}$/;
|
|
57
|
+
/** The SDKs' comment bound (scores.md: up to 2,000 bytes). */
|
|
58
|
+
export const MAX_COMMENT_BYTES = 2000;
|
|
59
|
+
/** Why a payload cannot be sent ("" when it can). The same rules the SDKs apply to `feedback()`. */
|
|
60
|
+
/** A comment's size in UTF-8 bytes, the unit the SDKs bound it in. */
|
|
61
|
+
export function commentBytes(comment) {
|
|
62
|
+
return new TextEncoder().encode(comment).length;
|
|
63
|
+
}
|
|
64
|
+
export function validatePayload(p) {
|
|
65
|
+
if (!TRACE.test(p.trace_id) || /^0+$/.test(p.trace_id))
|
|
66
|
+
return "trace-id must be 32 lowercase hex characters, not all zero";
|
|
67
|
+
if (p.span_id !== undefined && (!SPAN.test(p.span_id) || /^0+$/.test(p.span_id)))
|
|
68
|
+
return "span-id must be 16 lowercase hex characters";
|
|
69
|
+
if (typeof p.value !== "boolean")
|
|
70
|
+
return "value must be true or false";
|
|
71
|
+
if (!NAME.test(p.name))
|
|
72
|
+
return "name must be 1-64 chars of a-z 0-9 _ . -";
|
|
73
|
+
if (p.comment !== undefined && commentBytes(p.comment) > MAX_COMMENT_BYTES)
|
|
74
|
+
return `comment is over ${MAX_COMMENT_BYTES} bytes`;
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Whether `endpoint` may be posted to from `origin`: relative and same-origin
|
|
79
|
+
* URLs always; another origin only with `allowCrossOrigin` (the trace id and
|
|
80
|
+
* the comment are the app's data, so they go to the app's own backend by
|
|
81
|
+
* default). Never a non-http(s) scheme.
|
|
82
|
+
*/
|
|
83
|
+
export function endpointAllowed(endpoint, origin, allowCrossOrigin = false) {
|
|
84
|
+
let u;
|
|
85
|
+
try {
|
|
86
|
+
u = new URL(endpoint, origin);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (u.protocol !== "http:" && u.protocol !== "https:")
|
|
92
|
+
return false;
|
|
93
|
+
return allowCrossOrigin || u.origin === origin;
|
|
94
|
+
}
|
|
95
|
+
/** The theme variables (set them on the element or any ancestor). */
|
|
96
|
+
export const CSS_VARIABLES = [
|
|
97
|
+
"--sb-feedback-font",
|
|
98
|
+
"--sb-feedback-fg",
|
|
99
|
+
"--sb-feedback-muted",
|
|
100
|
+
"--sb-feedback-bg",
|
|
101
|
+
"--sb-feedback-border",
|
|
102
|
+
"--sb-feedback-accent",
|
|
103
|
+
"--sb-feedback-accent-fg",
|
|
104
|
+
"--sb-feedback-radius",
|
|
105
|
+
"--sb-feedback-gap",
|
|
106
|
+
"--sb-feedback-focus",
|
|
107
|
+
"--sb-feedback-error",
|
|
108
|
+
];
|
|
109
|
+
const STYLE = `
|
|
110
|
+
:host { display: inline-block; font: var(--sb-feedback-font, inherit); color: var(--sb-feedback-fg, inherit); }
|
|
111
|
+
:host([hidden]) { display: none; }
|
|
112
|
+
.root { display: flex; flex-direction: column; gap: var(--sb-feedback-gap, 0.5em); }
|
|
113
|
+
.row { display: flex; align-items: center; gap: var(--sb-feedback-gap, 0.5em); flex-wrap: wrap; }
|
|
114
|
+
.question { color: var(--sb-feedback-muted, inherit); }
|
|
115
|
+
button { font: inherit; color: inherit; cursor: pointer; background: var(--sb-feedback-bg, transparent);
|
|
116
|
+
border: 1px solid var(--sb-feedback-border, currentColor); border-radius: var(--sb-feedback-radius, 0.5em);
|
|
117
|
+
padding: 0.25em 0.6em; min-width: 2.75em; min-height: 2.75em; }
|
|
118
|
+
button[aria-pressed="true"], button.primary { background: var(--sb-feedback-accent, #2b6cb0); color: var(--sb-feedback-accent-fg, #fff);
|
|
119
|
+
border-color: var(--sb-feedback-accent, #2b6cb0); }
|
|
120
|
+
button:disabled { cursor: default; opacity: 0.6; }
|
|
121
|
+
button:focus-visible, textarea:focus-visible { outline: 2px solid var(--sb-feedback-focus, #2b6cb0); outline-offset: 2px; }
|
|
122
|
+
textarea { font: inherit; color: inherit; background: var(--sb-feedback-bg, transparent); width: 100%; box-sizing: border-box;
|
|
123
|
+
border: 1px solid var(--sb-feedback-border, currentColor); border-radius: var(--sb-feedback-radius, 0.5em); padding: 0.4em; min-height: 4em; }
|
|
124
|
+
.status { min-height: 1.2em; }
|
|
125
|
+
.status.error, .hint { color: var(--sb-feedback-error, #c53030); }
|
|
126
|
+
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
|
127
|
+
`;
|
|
128
|
+
/** One constructed sheet per document (a sheet is adoptable only in the document that built it). */
|
|
129
|
+
const sheets = new WeakMap();
|
|
130
|
+
/**
|
|
131
|
+
* Put the styles on the shadow root without inline CSS where the browser
|
|
132
|
+
* can: a constructed stylesheet (`adoptedStyleSheets`) is not inline style,
|
|
133
|
+
* so a strict `style-src` Content Security Policy allows it. Older browsers
|
|
134
|
+
* get a `<style>` element carrying the element's `nonce` attribute, which a
|
|
135
|
+
* nonce-based policy allows. Returns which one it used.
|
|
136
|
+
*/
|
|
137
|
+
export function applyStyles(root, nonce) {
|
|
138
|
+
const doc = root.ownerDocument;
|
|
139
|
+
const Sheet = doc.defaultView?.CSSStyleSheet;
|
|
140
|
+
const canAdopt = "adoptedStyleSheets" in root && typeof Sheet === "function" && typeof Sheet.prototype.replaceSync === "function";
|
|
141
|
+
if (canAdopt) {
|
|
142
|
+
try {
|
|
143
|
+
let sheet = sheets.get(doc);
|
|
144
|
+
if (!sheet) {
|
|
145
|
+
sheet = new Sheet();
|
|
146
|
+
sheet.replaceSync(STYLE);
|
|
147
|
+
sheets.set(doc, sheet);
|
|
148
|
+
}
|
|
149
|
+
const own = sheet;
|
|
150
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets.filter((x) => x !== own), own];
|
|
151
|
+
return "adopted";
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
// fall through to the element
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const el = root.ownerDocument.createElement("style");
|
|
158
|
+
if (nonce)
|
|
159
|
+
el.setAttribute("nonce", nonce);
|
|
160
|
+
el.textContent = STYLE;
|
|
161
|
+
root.prepend(el);
|
|
162
|
+
return "style";
|
|
163
|
+
}
|
|
164
|
+
/* A base class that also loads where there is no DOM (server rendering, Node): the element is only defined in a browser. */
|
|
165
|
+
const Base = (typeof HTMLElement !== "undefined" ? HTMLElement : class {
|
|
166
|
+
});
|
|
167
|
+
let seq = 0;
|
|
168
|
+
/**
|
|
169
|
+
* The element. Attributes: `trace-id` (required), `span-id`, `endpoint`,
|
|
170
|
+
* `name` (default `user_feedback`), `comment` (`off` | `optional` |
|
|
171
|
+
* `required`), `token`, `cross-origin` (allow an endpoint on another
|
|
172
|
+
* origin), `credentials` (`same-origin` default, `include`, `omit`),
|
|
173
|
+
* `disabled`, `nonce` (for a nonce-based CSP in browsers without
|
|
174
|
+
* constructable stylesheets), and the texts (`question`, `label-up`,
|
|
175
|
+
* `label-down`, `comment-label`, `comment-placeholder`, `submit-label`,
|
|
176
|
+
* `skip-label`, `thanks`, `error-text`, `comment-required-text`,
|
|
177
|
+
* `comment-too-long-text`). Properties: `fetch` (a
|
|
178
|
+
* fetch to use instead of the global one), `value` (the vote, or null),
|
|
179
|
+
* `reset()`.
|
|
180
|
+
*/
|
|
181
|
+
export class FeedbackElement extends Base {
|
|
182
|
+
constructor() {
|
|
183
|
+
super(...arguments);
|
|
184
|
+
/** A fetch to use instead of the global one (tests, an app's own client). */
|
|
185
|
+
this.fetch = null;
|
|
186
|
+
this.vote = null;
|
|
187
|
+
this.stage = "vote";
|
|
188
|
+
this.lastPayload = null;
|
|
189
|
+
this.uid = `sbf${++seq}`;
|
|
190
|
+
this.built = false;
|
|
191
|
+
}
|
|
192
|
+
static get observedAttributes() {
|
|
193
|
+
return ["trace-id", "span-id", "comment", "disabled", ...Object.values(TEXT_ATTRS)];
|
|
194
|
+
}
|
|
195
|
+
/** The end user's vote (true up, false down), or null before one. */
|
|
196
|
+
get value() {
|
|
197
|
+
return this.vote;
|
|
198
|
+
}
|
|
199
|
+
/** Back to the start (for example when the element is reused for another answer). */
|
|
200
|
+
reset() {
|
|
201
|
+
this.vote = null;
|
|
202
|
+
this.stage = "vote";
|
|
203
|
+
this.lastPayload = null;
|
|
204
|
+
this.render();
|
|
205
|
+
}
|
|
206
|
+
connectedCallback() {
|
|
207
|
+
if (!this.built)
|
|
208
|
+
this.build();
|
|
209
|
+
this.render();
|
|
210
|
+
}
|
|
211
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
212
|
+
if (oldValue === newValue || !this.built)
|
|
213
|
+
return;
|
|
214
|
+
if (name === "trace-id" || name === "span-id")
|
|
215
|
+
this.vote = null; // a new answer: a new vote
|
|
216
|
+
if (name === "trace-id" || name === "span-id")
|
|
217
|
+
this.stage = "vote";
|
|
218
|
+
this.render();
|
|
219
|
+
}
|
|
220
|
+
texts() {
|
|
221
|
+
const out = { ...DEFAULT_TEXTS };
|
|
222
|
+
for (const [k, attr] of Object.entries(TEXT_ATTRS)) {
|
|
223
|
+
const v = this.getAttribute(attr);
|
|
224
|
+
if (v !== null)
|
|
225
|
+
out[k] = v;
|
|
226
|
+
}
|
|
227
|
+
if (this.commentMode() === "required" && this.getAttribute("comment-label") === null)
|
|
228
|
+
out.commentLabel = "Tell us more";
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
commentMode() {
|
|
232
|
+
const m = (this.getAttribute("comment") ?? "off").trim().toLowerCase();
|
|
233
|
+
return m === "optional" || m === "required" ? m : "off";
|
|
234
|
+
}
|
|
235
|
+
$(sel) {
|
|
236
|
+
return this.shadowRoot.querySelector(sel);
|
|
237
|
+
}
|
|
238
|
+
build() {
|
|
239
|
+
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
240
|
+
const id = this.uid;
|
|
241
|
+
root.innerHTML = `<div class="root" part="root">
|
|
242
|
+
<div class="row" role="group" aria-labelledby="${id}-q" part="vote">
|
|
243
|
+
<span class="question" id="${id}-q" part="question"></span>
|
|
244
|
+
<button type="button" class="up" part="button up" aria-pressed="false"><span aria-hidden="true">👍</span><span class="sr up-label"></span></button>
|
|
245
|
+
<button type="button" class="down" part="button down" aria-pressed="false"><span aria-hidden="true">👎</span><span class="sr down-label"></span></button>
|
|
246
|
+
</div>
|
|
247
|
+
<form class="comment" part="comment" hidden novalidate>
|
|
248
|
+
<label for="${id}-c" class="comment-label" part="comment-label"></label>
|
|
249
|
+
<textarea id="${id}-c" part="textarea" maxlength="${MAX_COMMENT_BYTES}" aria-describedby="${id}-h"></textarea>
|
|
250
|
+
<span class="hint" id="${id}-h" part="hint" aria-live="assertive"></span>
|
|
251
|
+
<div class="row">
|
|
252
|
+
<button type="submit" class="primary send" part="button submit"></button>
|
|
253
|
+
<button type="button" class="skip" part="button skip"></button>
|
|
254
|
+
</div>
|
|
255
|
+
</form>
|
|
256
|
+
<div class="status" part="status" role="status" aria-live="polite"></div>
|
|
257
|
+
</div>`;
|
|
258
|
+
applyStyles(root, this.getAttribute("nonce"));
|
|
259
|
+
this.$(".up").addEventListener("click", () => this.choose(true));
|
|
260
|
+
this.$(".down").addEventListener("click", () => this.choose(false));
|
|
261
|
+
const form = this.$(".comment");
|
|
262
|
+
form.addEventListener("submit", (e) => {
|
|
263
|
+
e.preventDefault();
|
|
264
|
+
this.submitComment(false);
|
|
265
|
+
});
|
|
266
|
+
this.$(".skip").addEventListener("click", () => this.submitComment(true));
|
|
267
|
+
const ta = this.$("textarea");
|
|
268
|
+
// maxlength counts characters; the bound is bytes, so say so as they type.
|
|
269
|
+
ta.addEventListener("input", () => this.checkLength());
|
|
270
|
+
ta.addEventListener("keydown", (e) => {
|
|
271
|
+
const k = e;
|
|
272
|
+
if (k.key === "Escape") {
|
|
273
|
+
k.preventDefault();
|
|
274
|
+
this.submitComment(true);
|
|
275
|
+
}
|
|
276
|
+
else if (k.key === "Enter" && (k.ctrlKey || k.metaKey)) {
|
|
277
|
+
k.preventDefault();
|
|
278
|
+
this.submitComment(false);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
this.built = true;
|
|
282
|
+
}
|
|
283
|
+
render() {
|
|
284
|
+
if (!this.built)
|
|
285
|
+
return;
|
|
286
|
+
const t = this.texts();
|
|
287
|
+
const disabled = this.hasAttribute("disabled") || this.stage === "sending" || this.stage === "done";
|
|
288
|
+
this.$(".question").textContent = t.question;
|
|
289
|
+
for (const [cls, label, on] of [
|
|
290
|
+
["up", t.labelUp, this.vote === true],
|
|
291
|
+
["down", t.labelDown, this.vote === false],
|
|
292
|
+
]) {
|
|
293
|
+
const b = this.$(`.${cls}`);
|
|
294
|
+
b.setAttribute("aria-pressed", String(on));
|
|
295
|
+
b.setAttribute("aria-label", label);
|
|
296
|
+
b.title = label;
|
|
297
|
+
this.$(`.${cls}-label`).textContent = label;
|
|
298
|
+
b.disabled = disabled;
|
|
299
|
+
}
|
|
300
|
+
const form = this.$(".comment");
|
|
301
|
+
form.hidden = this.stage !== "comment";
|
|
302
|
+
this.$(".comment-label").textContent = t.commentLabel;
|
|
303
|
+
const ta = this.$("textarea");
|
|
304
|
+
ta.placeholder = t.commentPlaceholder;
|
|
305
|
+
const required = this.commentMode() === "required" && this.vote === false;
|
|
306
|
+
if (required)
|
|
307
|
+
ta.setAttribute("aria-required", "true");
|
|
308
|
+
else
|
|
309
|
+
ta.removeAttribute("aria-required");
|
|
310
|
+
this.$(".send").textContent = t.submit;
|
|
311
|
+
const skip = this.$(".skip");
|
|
312
|
+
skip.textContent = t.skip;
|
|
313
|
+
skip.hidden = required;
|
|
314
|
+
const status = this.$(".status");
|
|
315
|
+
status.classList.toggle("error", this.stage === "error");
|
|
316
|
+
status.textContent = this.stage === "done" ? t.thanks : this.stage === "error" ? t.error : "";
|
|
317
|
+
}
|
|
318
|
+
choose(value) {
|
|
319
|
+
if (this.hasAttribute("disabled") || this.stage === "sending" || this.stage === "done")
|
|
320
|
+
return;
|
|
321
|
+
this.vote = value;
|
|
322
|
+
this.$(".hint").textContent = "";
|
|
323
|
+
if (this.commentMode() === "off") {
|
|
324
|
+
void this.send(undefined);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
this.stage = "comment";
|
|
328
|
+
this.render();
|
|
329
|
+
this.$("textarea").focus();
|
|
330
|
+
}
|
|
331
|
+
/** Mark the comment invalid while it is over the byte bound; true when it fits. */
|
|
332
|
+
checkLength() {
|
|
333
|
+
const ta = this.$("textarea");
|
|
334
|
+
const over = commentBytes(ta.value.trim()) > MAX_COMMENT_BYTES;
|
|
335
|
+
if (over) {
|
|
336
|
+
ta.setAttribute("aria-invalid", "true");
|
|
337
|
+
this.$(".hint").textContent = this.texts().commentTooLong;
|
|
338
|
+
}
|
|
339
|
+
else if (this.$(".hint").textContent === this.texts().commentTooLong) {
|
|
340
|
+
ta.removeAttribute("aria-invalid");
|
|
341
|
+
this.$(".hint").textContent = "";
|
|
342
|
+
}
|
|
343
|
+
return !over;
|
|
344
|
+
}
|
|
345
|
+
submitComment(skip) {
|
|
346
|
+
if (this.stage !== "comment" || this.vote === null)
|
|
347
|
+
return;
|
|
348
|
+
const ta = this.$("textarea");
|
|
349
|
+
const text = skip ? "" : ta.value.trim();
|
|
350
|
+
if (!skip && !this.checkLength()) {
|
|
351
|
+
ta.focus();
|
|
352
|
+
return; // the end user shortens it; the vote is kept
|
|
353
|
+
}
|
|
354
|
+
if (!text && this.commentMode() === "required" && this.vote === false) {
|
|
355
|
+
if (skip)
|
|
356
|
+
return; // a required comment cannot be skipped
|
|
357
|
+
this.$(".hint").textContent = this.texts().commentRequired;
|
|
358
|
+
ta.setAttribute("aria-invalid", "true");
|
|
359
|
+
ta.focus();
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
ta.removeAttribute("aria-invalid");
|
|
363
|
+
void this.send(text || undefined);
|
|
364
|
+
}
|
|
365
|
+
/** Build the payload from the attributes; null (with an error event) when it cannot be sent. */
|
|
366
|
+
payload(comment) {
|
|
367
|
+
const p = {
|
|
368
|
+
trace_id: (this.getAttribute("trace-id") ?? "").trim().toLowerCase(),
|
|
369
|
+
value: this.vote,
|
|
370
|
+
name: (this.getAttribute("name") ?? DEFAULT_NAME).trim().toLowerCase(),
|
|
371
|
+
};
|
|
372
|
+
const span = (this.getAttribute("span-id") ?? "").trim().toLowerCase();
|
|
373
|
+
if (span)
|
|
374
|
+
p.span_id = span;
|
|
375
|
+
if (comment)
|
|
376
|
+
p.comment = comment;
|
|
377
|
+
const token = this.getAttribute("token");
|
|
378
|
+
if (token)
|
|
379
|
+
p.token = token;
|
|
380
|
+
const why = validatePayload(p);
|
|
381
|
+
if (why) {
|
|
382
|
+
this.fail(p, 0, why);
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
return p;
|
|
386
|
+
}
|
|
387
|
+
fail(p, status, error) {
|
|
388
|
+
this.stage = "error";
|
|
389
|
+
this.render();
|
|
390
|
+
this.dispatchEvent(new CustomEvent(ERROR_EVENT, { bubbles: true, composed: true, detail: { payload: p, status, error } }));
|
|
391
|
+
}
|
|
392
|
+
async send(comment) {
|
|
393
|
+
const p = this.payload(comment);
|
|
394
|
+
if (!p)
|
|
395
|
+
return;
|
|
396
|
+
this.lastPayload = p;
|
|
397
|
+
const go = this.dispatchEvent(new CustomEvent(FEEDBACK_EVENT, { bubbles: true, composed: true, cancelable: true, detail: p }));
|
|
398
|
+
const endpoint = this.getAttribute("endpoint");
|
|
399
|
+
if (!go || !endpoint) {
|
|
400
|
+
// The app handles the event itself (or asked the widget not to POST).
|
|
401
|
+
this.stage = "done";
|
|
402
|
+
this.render();
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const origin = typeof location !== "undefined" ? location.origin : "http://localhost";
|
|
406
|
+
if (!endpointAllowed(endpoint, origin, this.hasAttribute("cross-origin"))) {
|
|
407
|
+
this.fail(p, 0, `endpoint ${endpoint} is not on this page's origin (add the cross-origin attribute to allow it)`);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
this.stage = "sending";
|
|
411
|
+
this.render();
|
|
412
|
+
const f = this.fetch ?? (typeof fetch === "function" ? (i, n) => fetch(i, n) : null);
|
|
413
|
+
if (!f) {
|
|
414
|
+
this.fail(p, 0, "no fetch available");
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const cred = this.getAttribute("credentials");
|
|
418
|
+
try {
|
|
419
|
+
const resp = await f(endpoint, {
|
|
420
|
+
method: "POST",
|
|
421
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
422
|
+
body: JSON.stringify(p),
|
|
423
|
+
credentials: cred === "include" || cred === "omit" ? cred : "same-origin",
|
|
424
|
+
redirect: "error",
|
|
425
|
+
});
|
|
426
|
+
if (!resp.ok) {
|
|
427
|
+
this.fail(p, resp.status, `HTTP ${resp.status}`);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
this.stage = "done";
|
|
431
|
+
this.render();
|
|
432
|
+
this.dispatchEvent(new CustomEvent(SENT_EVENT, { bubbles: true, composed: true, detail: { payload: p, status: resp.status } }));
|
|
433
|
+
}
|
|
434
|
+
catch (err) {
|
|
435
|
+
this.fail(p, 0, err instanceof Error ? err.message : String(err));
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/** Send the last vote again after an error (no-op otherwise). */
|
|
439
|
+
retry() {
|
|
440
|
+
if (this.stage !== "error" || !this.lastPayload)
|
|
441
|
+
return;
|
|
442
|
+
this.vote = this.lastPayload.value;
|
|
443
|
+
void this.send(this.lastPayload.comment);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Register the element (idempotent; a no-op where there is no
|
|
448
|
+
* `customElements`, such as server rendering). Returns the tag name.
|
|
449
|
+
*/
|
|
450
|
+
export function defineFeedbackElement(tag = TAG) {
|
|
451
|
+
const reg = typeof customElements !== "undefined" ? customElements : null;
|
|
452
|
+
if (reg && !reg.get(tag))
|
|
453
|
+
reg.define(tag, tag === TAG ? FeedbackElement : class extends FeedbackElement {
|
|
454
|
+
});
|
|
455
|
+
return tag;
|
|
456
|
+
}
|
|
457
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRo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* ```tsx
|
|
5
|
+
* import { SuperbasedFeedback } from "@superbased/feedback-widget/react";
|
|
6
|
+
*
|
|
7
|
+
* <SuperbasedFeedback traceId={answer.traceId} spanId={answer.spanId} endpoint="/api/feedback" comment="optional" />
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* It renders the custom element (registering it on first mount) and wires
|
|
11
|
+
* the callbacks to its events. The browser still never holds an SDK key:
|
|
12
|
+
* the element posts to `endpoint` on your own backend, which calls the
|
|
13
|
+
* SDK's `feedback()`.
|
|
14
|
+
*/
|
|
15
|
+
import * as React from "react";
|
|
16
|
+
import { type CommentMode, type FeedbackElement, type FeedbackPayload } from "./index.js";
|
|
17
|
+
export interface SuperbasedFeedbackProps {
|
|
18
|
+
/** The trace the answer belongs to (32 hex), as your server rendered it. */
|
|
19
|
+
traceId: string;
|
|
20
|
+
/** The answer's span (16 hex). */
|
|
21
|
+
spanId?: string;
|
|
22
|
+
/** Your backend's route (same origin unless `crossOrigin`). Without it, only `onFeedback` runs. */
|
|
23
|
+
endpoint?: string;
|
|
24
|
+
/** The score name (default `user_feedback`). */
|
|
25
|
+
name?: string;
|
|
26
|
+
comment?: CommentMode;
|
|
27
|
+
/** An opaque value your server rendered with the answer, passed through to your endpoint. */
|
|
28
|
+
token?: string;
|
|
29
|
+
crossOrigin?: boolean;
|
|
30
|
+
credentials?: "same-origin" | "include" | "omit";
|
|
31
|
+
disabled?: boolean;
|
|
32
|
+
question?: string;
|
|
33
|
+
labelUp?: string;
|
|
34
|
+
labelDown?: string;
|
|
35
|
+
commentLabel?: string;
|
|
36
|
+
commentPlaceholder?: string;
|
|
37
|
+
submitLabel?: string;
|
|
38
|
+
skipLabel?: string;
|
|
39
|
+
thanks?: string;
|
|
40
|
+
errorText?: string;
|
|
41
|
+
commentRequiredText?: string;
|
|
42
|
+
commentTooLongText?: string;
|
|
43
|
+
/** The nonce of a nonce-based CSP, for browsers without constructable stylesheets. */
|
|
44
|
+
nonce?: string;
|
|
45
|
+
/** Called with the payload before the POST; call `event.preventDefault()` to skip the POST. */
|
|
46
|
+
onFeedback?: (payload: FeedbackPayload, event: CustomEvent<FeedbackPayload>) => void;
|
|
47
|
+
onSent?: (detail: {
|
|
48
|
+
payload: FeedbackPayload;
|
|
49
|
+
status: number;
|
|
50
|
+
}) => void;
|
|
51
|
+
onError?: (detail: {
|
|
52
|
+
payload: FeedbackPayload;
|
|
53
|
+
status: number;
|
|
54
|
+
error: string;
|
|
55
|
+
}) => void;
|
|
56
|
+
/** A fetch to use instead of the global one. */
|
|
57
|
+
fetch?: FeedbackElement["fetch"];
|
|
58
|
+
className?: string;
|
|
59
|
+
style?: React.CSSProperties;
|
|
60
|
+
}
|
|
61
|
+
/** The element's attributes for a set of props (exported for tests and server rendering). */
|
|
62
|
+
export declare function feedbackAttributes(props: SuperbasedFeedbackProps): Record<string, string>;
|
|
63
|
+
export declare function SuperbasedFeedback(props: SuperbasedFeedbackProps): React.ReactElement;
|
|
64
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAML,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,eAAe,EACrB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,uBAAuB;IACtC,4EAA4E;IAC5E,OAAO,EAAE,MAAM,CAAC;IAChB,kCAAkC;IAClC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mGAAmG;IACnG,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,6FAA6F;IAC7F,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,OAAO,CAAC;I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zF;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,uBAAuB,GAAG,KAAK,CAAC,YAAY,CAgCrF"}
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React wrapper for `<superbased-feedback>` (React 18 and 19).
|
|
3
|
+
*
|
|
4
|
+
* ```tsx
|
|
5
|
+
* import { SuperbasedFeedback } from "@superbased/feedback-widget/react";
|
|
6
|
+
*
|
|
7
|
+
* <SuperbasedFeedback traceId={answer.traceId} spanId={answer.spanId} endpoint="/api/feedback" comment="optional" />
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* It renders the custom element (registering it on first mount) and wires
|
|
11
|
+
* the callbacks to its events. The browser still never holds an SDK key:
|
|
12
|
+
* the element posts to `endpoint` on your own backend, which calls the
|
|
13
|
+
* SDK's `feedback()`.
|
|
14
|
+
*/
|
|
15
|
+
import * as React from "react";
|
|
16
|
+
import { defineFeedbackElement, ERROR_EVENT, FEEDBACK_EVENT, SENT_EVENT, TAG, } from "./index.js";
|
|
17
|
+
const ATTRS = [
|
|
18
|
+
["traceId", "trace-id"],
|
|
19
|
+
["spanId", "span-id"],
|
|
20
|
+
["endpoint", "endpoint"],
|
|
21
|
+
["name", "name"],
|
|
22
|
+
["comment", "comment"],
|
|
23
|
+
["token", "token"],
|
|
24
|
+
["credentials", "credentials"],
|
|
25
|
+
["question", "question"],
|
|
26
|
+
["labelUp", "label-up"],
|
|
27
|
+
["labelDown", "label-down"],
|
|
28
|
+
["commentLabel", "comment-label"],
|
|
29
|
+
["commentPlaceholder", "comment-placeholder"],
|
|
30
|
+
["submitLabel", "submit-label"],
|
|
31
|
+
["skipLabel", "skip-label"],
|
|
32
|
+
["thanks", "thanks"],
|
|
33
|
+
["errorText", "error-text"],
|
|
34
|
+
["commentRequiredText", "comment-required-text"],
|
|
35
|
+
["commentTooLongText", "comment-too-long-text"],
|
|
36
|
+
["nonce", "nonce"],
|
|
37
|
+
];
|
|
38
|
+
/** The element's attributes for a set of props (exported for tests and server rendering). */
|
|
39
|
+
export function feedbackAttributes(props) {
|
|
40
|
+
const out = {};
|
|
41
|
+
for (const [prop, attr] of ATTRS) {
|
|
42
|
+
const v = props[prop];
|
|
43
|
+
if (typeof v === "string" && v !== "")
|
|
44
|
+
out[attr] = v;
|
|
45
|
+
}
|
|
46
|
+
if (props.crossOrigin)
|
|
47
|
+
out["cross-origin"] = "";
|
|
48
|
+
if (props.disabled)
|
|
49
|
+
out.disabled = "";
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
export function SuperbasedFeedback(props) {
|
|
53
|
+
const ref = React.useRef(null);
|
|
54
|
+
const cb = React.useRef(props);
|
|
55
|
+
cb.current = props;
|
|
56
|
+
React.useEffect(() => {
|
|
57
|
+
defineFeedbackElement();
|
|
58
|
+
}, []);
|
|
59
|
+
React.useEffect(() => {
|
|
60
|
+
const el = ref.current;
|
|
61
|
+
if (!el)
|
|
62
|
+
return;
|
|
63
|
+
el.fetch = props.fetch ?? null;
|
|
64
|
+
}, [props.fetch]);
|
|
65
|
+
React.useEffect(() => {
|
|
66
|
+
const el = ref.current;
|
|
67
|
+
if (!el)
|
|
68
|
+
return;
|
|
69
|
+
const onFeedback = (e) => cb.current.onFeedback?.(e.detail, e);
|
|
70
|
+
const onSent = (e) => cb.current.onSent?.(e.detail);
|
|
71
|
+
const onError = (e) => cb.current.onError?.(e.detail);
|
|
72
|
+
el.addEventListener(FEEDBACK_EVENT, onFeedback);
|
|
73
|
+
el.addEventListener(SENT_EVENT, onSent);
|
|
74
|
+
el.addEventListener(ERROR_EVENT, onError);
|
|
75
|
+
return () => {
|
|
76
|
+
el.removeEventListener(FEEDBACK_EVENT, onFeedback);
|
|
77
|
+
el.removeEventListener(SENT_EVENT, onSent);
|
|
78
|
+
el.removeEventListener(ERROR_EVENT, onError);
|
|
79
|
+
};
|
|
80
|
+
}, []);
|
|
81
|
+
// `class` (not className): React passes custom-element props through as attributes.
|
|
82
|
+
const attrs = { ...feedbackAttributes(props), ref };
|
|
83
|
+
if (props.className)
|
|
84
|
+
attrs.class = props.className;
|
|
85
|
+
if (props.style)
|
|
86
|
+
attrs.style = props.style;
|
|
87
|
+
return React.createElement(TAG, attrs);
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,cAAc,EACd,UAAU,EACV,GAAG,GAIJ,MAAM,YAAY,CAAC;AAwCpB,MAAM,KAAK,GAAmD;IAC5D,CAAC,SAAS,EAAE,UAAU,CAAC;IACvB,CAAC,QAAQ,EAAE,SAAS,CAAC;IACrB,CAAC,UAAU,EAAE,UAAU,CAAC;IACxB,CAAC,MAAM,EAAE,MAAM,CAAC;IAChB,CAAC,SAAS,EAAE,SAAS,CAAC;IACtB,CAAC,OAAO,EAAE,OAAO,CAAC;IAClB,CAAC,aAAa,EAAE,aAAa,CAAC;IAC9B,CAAC,UAAU,EAAE,UAAU,CAAC;IACxB,CAAC,SAAS,EAAE,UAAU,CAAC;IACvB,CAAC,WAAW,EAAE,YAAY,CAAC;IAC3B,CAAC,cAAc,EAAE,eAAe,CAAC;IACjC,CAAC,oBAAoB,EAAE,qBAAqB,CAAC;IAC7C,CAAC,aAAa,EAAE,cAAc,CAAC;IAC/B,CAAC,WAAW,EAAE,YAAY,CAAC;IAC3B,CAAC,QAAQ,EAAE,QAAQ,CAAC;IACpB,CAAC,WAAW,EAAE,YAAY,CAAC;IAC3B,CAAC,qBAAqB,EAAE,uBAAuB,CAAC;IAChD,CAAC,oBAAoB,EAAE,uBAAuB,CAAC;IAC/C,CAAC,OAAO,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,6FAA6F;A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