@classic-homes/feedback-react 0.1.2 → 0.2.5

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 ADDED
@@ -0,0 +1,56 @@
1
+ # @classic-homes/feedback-react
2
+
3
+ React feedback widget for chapi: an edge tab, selection comments, and screenshots. It renders the same markup as `@classic-homes/feedback-svelte`.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @classic-homes/feedback-react react react-dom
9
+ ```
10
+
11
+ Peer dependencies: `react@^18 || ^19`, `react-dom@^18 || ^19`.
12
+
13
+ ## Quick Start
14
+
15
+ ```tsx
16
+ import '@classic-homes/feedback-core/styles.css';
17
+ import { FeedbackWidget, openFeedback } from '@classic-homes/feedback-react';
18
+
19
+ export function App() {
20
+ return (
21
+ <>
22
+ <FeedbackWidget apiBase="https://chapi.example.com" source="app" />
23
+ <button onClick={() => openFeedback()}>Send feedback</button>
24
+ </>
25
+ );
26
+ }
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ```tsx
32
+ <FeedbackWidget
33
+ apiBase="https://chapi.example.com"
34
+ source="app"
35
+ selection={{ container: '.article' }}
36
+ maxAttachments={3}
37
+ user={{ name: 'Pat Example', email: 'pat@example.com' }}
38
+ getContext={() => ({ route: window.location.pathname })}
39
+ submit={async (payload, files) => {
40
+ // Custom transport, e.g. an offline queue. Return { id } or { queued: true }.
41
+ return { id: crypto.randomUUID() };
42
+ }}
43
+ />
44
+ ```
45
+
46
+ Key props (from `FeedbackConfig`): `apiBase` and `source` (required), `getToken`, `user`, `turnstileSiteKey`, `selection`, `categories`, `getContext`, `trigger` (`'tab'` or `'none'`), `tabLabel`, `maxAttachments` (default 1, max 3), `allowScreenshotCapture`, `submit`, `onSubmitted`.
47
+
48
+ ## Exports
49
+
50
+ - **Component**: `FeedbackWidget` (props type `FeedbackWidgetProps`)
51
+ - **Hook**: `useFeedback()`
52
+ - **Functions**: `openFeedback`, `closeFeedback`, `submitFeedback`, `isFeedbackError`
53
+ - **Classes and constants**: `FeedbackError`, `DEFAULT_CATEGORIES`
54
+ - **Types**: `FeedbackConfig`, `FeedbackPayload`, `FeedbackCategoryOption`, `FeedbackErrorCode`, `FeedbackPageContext`, `FeedbackState`, `OpenFeedbackOptions`, `SelectionData`, `SubmitResult`
55
+
56
+ Import the stylesheet from `@classic-homes/feedback-core/styles.css` once in your app.
package/dist/index.cjs CHANGED
@@ -176,7 +176,11 @@ function FeedbackDialog({
176
176
  const closingRef = (0, import_react.useRef)(false);
177
177
  const pointerOnBackdropRef = (0, import_react.useRef)(false);
178
178
  const attachmentsRef = (0, import_react.useRef)([]);
179
- attachmentsRef.current = attachments;
179
+ const commitAttachments = (next) => {
180
+ attachmentsRef.current = next;
181
+ setAttachments(next);
182
+ };
183
+ const aliveRef = (0, import_react.useRef)(true);
180
184
  const done = status === "success" || status === "queued";
181
185
  const dirty = !done && (comment.trim().length > 0 || attachments.length > 0);
182
186
  const stateRef = (0, import_react.useRef)({ dirty, status, picking });
@@ -259,14 +263,15 @@ function FeedbackDialog({
259
263
  if (!showTurnstile || !el || !config.turnstileSiteKey || turnstileRef.current) return;
260
264
  turnstileRef.current = (0, import_feedback_core.renderTurnstile)(el, config.turnstileSiteKey);
261
265
  }, [showTurnstile, config.turnstileSiteKey]);
262
- (0, import_react.useEffect)(
263
- () => () => {
266
+ (0, import_react.useEffect)(() => {
267
+ aliveRef.current = true;
268
+ return () => {
269
+ aliveRef.current = false;
264
270
  turnstileRef.current?.remove();
265
271
  turnstileRef.current = null;
266
272
  for (const a of attachmentsRef.current) URL.revokeObjectURL(a.url);
267
- },
268
- []
269
- );
273
+ };
274
+ }, []);
270
275
  (0, import_react.useEffect)(() => {
271
276
  if (!picking || !options.selectionContainer) return;
272
277
  const watcher = (0, import_feedback_core.createSelectionWatcher)({
@@ -304,30 +309,24 @@ function FeedbackDialog({
304
309
  for (const file of accepted) {
305
310
  try {
306
311
  const blob = await (0, import_feedback_core.prepareImage)(file);
307
- const attachment = {
308
- id: (0, import_feedback_core.createClientId)(),
309
- blob,
310
- url: URL.createObjectURL(blob)
311
- };
312
- setAttachments((prev) => {
313
- if (prev.length >= options.maxAttachments) {
314
- URL.revokeObjectURL(attachment.url);
315
- return prev;
316
- }
317
- return [...prev, attachment];
318
- });
312
+ if (!aliveRef.current) return;
313
+ if (attachmentsRef.current.length >= options.maxAttachments) break;
314
+ commitAttachments([
315
+ ...attachmentsRef.current,
316
+ { id: (0, import_feedback_core.createClientId)(), blob, url: URL.createObjectURL(blob) }
317
+ ]);
319
318
  } catch (err) {
319
+ if (!aliveRef.current) return;
320
320
  setAttachError((0, import_feedback_core.errorMessage)(err));
321
321
  }
322
322
  }
323
323
  setProcessing(false);
324
324
  };
325
325
  const removeAttachment = (attachmentId) => {
326
- setAttachments((prev) => {
327
- const target = prev.find((a) => a.id === attachmentId);
328
- if (target) URL.revokeObjectURL(target.url);
329
- return prev.filter((a) => a.id !== attachmentId);
330
- });
326
+ const prev = attachmentsRef.current;
327
+ const target = prev.find((a) => a.id === attachmentId);
328
+ if (target) URL.revokeObjectURL(target.url);
329
+ commitAttachments(prev.filter((a) => a.id !== attachmentId));
331
330
  setAttachError(null);
332
331
  };
333
332
  const onCapture = async () => {
@@ -335,11 +334,12 @@ function FeedbackDialog({
335
334
  setAttachError(null);
336
335
  try {
337
336
  const blob = await (0, import_feedback_core.capturePage)();
337
+ if (!aliveRef.current) return;
338
338
  await addFiles([blob]);
339
339
  } catch {
340
- setAttachError(import_feedback_core.MESSAGES.captureFailed);
340
+ if (aliveRef.current) setAttachError(import_feedback_core.MESSAGES.captureFailed);
341
341
  } finally {
342
- setCapturing(false);
342
+ if (aliveRef.current) setCapturing(false);
343
343
  }
344
344
  };
345
345
  const onFileChange = (e) => {
package/dist/index.js CHANGED
@@ -176,7 +176,11 @@ function FeedbackDialog({
176
176
  const closingRef = useRef(false);
177
177
  const pointerOnBackdropRef = useRef(false);
178
178
  const attachmentsRef = useRef([]);
179
- attachmentsRef.current = attachments;
179
+ const commitAttachments = (next) => {
180
+ attachmentsRef.current = next;
181
+ setAttachments(next);
182
+ };
183
+ const aliveRef = useRef(true);
180
184
  const done = status === "success" || status === "queued";
181
185
  const dirty = !done && (comment.trim().length > 0 || attachments.length > 0);
182
186
  const stateRef = useRef({ dirty, status, picking });
@@ -259,14 +263,15 @@ function FeedbackDialog({
259
263
  if (!showTurnstile || !el || !config.turnstileSiteKey || turnstileRef.current) return;
260
264
  turnstileRef.current = renderTurnstile(el, config.turnstileSiteKey);
261
265
  }, [showTurnstile, config.turnstileSiteKey]);
262
- useEffect(
263
- () => () => {
266
+ useEffect(() => {
267
+ aliveRef.current = true;
268
+ return () => {
269
+ aliveRef.current = false;
264
270
  turnstileRef.current?.remove();
265
271
  turnstileRef.current = null;
266
272
  for (const a of attachmentsRef.current) URL.revokeObjectURL(a.url);
267
- },
268
- []
269
- );
273
+ };
274
+ }, []);
270
275
  useEffect(() => {
271
276
  if (!picking || !options.selectionContainer) return;
272
277
  const watcher = createSelectionWatcher({
@@ -304,30 +309,24 @@ function FeedbackDialog({
304
309
  for (const file of accepted) {
305
310
  try {
306
311
  const blob = await prepareImage(file);
307
- const attachment = {
308
- id: createClientId(),
309
- blob,
310
- url: URL.createObjectURL(blob)
311
- };
312
- setAttachments((prev) => {
313
- if (prev.length >= options.maxAttachments) {
314
- URL.revokeObjectURL(attachment.url);
315
- return prev;
316
- }
317
- return [...prev, attachment];
318
- });
312
+ if (!aliveRef.current) return;
313
+ if (attachmentsRef.current.length >= options.maxAttachments) break;
314
+ commitAttachments([
315
+ ...attachmentsRef.current,
316
+ { id: createClientId(), blob, url: URL.createObjectURL(blob) }
317
+ ]);
319
318
  } catch (err) {
319
+ if (!aliveRef.current) return;
320
320
  setAttachError(errorMessage(err));
321
321
  }
322
322
  }
323
323
  setProcessing(false);
324
324
  };
325
325
  const removeAttachment = (attachmentId) => {
326
- setAttachments((prev) => {
327
- const target = prev.find((a) => a.id === attachmentId);
328
- if (target) URL.revokeObjectURL(target.url);
329
- return prev.filter((a) => a.id !== attachmentId);
330
- });
326
+ const prev = attachmentsRef.current;
327
+ const target = prev.find((a) => a.id === attachmentId);
328
+ if (target) URL.revokeObjectURL(target.url);
329
+ commitAttachments(prev.filter((a) => a.id !== attachmentId));
331
330
  setAttachError(null);
332
331
  };
333
332
  const onCapture = async () => {
@@ -335,11 +334,12 @@ function FeedbackDialog({
335
334
  setAttachError(null);
336
335
  try {
337
336
  const blob = await capturePage();
337
+ if (!aliveRef.current) return;
338
338
  await addFiles([blob]);
339
339
  } catch {
340
- setAttachError(MESSAGES.captureFailed);
340
+ if (aliveRef.current) setAttachError(MESSAGES.captureFailed);
341
341
  } finally {
342
- setCapturing(false);
342
+ if (aliveRef.current) setCapturing(false);
343
343
  }
344
344
  };
345
345
  const onFileChange = (e) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/feedback-react",
3
- "version": "0.1.2",
3
+ "version": "0.2.5",
4
4
  "description": "React feedback widget (edge tab, selection comments, screenshots) for chapi",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -38,11 +38,11 @@
38
38
  },
39
39
  "devDependencies": {
40
40
  "@testing-library/jest-dom": "^6.6.0",
41
- "@testing-library/react": "^16.0.0",
42
- "@testing-library/user-event": "^14.5.0",
41
+ "@testing-library/react": "^16.3.3",
42
+ "@testing-library/user-event": "^14.6.13",
43
43
  "@types/react": "^18.2.0",
44
44
  "@types/react-dom": "^18.2.0",
45
- "@vitejs/plugin-react": "^6.0.0",
45
+ "@vitejs/plugin-react": "^6.3.0",
46
46
  "jsdom": "^25.0.0",
47
47
  "react": "^18.2.0",
48
48
  "react-dom": "^18.2.0",