react-linear-feedback 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 +131 -0
- package/dist/react/index.cjs +627 -0
- package/dist/react/index.d.cts +109 -0
- package/dist/react/index.d.ts +109 -0
- package/dist/react/index.js +622 -0
- package/dist/server/index.cjs +170 -0
- package/dist/server/index.d.cts +85 -0
- package/dist/server/index.d.ts +85 -0
- package/dist/server/index.js +165 -0
- package/package.json +76 -0
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useState, useRef, useEffect } from 'react';
|
|
3
|
+
import { domToCanvas } from 'modern-screenshot';
|
|
4
|
+
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
5
|
+
|
|
6
|
+
// src/react/feedback-gate.tsx
|
|
7
|
+
|
|
8
|
+
// src/react/anchor.ts
|
|
9
|
+
var MAX_DEPTH = 5;
|
|
10
|
+
var OVERLAY_ATTR = "data-feedback-overlay";
|
|
11
|
+
function escapeForSelector(value) {
|
|
12
|
+
if (typeof CSS !== "undefined" && typeof CSS.escape === "function") return CSS.escape(value);
|
|
13
|
+
return value.replace(/[^\w-]/g, (c) => `\\${c}`);
|
|
14
|
+
}
|
|
15
|
+
function getStableSelector(el, maxDepth = MAX_DEPTH) {
|
|
16
|
+
const parts = [];
|
|
17
|
+
let cur = el;
|
|
18
|
+
while (cur && parts.length < maxDepth && cur !== document.body && cur !== document.documentElement) {
|
|
19
|
+
const testid = cur.getAttribute("data-testid");
|
|
20
|
+
if (testid) {
|
|
21
|
+
parts.unshift(`[data-testid="${escapeForSelector(testid)}"]`);
|
|
22
|
+
return parts.join(" > ");
|
|
23
|
+
}
|
|
24
|
+
if (cur.id) {
|
|
25
|
+
parts.unshift(`#${escapeForSelector(cur.id)}`);
|
|
26
|
+
return parts.join(" > ");
|
|
27
|
+
}
|
|
28
|
+
let part = cur.tagName.toLowerCase();
|
|
29
|
+
const parent = cur.parentElement;
|
|
30
|
+
if (parent) {
|
|
31
|
+
const sameTag = Array.from(parent.children).filter((s) => s.tagName === cur.tagName);
|
|
32
|
+
if (sameTag.length > 1) {
|
|
33
|
+
part += `:nth-of-type(${sameTag.indexOf(cur) + 1})`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
parts.unshift(part);
|
|
37
|
+
cur = parent;
|
|
38
|
+
}
|
|
39
|
+
return parts.join(" > ");
|
|
40
|
+
}
|
|
41
|
+
function elementBelowOverlay(clientX, clientY) {
|
|
42
|
+
const stack = document.elementsFromPoint(clientX, clientY);
|
|
43
|
+
return stack.find((el) => !el.closest(`[${OVERLAY_ATTR}]`)) ?? null;
|
|
44
|
+
}
|
|
45
|
+
function describeElementAt(clientX, clientY) {
|
|
46
|
+
const el = elementBelowOverlay(clientX, clientY);
|
|
47
|
+
if (!el) return null;
|
|
48
|
+
return getStableSelector(el) || null;
|
|
49
|
+
}
|
|
50
|
+
var FEEDBACK_OVERLAY_ATTR = OVERLAY_ATTR;
|
|
51
|
+
var MAX_SIDE = 1600;
|
|
52
|
+
async function submitFeedback(annotation, opts = {}) {
|
|
53
|
+
const endpoint = opts.endpoint ?? "/api/feedback";
|
|
54
|
+
const context = collectContext(annotation);
|
|
55
|
+
const screenshot = await captureAnnotatedScreenshot(annotation);
|
|
56
|
+
const payload = { annotation, context, screenshot };
|
|
57
|
+
try {
|
|
58
|
+
const res = await fetch(endpoint, {
|
|
59
|
+
method: "POST",
|
|
60
|
+
headers: { "Content-Type": "application/json" },
|
|
61
|
+
body: JSON.stringify(payload)
|
|
62
|
+
});
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
const detail = await res.text().catch(() => "");
|
|
65
|
+
console.error("[feedback] submit failed", res.status, detail);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return await res.json();
|
|
69
|
+
} catch (err) {
|
|
70
|
+
console.error("[feedback] submit error", err);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function captureAnnotatedScreenshot(annotation) {
|
|
75
|
+
try {
|
|
76
|
+
const docEl = document.documentElement;
|
|
77
|
+
const pageW = docEl.scrollWidth;
|
|
78
|
+
const pageH = docEl.scrollHeight;
|
|
79
|
+
const vw = window.innerWidth;
|
|
80
|
+
const vh = window.innerHeight;
|
|
81
|
+
const scale = Math.min(1, MAX_SIDE / Math.max(vw, vh));
|
|
82
|
+
const full = await domToCanvas(document.body, {
|
|
83
|
+
scale,
|
|
84
|
+
backgroundColor: "#ffffff",
|
|
85
|
+
// Exclude the feedback overlay itself (carries data-feedback-overlay → dataset.feedbackOverlay).
|
|
86
|
+
filter: (node) => !(node instanceof HTMLElement && node.dataset.feedbackOverlay !== void 0)
|
|
87
|
+
});
|
|
88
|
+
const ratioX = full.width / pageW;
|
|
89
|
+
const ratioY = full.height / pageH;
|
|
90
|
+
const out = document.createElement("canvas");
|
|
91
|
+
out.width = Math.max(1, Math.round(vw * ratioX));
|
|
92
|
+
out.height = Math.max(1, Math.round(vh * ratioY));
|
|
93
|
+
const ctx = out.getContext("2d");
|
|
94
|
+
if (ctx) {
|
|
95
|
+
ctx.drawImage(full, window.scrollX * ratioX, window.scrollY * ratioY, vw * ratioX, vh * ratioY, 0, 0, out.width, out.height);
|
|
96
|
+
const { x, y, width, height } = annotation.rect;
|
|
97
|
+
const rx = (x - window.scrollX) * ratioX;
|
|
98
|
+
const ry = (y - window.scrollY) * ratioY;
|
|
99
|
+
ctx.lineWidth = Math.max(2, 3 * ratioX);
|
|
100
|
+
ctx.strokeStyle = "#ff0055";
|
|
101
|
+
ctx.fillStyle = "rgba(255, 0, 85, 0.12)";
|
|
102
|
+
ctx.fillRect(rx, ry, width * ratioX, height * ratioY);
|
|
103
|
+
ctx.strokeRect(rx, ry, width * ratioX, height * ratioY);
|
|
104
|
+
}
|
|
105
|
+
return out.toDataURL("image/jpeg", 0.85);
|
|
106
|
+
} catch (err) {
|
|
107
|
+
console.warn("[feedback] screenshot capture failed, sending without image", err);
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function collectContext(annotation) {
|
|
112
|
+
const viewportX = annotation.rect.x + annotation.rect.width / 2 - window.scrollX;
|
|
113
|
+
const viewportY = annotation.rect.y + annotation.rect.height / 2 - window.scrollY;
|
|
114
|
+
return {
|
|
115
|
+
url: window.location.href,
|
|
116
|
+
pathname: window.location.pathname,
|
|
117
|
+
title: document.title,
|
|
118
|
+
viewport: { width: window.innerWidth, height: window.innerHeight, dpr: window.devicePixelRatio },
|
|
119
|
+
scroll: { x: window.scrollX, y: window.scrollY },
|
|
120
|
+
userAgent: navigator.userAgent,
|
|
121
|
+
referrer: document.referrer,
|
|
122
|
+
elementHint: describeElementAt(viewportX, viewportY),
|
|
123
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function Svg({ size = 16, children, ...props }) {
|
|
127
|
+
return /* @__PURE__ */ jsx(
|
|
128
|
+
"svg",
|
|
129
|
+
{
|
|
130
|
+
width: size,
|
|
131
|
+
height: size,
|
|
132
|
+
viewBox: "0 0 24 24",
|
|
133
|
+
fill: "none",
|
|
134
|
+
stroke: "currentColor",
|
|
135
|
+
strokeWidth: 2,
|
|
136
|
+
strokeLinecap: "round",
|
|
137
|
+
strokeLinejoin: "round",
|
|
138
|
+
"aria-hidden": "true",
|
|
139
|
+
...props,
|
|
140
|
+
children
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
var MessageIcon = (p) => /* @__PURE__ */ jsx(Svg, { ...p, children: /* @__PURE__ */ jsx("path", { d: "M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8z" }) });
|
|
145
|
+
var XIcon = (p) => /* @__PURE__ */ jsx(Svg, { ...p, children: /* @__PURE__ */ jsx("path", { d: "M18 6 6 18M6 6l12 12" }) });
|
|
146
|
+
var EditIcon = (p) => /* @__PURE__ */ jsxs(Svg, { ...p, children: [
|
|
147
|
+
/* @__PURE__ */ jsx("path", { d: "M12 20h9" }),
|
|
148
|
+
/* @__PURE__ */ jsx("path", { d: "M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4 12.5-12.5z" })
|
|
149
|
+
] });
|
|
150
|
+
var CheckCircleIcon = (p) => /* @__PURE__ */ jsxs(Svg, { ...p, children: [
|
|
151
|
+
/* @__PURE__ */ jsx("path", { d: "M22 11.08V12a10 10 0 1 1-5.93-9.14" }),
|
|
152
|
+
/* @__PURE__ */ jsx("path", { d: "M22 4 12 14.01l-3-3" })
|
|
153
|
+
] });
|
|
154
|
+
var AlertTriangleIcon = (p) => /* @__PURE__ */ jsxs(Svg, { ...p, children: [
|
|
155
|
+
/* @__PURE__ */ jsx("path", { d: "M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" }),
|
|
156
|
+
/* @__PURE__ */ jsx("path", { d: "M12 9v4" }),
|
|
157
|
+
/* @__PURE__ */ jsx("path", { d: "M12 17h.01" })
|
|
158
|
+
] });
|
|
159
|
+
var ArrowUpIcon = (p) => /* @__PURE__ */ jsx(Svg, { ...p, children: /* @__PURE__ */ jsx("path", { d: "M12 19V5M5 12l7-7 7 7" }) });
|
|
160
|
+
var DotIcon = (p) => /* @__PURE__ */ jsx(Svg, { ...p, strokeWidth: 0, children: /* @__PURE__ */ jsx("circle", { cx: "12", cy: "12", r: "6", fill: "currentColor" }) });
|
|
161
|
+
var TYPE_ICONS = {
|
|
162
|
+
bug: AlertTriangleIcon,
|
|
163
|
+
improvement: ArrowUpIcon,
|
|
164
|
+
dot: DotIcon
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// src/react/styles.ts
|
|
168
|
+
var STYLE_ID = "lfb-styles";
|
|
169
|
+
var CSS2 = `
|
|
170
|
+
.lfb-doc-layer, .lfb-fixed-layer {
|
|
171
|
+
--lfb-brand: #6366f1;
|
|
172
|
+
--lfb-fg: #181d27;
|
|
173
|
+
--lfb-fg-secondary: #414651;
|
|
174
|
+
--lfb-fg-tertiary: #717680;
|
|
175
|
+
--lfb-surface: #ffffff;
|
|
176
|
+
--lfb-surface-hover: #f5f5f5;
|
|
177
|
+
--lfb-border: #e9eaeb;
|
|
178
|
+
--lfb-radius: 12px;
|
|
179
|
+
--lfb-rect: #ff0055;
|
|
180
|
+
--lfb-z: 2147483640;
|
|
181
|
+
--lfb-font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
182
|
+
}
|
|
183
|
+
.lfb-doc-layer, .lfb-doc-layer *, .lfb-fixed-layer, .lfb-fixed-layer * { box-sizing: border-box; }
|
|
184
|
+
|
|
185
|
+
.lfb-doc-layer { position: absolute; inset-inline: 0; top: 0; pointer-events: none; z-index: var(--lfb-z); }
|
|
186
|
+
.lfb-fixed-layer { position: fixed; inset: 0; pointer-events: none; z-index: calc(var(--lfb-z) + 1); }
|
|
187
|
+
|
|
188
|
+
.lfb-rect { position: absolute; border: 2px solid var(--lfb-rect); background: rgba(255,0,85,0.12); border-radius: 3px; pointer-events: none; }
|
|
189
|
+
|
|
190
|
+
.lfb-anchor { position: absolute; pointer-events: auto; }
|
|
191
|
+
|
|
192
|
+
.lfb-card {
|
|
193
|
+
background: var(--lfb-surface);
|
|
194
|
+
color: var(--lfb-fg);
|
|
195
|
+
border: 1px solid var(--lfb-border);
|
|
196
|
+
border-radius: var(--lfb-radius);
|
|
197
|
+
box-shadow: 0 12px 32px rgba(0,0,0,0.16), 0 2px 6px rgba(0,0,0,0.08);
|
|
198
|
+
padding: 12px;
|
|
199
|
+
font-family: var(--lfb-font);
|
|
200
|
+
font-size: 14px;
|
|
201
|
+
line-height: 1.4;
|
|
202
|
+
}
|
|
203
|
+
.lfb-composer { position: absolute; top: 100%; left: 0; margin-top: 8px; width: 320px; max-width: calc(100vw - 32px); }
|
|
204
|
+
|
|
205
|
+
.lfb-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
|
|
206
|
+
.lfb-eyebrow { font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--lfb-fg-tertiary); }
|
|
207
|
+
.lfb-sub { margin: 4px 0 0; font-size: 12px; color: var(--lfb-fg-tertiary); }
|
|
208
|
+
|
|
209
|
+
.lfb-iconbtn { display: inline-flex; align-items: center; justify-content: center; padding: 4px; margin: -4px; border: 0; background: none; color: var(--lfb-fg-tertiary); border-radius: 6px; cursor: pointer; }
|
|
210
|
+
.lfb-iconbtn:hover { background: var(--lfb-surface-hover); color: var(--lfb-fg); }
|
|
211
|
+
|
|
212
|
+
.lfb-field-label { font-size: 12px; font-weight: 500; color: var(--lfb-fg-secondary); }
|
|
213
|
+
.lfb-types { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 6px; }
|
|
214
|
+
.lfb-type { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 8px; font-size: 14px; font-weight: 500; font-family: inherit; background: var(--lfb-surface); color: var(--lfb-fg-secondary); border: 1px solid var(--lfb-border); cursor: pointer; transition: background 0.1s, box-shadow 0.1s, border-color 0.1s; }
|
|
215
|
+
.lfb-type:hover { background: var(--lfb-surface-hover); }
|
|
216
|
+
.lfb-type[aria-pressed="true"] { color: var(--lfb-fg); border-color: var(--lfb-brand); box-shadow: inset 0 0 0 1px var(--lfb-brand); }
|
|
217
|
+
.lfb-swatch { width: 20px; height: 20px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; color: #fff; flex-shrink: 0; }
|
|
218
|
+
|
|
219
|
+
.lfb-textarea, .lfb-input {
|
|
220
|
+
width: 100%; margin-top: 12px; border-radius: 8px; background: var(--lfb-surface); color: var(--lfb-fg);
|
|
221
|
+
border: 1px solid var(--lfb-border); padding: 8px 10px; font-size: 14px; font-family: inherit; outline: none;
|
|
222
|
+
}
|
|
223
|
+
.lfb-textarea { resize: none; }
|
|
224
|
+
.lfb-input { margin-top: 8px; font-size: 13px; }
|
|
225
|
+
.lfb-textarea:focus, .lfb-input:focus { border-color: var(--lfb-brand); box-shadow: 0 0 0 2px color-mix(in srgb, var(--lfb-brand) 30%, transparent); }
|
|
226
|
+
.lfb-textarea::placeholder, .lfb-input::placeholder { color: var(--lfb-fg-tertiary); }
|
|
227
|
+
.lfb-textarea:disabled { opacity: 0.5; }
|
|
228
|
+
|
|
229
|
+
.lfb-namerow { margin-top: 8px; display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--lfb-fg-tertiary); }
|
|
230
|
+
.lfb-name { color: var(--lfb-fg-secondary); font-weight: 600; }
|
|
231
|
+
.lfb-link { display: inline-flex; align-items: center; gap: 4px; padding: 2px 6px; border: 0; background: none; color: var(--lfb-brand); font-weight: 500; font-size: 12px; font-family: inherit; cursor: pointer; border-radius: 4px; }
|
|
232
|
+
.lfb-link:hover { background: var(--lfb-surface-hover); }
|
|
233
|
+
|
|
234
|
+
.lfb-error { margin-top: 8px; font-size: 12px; color: #d92d20; }
|
|
235
|
+
.lfb-actions { margin-top: 12px; display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
|
|
236
|
+
.lfb-btn { padding: 6px 12px; border-radius: 8px; font-size: 14px; font-weight: 600; font-family: inherit; cursor: pointer; border: 0; transition: background 0.1s; }
|
|
237
|
+
.lfb-btn:disabled { opacity: 0.5; cursor: not-allowed; }
|
|
238
|
+
.lfb-btn-ghost { background: none; color: var(--lfb-fg-secondary); }
|
|
239
|
+
.lfb-btn-ghost:hover:not(:disabled) { background: var(--lfb-surface-hover); }
|
|
240
|
+
.lfb-btn-primary { background: var(--lfb-brand); color: #fff; }
|
|
241
|
+
.lfb-btn-primary:hover:not(:disabled) { background: color-mix(in srgb, var(--lfb-brand) 88%, black); }
|
|
242
|
+
|
|
243
|
+
.lfb-draw { position: absolute; inset: 0; pointer-events: auto; cursor: crosshair; background: rgba(0,0,0,0.04); user-select: none; }
|
|
244
|
+
.lfb-hint { position: absolute; top: 16px; left: 50%; transform: translateX(-50%); background: #181d27; color: #fff; padding: 8px 16px; border-radius: 9999px; font-size: 12px; font-weight: 500; font-family: var(--lfb-font); box-shadow: 0 8px 20px rgba(0,0,0,0.25); white-space: nowrap; }
|
|
245
|
+
|
|
246
|
+
.lfb-stack { position: absolute; bottom: 16px; right: 16px; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; pointer-events: none; }
|
|
247
|
+
.lfb-stack > * { pointer-events: auto; }
|
|
248
|
+
.lfb-stack--bottom-left { right: auto; left: 16px; align-items: flex-start; }
|
|
249
|
+
.lfb-stack--top-right { bottom: auto; top: 16px; }
|
|
250
|
+
.lfb-stack--top-left { bottom: auto; top: 16px; right: auto; left: 16px; align-items: flex-start; }
|
|
251
|
+
|
|
252
|
+
.lfb-fab { display: inline-flex; align-items: center; gap: 8px; border: 0; border-radius: 9999px; padding: 12px 16px; font-size: 14px; font-weight: 600; font-family: var(--lfb-font); cursor: pointer; background: var(--lfb-brand); color: #fff; box-shadow: 0 10px 25px rgba(0,0,0,0.18); transition: transform 0.1s, background 0.1s; }
|
|
253
|
+
.lfb-fab:hover { transform: scale(1.05); background: color-mix(in srgb, var(--lfb-brand) 88%, black); }
|
|
254
|
+
.lfb-fab--active { background: var(--lfb-surface); color: var(--lfb-fg); border: 1px solid var(--lfb-border); }
|
|
255
|
+
.lfb-fab--active:hover { background: var(--lfb-surface-hover); }
|
|
256
|
+
|
|
257
|
+
.lfb-toast { display: flex; align-items: flex-start; gap: 10px; width: 300px; max-width: calc(100vw - 32px); }
|
|
258
|
+
.lfb-toast-icon { color: #17b26a; flex-shrink: 0; margin-top: 1px; }
|
|
259
|
+
.lfb-toast-body { min-width: 0; flex: 1; }
|
|
260
|
+
.lfb-toast-title { font-size: 14px; font-weight: 600; color: var(--lfb-fg); }
|
|
261
|
+
.lfb-toast-text { margin-top: 2px; font-size: 12px; color: var(--lfb-fg-tertiary); }
|
|
262
|
+
.lfb-toast-link { color: var(--lfb-brand); font-weight: 500; text-decoration: none; }
|
|
263
|
+
.lfb-toast-link:hover { text-decoration: underline; }
|
|
264
|
+
`;
|
|
265
|
+
function ensureStyles() {
|
|
266
|
+
if (typeof document === "undefined") return;
|
|
267
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
268
|
+
const style = document.createElement("style");
|
|
269
|
+
style.id = STYLE_ID;
|
|
270
|
+
style.textContent = CSS2;
|
|
271
|
+
document.head.appendChild(style);
|
|
272
|
+
}
|
|
273
|
+
var MIN_DRAG = 12;
|
|
274
|
+
var DEFAULT_BOX = { width: 220, height: 130 };
|
|
275
|
+
var DEFAULT_TYPES = [
|
|
276
|
+
{ id: "bug", label: "Bug", color: "#ef4444", icon: "bug" },
|
|
277
|
+
{ id: "improvement", label: "Improvement", color: "#22c55e", icon: "improvement" }
|
|
278
|
+
];
|
|
279
|
+
function FeedbackWidget({
|
|
280
|
+
endpoint,
|
|
281
|
+
brandColor,
|
|
282
|
+
position = "bottom-right",
|
|
283
|
+
types = DEFAULT_TYPES,
|
|
284
|
+
nameRequired = true,
|
|
285
|
+
nameStorageKey = "wh_feedback_name",
|
|
286
|
+
fabLabel = "Give feedback"
|
|
287
|
+
}) {
|
|
288
|
+
const [mode, setMode] = useState({ kind: "idle" });
|
|
289
|
+
const [name, setName] = useState("");
|
|
290
|
+
const [nameDraft, setNameDraft] = useState("");
|
|
291
|
+
const [editingName, setEditingName] = useState(false);
|
|
292
|
+
const [text, setText] = useState("");
|
|
293
|
+
const [issueType, setIssueType] = useState(types[0]?.id ?? "bug");
|
|
294
|
+
const [submitting, setSubmitting] = useState(false);
|
|
295
|
+
const [error, setError] = useState(null);
|
|
296
|
+
const [result, setResult] = useState(null);
|
|
297
|
+
const [drag, setDrag] = useState(null);
|
|
298
|
+
const textareaRef = useRef(null);
|
|
299
|
+
const nameInputRef = useRef(null);
|
|
300
|
+
useEffect(() => {
|
|
301
|
+
ensureStyles();
|
|
302
|
+
try {
|
|
303
|
+
const stored = window.localStorage.getItem(nameStorageKey);
|
|
304
|
+
if (stored) setName(stored);
|
|
305
|
+
} catch {
|
|
306
|
+
}
|
|
307
|
+
}, [nameStorageKey]);
|
|
308
|
+
useEffect(() => {
|
|
309
|
+
if (mode.kind !== "drawing" && mode.kind !== "naming") return;
|
|
310
|
+
const onKey = (e) => {
|
|
311
|
+
if (e.key === "Escape") {
|
|
312
|
+
setMode({ kind: "idle" });
|
|
313
|
+
setDrag(null);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
document.addEventListener("keydown", onKey);
|
|
317
|
+
return () => document.removeEventListener("keydown", onKey);
|
|
318
|
+
}, [mode.kind]);
|
|
319
|
+
useEffect(() => {
|
|
320
|
+
if (mode.kind === "composing") {
|
|
321
|
+
const t = window.setTimeout(() => textareaRef.current?.focus(), 0);
|
|
322
|
+
return () => window.clearTimeout(t);
|
|
323
|
+
}
|
|
324
|
+
if (mode.kind === "naming") {
|
|
325
|
+
const t = window.setTimeout(() => nameInputRef.current?.focus(), 0);
|
|
326
|
+
return () => window.clearTimeout(t);
|
|
327
|
+
}
|
|
328
|
+
}, [mode.kind]);
|
|
329
|
+
const persistName = (value) => {
|
|
330
|
+
try {
|
|
331
|
+
window.localStorage.setItem(nameStorageKey, value);
|
|
332
|
+
} catch {
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
const startFlow = () => {
|
|
336
|
+
if (mode.kind === "drawing" || mode.kind === "naming") {
|
|
337
|
+
setMode({ kind: "idle" });
|
|
338
|
+
setDrag(null);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
setResult(null);
|
|
342
|
+
if (nameRequired && !name.trim()) {
|
|
343
|
+
setNameDraft("");
|
|
344
|
+
setMode({ kind: "naming" });
|
|
345
|
+
} else {
|
|
346
|
+
setMode({ kind: "drawing" });
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
const confirmName = (e) => {
|
|
350
|
+
e.preventDefault();
|
|
351
|
+
const t = nameDraft.trim();
|
|
352
|
+
if (!t) return;
|
|
353
|
+
setName(t);
|
|
354
|
+
persistName(t);
|
|
355
|
+
setMode({ kind: "drawing" });
|
|
356
|
+
};
|
|
357
|
+
const onDrawMouseDown = (e) => {
|
|
358
|
+
if (mode.kind !== "drawing") return;
|
|
359
|
+
e.preventDefault();
|
|
360
|
+
const p = { x: e.clientX, y: e.clientY };
|
|
361
|
+
setDrag({ start: p, current: p });
|
|
362
|
+
};
|
|
363
|
+
const onDrawMouseMove = (e) => {
|
|
364
|
+
if (mode.kind !== "drawing" || !drag) return;
|
|
365
|
+
setDrag((d) => d ? { ...d, current: { x: e.clientX, y: e.clientY } } : d);
|
|
366
|
+
};
|
|
367
|
+
const onDrawMouseUp = (e) => {
|
|
368
|
+
if (mode.kind !== "drawing" || !drag) return;
|
|
369
|
+
const start = drag.start;
|
|
370
|
+
const end = { x: e.clientX, y: e.clientY };
|
|
371
|
+
let vx = Math.min(start.x, end.x);
|
|
372
|
+
let vy = Math.min(start.y, end.y);
|
|
373
|
+
let vw = Math.abs(end.x - start.x);
|
|
374
|
+
let vh = Math.abs(end.y - start.y);
|
|
375
|
+
if (vw < MIN_DRAG || vh < MIN_DRAG) {
|
|
376
|
+
vw = Math.max(vw, DEFAULT_BOX.width);
|
|
377
|
+
vh = Math.max(vh, DEFAULT_BOX.height);
|
|
378
|
+
vx = end.x - vw / 2;
|
|
379
|
+
vy = end.y - vh / 2;
|
|
380
|
+
}
|
|
381
|
+
const rect = { x: Math.max(0, vx + window.scrollX), y: Math.max(0, vy + window.scrollY), width: vw, height: vh };
|
|
382
|
+
setDrag(null);
|
|
383
|
+
setText("");
|
|
384
|
+
setIssueType(types[0]?.id ?? "bug");
|
|
385
|
+
setError(null);
|
|
386
|
+
setEditingName(false);
|
|
387
|
+
setMode({ kind: "composing", rect });
|
|
388
|
+
};
|
|
389
|
+
const cancelComposer = () => {
|
|
390
|
+
setMode({ kind: "idle" });
|
|
391
|
+
setText("");
|
|
392
|
+
setError(null);
|
|
393
|
+
setEditingName(false);
|
|
394
|
+
setSubmitting(false);
|
|
395
|
+
};
|
|
396
|
+
const handleSubmit = async (e) => {
|
|
397
|
+
e.preventDefault();
|
|
398
|
+
if (mode.kind !== "composing" || submitting) return;
|
|
399
|
+
const trimmedNote = text.trim();
|
|
400
|
+
if (!trimmedNote) return;
|
|
401
|
+
const trimmedName = name.trim();
|
|
402
|
+
const selected = types.find((t) => t.id === issueType);
|
|
403
|
+
setSubmitting(true);
|
|
404
|
+
setError(null);
|
|
405
|
+
if (trimmedName) persistName(trimmedName);
|
|
406
|
+
const res = await submitFeedback(
|
|
407
|
+
{ rect: mode.rect, note: trimmedNote, type: issueType, typeLabel: selected?.label, name: trimmedName || void 0 },
|
|
408
|
+
{ endpoint }
|
|
409
|
+
);
|
|
410
|
+
setSubmitting(false);
|
|
411
|
+
if (res) {
|
|
412
|
+
setResult(res);
|
|
413
|
+
setText("");
|
|
414
|
+
setEditingName(false);
|
|
415
|
+
setMode({ kind: "idle" });
|
|
416
|
+
} else {
|
|
417
|
+
setError("Couldn't send \u2014 please try again.");
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
const rootStyle = { display: "contents", ...brandColor ? { "--lfb-brand": brandColor } : {} };
|
|
421
|
+
const overlayProps = { [FEEDBACK_OVERLAY_ATTR]: "" };
|
|
422
|
+
const live = drag ? {
|
|
423
|
+
left: Math.min(drag.start.x, drag.current.x),
|
|
424
|
+
top: Math.min(drag.start.y, drag.current.y),
|
|
425
|
+
width: Math.abs(drag.current.x - drag.start.x),
|
|
426
|
+
height: Math.abs(drag.current.y - drag.start.y)
|
|
427
|
+
} : null;
|
|
428
|
+
return /* @__PURE__ */ jsxs("div", { className: "lfb-root", style: rootStyle, children: [
|
|
429
|
+
/* @__PURE__ */ jsx("div", { ...overlayProps, className: "lfb-doc-layer", children: mode.kind === "composing" && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
430
|
+
/* @__PURE__ */ jsx("div", { className: "lfb-rect", style: { left: mode.rect.x, top: mode.rect.y, width: mode.rect.width, height: mode.rect.height } }),
|
|
431
|
+
/* @__PURE__ */ jsx("div", { className: "lfb-anchor", style: { left: mode.rect.x, top: mode.rect.y + mode.rect.height }, children: /* @__PURE__ */ jsxs("form", { className: "lfb-card lfb-composer", onSubmit: handleSubmit, children: [
|
|
432
|
+
/* @__PURE__ */ jsxs("div", { className: "lfb-row", children: [
|
|
433
|
+
/* @__PURE__ */ jsx("span", { className: "lfb-eyebrow", children: "New feedback" }),
|
|
434
|
+
/* @__PURE__ */ jsx("button", { type: "button", className: "lfb-iconbtn", "aria-label": "Cancel", onClick: cancelComposer, children: /* @__PURE__ */ jsx(XIcon, {}) })
|
|
435
|
+
] }),
|
|
436
|
+
/* @__PURE__ */ jsxs("div", { style: { marginTop: 12 }, children: [
|
|
437
|
+
/* @__PURE__ */ jsx("span", { className: "lfb-field-label", children: "Issue type" }),
|
|
438
|
+
/* @__PURE__ */ jsx("div", { className: "lfb-types", children: types.map((t) => {
|
|
439
|
+
const Icon = TYPE_ICONS[t.icon ?? "dot"];
|
|
440
|
+
return /* @__PURE__ */ jsxs(
|
|
441
|
+
"button",
|
|
442
|
+
{
|
|
443
|
+
type: "button",
|
|
444
|
+
className: "lfb-type",
|
|
445
|
+
"aria-pressed": issueType === t.id,
|
|
446
|
+
onClick: () => setIssueType(t.id),
|
|
447
|
+
children: [
|
|
448
|
+
/* @__PURE__ */ jsx("span", { className: "lfb-swatch", style: { background: t.color }, children: /* @__PURE__ */ jsx(Icon, { size: 14 }) }),
|
|
449
|
+
t.label
|
|
450
|
+
]
|
|
451
|
+
},
|
|
452
|
+
t.id
|
|
453
|
+
);
|
|
454
|
+
}) })
|
|
455
|
+
] }),
|
|
456
|
+
/* @__PURE__ */ jsx(
|
|
457
|
+
"textarea",
|
|
458
|
+
{
|
|
459
|
+
ref: textareaRef,
|
|
460
|
+
className: "lfb-textarea",
|
|
461
|
+
placeholder: "What's on your mind?",
|
|
462
|
+
value: text,
|
|
463
|
+
onChange: (e) => setText(e.target.value),
|
|
464
|
+
maxLength: 5e3,
|
|
465
|
+
required: true,
|
|
466
|
+
disabled: submitting,
|
|
467
|
+
rows: 3,
|
|
468
|
+
onKeyDown: (e) => {
|
|
469
|
+
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
|
470
|
+
e.preventDefault();
|
|
471
|
+
handleSubmit(e);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
),
|
|
476
|
+
editingName ? /* @__PURE__ */ jsx(
|
|
477
|
+
"input",
|
|
478
|
+
{
|
|
479
|
+
className: "lfb-input",
|
|
480
|
+
type: "text",
|
|
481
|
+
value: name,
|
|
482
|
+
autoFocus: true,
|
|
483
|
+
maxLength: 80,
|
|
484
|
+
onChange: (e) => setName(e.target.value),
|
|
485
|
+
onBlur: () => {
|
|
486
|
+
const t = name.trim();
|
|
487
|
+
if (t) persistName(t);
|
|
488
|
+
setEditingName(false);
|
|
489
|
+
},
|
|
490
|
+
onKeyDown: (e) => {
|
|
491
|
+
if (e.key === "Enter") {
|
|
492
|
+
e.preventDefault();
|
|
493
|
+
e.target.blur();
|
|
494
|
+
}
|
|
495
|
+
if (e.key === "Escape") {
|
|
496
|
+
e.preventDefault();
|
|
497
|
+
setEditingName(false);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
) : /* @__PURE__ */ jsxs("div", { className: "lfb-namerow", children: [
|
|
502
|
+
/* @__PURE__ */ jsxs("span", { children: [
|
|
503
|
+
"Posting as ",
|
|
504
|
+
/* @__PURE__ */ jsx("span", { className: "lfb-name", children: name || "anonymous" })
|
|
505
|
+
] }),
|
|
506
|
+
/* @__PURE__ */ jsxs("button", { type: "button", className: "lfb-link", onClick: () => setEditingName(true), children: [
|
|
507
|
+
/* @__PURE__ */ jsx(EditIcon, { size: 12 }),
|
|
508
|
+
"change"
|
|
509
|
+
] })
|
|
510
|
+
] }),
|
|
511
|
+
error && /* @__PURE__ */ jsx("p", { className: "lfb-error", children: error }),
|
|
512
|
+
/* @__PURE__ */ jsxs("div", { className: "lfb-actions", children: [
|
|
513
|
+
/* @__PURE__ */ jsx("button", { type: "button", className: "lfb-btn lfb-btn-ghost", onClick: cancelComposer, disabled: submitting, children: "Cancel" }),
|
|
514
|
+
/* @__PURE__ */ jsx("button", { type: "submit", className: "lfb-btn lfb-btn-primary", disabled: submitting || !text.trim(), children: submitting ? "Sending\u2026" : "Send to Linear" })
|
|
515
|
+
] })
|
|
516
|
+
] }) })
|
|
517
|
+
] }) }),
|
|
518
|
+
/* @__PURE__ */ jsxs("div", { ...overlayProps, className: "lfb-fixed-layer", children: [
|
|
519
|
+
mode.kind === "drawing" && /* @__PURE__ */ jsxs("div", { className: "lfb-draw", role: "presentation", onMouseDown: onDrawMouseDown, onMouseMove: onDrawMouseMove, onMouseUp: onDrawMouseUp, children: [
|
|
520
|
+
/* @__PURE__ */ jsx("div", { className: "lfb-hint", children: "Drag to select an area \xB7 Esc to cancel" }),
|
|
521
|
+
live && /* @__PURE__ */ jsx("div", { className: "lfb-rect", style: live })
|
|
522
|
+
] }),
|
|
523
|
+
mode.kind !== "composing" && /* @__PURE__ */ jsxs("div", { className: `lfb-stack${position === "bottom-right" ? "" : ` lfb-stack--${position}`}`, children: [
|
|
524
|
+
mode.kind === "naming" && /* @__PURE__ */ jsxs("form", { className: "lfb-card", style: { width: 288, maxWidth: "calc(100vw - 32px)" }, onSubmit: confirmName, children: [
|
|
525
|
+
/* @__PURE__ */ jsxs("div", { className: "lfb-row", children: [
|
|
526
|
+
/* @__PURE__ */ jsx("span", { className: "lfb-eyebrow", children: "What's your name?" }),
|
|
527
|
+
/* @__PURE__ */ jsx("button", { type: "button", className: "lfb-iconbtn", "aria-label": "Cancel", onClick: () => setMode({ kind: "idle" }), children: /* @__PURE__ */ jsx(XIcon, {}) })
|
|
528
|
+
] }),
|
|
529
|
+
/* @__PURE__ */ jsx("p", { className: "lfb-sub", children: "Saved locally so you don't have to type it again." }),
|
|
530
|
+
/* @__PURE__ */ jsx(
|
|
531
|
+
"input",
|
|
532
|
+
{
|
|
533
|
+
ref: nameInputRef,
|
|
534
|
+
className: "lfb-input",
|
|
535
|
+
type: "text",
|
|
536
|
+
placeholder: "Olivia Rhye",
|
|
537
|
+
value: nameDraft,
|
|
538
|
+
maxLength: 80,
|
|
539
|
+
required: true,
|
|
540
|
+
onChange: (e) => setNameDraft(e.target.value)
|
|
541
|
+
}
|
|
542
|
+
),
|
|
543
|
+
/* @__PURE__ */ jsxs("div", { className: "lfb-actions", children: [
|
|
544
|
+
/* @__PURE__ */ jsx("button", { type: "button", className: "lfb-btn lfb-btn-ghost", onClick: () => setMode({ kind: "idle" }), children: "Cancel" }),
|
|
545
|
+
/* @__PURE__ */ jsx("button", { type: "submit", className: "lfb-btn lfb-btn-primary", disabled: !nameDraft.trim(), children: "Continue" })
|
|
546
|
+
] })
|
|
547
|
+
] }),
|
|
548
|
+
result && mode.kind === "idle" && /* @__PURE__ */ jsxs("div", { className: "lfb-card lfb-toast", children: [
|
|
549
|
+
/* @__PURE__ */ jsx("span", { className: "lfb-toast-icon", children: /* @__PURE__ */ jsx(CheckCircleIcon, { size: 20 }) }),
|
|
550
|
+
/* @__PURE__ */ jsxs("div", { className: "lfb-toast-body", children: [
|
|
551
|
+
/* @__PURE__ */ jsx("div", { className: "lfb-toast-title", children: "Feedback sent" }),
|
|
552
|
+
/* @__PURE__ */ jsxs("div", { className: "lfb-toast-text", children: [
|
|
553
|
+
"Created ",
|
|
554
|
+
result.identifier ?? "an issue",
|
|
555
|
+
".",
|
|
556
|
+
result.url && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
557
|
+
" ",
|
|
558
|
+
/* @__PURE__ */ jsx("a", { className: "lfb-toast-link", href: result.url, target: "_blank", rel: "noopener noreferrer", children: "View in Linear" })
|
|
559
|
+
] })
|
|
560
|
+
] })
|
|
561
|
+
] }),
|
|
562
|
+
/* @__PURE__ */ jsx("button", { type: "button", className: "lfb-iconbtn", "aria-label": "Dismiss", onClick: () => setResult(null), children: /* @__PURE__ */ jsx(XIcon, {}) })
|
|
563
|
+
] }),
|
|
564
|
+
/* @__PURE__ */ jsx(
|
|
565
|
+
"button",
|
|
566
|
+
{
|
|
567
|
+
type: "button",
|
|
568
|
+
className: `lfb-fab${mode.kind === "idle" ? "" : " lfb-fab--active"}`,
|
|
569
|
+
"aria-label": mode.kind === "idle" ? fabLabel : "Cancel feedback",
|
|
570
|
+
onClick: startFlow,
|
|
571
|
+
children: mode.kind === "idle" ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
572
|
+
/* @__PURE__ */ jsx(MessageIcon, { size: 18 }),
|
|
573
|
+
" ",
|
|
574
|
+
fabLabel
|
|
575
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
576
|
+
/* @__PURE__ */ jsx(XIcon, { size: 18 }),
|
|
577
|
+
" Cancel"
|
|
578
|
+
] })
|
|
579
|
+
}
|
|
580
|
+
)
|
|
581
|
+
] })
|
|
582
|
+
] })
|
|
583
|
+
] });
|
|
584
|
+
}
|
|
585
|
+
var DEFAULT_MAX_AGE = 60 * 60 * 24 * 90;
|
|
586
|
+
function writeCookie(name, value, enabled, maxAge) {
|
|
587
|
+
if (typeof document === "undefined") return;
|
|
588
|
+
document.cookie = enabled ? `${name}=${value}; path=/; max-age=${maxAge}; SameSite=Lax` : `${name}=; path=/; max-age=0; SameSite=Lax`;
|
|
589
|
+
}
|
|
590
|
+
function readCookie(name, value) {
|
|
591
|
+
if (typeof document === "undefined") return false;
|
|
592
|
+
return document.cookie.split(";").map((c) => c.trim()).some((c) => c === `${name}=${value}`);
|
|
593
|
+
}
|
|
594
|
+
function FeedbackGate({
|
|
595
|
+
urlParam = "feedback",
|
|
596
|
+
cookieName = "wh_feedback",
|
|
597
|
+
cookieValue = "1",
|
|
598
|
+
cookieMaxAgeSeconds = DEFAULT_MAX_AGE,
|
|
599
|
+
...widgetProps
|
|
600
|
+
}) {
|
|
601
|
+
const [enabled, setEnabled] = useState(false);
|
|
602
|
+
useEffect(() => {
|
|
603
|
+
if (typeof window === "undefined") return;
|
|
604
|
+
const params = new URLSearchParams(window.location.search);
|
|
605
|
+
const param = params.get(urlParam);
|
|
606
|
+
if (param != null) {
|
|
607
|
+
const turnOn = param !== "0";
|
|
608
|
+
writeCookie(cookieName, cookieValue, turnOn, cookieMaxAgeSeconds);
|
|
609
|
+
setEnabled(turnOn);
|
|
610
|
+
params.delete(urlParam);
|
|
611
|
+
const qs = params.toString();
|
|
612
|
+
const url = window.location.pathname + (qs ? `?${qs}` : "") + window.location.hash;
|
|
613
|
+
window.history.replaceState(null, "", url);
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
setEnabled(readCookie(cookieName, cookieValue));
|
|
617
|
+
}, [urlParam, cookieName, cookieValue, cookieMaxAgeSeconds]);
|
|
618
|
+
if (!enabled) return null;
|
|
619
|
+
return /* @__PURE__ */ jsx(FeedbackWidget, { ...widgetProps });
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
export { DEFAULT_TYPES, FeedbackGate, FeedbackWidget, submitFeedback };
|