@kendew-agency/react-feedback-layer 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/dist/index.js ADDED
@@ -0,0 +1,712 @@
1
+ // src/components/comment-overlay.tsx
2
+ import { useRef, useState } from "react";
3
+
4
+ // src/contexts/comment-context.tsx
5
+ import { createContext, useContext, useEffect, useReducer } from "react";
6
+
7
+ // src/lib/tx.ts
8
+ import { TryX } from "@j31/tryx-handler";
9
+ var tx = new TryX({
10
+ timeout: 1e4
11
+ // 10 seconds
12
+ });
13
+
14
+ // src/utils/hasStatus.ts
15
+ function hasStatus(status) {
16
+ return (comment) => comment.status === status;
17
+ }
18
+
19
+ // src/contexts/comment-context.tsx
20
+ import { jsx } from "react/jsx-runtime";
21
+ var CommentContext = createContext(null);
22
+ var commentReducer = (state, action) => {
23
+ switch (action.type) {
24
+ case "REGISTER": {
25
+ const newId = `${state.config?.idPrefix ?? ""}${crypto.randomUUID()}`;
26
+ return {
27
+ ...state,
28
+ overlayState: "editing",
29
+ focussedComment: newId,
30
+ comments: [
31
+ ...state.comments,
32
+ {
33
+ id: newId,
34
+ user: state.currentUser,
35
+ position: {
36
+ x: action.position.x,
37
+ y: action.position.y
38
+ },
39
+ content: "",
40
+ createdAt: /* @__PURE__ */ new Date(),
41
+ status: "draft",
42
+ indicator: action.indicatorPosition ? {
43
+ width: action.indicatorPosition?.width,
44
+ height: action.indicatorPosition?.height
45
+ } : null
46
+ }
47
+ ]
48
+ };
49
+ }
50
+ case "UPDATE_COMMENTS": {
51
+ return {
52
+ ...state,
53
+ comments: action.comments
54
+ };
55
+ }
56
+ case "DELETE":
57
+ return {
58
+ ...state,
59
+ overlayState: "idle",
60
+ focussedComment: null,
61
+ comments: state.comments.filter((comment) => comment.id !== action.id)
62
+ };
63
+ case "EDIT":
64
+ return {
65
+ ...state,
66
+ overlayState: "idle",
67
+ focussedComment: null,
68
+ comments: state.comments.map((comment) => {
69
+ if (comment.id === action.id) {
70
+ return { ...comment, ...action.newComment };
71
+ }
72
+ return comment;
73
+ })
74
+ };
75
+ case "FOCUS":
76
+ return {
77
+ ...state,
78
+ overlayState: "editing",
79
+ focussedComment: action.id
80
+ };
81
+ case "TOGGLE_OVERLAY":
82
+ return {
83
+ ...state,
84
+ overlayState: state.overlayState === "inactive" ? "idle" : "inactive"
85
+ };
86
+ case "CHANGE_OVERLAYSTATE":
87
+ return {
88
+ ...state,
89
+ overlayState: action.to
90
+ };
91
+ case "UPDATE_VISIBILITY":
92
+ return {
93
+ ...state,
94
+ config: {
95
+ ...state.config,
96
+ commentVisibility: {
97
+ ...state.config?.commentVisibility,
98
+ ...action.visibility
99
+ }
100
+ }
101
+ };
102
+ case "RESET_DRAFT_COMMENTS": {
103
+ return {
104
+ ...state,
105
+ comments: state.comments.filter(
106
+ (comment) => comment.status !== "draft"
107
+ ),
108
+ focussedComment: null
109
+ };
110
+ }
111
+ case "RESET_RESOLVING_COMMENTS":
112
+ return {
113
+ ...state,
114
+ comments: state.comments.filter(
115
+ (comment) => comment.status !== "resolving"
116
+ ),
117
+ focussedComment: null
118
+ };
119
+ default: {
120
+ console.warn(`Unhandled action type, returning state`, action);
121
+ return state;
122
+ }
123
+ }
124
+ };
125
+ var CommentContextProvider = ({
126
+ children,
127
+ /**
128
+ * The initial comment coming from Sanity
129
+ */
130
+ initialComments,
131
+ /**
132
+ * Initial state of the comment overlay
133
+ * This is used to determine the initial state of the comment overlay
134
+ * @default "inactive"
135
+ */
136
+ initialOverlayState,
137
+ onConfirm,
138
+ onResolve,
139
+ currentUser,
140
+ subscription,
141
+ config = {
142
+ commentVisibility: {
143
+ hideResolved: false,
144
+ hideResolving: false
145
+ }
146
+ },
147
+ onError
148
+ }) => {
149
+ const [state, dispatch] = useReducer(commentReducer, {
150
+ comments: initialComments || [],
151
+ overlayState: initialOverlayState || "inactive",
152
+ currentUser,
153
+ focussedComment: null,
154
+ config
155
+ });
156
+ useEffect(() => {
157
+ if (!subscription) return;
158
+ subscription.subscribe((update) => {
159
+ dispatch({ type: "UPDATE_COMMENTS", comments: update });
160
+ });
161
+ return () => {
162
+ subscription.unsubscribe();
163
+ };
164
+ }, [subscription]);
165
+ const registerComment = (position, indicatorPosition) => {
166
+ dispatch({
167
+ type: "REGISTER",
168
+ position,
169
+ indicatorPosition: indicatorPosition || null
170
+ });
171
+ };
172
+ const deleteComment = (id) => {
173
+ dispatch({ type: "DELETE", id });
174
+ };
175
+ const updateComment = (id, newComment) => {
176
+ dispatch({ type: "EDIT", id, newComment });
177
+ };
178
+ const getActiveComment = () => {
179
+ return state.comments.find(
180
+ (comment) => comment.id === state.focussedComment
181
+ );
182
+ };
183
+ const focusOnComment = (id) => {
184
+ dispatch({ type: "FOCUS", id });
185
+ };
186
+ const toggleOverlay = () => {
187
+ dispatch({ type: "TOGGLE_OVERLAY" });
188
+ };
189
+ const getDraftComments = () => {
190
+ return state.comments.filter(hasStatus("draft"));
191
+ };
192
+ const getConfirmedComments = () => {
193
+ return state.comments.filter(hasStatus("published"));
194
+ };
195
+ const getResolvingComments = () => {
196
+ return state.comments.filter(hasStatus("resolving"));
197
+ };
198
+ const getResolvedComments = () => {
199
+ return state.comments.filter(hasStatus("resolved"));
200
+ };
201
+ const toggleResolvingComment = (id) => {
202
+ const comment = state.comments.find((comment2) => comment2.id === id);
203
+ dispatch({
204
+ type: "EDIT",
205
+ id,
206
+ newComment: {
207
+ status: comment?.status === "resolving" ? "published" : "resolving"
208
+ }
209
+ });
210
+ };
211
+ const confirmComments = async () => {
212
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: "saving" });
213
+ const draftComments = getDraftComments();
214
+ const { error } = await tx.executeAsync(
215
+ async () => onConfirm(
216
+ draftComments.map((c) => ({
217
+ ...c,
218
+ status: "published"
219
+ }))
220
+ )
221
+ );
222
+ if (error) {
223
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: "error" });
224
+ if (onError) {
225
+ onError(error);
226
+ }
227
+ return { error };
228
+ }
229
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
230
+ dispatch({ type: "RESET_DRAFT_COMMENTS" });
231
+ return { error: null };
232
+ };
233
+ const resolveComments = async () => {
234
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: "resolving" });
235
+ const resolvingComments = getResolvingComments();
236
+ const { error } = await tx.executeAsync(
237
+ async () => onResolve(
238
+ resolvingComments.map((c) => ({
239
+ ...c,
240
+ status: "resolved"
241
+ }))
242
+ )
243
+ );
244
+ if (error) {
245
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: "error" });
246
+ if (onError) {
247
+ onError(error);
248
+ }
249
+ return { error };
250
+ }
251
+ dispatch({ type: "RESET_RESOLVING_COMMENTS" });
252
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
253
+ return { error: null };
254
+ };
255
+ const updateCommentVisibility = (visibility) => {
256
+ dispatch({ type: "UPDATE_VISIBILITY", visibility });
257
+ };
258
+ const changeOverlayState = (state2) => {
259
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: state2 });
260
+ };
261
+ return /* @__PURE__ */ jsx(
262
+ CommentContext.Provider,
263
+ {
264
+ value: {
265
+ draftComments: getDraftComments(),
266
+ comments: getConfirmedComments(),
267
+ resolvingComments: getResolvingComments(),
268
+ resolvedComments: getResolvedComments(),
269
+ allComments: state.comments,
270
+ config: state.config,
271
+ changeOverlayState,
272
+ toggleResolvingComment,
273
+ overlayState: state.overlayState,
274
+ deleteComment,
275
+ updateComment,
276
+ focusOnComment,
277
+ registerComment,
278
+ getActiveComment,
279
+ toggleOverlay,
280
+ confirmComments,
281
+ currentUser,
282
+ resolveComments,
283
+ updateCommentVisibility
284
+ },
285
+ children
286
+ }
287
+ );
288
+ };
289
+ var useComments = () => {
290
+ const context = useContext(CommentContext);
291
+ if (!context) {
292
+ throw new Error("useComments must be used within a CommentContextProvider");
293
+ }
294
+ return context;
295
+ };
296
+
297
+ // src/utils/position.ts
298
+ var getRelativePos = (e, el) => {
299
+ const rect = el.getBoundingClientRect();
300
+ return { x: e.clientX - rect.left, y: e.clientY - rect.top };
301
+ };
302
+ var normalizeRect = (a, b) => {
303
+ const x = Math.min(a.x, b.x);
304
+ const y = Math.min(a.y, b.y);
305
+ const width = Math.abs(b.x - a.x);
306
+ const height = Math.abs(b.y - a.y);
307
+ return { x, y, width, height };
308
+ };
309
+
310
+ // src/components/comment-overlay.tsx
311
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
312
+ var DRAG_THRESHOLD = 4;
313
+ var CommentOverlay = ({ children }) => {
314
+ const {
315
+ overlayState,
316
+ registerComment,
317
+ focusOnComment,
318
+ getActiveComment,
319
+ changeOverlayState
320
+ } = useComments();
321
+ const [preview, setPreview] = useState(null);
322
+ const startRef = useRef(null);
323
+ const draggingRef = useRef(false);
324
+ const overlayRef = useRef(null);
325
+ const onPointerDown = (e) => {
326
+ if (overlayState === "editing") {
327
+ alert(
328
+ "Can not add a new comment. You are currently editing a comment. Please finish editing before adding a new one."
329
+ );
330
+ return;
331
+ }
332
+ const el = overlayRef.current;
333
+ if (!el) return;
334
+ el.setPointerCapture(e.pointerId);
335
+ const start = getRelativePos(e.nativeEvent, el);
336
+ startRef.current = start;
337
+ draggingRef.current = false;
338
+ setPreview(null);
339
+ };
340
+ const onPointerMove = (e) => {
341
+ const el = overlayRef.current;
342
+ const start = startRef.current;
343
+ if (!el || !start) return;
344
+ const current = getRelativePos(e.nativeEvent, el);
345
+ const dx = current.x - start.x;
346
+ const dy = current.y - start.y;
347
+ if (!draggingRef.current && Math.hypot(dx, dy) >= DRAG_THRESHOLD) {
348
+ draggingRef.current = true;
349
+ }
350
+ if (draggingRef.current) {
351
+ const rect = normalizeRect(start, current);
352
+ setPreview(rect);
353
+ }
354
+ };
355
+ const onPointerUp = (e) => {
356
+ const el = overlayRef.current;
357
+ const start = startRef.current;
358
+ if (!el || !start) return;
359
+ const end = getRelativePos(e.nativeEvent, el);
360
+ startRef.current = null;
361
+ if (!draggingRef.current) {
362
+ registerComment(start);
363
+ return;
364
+ }
365
+ draggingRef.current = false;
366
+ const rect = normalizeRect(start, end);
367
+ if (rect.width < DRAG_THRESHOLD && rect.height < DRAG_THRESHOLD) {
368
+ registerComment(start);
369
+ setPreview(null);
370
+ return;
371
+ }
372
+ const commentPosition = {
373
+ x: rect.x + rect.width,
374
+ y: rect.y + rect.height
375
+ };
376
+ registerComment(commentPosition, {
377
+ width: rect.width,
378
+ height: rect.height
379
+ });
380
+ setPreview(null);
381
+ };
382
+ if (overlayState === "inactive") return children;
383
+ return /* @__PURE__ */ jsxs("div", { "data-overlay": true, style: { position: "relative" }, children: [
384
+ /* @__PURE__ */ jsx2(
385
+ "div",
386
+ {
387
+ ref: overlayRef,
388
+ onPointerDown,
389
+ onPointerMove,
390
+ onPointerUp,
391
+ style: {
392
+ ...OVERLAY_STYLES,
393
+ ...overlayState === "editing" && EDITING_STYLES
394
+ }
395
+ }
396
+ ),
397
+ getActiveComment() && // biome-ignore lint: This element should only handle clicks as a guard
398
+ /* @__PURE__ */ jsx2(
399
+ "div",
400
+ {
401
+ "data-comment-guard": true,
402
+ onClick: () => {
403
+ focusOnComment(null);
404
+ changeOverlayState("idle");
405
+ },
406
+ style: {
407
+ width: "100%",
408
+ height: "100%",
409
+ position: "absolute",
410
+ inset: 0,
411
+ zIndex: 2,
412
+ isolation: "isolate"
413
+ }
414
+ }
415
+ ),
416
+ preview && /* @__PURE__ */ jsx2(
417
+ "div",
418
+ {
419
+ style: {
420
+ position: "absolute",
421
+ left: preview.x,
422
+ top: preview.y,
423
+ width: preview.width,
424
+ height: preview.height,
425
+ border: "1px dashed currentColor",
426
+ pointerEvents: "none"
427
+ }
428
+ }
429
+ ),
430
+ children
431
+ ] });
432
+ };
433
+ var OVERLAY_STYLES = {
434
+ width: "100%",
435
+ height: "100%",
436
+ position: "absolute",
437
+ inset: 0,
438
+ zIndex: 20,
439
+ cursor: "crosshair",
440
+ isolation: "isolate"
441
+ };
442
+ var EDITING_STYLES = {
443
+ pointerEvents: "none"
444
+ };
445
+
446
+ // src/components/comment-renderer.tsx
447
+ import { Fragment, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
448
+ var CommentRenderer = ({
449
+ Comment: Comment2,
450
+ DraftComment,
451
+ ResolvingComment,
452
+ ResolvedComment
453
+ }) => {
454
+ const {
455
+ comments,
456
+ draftComments,
457
+ overlayState,
458
+ resolvedComments,
459
+ resolvingComments,
460
+ config
461
+ } = useComments();
462
+ if (overlayState === "inactive") return null;
463
+ return /* @__PURE__ */ jsxs2(Fragment, { children: [
464
+ draftComments.map((comment) => {
465
+ return /* @__PURE__ */ jsx3(DraftComment, { comment }, comment.id);
466
+ }),
467
+ comments.map((comment) => {
468
+ return /* @__PURE__ */ jsx3(Comment2, { comment }, comment.id);
469
+ }),
470
+ resolvingComments.map((comment) => {
471
+ if (config?.commentVisibility?.hideResolving) return null;
472
+ return /* @__PURE__ */ jsx3(ResolvingComment, { comment }, comment.id);
473
+ }),
474
+ resolvedComments.map((comment) => {
475
+ if (config?.commentVisibility?.hideResolved) return null;
476
+ return /* @__PURE__ */ jsx3(ResolvedComment, { comment }, comment.id);
477
+ })
478
+ ] });
479
+ };
480
+
481
+ // src/components/comment-actions.tsx
482
+ import { Slot } from "@radix-ui/react-slot";
483
+ import { jsx as jsx4 } from "react/jsx-runtime";
484
+ var ToggleOverlayButton = ({ asChild, ...rest }) => {
485
+ const { toggleOverlay, overlayState } = useComments();
486
+ const Comp = asChild ? Slot : "button";
487
+ return /* @__PURE__ */ jsx4(
488
+ Comp,
489
+ {
490
+ "data-active": overlayState !== "inactive",
491
+ ...rest,
492
+ onClick: (e) => {
493
+ toggleOverlay();
494
+ rest.onClick?.(e);
495
+ }
496
+ }
497
+ );
498
+ };
499
+ var ConfirmComments = ({ asChild, ...rest }) => {
500
+ const { confirmComments } = useComments();
501
+ const Comp = asChild ? Slot : "button";
502
+ return /* @__PURE__ */ jsx4(
503
+ Comp,
504
+ {
505
+ ...rest,
506
+ onClick: (e) => {
507
+ confirmComments();
508
+ rest.onClick?.(e);
509
+ }
510
+ }
511
+ );
512
+ };
513
+ var ResolveComments = ({ asChild, ...rest }) => {
514
+ const { resolveComments } = useComments();
515
+ const Comp = asChild ? Slot : "button";
516
+ return /* @__PURE__ */ jsx4(
517
+ Comp,
518
+ {
519
+ ...rest,
520
+ onClick: (e) => {
521
+ resolveComments();
522
+ rest.onClick?.(e);
523
+ }
524
+ }
525
+ );
526
+ };
527
+
528
+ // src/components/comment.tsx
529
+ import {
530
+ useState as useState2
531
+ } from "react";
532
+ import { Slot as Slot2 } from "@radix-ui/react-slot";
533
+
534
+ // src/contexts/scoped-comment.tsx
535
+ import * as React from "react";
536
+ var CommentScopeContext = React.createContext(
537
+ null
538
+ );
539
+ function useCommentScope() {
540
+ const ctx = React.useContext(CommentScopeContext);
541
+ if (!ctx) {
542
+ throw new Error("Comment.* must be used within <Comment.Root />");
543
+ }
544
+ return ctx;
545
+ }
546
+
547
+ // src/components/comment.tsx
548
+ import { jsx as jsx5 } from "react/jsx-runtime";
549
+ var CommentRoot = ({
550
+ comment,
551
+ children,
552
+ defaultDraft,
553
+ ...rest
554
+ }) => {
555
+ const { getActiveComment } = useComments();
556
+ const isActive = getActiveComment()?.id === comment.id;
557
+ const [draft, setDraft] = useState2(defaultDraft ?? comment.content ?? "");
558
+ return /* @__PURE__ */ jsx5(
559
+ CommentScopeContext.Provider,
560
+ {
561
+ value: { comment, isActive, draft, setDraft },
562
+ children: /* @__PURE__ */ jsx5(
563
+ "div",
564
+ {
565
+ ...rest,
566
+ "data-comment": comment.id,
567
+ "data-active": isActive,
568
+ style: {
569
+ top: comment.position.y,
570
+ left: comment.position.x,
571
+ position: "absolute",
572
+ zIndex: isActive ? 40 : 30,
573
+ ...rest.style ?? {}
574
+ },
575
+ children
576
+ }
577
+ )
578
+ }
579
+ );
580
+ };
581
+ var CommentIndicator = ({ ...rest }) => {
582
+ const { comment, isActive } = useCommentScope();
583
+ const { config } = useComments();
584
+ if (!comment.indicator) return null;
585
+ if (config?.indicatorVisibility === "active" && !isActive) return null;
586
+ return /* @__PURE__ */ jsx5(
587
+ "div",
588
+ {
589
+ ...rest,
590
+ "data-parent": comment.id,
591
+ style: {
592
+ width: comment.indicator.width,
593
+ height: comment.indicator.height,
594
+ position: "absolute",
595
+ top: 0 - comment.indicator.height,
596
+ left: 0 - comment.indicator.width,
597
+ zIndex: 1,
598
+ pointerEvents: "none",
599
+ border: "1px dashed black",
600
+ ...rest.style
601
+ }
602
+ }
603
+ );
604
+ };
605
+ var CommentContent = ({ asChild, ...rest }) => {
606
+ const { draft } = useCommentScope();
607
+ const Comp = asChild ? Slot2 : "p";
608
+ return /* @__PURE__ */ jsx5(Comp, { ...rest, children: draft });
609
+ };
610
+ var CommentEdit = ({ asChild, ...rest }) => {
611
+ const { draft, setDraft } = useCommentScope();
612
+ const Comp = asChild ? Slot2 : "textarea";
613
+ return /* @__PURE__ */ jsx5(
614
+ Comp,
615
+ {
616
+ ...rest,
617
+ value: rest.value ?? draft,
618
+ name: "commentEdit",
619
+ placeholder: "Write a comment...",
620
+ onChange: (e) => {
621
+ setDraft(e.target.value);
622
+ rest.onChange?.(e);
623
+ }
624
+ }
625
+ );
626
+ };
627
+ var CommentConfirm = ({ asChild, ...rest }) => {
628
+ const { updateComment } = useComments();
629
+ const { comment, draft } = useCommentScope();
630
+ const Comp = asChild ? Slot2 : "button";
631
+ return /* @__PURE__ */ jsx5(
632
+ Comp,
633
+ {
634
+ ...rest,
635
+ disabled: !draft,
636
+ onClick: (e) => {
637
+ updateComment(comment.id, { content: draft });
638
+ rest.onClick?.(e);
639
+ }
640
+ }
641
+ );
642
+ };
643
+ var CommentDelete = ({ asChild, ...rest }) => {
644
+ const { deleteComment } = useComments();
645
+ const { comment } = useCommentScope();
646
+ const Comp = asChild ? Slot2 : "button";
647
+ return /* @__PURE__ */ jsx5(
648
+ Comp,
649
+ {
650
+ ...rest,
651
+ onClick: (e) => {
652
+ deleteComment(comment.id);
653
+ rest.onClick?.(e);
654
+ }
655
+ }
656
+ );
657
+ };
658
+ var CommentResolve = ({ asChild, ...rest }) => {
659
+ const { toggleResolvingComment } = useComments();
660
+ const { comment } = useCommentScope();
661
+ const Comp = asChild ? Slot2 : "button";
662
+ if (comment.status === "draft") {
663
+ console.error(
664
+ `A draft comment can not be marked as resolving, remove this button from this comment: ${comment.id}`
665
+ );
666
+ return null;
667
+ }
668
+ if (comment.status === "resolved") {
669
+ console.error(
670
+ `A resolved comment can not be marked as resolving, remove this button from this comment: ${comment.id}`
671
+ );
672
+ return null;
673
+ }
674
+ return /* @__PURE__ */ jsx5(
675
+ Comp,
676
+ {
677
+ ...rest,
678
+ onClick: (e) => {
679
+ toggleResolvingComment(comment.id);
680
+ rest.onClick?.(e);
681
+ }
682
+ }
683
+ );
684
+ };
685
+ var Comment = {
686
+ // biome-ignore lint/style/useNamingConvention: Modular export
687
+ Root: CommentRoot,
688
+ // biome-ignore lint/style/useNamingConvention: Modular export
689
+ Indicator: CommentIndicator,
690
+ // biome-ignore lint/style/useNamingConvention: Modular export
691
+ Content: CommentContent,
692
+ // biome-ignore lint/style/useNamingConvention: Modular export
693
+ Edit: CommentEdit,
694
+ // biome-ignore lint/style/useNamingConvention: Modular export
695
+ Confirm: CommentConfirm,
696
+ // biome-ignore lint/style/useNamingConvention: Modular export
697
+ Delete: CommentDelete,
698
+ // biome-ignore lint/style/useNamingConvention: Modular export
699
+ Resolve: CommentResolve
700
+ };
701
+ export {
702
+ Comment,
703
+ CommentContextProvider,
704
+ CommentOverlay,
705
+ CommentRenderer,
706
+ CommentScopeContext,
707
+ ConfirmComments,
708
+ ResolveComments,
709
+ ToggleOverlayButton,
710
+ useCommentScope,
711
+ useComments
712
+ };