@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/src/react.ts ADDED
@@ -0,0 +1,132 @@
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
+
17
+ import {
18
+ defineFeedbackElement,
19
+ ERROR_EVENT,
20
+ FEEDBACK_EVENT,
21
+ SENT_EVENT,
22
+ TAG,
23
+ type CommentMode,
24
+ type FeedbackElement,
25
+ type FeedbackPayload,
26
+ } from "./index.js";
27
+
28
+ export interface SuperbasedFeedbackProps {
29
+ /** The trace the answer belongs to (32 hex), as your server rendered it. */
30
+ traceId: string;
31
+ /** The answer's span (16 hex). */
32
+ spanId?: string;
33
+ /** Your backend's route (same origin unless `crossOrigin`). Without it, only `onFeedback` runs. */
34
+ endpoint?: string;
35
+ /** The score name (default `user_feedback`). */
36
+ name?: string;
37
+ comment?: CommentMode;
38
+ /** An opaque value your server rendered with the answer, passed through to your endpoint. */
39
+ token?: string;
40
+ crossOrigin?: boolean;
41
+ credentials?: "same-origin" | "include" | "omit";
42
+ disabled?: boolean;
43
+ question?: string;
44
+ labelUp?: string;
45
+ labelDown?: string;
46
+ commentLabel?: string;
47
+ commentPlaceholder?: string;
48
+ submitLabel?: string;
49
+ skipLabel?: string;
50
+ thanks?: string;
51
+ errorText?: string;
52
+ commentRequiredText?: string;
53
+ commentTooLongText?: string;
54
+ /** The nonce of a nonce-based CSP, for browsers without constructable stylesheets. */
55
+ nonce?: string;
56
+ /** Called with the payload before the POST; call `event.preventDefault()` to skip the POST. */
57
+ onFeedback?: (payload: FeedbackPayload, event: CustomEvent<FeedbackPayload>) => void;
58
+ onSent?: (detail: { payload: FeedbackPayload; status: number }) => void;
59
+ onError?: (detail: { payload: FeedbackPayload; status: number; error: string }) => void;
60
+ /** A fetch to use instead of the global one. */
61
+ fetch?: FeedbackElement["fetch"];
62
+ className?: string;
63
+ style?: React.CSSProperties;
64
+ }
65
+
66
+ const ATTRS: Array<[keyof SuperbasedFeedbackProps, string]> = [
67
+ ["traceId", "trace-id"],
68
+ ["spanId", "span-id"],
69
+ ["endpoint", "endpoint"],
70
+ ["name", "name"],
71
+ ["comment", "comment"],
72
+ ["token", "token"],
73
+ ["credentials", "credentials"],
74
+ ["question", "question"],
75
+ ["labelUp", "label-up"],
76
+ ["labelDown", "label-down"],
77
+ ["commentLabel", "comment-label"],
78
+ ["commentPlaceholder", "comment-placeholder"],
79
+ ["submitLabel", "submit-label"],
80
+ ["skipLabel", "skip-label"],
81
+ ["thanks", "thanks"],
82
+ ["errorText", "error-text"],
83
+ ["commentRequiredText", "comment-required-text"],
84
+ ["commentTooLongText", "comment-too-long-text"],
85
+ ["nonce", "nonce"],
86
+ ];
87
+
88
+ /** The element's attributes for a set of props (exported for tests and server rendering). */
89
+ export function feedbackAttributes(props: SuperbasedFeedbackProps): Record<string, string> {
90
+ const out: Record<string, string> = {};
91
+ for (const [prop, attr] of ATTRS) {
92
+ const v = props[prop];
93
+ if (typeof v === "string" && v !== "") out[attr] = v;
94
+ }
95
+ if (props.crossOrigin) out["cross-origin"] = "";
96
+ if (props.disabled) out.disabled = "";
97
+ return out;
98
+ }
99
+
100
+ export function SuperbasedFeedback(props: SuperbasedFeedbackProps): React.ReactElement {
101
+ const ref = React.useRef<FeedbackElement | null>(null);
102
+ const cb = React.useRef(props);
103
+ cb.current = props;
104
+ React.useEffect(() => {
105
+ defineFeedbackElement();
106
+ }, []);
107
+ React.useEffect(() => {
108
+ const el = ref.current;
109
+ if (!el) return;
110
+ el.fetch = props.fetch ?? null;
111
+ }, [props.fetch]);
112
+ React.useEffect(() => {
113
+ const el = ref.current;
114
+ if (!el) return;
115
+ const onFeedback = (e: Event) => cb.current.onFeedback?.((e as CustomEvent<FeedbackPayload>).detail, e as CustomEvent<FeedbackPayload>);
116
+ const onSent = (e: Event) => cb.current.onSent?.((e as CustomEvent).detail);
117
+ const onError = (e: Event) => cb.current.onError?.((e as CustomEvent).detail);
118
+ el.addEventListener(FEEDBACK_EVENT, onFeedback);
119
+ el.addEventListener(SENT_EVENT, onSent);
120
+ el.addEventListener(ERROR_EVENT, onError);
121
+ return () => {
122
+ el.removeEventListener(FEEDBACK_EVENT, onFeedback);
123
+ el.removeEventListener(SENT_EVENT, onSent);
124
+ el.removeEventListener(ERROR_EVENT, onError);
125
+ };
126
+ }, []);
127
+ // `class` (not className): React passes custom-element props through as attributes.
128
+ const attrs: Record<string, unknown> = { ...feedbackAttributes(props), ref };
129
+ if (props.className) attrs.class = props.className;
130
+ if (props.style) attrs.style = props.style;
131
+ return React.createElement(TAG, attrs);
132
+ }