@kendew-agency/react-feedback-layer 0.3.2 → 0.4.0-rc.1

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 CHANGED
@@ -1,30 +1,19 @@
1
1
  // src/components/comment-overlay.tsx
2
2
  import {
3
- useRef as useRef3,
3
+ useRef as useRef5,
4
4
  useState
5
5
  } from "react";
6
6
 
7
7
  // src/contexts/comment-context.tsx
8
8
  import {
9
- createContext,
10
- useContext,
11
- useEffect,
12
- useReducer,
13
- useRef as useRef2
9
+ createContext as createContext2,
10
+ useContext as useContext2,
11
+ useEffect as useEffect2,
12
+ useId,
13
+ useReducer as useReducer2,
14
+ useRef as useRef4
14
15
  } from "react";
15
16
 
16
- // src/lib/tx.ts
17
- import { TryX } from "@j31/tryx-handler";
18
- var tx = new TryX({
19
- timeout: 1e4
20
- // 10 seconds
21
- });
22
-
23
- // src/utils/hasStatus.ts
24
- function hasStatus(status) {
25
- return (comment) => comment.status === status;
26
- }
27
-
28
17
  // src/errors.ts
29
18
  var BaseError = class extends Error {
30
19
  constructor(message, code = "UNKNOWN_ERROR", status = 500) {
@@ -40,23 +29,88 @@ var InsertError = class extends BaseError {
40
29
  }
41
30
  };
42
31
  var ResolveError = class extends BaseError {
43
- constructor(message = "An error occured while resolving your comments") {
32
+ constructor(message = "An error occured while resolving your comment") {
44
33
  super(message, "RESOLVE_ERROR", 500);
45
34
  }
46
35
  };
47
36
  var ConfirmError = class extends BaseError {
48
- constructor(message = "An error occured while confirming your comments") {
37
+ constructor(message = "An error occured while confirming your session") {
49
38
  super(message, "CONFIRM_ERROR", 500);
50
39
  }
51
40
  };
41
+ var SessionCreateError = class extends BaseError {
42
+ constructor(message = "An error occured while creating your session") {
43
+ super(message, "SESSION_CREATE_ERROR", 500);
44
+ }
45
+ };
46
+ var AddError = class extends BaseError {
47
+ constructor(message = "An error occured while adding your comment") {
48
+ super(message, "ADD_ERROR", 500);
49
+ }
50
+ };
51
+ var EditError = class extends BaseError {
52
+ constructor(message = "An error occured while editing your comment") {
53
+ super(message, "EDIT_ERROR", 500);
54
+ }
55
+ };
56
+ var DeleteError = class extends BaseError {
57
+ constructor(message = "An error occured while deleting your comment") {
58
+ super(message, "DELETE_ERROR", 500);
59
+ }
60
+ };
61
+ var SessionStateError = class extends BaseError {
62
+ constructor(message = "This action is not allowed in the current session state") {
63
+ super(message, "SESSION_STATE_ERROR", 500);
64
+ }
65
+ };
66
+ var ReopenError = class extends BaseError {
67
+ constructor(message = "An error occured while reopening your comment") {
68
+ super(message, "REOPEN_ERROR", 500);
69
+ }
70
+ };
71
+ var StartResolvingError = class extends BaseError {
72
+ constructor(message = "An error occured while starting the resolution of your session") {
73
+ super(message, "START_RESOLVING_ERROR", 500);
74
+ }
75
+ };
76
+ var SessionResolveError = class extends BaseError {
77
+ constructor(message = "An error occured while resolving your session") {
78
+ super(message, "SESSION_RESOLVE_ERROR", 500);
79
+ }
80
+ };
81
+ var PermissionError = class extends BaseError {
82
+ constructor(message = "You do not have permission to perform this action") {
83
+ super(message, "PERMISSION_ERROR", 500);
84
+ }
85
+ };
86
+
87
+ // src/lib/to-known-error.ts
88
+ var toKnownError = (reason, ErrorClass) => {
89
+ const message = reason instanceof Error && reason.message.length > 0 ? reason.message : void 0;
90
+ return new ErrorClass(message);
91
+ };
92
+
93
+ // src/lib/tx.ts
94
+ import { TryX } from "@j31/tryx-handler";
95
+ var tx = new TryX({
96
+ timeout: 1e4
97
+ // 10 seconds
98
+ });
99
+
100
+ // src/utils/hasStatus.ts
101
+ function hasStatus(status) {
102
+ return (comment) => comment.status === status;
103
+ }
52
104
 
53
105
  // src/lib/use-comment-events.ts
54
106
  import { useCallback, useRef } from "react";
55
107
  var useCommentEvents = () => {
56
108
  const listenersRef = useRef({
57
109
  onFocusChange: /* @__PURE__ */ new Set(),
58
- onCommentsConfirmed: /* @__PURE__ */ new Set(),
59
- onCommentsResolved: /* @__PURE__ */ new Set(),
110
+ onCommentAdded: /* @__PURE__ */ new Set(),
111
+ onCommentEdited: /* @__PURE__ */ new Set(),
112
+ onCommentResolved: /* @__PURE__ */ new Set(),
113
+ onCommentReopened: /* @__PURE__ */ new Set(),
60
114
  onCommentDeleted: /* @__PURE__ */ new Set(),
61
115
  onCommentRegistered: /* @__PURE__ */ new Set()
62
116
  });
@@ -79,17 +133,553 @@ var useCommentEvents = () => {
79
133
  );
80
134
  const events = {
81
135
  onFocusChange: (callback) => subscribe("onFocusChange", callback),
82
- onCommentsConfirmed: (callback) => subscribe("onCommentsConfirmed", callback),
83
- onCommentsResolved: (callback) => subscribe("onCommentsResolved", callback),
136
+ onCommentAdded: (callback) => subscribe("onCommentAdded", callback),
137
+ onCommentEdited: (callback) => subscribe("onCommentEdited", callback),
138
+ onCommentResolved: (callback) => subscribe("onCommentResolved", callback),
139
+ onCommentReopened: (callback) => subscribe("onCommentReopened", callback),
84
140
  onCommentDeleted: (callback) => subscribe("onCommentDeleted", callback),
85
141
  onCommentRegistered: (callback) => subscribe("onCommentRegistered", callback)
86
142
  };
87
143
  return { events, emit };
88
144
  };
89
145
 
90
- // src/contexts/comment-context.tsx
146
+ // src/utils/select-visible-comments.ts
147
+ var normalizeDraftVisibility = (value) => value === "hidden" ? "hidden" : "visible";
148
+ var isLocated = (comment) => typeof comment.locationId === "string" && comment.locationId.length > 0;
149
+ var normalizeLocationId = (value) => typeof value === "string" && value.length > 0 ? value : void 0;
150
+ var applyDraftFilter = (comments, session, draftVisibility, currentUserId) => {
151
+ if (session.status !== "draft" || normalizeDraftVisibility(draftVisibility) !== "hidden") {
152
+ return comments;
153
+ }
154
+ return comments.filter((comment) => {
155
+ if (comment.status === "draft") {
156
+ return true;
157
+ }
158
+ const authorId = comment.user?.id;
159
+ return authorId === void 0 || authorId === currentUserId;
160
+ });
161
+ };
162
+ var selectCommentsByLocation = ({
163
+ comments,
164
+ locationId
165
+ }) => {
166
+ const location = normalizeLocationId(locationId);
167
+ if (location === void 0) {
168
+ return [];
169
+ }
170
+ return comments.filter(
171
+ (comment) => isLocated(comment) && comment.locationId === location
172
+ );
173
+ };
174
+ var selectVisibleComments = ({
175
+ comments,
176
+ session,
177
+ locationId,
178
+ draftVisibility = "visible",
179
+ currentUserId
180
+ }) => {
181
+ if (session === null) {
182
+ return [];
183
+ }
184
+ const inSession = selectCommentsByLocation({ comments, locationId }).filter(
185
+ (comment) => comment.sessionId === session.id
186
+ );
187
+ return applyDraftFilter(inSession, session, draftVisibility, currentUserId);
188
+ };
189
+ var selectCommentsBySession = ({
190
+ comments,
191
+ locationId,
192
+ session,
193
+ draftVisibility = "visible",
194
+ currentUserId
195
+ }) => {
196
+ const grouped = {};
197
+ for (const comment of selectCommentsByLocation({ comments, locationId })) {
198
+ const list = grouped[comment.sessionId];
199
+ if (list) {
200
+ list.push(comment);
201
+ } else {
202
+ grouped[comment.sessionId] = [comment];
203
+ }
204
+ }
205
+ if (session !== null) {
206
+ const active = grouped[session.id];
207
+ if (active) {
208
+ const filtered = applyDraftFilter(
209
+ active,
210
+ session,
211
+ draftVisibility,
212
+ currentUserId
213
+ );
214
+ if (filtered.length === 0) {
215
+ delete grouped[session.id];
216
+ } else {
217
+ grouped[session.id] = filtered;
218
+ }
219
+ }
220
+ }
221
+ return grouped;
222
+ };
223
+
224
+ // src/contexts/feedback-session-context.tsx
225
+ import {
226
+ createContext,
227
+ useCallback as useCallback3,
228
+ useContext,
229
+ useEffect,
230
+ useMemo,
231
+ useReducer,
232
+ useRef as useRef3
233
+ } from "react";
234
+
235
+ // src/lib/use-session-events.ts
236
+ import { useCallback as useCallback2, useRef as useRef2 } from "react";
237
+ var useSessionEvents = () => {
238
+ const listenersRef = useRef2({
239
+ onSessionCreated: /* @__PURE__ */ new Set(),
240
+ onSessionConfirmed: /* @__PURE__ */ new Set(),
241
+ onSessionResolving: /* @__PURE__ */ new Set(),
242
+ onSessionResolved: /* @__PURE__ */ new Set()
243
+ });
244
+ const subscribe = useCallback2(
245
+ (event, callback) => {
246
+ listenersRef.current[event].add(callback);
247
+ return () => {
248
+ listenersRef.current[event].delete(callback);
249
+ };
250
+ },
251
+ []
252
+ );
253
+ const emit = useCallback2(
254
+ (event, ...args) => {
255
+ for (const listener of listenersRef.current[event]) {
256
+ listener(...args);
257
+ }
258
+ },
259
+ []
260
+ );
261
+ const events = {
262
+ onSessionCreated: (callback) => subscribe("onSessionCreated", callback),
263
+ onSessionConfirmed: (callback) => subscribe("onSessionConfirmed", callback),
264
+ onSessionResolving: (callback) => subscribe("onSessionResolving", callback),
265
+ onSessionResolved: (callback) => subscribe("onSessionResolved", callback)
266
+ };
267
+ return { events, emit };
268
+ };
269
+
270
+ // src/utils/session-type.ts
271
+ var SESSION_TYPES = ["feedback"];
272
+ var isSessionType = (value) => SESSION_TYPES.includes(value);
273
+ var SESSION_STATUSES = [
274
+ "draft",
275
+ "confirmed",
276
+ "resolving",
277
+ "resolved"
278
+ ];
279
+ var parseInitialSession = (value) => {
280
+ if (value === null || typeof value !== "object") return null;
281
+ const status = value.status;
282
+ if (!SESSION_STATUSES.includes(status)) return null;
283
+ const session = value;
284
+ return {
285
+ ...value,
286
+ comments: session.comments ?? []
287
+ };
288
+ };
289
+
290
+ // src/utils/session-guards.ts
291
+ var locatedComments = (session) => session.comments.filter(isLocated);
292
+ var confirmRefusal = (session, localDrafts) => {
293
+ if (!session || session.status !== "draft") return "session is not a draft";
294
+ if (localDrafts > 0) return "a local draft comment exists";
295
+ if (locatedComments(session).length === 0) return "session has no comments";
296
+ if (!isSessionType(session.type)) return "unknown session type";
297
+ return null;
298
+ };
299
+ var resolveRefusal = (session) => {
300
+ if (session?.status !== "resolving") return "session is not resolving";
301
+ return locatedComments(session).some((c) => c.status === "published") ? "a comment is still published" : null;
302
+ };
303
+ var sumCounts = (counts) => {
304
+ let total = 0;
305
+ for (const count of counts.values()) total += count;
306
+ return total;
307
+ };
308
+
309
+ // src/utils/session-reducer.ts
310
+ var initialSessionReducerState = {
311
+ session: null,
312
+ sessionState: "idle"
313
+ };
314
+ var withSession = (state, session) => ({ ...state, session });
315
+ var onSessionCreated = (state, action) => {
316
+ const { confirmedAt: _c, resolvedAt: _r, ...rest } = action.session;
317
+ return withSession(state, {
318
+ ...rest,
319
+ participantIds: [...action.session.participantIds],
320
+ status: "draft",
321
+ comments: []
322
+ });
323
+ };
324
+ var onSessionConfirmed = (state, action) => {
325
+ const { session } = state;
326
+ if (!session || session.status !== "draft") return state;
327
+ return withSession(state, {
328
+ ...session,
329
+ status: "confirmed",
330
+ confirmedAt: action.confirmedAt
331
+ });
332
+ };
333
+ var onResolvingStarted = (state) => {
334
+ const { session } = state;
335
+ if (!session || session.status !== "confirmed") return state;
336
+ return withSession(state, { ...session, status: "resolving" });
337
+ };
338
+ var onSessionResolved = (state, action) => {
339
+ const { session } = state;
340
+ if (!session || session.status !== "resolving") return state;
341
+ return withSession(state, {
342
+ ...session,
343
+ status: "resolved",
344
+ resolvedAt: action.resolvedAt
345
+ });
346
+ };
347
+ var onCommentCommitted = (state, action) => {
348
+ const { session } = state;
349
+ if (!session || session.status !== "draft") return state;
350
+ const { comment } = action;
351
+ const exists = session.comments.some((c) => c.id === comment.id);
352
+ const comments = exists ? session.comments.map((c) => c.id === comment.id ? comment : c) : [...session.comments, comment];
353
+ const userId = comment.user?.id;
354
+ const participantIds = userId !== void 0 && !session.participantIds.includes(userId) ? [...session.participantIds, userId] : session.participantIds;
355
+ return withSession(state, { ...session, comments, participantIds });
356
+ };
357
+ var onCommentStatusChanged = (state, action) => {
358
+ const { session } = state;
359
+ if (!session || session.status !== "resolving") return state;
360
+ const { comment } = action;
361
+ return withSession(state, {
362
+ ...session,
363
+ comments: session.comments.map((c) => c.id === comment.id ? comment : c)
364
+ });
365
+ };
366
+ var onCommentRemoved = (state, action) => {
367
+ const { session } = state;
368
+ if (!session || session.status !== "draft") return state;
369
+ return withSession(state, {
370
+ ...session,
371
+ comments: session.comments.filter((c) => c.id !== action.commentId)
372
+ });
373
+ };
374
+ var onSetOperationState = (state, action) => {
375
+ if (state.sessionState === action.to) return state;
376
+ return { ...state, sessionState: action.to };
377
+ };
378
+ var sessionReducer = (state, action) => {
379
+ switch (action.type) {
380
+ case "SESSION_CREATED":
381
+ return onSessionCreated(state, action);
382
+ case "SESSION_CONFIRMED":
383
+ return onSessionConfirmed(state, action);
384
+ case "SESSION_RESOLVING_STARTED":
385
+ return onResolvingStarted(state, action);
386
+ case "SESSION_RESOLVED":
387
+ return onSessionResolved(state, action);
388
+ case "COMMENT_COMMITTED":
389
+ return onCommentCommitted(state, action);
390
+ case "COMMENT_STATUS_CHANGED":
391
+ return onCommentStatusChanged(state, action);
392
+ case "COMMENT_REMOVED":
393
+ return onCommentRemoved(state, action);
394
+ case "SET_OPERATION_STATE":
395
+ return onSetOperationState(state, action);
396
+ default:
397
+ return state;
398
+ }
399
+ };
400
+
401
+ // src/contexts/feedback-session-context.tsx
91
402
  import { jsx } from "react/jsx-runtime";
92
- var CommentContext = createContext(null);
403
+ var FeedbackSessionContext = createContext(
404
+ null
405
+ );
406
+ var SessionBridgeContext = createContext(null);
407
+ var MAX_PROJECT_ID_LENGTH = 128;
408
+ var isValidProjectId = (projectId) => projectId === void 0 || typeof projectId === "string" && projectId.length >= 1 && projectId.length <= MAX_PROJECT_ID_LENGTH;
409
+ var pick = (value, fallback) => value !== void 0 ? value : fallback;
410
+ var createRefusal = (type, projectId, inFlight, session, hasCallback, user) => {
411
+ if (!user) return "user is required";
412
+ if (!isSessionType(type)) return "unknown session type";
413
+ if (!isValidProjectId(projectId)) {
414
+ return "projectId must be 1 to 128 characters";
415
+ }
416
+ if (inFlight === "creating") return "creation already in progress";
417
+ if (session?.status === "draft") return "draft session already exists";
418
+ if (!hasCallback) return "onCreateSession is not provided";
419
+ return null;
420
+ };
421
+ var FeedbackSessionProvider = ({
422
+ children,
423
+ initialSession,
424
+ projectId,
425
+ canResolveSession = false,
426
+ onCreateSession,
427
+ onConfirmSession,
428
+ onStartResolving,
429
+ onResolveSession,
430
+ onError
431
+ }) => {
432
+ const initialParsedRef = useRef3(
433
+ void 0
434
+ );
435
+ if (initialParsedRef.current === void 0) {
436
+ initialParsedRef.current = initialSession === void 0 ? null : parseInitialSession(initialSession);
437
+ }
438
+ const [state, dispatch] = useReducer(
439
+ sessionReducer,
440
+ initialParsedRef.current,
441
+ (session) => ({
442
+ ...initialSessionReducerState,
443
+ session
444
+ })
445
+ );
446
+ const stateRef = useRef3({
447
+ ...initialSessionReducerState,
448
+ session: initialParsedRef.current
449
+ });
450
+ const apply = useCallback3((action) => {
451
+ stateRef.current = sessionReducer(stateRef.current, action);
452
+ dispatch(action);
453
+ }, []);
454
+ const canResolveRef = useRef3(canResolveSession);
455
+ canResolveRef.current = canResolveSession;
456
+ const onErrorRef = useRef3(onError);
457
+ onErrorRef.current = onError;
458
+ const inFlightRef = useRef3("idle");
459
+ const localDraftCountsRef = useRef3(/* @__PURE__ */ new Map());
460
+ const { events, emit } = useSessionEvents();
461
+ const report = useCallback3((error) => {
462
+ onErrorRef.current?.(error);
463
+ }, []);
464
+ useEffect(() => {
465
+ if (initialSession !== void 0 && initialParsedRef.current === null) {
466
+ report(new SessionStateError("initialSession has an invalid status"));
467
+ }
468
+ }, []);
469
+ const createSession = useCallback3(
470
+ async (options) => {
471
+ const type = options?.type ?? "feedback";
472
+ const user = options?.user;
473
+ const sessionProjectId = pick(options?.projectId, projectId);
474
+ const refusal = createRefusal(
475
+ type,
476
+ sessionProjectId,
477
+ inFlightRef.current,
478
+ stateRef.current.session,
479
+ onCreateSession !== void 0,
480
+ user
481
+ );
482
+ if (refusal !== null || !onCreateSession) {
483
+ const error = new SessionCreateError(refusal ?? void 0);
484
+ report(error);
485
+ return { error };
486
+ }
487
+ inFlightRef.current = "creating";
488
+ apply({ type: "SET_OPERATION_STATE", to: "creating" });
489
+ try {
490
+ const input = {
491
+ type,
492
+ user,
493
+ projectId: sessionProjectId
494
+ };
495
+ const { data, error } = await tx.executeAsync(
496
+ async () => onCreateSession(input)
497
+ );
498
+ if (error || !data) {
499
+ const known = toKnownError(error, SessionCreateError);
500
+ report(known);
501
+ return { error: known };
502
+ }
503
+ apply({
504
+ type: "SESSION_CREATED",
505
+ session: { ...data, status: "draft", type }
506
+ });
507
+ const created = stateRef.current.session;
508
+ if (created) emit("onSessionCreated", created);
509
+ return { error: null };
510
+ } finally {
511
+ inFlightRef.current = "idle";
512
+ apply({ type: "SET_OPERATION_STATE", to: "idle" });
513
+ }
514
+ },
515
+ [apply, emit, onCreateSession, projectId, report]
516
+ );
517
+ const confirmSession = useCallback3(async () => {
518
+ if (inFlightRef.current === "confirming") return { error: null };
519
+ const session = stateRef.current.session;
520
+ const refusal = confirmRefusal(
521
+ session,
522
+ sumCounts(localDraftCountsRef.current)
523
+ );
524
+ if (refusal !== null || !session) {
525
+ const error = new SessionStateError(refusal ?? void 0);
526
+ report(error);
527
+ return { error };
528
+ }
529
+ inFlightRef.current = "confirming";
530
+ apply({ type: "SET_OPERATION_STATE", to: "confirming" });
531
+ try {
532
+ if (onConfirmSession) {
533
+ const { error } = await tx.executeAsync(
534
+ async () => onConfirmSession(session.id, session.type)
535
+ );
536
+ if (error) {
537
+ const known = toKnownError(error, ConfirmError);
538
+ report(known);
539
+ return { error: known };
540
+ }
541
+ }
542
+ apply({ type: "SESSION_CONFIRMED", confirmedAt: /* @__PURE__ */ new Date() });
543
+ const confirmed = stateRef.current.session;
544
+ if (confirmed) emit("onSessionConfirmed", confirmed);
545
+ return { error: null };
546
+ } finally {
547
+ inFlightRef.current = "idle";
548
+ apply({ type: "SET_OPERATION_STATE", to: "idle" });
549
+ }
550
+ }, [apply, emit, onConfirmSession, report]);
551
+ const refuse = useCallback3(
552
+ (error) => {
553
+ report(error);
554
+ return { error };
555
+ },
556
+ [report]
557
+ );
558
+ const runTransition = useCallback3(
559
+ async (step) => {
560
+ if (!canResolveRef.current) return refuse(new PermissionError());
561
+ if (inFlightRef.current === step.pending) return { error: null };
562
+ const session = stateRef.current.session;
563
+ const refusal = step.guard(session);
564
+ if (refusal !== null || !session) {
565
+ return refuse(new SessionStateError(refusal ?? void 0));
566
+ }
567
+ inFlightRef.current = step.pending;
568
+ apply({ type: "SET_OPERATION_STATE", to: step.pending });
569
+ try {
570
+ const call = step.call;
571
+ if (call) {
572
+ const { error } = await tx.executeAsync(async () => call(session.id));
573
+ if (error) return refuse(toKnownError(error, step.failure));
574
+ }
575
+ step.commit();
576
+ return { error: null };
577
+ } finally {
578
+ inFlightRef.current = "idle";
579
+ apply({ type: "SET_OPERATION_STATE", to: "idle" });
580
+ }
581
+ },
582
+ [apply, refuse]
583
+ );
584
+ const startResolving = useCallback3(
585
+ () => runTransition({
586
+ pending: "startingResolution",
587
+ guard: (session) => session?.status === "confirmed" ? null : "session is not confirmed",
588
+ call: onStartResolving,
589
+ failure: StartResolvingError,
590
+ commit: () => {
591
+ apply({ type: "SESSION_RESOLVING_STARTED" });
592
+ const resolving = stateRef.current.session;
593
+ if (resolving) emit("onSessionResolving", resolving);
594
+ }
595
+ }),
596
+ [apply, emit, onStartResolving, runTransition]
597
+ );
598
+ const resolveSession = useCallback3(
599
+ () => runTransition({
600
+ pending: "completingResolution",
601
+ guard: resolveRefusal,
602
+ call: onResolveSession,
603
+ failure: SessionResolveError,
604
+ commit: () => {
605
+ apply({ type: "SESSION_RESOLVED", resolvedAt: /* @__PURE__ */ new Date() });
606
+ const resolved = stateRef.current.session;
607
+ if (resolved) emit("onSessionResolved", resolved);
608
+ }
609
+ }),
610
+ [apply, emit, onResolveSession, runTransition]
611
+ );
612
+ const commitComment = useCallback3(
613
+ (comment) => apply({ type: "COMMENT_COMMITTED", comment }),
614
+ [apply]
615
+ );
616
+ const commitCommentStatus = useCallback3(
617
+ (comment) => apply({ type: "COMMENT_STATUS_CHANGED", comment }),
618
+ [apply]
619
+ );
620
+ const removeComment = useCallback3(
621
+ (commentId) => apply({ type: "COMMENT_REMOVED", commentId }),
622
+ [apply]
623
+ );
624
+ const setLocalDraftCount = useCallback3(
625
+ (providerId, count) => {
626
+ if (count > 0) localDraftCountsRef.current.set(providerId, count);
627
+ else localDraftCountsRef.current.delete(providerId);
628
+ },
629
+ []
630
+ );
631
+ const bridge = useMemo(
632
+ () => ({
633
+ session: state.session,
634
+ canResolve: canResolveSession,
635
+ commitComment,
636
+ commitCommentStatus,
637
+ removeComment,
638
+ setLocalDraftCount
639
+ }),
640
+ [
641
+ state.session,
642
+ canResolveSession,
643
+ commitComment,
644
+ commitCommentStatus,
645
+ removeComment,
646
+ setLocalDraftCount
647
+ ]
648
+ );
649
+ const value = {
650
+ session: state.session,
651
+ sessionState: state.sessionState,
652
+ canResolve: canResolveSession,
653
+ createSession,
654
+ confirmSession,
655
+ startResolving,
656
+ resolveSession,
657
+ events
658
+ };
659
+ return /* @__PURE__ */ jsx(FeedbackSessionContext.Provider, { value, children: /* @__PURE__ */ jsx(SessionBridgeContext.Provider, { value: bridge, children }) });
660
+ };
661
+ var useFeedbackSession = () => {
662
+ const context = useContext(FeedbackSessionContext);
663
+ if (!context) {
664
+ throw new Error(
665
+ "useFeedbackSession must be used within a FeedbackSessionProvider"
666
+ );
667
+ }
668
+ return context;
669
+ };
670
+ var useSessionBridge = () => {
671
+ const bridge = useContext(SessionBridgeContext);
672
+ if (!bridge) {
673
+ throw new Error(
674
+ "CommentContextProvider must be used within a FeedbackSessionProvider"
675
+ );
676
+ }
677
+ return bridge;
678
+ };
679
+
680
+ // src/contexts/comment-context.tsx
681
+ import { jsx as jsx2 } from "react/jsx-runtime";
682
+ var CommentContext = createContext2(null);
93
683
  var commentReducer = (state, action) => {
94
684
  switch (action.type) {
95
685
  case "REGISTER": {
@@ -102,6 +692,8 @@ var commentReducer = (state, action) => {
102
692
  ...state.comments,
103
693
  {
104
694
  id: newId,
695
+ sessionId: action.sessionId,
696
+ locationId: action.locationId,
105
697
  projectId: state.projectId,
106
698
  user: state.currentUser,
107
699
  position: {
@@ -176,22 +768,14 @@ var commentReducer = (state, action) => {
176
768
  }
177
769
  }
178
770
  };
179
- case "RESET_DRAFT_COMMENTS": {
771
+ case "REPLACE_COMMENT":
180
772
  return {
181
773
  ...state,
182
- comments: state.comments.filter(
183
- (comment) => comment.status !== "draft"
184
- ),
185
- focussedComment: null
186
- };
187
- }
188
- case "RESET_RESOLVING_COMMENTS":
189
- return {
190
- ...state,
191
- comments: state.comments.filter(
192
- (comment) => comment.status !== "resolving"
193
- ),
194
- focussedComment: null
774
+ overlayState: "idle",
775
+ focussedComment: null,
776
+ comments: state.comments.map(
777
+ (comment) => comment.id === action.id ? action.comment : comment
778
+ )
195
779
  };
196
780
  default: {
197
781
  console.warn(`Unhandled action type, returning state`, action);
@@ -211,30 +795,53 @@ var CommentContextProvider = ({
211
795
  * @default "inactive"
212
796
  */
213
797
  initialOverlayState,
214
- onConfirm,
215
- onResolve,
216
- currentUser,
798
+ currentUser: currentUserProp,
217
799
  subscription,
218
800
  config = {
219
801
  commentVisibility: {
220
- hideResolved: false,
221
- hideResolving: false
802
+ hideResolved: false
222
803
  }
223
804
  },
224
805
  projectId,
225
- onError
806
+ locationId: locationIdProp,
807
+ onError,
808
+ onAdd,
809
+ onEdit,
810
+ onDelete,
811
+ onResolve,
812
+ onReopen
226
813
  }) => {
227
- const [state, dispatch] = useReducer(commentReducer, {
228
- comments: initialComments || [],
229
- overlayState: initialOverlayState || "inactive",
230
- currentUser,
231
- focussedComment: null,
232
- config,
233
- projectId
234
- });
814
+ const locationId = normalizeLocationId(locationIdProp);
815
+ const locationIdRef = useRef4(locationId);
816
+ locationIdRef.current = locationId;
817
+ const hostRef = useRef4({ onAdd, onEdit, onDelete, onResolve, onReopen });
818
+ hostRef.current = { onAdd, onEdit, onDelete, onResolve, onReopen };
819
+ const bridge = useSessionBridge();
820
+ const bridgeRef = useRef4(bridge);
821
+ bridgeRef.current = bridge;
822
+ const canResolveRef = useRef4(bridge.canResolve);
823
+ canResolveRef.current = bridge.canResolve;
824
+ const currentUser = currentUserProp ?? null;
825
+ const currentUserRef = useRef4(currentUser);
826
+ currentUserRef.current = currentUser;
827
+ const [state, dispatch] = useReducer2(
828
+ (current, action) => commentReducer(
829
+ { ...current, currentUser: currentUserRef.current },
830
+ action
831
+ ),
832
+ void 0,
833
+ () => ({
834
+ comments: initialComments ?? bridge.session?.comments ?? [],
835
+ overlayState: initialOverlayState || "inactive",
836
+ currentUser,
837
+ focussedComment: null,
838
+ config,
839
+ projectId
840
+ })
841
+ );
235
842
  const { events, emit } = useCommentEvents();
236
- const prevFocusRef = useRef2(null);
237
- useEffect(() => {
843
+ const prevFocusRef = useRef4(null);
844
+ useEffect2(() => {
238
845
  if (state.focussedComment !== prevFocusRef.current) {
239
846
  const comment = state.comments.find(
240
847
  (c) => c.id === state.focussedComment
@@ -243,7 +850,7 @@ var CommentContextProvider = ({
243
850
  prevFocusRef.current = state.focussedComment;
244
851
  }
245
852
  }, [state.focussedComment, state.comments, emit]);
246
- useEffect(() => {
853
+ useEffect2(() => {
247
854
  if (!subscription) return;
248
855
  subscription.subscribe((data) => {
249
856
  dispatch({ type: "APPEND_COMMENT", comment: data });
@@ -253,14 +860,26 @@ var CommentContextProvider = ({
253
860
  };
254
861
  }, [subscription]);
255
862
  const registerComment = (position, indicatorPosition) => {
863
+ const session = bridgeRef.current.session;
864
+ if (session?.status !== "draft") {
865
+ onError?.(new SessionStateError());
866
+ return;
867
+ }
868
+ const currentLocationId = locationIdRef.current;
869
+ if (currentLocationId === void 0) {
870
+ onError?.(new AddError("locationId is missing"));
871
+ return;
872
+ }
256
873
  dispatch({
257
874
  type: "REGISTER",
875
+ sessionId: session.id,
876
+ locationId: currentLocationId,
258
877
  position,
259
878
  indicatorPosition: indicatorPosition || null
260
879
  });
261
880
  };
262
- const prevCommentsLengthRef = useRef2(state.comments.length);
263
- useEffect(() => {
881
+ const prevCommentsLengthRef = useRef4(state.comments.length);
882
+ useEffect2(() => {
264
883
  if (state.comments.length > prevCommentsLengthRef.current) {
265
884
  const lastComment = state.comments[state.comments.length - 1];
266
885
  if (lastComment && lastComment.status === "draft") {
@@ -269,13 +888,154 @@ var CommentContextProvider = ({
269
888
  }
270
889
  prevCommentsLengthRef.current = state.comments.length;
271
890
  }, [state.comments, emit]);
272
- const deleteComment = (id) => {
891
+ const providerId = useId();
892
+ const localDraftCount = state.comments.filter(hasStatus("draft")).length;
893
+ useEffect2(() => {
894
+ bridgeRef.current.setLocalDraftCount(providerId, localDraftCount);
895
+ }, [providerId, localDraftCount]);
896
+ useEffect2(
897
+ () => () => bridgeRef.current.setLocalDraftCount(providerId, 0),
898
+ [providerId]
899
+ );
900
+ const fail = (error) => {
901
+ onError?.(error);
902
+ return { error };
903
+ };
904
+ const findComment = (id) => state.comments.find((comment) => comment.id === id);
905
+ const addComment = async (draft, content) => {
906
+ if (content.trim().length === 0) {
907
+ return fail(new AddError("content is empty"));
908
+ }
909
+ const persisted = {
910
+ ...draft,
911
+ content,
912
+ status: "published"
913
+ };
914
+ const { onAdd: onAdd2 } = hostRef.current;
915
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: "saving" });
916
+ if (onAdd2) {
917
+ const { error } = await tx.executeAsync(async () => onAdd2(persisted));
918
+ if (error) {
919
+ dispatch({ type: "CHANGE_OVERLAYSTATE", to: "editing" });
920
+ return fail(toKnownError(error, AddError));
921
+ }
922
+ }
923
+ dispatch({ type: "REPLACE_COMMENT", id: draft.id, comment: persisted });
924
+ bridgeRef.current.commitComment(persisted);
925
+ emit("onCommentAdded", persisted);
926
+ return { error: null };
927
+ };
928
+ const editComment = async (comment, content) => {
929
+ if (content === comment.content) return { error: null };
930
+ const updated = {
931
+ ...comment,
932
+ content,
933
+ status: "published"
934
+ };
935
+ const { onEdit: onEdit2 } = hostRef.current;
936
+ if (onEdit2) {
937
+ const { error } = await tx.executeAsync(async () => onEdit2(updated));
938
+ if (error) return fail(toKnownError(error, EditError));
939
+ }
940
+ dispatch({ type: "REPLACE_COMMENT", id: comment.id, comment: updated });
941
+ bridgeRef.current.commitComment(updated);
942
+ emit("onCommentEdited", updated);
943
+ return { error: null };
944
+ };
945
+ const updateComment = async (id, newComment) => {
946
+ const session = bridgeRef.current.session;
947
+ if (session?.status !== "draft") {
948
+ return fail(new SessionStateError());
949
+ }
950
+ const comment = findComment(id);
951
+ if (!comment) return fail(new EditError("comment not found"));
952
+ if (comment.status === "draft") {
953
+ if (comment.sessionId !== session.id) {
954
+ return fail(new SessionStateError());
955
+ }
956
+ return addComment(comment, newComment.content);
957
+ }
958
+ if (comment.status === "published") {
959
+ return editComment(comment, newComment.content);
960
+ }
961
+ return fail(new EditError("a resolved comment cannot be edited"));
962
+ };
963
+ const removePersisted = async (id) => {
964
+ const { onDelete: onDelete2 } = hostRef.current;
965
+ if (onDelete2) {
966
+ const { error } = await tx.executeAsync(async () => onDelete2(id));
967
+ if (error) return fail(toKnownError(error, DeleteError));
968
+ }
969
+ bridgeRef.current.removeComment(id);
273
970
  dispatch({ type: "DELETE", id });
274
971
  emit("onCommentDeleted", id);
972
+ return { error: null };
275
973
  };
276
- const updateComment = (id, newComment) => {
277
- dispatch({ type: "EDIT", id, newComment });
974
+ const deleteComment = async (id) => {
975
+ if (bridgeRef.current.session?.status !== "draft") {
976
+ return fail(new SessionStateError());
977
+ }
978
+ const comment = findComment(id);
979
+ if (!comment) return fail(new DeleteError("comment not found"));
980
+ if (comment.status === "draft") {
981
+ dispatch({ type: "DELETE", id });
982
+ emit("onCommentDeleted", id);
983
+ return { error: null };
984
+ }
985
+ return removePersisted(id);
278
986
  };
987
+ const statusInFlightRef = useRef4(/* @__PURE__ */ new Set());
988
+ const guardStatusChange = (id, from, failure) => {
989
+ if (!canResolveRef.current) return fail(new PermissionError());
990
+ if (bridgeRef.current.session?.status !== "resolving") {
991
+ return fail(new SessionStateError());
992
+ }
993
+ const comment = findComment(id);
994
+ if (!comment) return fail(new failure("comment not found"));
995
+ if (comment.status !== from) return { error: null };
996
+ if (statusInFlightRef.current.has(id)) return { error: null };
997
+ return { comment };
998
+ };
999
+ const changeStatus = async (id, step) => {
1000
+ const guard = guardStatusChange(id, step.from, step.failure);
1001
+ if (!("comment" in guard)) return guard;
1002
+ statusInFlightRef.current.add(id);
1003
+ try {
1004
+ const next = step.build(guard.comment);
1005
+ if (step.call) {
1006
+ const { call } = step;
1007
+ const { error } = await tx.executeAsync(async () => call(next));
1008
+ if (error) return fail(toKnownError(error, step.failure));
1009
+ }
1010
+ dispatch({ type: "REPLACE_COMMENT", id, comment: next });
1011
+ bridgeRef.current.commitCommentStatus(next);
1012
+ emit(step.event, next);
1013
+ return { error: null };
1014
+ } finally {
1015
+ statusInFlightRef.current.delete(id);
1016
+ }
1017
+ };
1018
+ const resolveComment = (id) => changeStatus(id, {
1019
+ from: "published",
1020
+ build: (comment) => ({
1021
+ ...comment,
1022
+ status: "resolved",
1023
+ resolvedAt: /* @__PURE__ */ new Date()
1024
+ }),
1025
+ call: hostRef.current.onResolve,
1026
+ failure: ResolveError,
1027
+ event: "onCommentResolved"
1028
+ });
1029
+ const reopenComment = (id) => changeStatus(id, {
1030
+ from: "resolved",
1031
+ build: (comment) => {
1032
+ const { resolvedAt: _resolvedAt, ...rest } = comment;
1033
+ return { ...rest, status: "published" };
1034
+ },
1035
+ call: hostRef.current.onReopen,
1036
+ failure: ReopenError,
1037
+ event: "onCommentReopened"
1038
+ });
279
1039
  const getActiveComment = () => {
280
1040
  return state.comments.find(
281
1041
  (comment) => comment.id === state.focussedComment
@@ -287,81 +1047,21 @@ var CommentContextProvider = ({
287
1047
  const toggleOverlay = () => {
288
1048
  dispatch({ type: "TOGGLE_OVERLAY" });
289
1049
  };
290
- const getDraftComments = () => {
291
- return state.comments.filter(hasStatus("draft"));
292
- };
293
- const getConfirmedComments = () => {
294
- return state.comments.filter(hasStatus("published"));
295
- };
296
- const getResolvingComments = () => {
297
- return state.comments.filter(hasStatus("resolving"));
298
- };
299
- const getResolvedComments = () => {
300
- return state.comments.filter(hasStatus("resolved"));
301
- };
302
- const toggleResolvingComment = (id) => {
303
- const comment = state.comments.find((comment2) => comment2.id === id);
304
- dispatch({
305
- type: "EDIT",
306
- id,
307
- newComment: {
308
- status: comment?.status === "resolving" ? "published" : "resolving"
309
- }
310
- });
311
- };
312
- const confirmComments = async () => {
313
- dispatch({ type: "CHANGE_OVERLAYSTATE", to: "saving" });
314
- const draftComments = getDraftComments();
315
- const { error } = await tx.executeAsync(
316
- async () => onConfirm(
317
- draftComments.map((c) => ({
318
- ...c,
319
- status: "published"
320
- })),
321
- projectId
322
- )
323
- );
324
- if (error) {
325
- if (onError) {
326
- onError(new ConfirmError(error.message));
327
- }
328
- dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
329
- return { error: new ConfirmError(error.message) };
330
- }
331
- dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
332
- dispatch({ type: "RESET_DRAFT_COMMENTS" });
333
- emit(
334
- "onCommentsConfirmed",
335
- draftComments.map((c) => ({ ...c, status: "published" }))
336
- );
337
- return { error: null };
338
- };
339
- const resolveComments = async () => {
340
- dispatch({ type: "CHANGE_OVERLAYSTATE", to: "resolving" });
341
- const resolvingComments = getResolvingComments();
342
- const { error } = await tx.executeAsync(
343
- async () => onResolve(
344
- resolvingComments.map((c) => ({
345
- ...c,
346
- status: "resolved"
347
- }))
348
- )
349
- );
350
- if (error) {
351
- if (onError) {
352
- onError(new ResolveError(error.message));
353
- }
354
- dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
355
- return { error: new ResolveError(error.message) };
356
- }
357
- dispatch({ type: "CHANGE_OVERLAYSTATE", to: "idle" });
358
- dispatch({ type: "RESET_RESOLVING_COMMENTS" });
359
- emit(
360
- "onCommentsResolved",
361
- resolvingComments.map((c) => ({ ...c, status: "resolved" }))
362
- );
363
- return { error: null };
364
- };
1050
+ const activeSession = bridge.session ? { id: bridge.session.id, status: bridge.session.status } : null;
1051
+ const visibleComments = selectVisibleComments({
1052
+ comments: state.comments,
1053
+ session: activeSession,
1054
+ locationId,
1055
+ draftVisibility: state.config?.draftVisibility,
1056
+ currentUserId: currentUser?.id
1057
+ });
1058
+ const commentsBySession = selectCommentsBySession({
1059
+ comments: state.comments,
1060
+ locationId,
1061
+ session: activeSession,
1062
+ draftVisibility: state.config?.draftVisibility,
1063
+ currentUserId: currentUser?.id
1064
+ });
365
1065
  const updateCommentVisibility = (visibility) => {
366
1066
  dispatch({ type: "UPDATE_VISIBILITY", visibility });
367
1067
  };
@@ -373,18 +1073,17 @@ var CommentContextProvider = ({
373
1073
  onError(error);
374
1074
  }
375
1075
  };
376
- return /* @__PURE__ */ jsx(
1076
+ return /* @__PURE__ */ jsx2(
377
1077
  CommentContext.Provider,
378
1078
  {
379
1079
  value: {
380
- draftComments: getDraftComments(),
381
- comments: getConfirmedComments(),
382
- resolvingComments: getResolvingComments(),
383
- resolvedComments: getResolvedComments(),
384
- allComments: state.comments,
1080
+ draftComments: visibleComments.filter(hasStatus("draft")),
1081
+ comments: visibleComments.filter(hasStatus("published")),
1082
+ resolvedComments: visibleComments.filter(hasStatus("resolved")),
1083
+ allComments: visibleComments,
1084
+ commentsBySession,
385
1085
  config: state.config,
386
1086
  changeOverlayState,
387
- toggleResolvingComment,
388
1087
  overlayState: state.overlayState,
389
1088
  deleteComment,
390
1089
  updateComment,
@@ -392,9 +1091,10 @@ var CommentContextProvider = ({
392
1091
  registerComment,
393
1092
  getActiveComment,
394
1093
  toggleOverlay,
395
- confirmComments,
396
1094
  currentUser,
397
- resolveComments,
1095
+ resolveComment,
1096
+ reopenComment,
1097
+ canResolve: bridge.canResolve,
398
1098
  updateCommentVisibility,
399
1099
  triggerError,
400
1100
  events
@@ -404,7 +1104,8 @@ var CommentContextProvider = ({
404
1104
  );
405
1105
  };
406
1106
  var useComments = () => {
407
- const context = useContext(CommentContext);
1107
+ useSessionBridge();
1108
+ const context = useContext2(CommentContext);
408
1109
  if (!context) {
409
1110
  throw new Error("useComments must be used within a CommentContextProvider");
410
1111
  }
@@ -425,7 +1126,7 @@ var normalizeRect = (a, b) => {
425
1126
  };
426
1127
 
427
1128
  // src/components/comment-overlay.tsx
428
- import { jsx as jsx2, jsxs } from "react/jsx-runtime";
1129
+ import { jsx as jsx3, jsxs } from "react/jsx-runtime";
429
1130
  var DRAG_THRESHOLD = 4;
430
1131
  var CommentOverlay = ({ children, ...rest }) => {
431
1132
  const {
@@ -437,9 +1138,9 @@ var CommentOverlay = ({ children, ...rest }) => {
437
1138
  triggerError
438
1139
  } = useComments();
439
1140
  const [preview, setPreview] = useState(null);
440
- const startRef = useRef3(null);
441
- const draggingRef = useRef3(false);
442
- const overlayRef = useRef3(null);
1141
+ const startRef = useRef5(null);
1142
+ const draggingRef = useRef5(false);
1143
+ const overlayRef = useRef5(null);
443
1144
  const onPointerDown = (e) => {
444
1145
  if (overlayState === "editing") {
445
1146
  triggerError(
@@ -501,7 +1202,7 @@ var CommentOverlay = ({ children, ...rest }) => {
501
1202
  };
502
1203
  if (overlayState === "inactive") return children;
503
1204
  return /* @__PURE__ */ jsxs("div", { "data-overlay": true, style: { position: "relative", ...rest.style }, ...rest, children: [
504
- /* @__PURE__ */ jsx2(
1205
+ /* @__PURE__ */ jsx3(
505
1206
  "div",
506
1207
  {
507
1208
  ref: overlayRef,
@@ -515,7 +1216,7 @@ var CommentOverlay = ({ children, ...rest }) => {
515
1216
  }
516
1217
  ),
517
1218
  getActiveComment() && // biome-ignore lint: This element should only handle clicks as a guard
518
- /* @__PURE__ */ jsx2(
1219
+ /* @__PURE__ */ jsx3(
519
1220
  "div",
520
1221
  {
521
1222
  "data-comment-guard": true,
@@ -541,7 +1242,7 @@ var CommentOverlay = ({ children, ...rest }) => {
541
1242
  }
542
1243
  }
543
1244
  ),
544
- preview && /* @__PURE__ */ jsx2(
1245
+ preview && /* @__PURE__ */ jsx3(
545
1246
  "div",
546
1247
  {
547
1248
  style: {
@@ -573,47 +1274,35 @@ var EDITING_STYLES = {
573
1274
  };
574
1275
 
575
1276
  // src/components/comment-renderer.tsx
576
- import { Fragment, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
1277
+ import { Fragment, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
577
1278
  var CommentRenderer = ({
578
1279
  Comment: Comment2,
579
1280
  DraftComment,
580
- ResolvingComment,
581
1281
  ResolvedComment
582
1282
  }) => {
583
- const {
584
- comments,
585
- draftComments,
586
- overlayState,
587
- resolvedComments,
588
- resolvingComments,
589
- config
590
- } = useComments();
1283
+ const { comments, draftComments, overlayState, resolvedComments, config } = useComments();
591
1284
  if (overlayState === "inactive") return null;
592
1285
  return /* @__PURE__ */ jsxs2(Fragment, { children: [
593
1286
  draftComments.map((comment) => {
594
- return /* @__PURE__ */ jsx3(DraftComment, { comment }, comment.id);
1287
+ return /* @__PURE__ */ jsx4(DraftComment, { comment }, comment.id);
595
1288
  }),
596
1289
  comments.map((comment) => {
597
- return /* @__PURE__ */ jsx3(Comment2, { comment }, comment.id);
598
- }),
599
- resolvingComments.map((comment) => {
600
- if (config?.commentVisibility?.hideResolving) return null;
601
- return /* @__PURE__ */ jsx3(ResolvingComment, { comment }, comment.id);
1290
+ return /* @__PURE__ */ jsx4(Comment2, { comment }, comment.id);
602
1291
  }),
603
1292
  resolvedComments.map((comment) => {
604
1293
  if (config?.commentVisibility?.hideResolved) return null;
605
- return /* @__PURE__ */ jsx3(ResolvedComment, { comment }, comment.id);
1294
+ return /* @__PURE__ */ jsx4(ResolvedComment, { comment }, comment.id);
606
1295
  })
607
1296
  ] });
608
1297
  };
609
1298
 
610
1299
  // src/components/comment-actions.tsx
611
1300
  import { Slot } from "@radix-ui/react-slot";
612
- import { jsx as jsx4 } from "react/jsx-runtime";
1301
+ import { jsx as jsx5 } from "react/jsx-runtime";
613
1302
  var ToggleOverlayButton = ({ asChild, ...rest }) => {
614
1303
  const { toggleOverlay, overlayState } = useComments();
615
1304
  const Comp = asChild ? Slot : "button";
616
- return /* @__PURE__ */ jsx4(
1305
+ return /* @__PURE__ */ jsx5(
617
1306
  Comp,
618
1307
  {
619
1308
  "data-active": overlayState !== "inactive",
@@ -625,34 +1314,6 @@ var ToggleOverlayButton = ({ asChild, ...rest }) => {
625
1314
  }
626
1315
  );
627
1316
  };
628
- var ConfirmComments = ({ asChild, ...rest }) => {
629
- const { confirmComments } = useComments();
630
- const Comp = asChild ? Slot : "button";
631
- return /* @__PURE__ */ jsx4(
632
- Comp,
633
- {
634
- ...rest,
635
- onClick: (e) => {
636
- confirmComments();
637
- rest.onClick?.(e);
638
- }
639
- }
640
- );
641
- };
642
- var ResolveComments = ({ asChild, ...rest }) => {
643
- const { resolveComments } = useComments();
644
- const Comp = asChild ? Slot : "button";
645
- return /* @__PURE__ */ jsx4(
646
- Comp,
647
- {
648
- ...rest,
649
- onClick: (e) => {
650
- resolveComments();
651
- rest.onClick?.(e);
652
- }
653
- }
654
- );
655
- };
656
1317
 
657
1318
  // src/components/comment.tsx
658
1319
  import {
@@ -674,7 +1335,7 @@ function useCommentScope() {
674
1335
  }
675
1336
 
676
1337
  // src/components/comment.tsx
677
- import { jsx as jsx5 } from "react/jsx-runtime";
1338
+ import { jsx as jsx6 } from "react/jsx-runtime";
678
1339
  var CommentRoot = ({
679
1340
  comment,
680
1341
  defaultDraft,
@@ -690,11 +1351,11 @@ var CommentRoot = ({
690
1351
  top: comment.position.y,
691
1352
  left: comment.position.x
692
1353
  };
693
- return /* @__PURE__ */ jsx5(
1354
+ return /* @__PURE__ */ jsx6(
694
1355
  CommentScopeContext.Provider,
695
1356
  {
696
1357
  value: { comment, isActive, draft, setDraft },
697
- children: /* @__PURE__ */ jsx5(
1358
+ children: /* @__PURE__ */ jsx6(
698
1359
  "div",
699
1360
  {
700
1361
  ...rest,
@@ -716,7 +1377,7 @@ var CommentIndicator = ({ ...rest }) => {
716
1377
  const { config } = useComments();
717
1378
  if (!comment.indicator) return null;
718
1379
  if (config?.indicatorVisibility === "active" && !isActive) return null;
719
- return /* @__PURE__ */ jsx5(
1380
+ return /* @__PURE__ */ jsx6(
720
1381
  "div",
721
1382
  {
722
1383
  ...rest,
@@ -738,12 +1399,12 @@ var CommentIndicator = ({ ...rest }) => {
738
1399
  var CommentContent = ({ asChild, ...rest }) => {
739
1400
  const { draft } = useCommentScope();
740
1401
  const Comp = asChild ? Slot2 : "p";
741
- return /* @__PURE__ */ jsx5(Comp, { ...rest, children: draft });
1402
+ return /* @__PURE__ */ jsx6(Comp, { ...rest, children: draft });
742
1403
  };
743
1404
  var CommentEdit = ({ asChild, ...rest }) => {
744
1405
  const { draft, setDraft } = useCommentScope();
745
1406
  const Comp = asChild ? Slot2 : "textarea";
746
- return /* @__PURE__ */ jsx5(
1407
+ return /* @__PURE__ */ jsx6(
747
1408
  Comp,
748
1409
  {
749
1410
  ...rest,
@@ -761,7 +1422,7 @@ var CommentConfirm = ({ asChild, ...rest }) => {
761
1422
  const { updateComment } = useComments();
762
1423
  const { comment, draft } = useCommentScope();
763
1424
  const Comp = asChild ? Slot2 : "button";
764
- return /* @__PURE__ */ jsx5(
1425
+ return /* @__PURE__ */ jsx6(
765
1426
  Comp,
766
1427
  {
767
1428
  ...rest,
@@ -777,7 +1438,7 @@ var CommentDelete = ({ asChild, ...rest }) => {
777
1438
  const { deleteComment } = useComments();
778
1439
  const { comment } = useCommentScope();
779
1440
  const Comp = asChild ? Slot2 : "button";
780
- return /* @__PURE__ */ jsx5(
1441
+ return /* @__PURE__ */ jsx6(
781
1442
  Comp,
782
1443
  {
783
1444
  ...rest,
@@ -789,27 +1450,54 @@ var CommentDelete = ({ asChild, ...rest }) => {
789
1450
  );
790
1451
  };
791
1452
  var CommentResolve = ({ asChild, ...rest }) => {
792
- const { toggleResolvingComment } = useComments();
1453
+ const { resolveComment } = useComments();
793
1454
  const { comment } = useCommentScope();
794
1455
  const Comp = asChild ? Slot2 : "button";
795
1456
  if (comment.status === "draft") {
796
1457
  console.error(
797
- `A draft comment can not be marked as resolving, remove this button from this comment: ${comment.id}`
1458
+ `A draft comment can not be resolved, remove this button from this comment: ${comment.id}`
798
1459
  );
799
1460
  return null;
800
1461
  }
801
1462
  if (comment.status === "resolved") {
802
1463
  console.error(
803
- `A resolved comment can not be marked as resolving, remove this button from this comment: ${comment.id}`
1464
+ `A resolved comment can not be resolved again, remove this button from this comment: ${comment.id}`
804
1465
  );
805
1466
  return null;
806
1467
  }
807
- return /* @__PURE__ */ jsx5(
1468
+ return /* @__PURE__ */ jsx6(
1469
+ Comp,
1470
+ {
1471
+ ...rest,
1472
+ onClick: (e) => {
1473
+ resolveComment(comment.id);
1474
+ rest.onClick?.(e);
1475
+ }
1476
+ }
1477
+ );
1478
+ };
1479
+ var CommentReopen = ({ asChild, ...rest }) => {
1480
+ const { reopenComment } = useComments();
1481
+ const { comment } = useCommentScope();
1482
+ const Comp = asChild ? Slot2 : "button";
1483
+ if (comment.status === "draft") {
1484
+ console.error(
1485
+ `A draft comment can not be reopened, remove this button from this comment: ${comment.id}`
1486
+ );
1487
+ return null;
1488
+ }
1489
+ if (comment.status === "published") {
1490
+ console.error(
1491
+ `A published comment can not be reopened, remove this button from this comment: ${comment.id}`
1492
+ );
1493
+ return null;
1494
+ }
1495
+ return /* @__PURE__ */ jsx6(
808
1496
  Comp,
809
1497
  {
810
1498
  ...rest,
811
1499
  onClick: (e) => {
812
- toggleResolvingComment(comment.id);
1500
+ reopenComment(comment.id);
813
1501
  rest.onClick?.(e);
814
1502
  }
815
1503
  }
@@ -865,13 +1553,81 @@ var Comment = {
865
1553
  // biome-ignore lint/style/useNamingConvention: Modular export
866
1554
  Delete: CommentDelete,
867
1555
  /**
868
- * Toggles the resolving status of a comment
1556
+ * Resolves a comment
869
1557
  *
870
- * @description Toggles the comment's resolving status, which will show a different UI state
1558
+ * @description Calls `resolveComment` for this comment. Renders nothing (and logs an error) for draft and resolved comments
871
1559
  * @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
872
1560
  */
873
1561
  // biome-ignore lint/style/useNamingConvention: Modular export
874
- Resolve: CommentResolve
1562
+ Resolve: CommentResolve,
1563
+ /**
1564
+ * Reopens a resolved comment
1565
+ *
1566
+ * @description Calls `reopenComment` for this comment. Renders nothing (and logs an error) for draft and published comments
1567
+ * @param asChild as whether to use the child as the base element instead of the default `<button>` tag. This allows for more flexibility
1568
+ */
1569
+ // biome-ignore lint/style/useNamingConvention: Modular export
1570
+ Reopen: CommentReopen
1571
+ };
1572
+
1573
+ // src/components/session-actions.tsx
1574
+ import { Slot as Slot3 } from "@radix-ui/react-slot";
1575
+ import { jsx as jsx7 } from "react/jsx-runtime";
1576
+ var CreateSession = ({ asChild, user, ...rest }) => {
1577
+ const { createSession } = useFeedbackSession();
1578
+ const Comp = asChild ? Slot3 : "button";
1579
+ return /* @__PURE__ */ jsx7(
1580
+ Comp,
1581
+ {
1582
+ ...rest,
1583
+ onClick: (e) => {
1584
+ void createSession({ user });
1585
+ rest.onClick?.(e);
1586
+ }
1587
+ }
1588
+ );
1589
+ };
1590
+ var ConfirmSession = ({ asChild, ...rest }) => {
1591
+ const { confirmSession } = useFeedbackSession();
1592
+ const Comp = asChild ? Slot3 : "button";
1593
+ return /* @__PURE__ */ jsx7(
1594
+ Comp,
1595
+ {
1596
+ ...rest,
1597
+ onClick: (e) => {
1598
+ void confirmSession();
1599
+ rest.onClick?.(e);
1600
+ }
1601
+ }
1602
+ );
1603
+ };
1604
+ var StartResolving = ({ asChild, ...rest }) => {
1605
+ const { startResolving } = useFeedbackSession();
1606
+ const Comp = asChild ? Slot3 : "button";
1607
+ return /* @__PURE__ */ jsx7(
1608
+ Comp,
1609
+ {
1610
+ ...rest,
1611
+ onClick: (e) => {
1612
+ void startResolving();
1613
+ rest.onClick?.(e);
1614
+ }
1615
+ }
1616
+ );
1617
+ };
1618
+ var ResolveSession = ({ asChild, ...rest }) => {
1619
+ const { resolveSession } = useFeedbackSession();
1620
+ const Comp = asChild ? Slot3 : "button";
1621
+ return /* @__PURE__ */ jsx7(
1622
+ Comp,
1623
+ {
1624
+ ...rest,
1625
+ onClick: (e) => {
1626
+ void resolveSession();
1627
+ rest.onClick?.(e);
1628
+ }
1629
+ }
1630
+ );
875
1631
  };
876
1632
  export {
877
1633
  Comment,
@@ -879,9 +1635,14 @@ export {
879
1635
  CommentOverlay,
880
1636
  CommentRenderer,
881
1637
  CommentScopeContext,
882
- ConfirmComments,
883
- ResolveComments,
1638
+ ConfirmSession,
1639
+ CreateSession,
1640
+ FeedbackSessionProvider,
1641
+ ResolveSession,
1642
+ StartResolving,
884
1643
  ToggleOverlayButton,
1644
+ commentReducer,
885
1645
  useCommentScope,
886
- useComments
1646
+ useComments,
1647
+ useFeedbackSession
887
1648
  };