@kendew-agency/react-feedback-layer 0.4.0-rc.0 → 0.4.0-rc.2

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/types.d.ts CHANGED
@@ -1,6 +1,411 @@
1
- import { C as CommentType, P as Position, g as Indicator, h as CommentOverlayState, i as CommentVisibility } from './use-session-events-DFksOgVA.js';
2
- export { A as ActiveFeedbackSession, b as CommentContext, p as CommentEventMap, r as CommentEvents, a as CommentOverlayProps, n as CommentState, m as CommentWithStatus, o as Config, l as ConfirmedComment, k as ConfirmedStatus, c as CreateSessionInput, D as DraftVisibility, F as FeedbackSession, I as InitialFeedbackSession, K as KnownError, O as OverlayErrorCode, R as RealtimeSubscription, t as SessionEventMap, e as SessionEvents, d as SessionOperationState, s as SessionStatus, S as SessionType, j as Status, q as Unsubscribe, U as User, u as UserContextType, f as UserProviderProps } from './use-session-events-DFksOgVA.js';
3
- import 'react';
1
+ import { ReactNode } from 'react';
2
+
3
+ /**
4
+ * Event map for comment-related events
5
+ * Each key is an event name and the value is the callback signature
6
+ */
7
+ type CommentEventMap = {
8
+ /**
9
+ * Triggered when focus changes to a different comment
10
+ * @param commentId - The id of the newly focused comment, or null if unfocused
11
+ * @param comment - The focused comment object, or undefined if unfocused
12
+ */
13
+ onFocusChange: (commentId: string | null, comment: CommentType | undefined) => void;
14
+ /**
15
+ * Triggered when a comment was persisted through onAdd
16
+ */
17
+ onCommentAdded: (comment: CommentType) => void;
18
+ /**
19
+ * Triggered when a comment was persisted through onEdit
20
+ */
21
+ onCommentEdited: (comment: CommentType) => void;
22
+ /**
23
+ * Triggered when a comment was resolved
24
+ */
25
+ onCommentResolved: (comment: CommentType) => void;
26
+ /**
27
+ * Triggered when a resolved comment was reopened
28
+ */
29
+ onCommentReopened: (comment: CommentType) => void;
30
+ /**
31
+ * Triggered when a comment is deleted
32
+ * @param commentId - The id of the deleted comment
33
+ */
34
+ onCommentDeleted: (commentId: string) => void;
35
+ /**
36
+ * Triggered when a new comment is registered (draft created)
37
+ * @param comment - The newly registered comment
38
+ */
39
+ onCommentRegistered: (comment: CommentType) => void;
40
+ };
41
+ /**
42
+ * A function to unsubscribe from an event
43
+ */
44
+ type Unsubscribe = () => void;
45
+ /**
46
+ * The events object exposed via useComments()
47
+ * Each method accepts a callback and returns an unsubscribe function
48
+ */
49
+ type CommentEvents = {
50
+ [K in keyof CommentEventMap]: (callback: CommentEventMap[K]) => Unsubscribe;
51
+ };
52
+
53
+ declare class BaseError extends Error {
54
+ code: OverlayErrorCode;
55
+ status: number;
56
+ constructor(message: string, code?: OverlayErrorCode, status?: number);
57
+ }
58
+ /** Error for resolving a single comment (`onResolve`). */
59
+ declare class ResolveError extends BaseError {
60
+ constructor(message?: string);
61
+ }
62
+ /** Session confirmation error (`onConfirm`). */
63
+ declare class ConfirmError extends BaseError {
64
+ constructor(message?: string);
65
+ }
66
+ declare class SessionCreateError extends BaseError {
67
+ constructor(message?: string);
68
+ }
69
+ declare class AddError extends BaseError {
70
+ constructor(message?: string);
71
+ }
72
+ declare class EditError extends BaseError {
73
+ constructor(message?: string);
74
+ }
75
+ declare class DeleteError extends BaseError {
76
+ constructor(message?: string);
77
+ }
78
+ declare class SessionStateError extends BaseError {
79
+ constructor(message?: string);
80
+ }
81
+ declare class ReopenError extends BaseError {
82
+ constructor(message?: string);
83
+ }
84
+ declare class StartResolvingError extends BaseError {
85
+ constructor(message?: string);
86
+ }
87
+ /** Error for resolving a session (`onResolveSession`). */
88
+ declare class SessionResolveError extends BaseError {
89
+ constructor(message?: string);
90
+ }
91
+ declare class PermissionError extends BaseError {
92
+ constructor(message?: string);
93
+ }
94
+
95
+ type OverlayErrorCode = "RESOLVE_ERROR" | "CONFIRM_ERROR" | "UNKNOWN_ERROR" | "INSERT_ERROR" | "SESSION_CREATE_ERROR" | "ADD_ERROR" | "EDIT_ERROR" | "DELETE_ERROR" | "SESSION_STATE_ERROR" | "REOPEN_ERROR" | "START_RESOLVING_ERROR" | "SESSION_RESOLVE_ERROR" | "PERMISSION_ERROR";
96
+ type KnownError = ConfirmError | ResolveError | SessionCreateError | AddError | EditError | DeleteError | SessionStateError | ReopenError | StartResolvingError | SessionResolveError | PermissionError;
97
+
98
+ type CommentState = {
99
+ /**
100
+ * All confirmed comments
101
+ * @description This is used to track all confirmed comments
102
+ */
103
+ comments: CommentType[];
104
+ /**
105
+ * The state of the comment overlay
106
+ * @description This value is used to track the state of the comment overlay
107
+ * @see CommentOverlayState
108
+ */
109
+ overlayState: CommentOverlayState;
110
+ /**
111
+ * The current loged in user.
112
+ * @description This data will be included when a user adds a comment
113
+ */
114
+ currentUser: User | null;
115
+ /**
116
+ * Track the focussed comment when editing or adding a comment
117
+ * The comment key will be stored in this state
118
+ */
119
+ focussedComment: string | null;
120
+ /**
121
+ * Any error that occurs during comment operations
122
+ * The value can be used to display an error message to the user
123
+ */
124
+ error?: Error;
125
+ /**
126
+ * Optional configuration for the comment layer
127
+ */
128
+ config?: Config | undefined;
129
+ /**
130
+ * Attached a project ID to the current overlay
131
+ * @description This ID will be passed to the comment actions. You can attach comments to a project by storing this ID in your database
132
+ */
133
+ projectId?: string | undefined;
134
+ };
135
+ /**
136
+ * The state of the comment overlay
137
+ * @description This is used to track the state of the comment overlay
138
+ */
139
+ type CommentOverlayState = "idle" | "editing" | "saving" | "resolving" | "inactive";
140
+ type CommentVisibility = {
141
+ /**
142
+ * Hide comments that are marked as resolved
143
+ * @default false
144
+ */
145
+ hideResolved?: boolean;
146
+ };
147
+ /**
148
+ * Whether persisted comments of other users are shown in a draft session
149
+ */
150
+ type DraftVisibility = "hidden" | "visible";
151
+ type CommentOverlayProps = {
152
+ /**
153
+ * Initial comments present when opening the comment overlay
154
+ * @description These comments usually come from your database
155
+ */
156
+ initialComments?: ConfirmedComment[];
157
+ /**
158
+ * The current user
159
+ *
160
+ * @description This is used to identify the user in your database. If you can not provide a user, comments will be added anonymously
161
+ * @default null
162
+ */
163
+ currentUser?: User | null;
164
+ /**
165
+ * The children of the comment overlay
166
+ * @description This is where the comments will be rendered
167
+ */
168
+ children: ReactNode;
169
+ /**
170
+ * The state of the overlay on initial render
171
+ * @default "inactive"
172
+ */
173
+ initialOverlayState?: CommentOverlayState;
174
+ /**
175
+ * Realtime subscription if supported by you DB
176
+ * @description You can use this feature with a realtime DB or a websocket
177
+ * @beta this featue is not yet fully tested and is subject to change
178
+ */
179
+ subscription?: RealtimeSubscription<CommentType>;
180
+ /**
181
+ * Callback to persist a newly added comment
182
+ */
183
+ onAdd?: (comment: ConfirmedComment) => Promise<void>;
184
+ /**
185
+ * Callback to persist an edited comment
186
+ */
187
+ onEdit?: (comment: ConfirmedComment) => Promise<void>;
188
+ /**
189
+ * Callback to persist a resolved comment
190
+ */
191
+ onResolve?: (comment: ConfirmedComment) => Promise<void>;
192
+ /**
193
+ * Callback to persist a reopened comment
194
+ */
195
+ onReopen?: (comment: ConfirmedComment) => Promise<void>;
196
+ /**
197
+ * Callback to delete a persisted comment
198
+ */
199
+ onDelete?: (commentId: string) => Promise<void>;
200
+ /**
201
+ * Callback to handle errors
202
+ *
203
+ * @param error as the error that occurred
204
+ */
205
+ onError?: (error: KnownError) => void;
206
+ /**
207
+ * Optional configuration for the comment layer
208
+ */
209
+ config?: Config;
210
+ /**
211
+ * Attached a project ID to the current overlay
212
+ * @description This ID will be passed to the comment actions. You can attach comments to a project by storing this ID in your database
213
+ * @example
214
+ * ```ts
215
+ * projectId: "proj_123456789"
216
+ * ```
217
+ */
218
+ projectId?: string;
219
+ /**
220
+ * The id of the location this provider renders, for example a page name.
221
+ * @description New comments are stored with this id. The overlay shows only
222
+ * the comments placed at this location. Comments of all locations are loaded
223
+ * so session-level checks can see them.
224
+ * When omitted or an empty string, the provider shows no comments and
225
+ * refuses to create comments.
226
+ */
227
+ locationId?: string | undefined;
228
+ };
229
+ interface RealtimeSubscription<T> {
230
+ subscribe(onUpdate: (data: T) => void): void;
231
+ unsubscribe(): void;
232
+ }
233
+ type Config = {
234
+ /**
235
+ * A custom prefix for the comment ids
236
+ * @example
237
+ * ```ts
238
+ * idPrefix: "proj_a_" // Results in id="proj_a_a096e551-9822-4d10-876c-70651be36c70"
239
+ * ```
240
+ */
241
+ idPrefix?: string;
242
+ /**
243
+ * The visibility of the comment indicator
244
+ * @default 'always'
245
+ */
246
+ indicatorVisibility?: "always" | "active";
247
+ /**
248
+ * Mode of the comment overlay
249
+ * @default "onConfirm"
250
+ */
251
+ mode?: "onConfirm";
252
+ /**
253
+ * The visibility of the different type of comments
254
+ * @description This is used to temporarily hide comments of a certain type if a user whishes a cleaner view
255
+ * @see CommentVisibility
256
+ */
257
+ commentVisibility?: CommentVisibility;
258
+ /**
259
+ * Whether persisted comments of other users are shown while the session is a draft
260
+ * @default "visible"
261
+ */
262
+ draftVisibility?: DraftVisibility;
263
+ };
264
+
265
+ type CommentType = {
266
+ /**
267
+ * The unique identifier of a comment
268
+ */
269
+ id: string;
270
+ /**
271
+ * The postion of a comment
272
+ * @description the postion holds both the x and y coordinates of a comment
273
+ */
274
+ position: Position;
275
+ /**
276
+ * The content of a comment
277
+ */
278
+ content: string;
279
+ /**
280
+ * The user who created the comment
281
+ */
282
+ user: User | null;
283
+ /**
284
+ * The date when the comment was created
285
+ */
286
+ createdAt: Date;
287
+ /**
288
+ * The date when the comment was resolved
289
+ */
290
+ resolvedAt?: Date;
291
+ /**
292
+ * The state of a comment tracking what the comment is currently doing
293
+ * @description This value will be omitted when sending the comment to your DB
294
+ */
295
+ status: Status;
296
+ /**
297
+ * An indicator to show the user where the comment is located
298
+ * @description shows a box around the content a comment is tied to
299
+ */
300
+ indicator?: Indicator | null;
301
+ /**
302
+ * The id of the feedback session a comment belongs to
303
+ */
304
+ sessionId: string;
305
+ /**
306
+ * The id of the location where the comment was placed
307
+ * @description A location is a logical place in your app, such as a page or
308
+ * a screen. Comments are shown only at the location they were placed at.
309
+ */
310
+ locationId: string;
311
+ /**
312
+ * The id of the project a comment is connected to
313
+ * @description useful for filtering comments by project
314
+ */
315
+ projectId?: string | undefined;
316
+ };
317
+ type Status = "draft" | "published" | "resolved";
318
+ type ConfirmedStatus = Extract<Status, "published" | "resolved">;
319
+ type ConfirmedComment = Omit<CommentType, "status"> & {
320
+ status: ConfirmedStatus;
321
+ };
322
+ type Indicator = {
323
+ /**
324
+ * The width of the indicator
325
+ */
326
+ width: number;
327
+ /**
328
+ * The height of the indicator
329
+ */
330
+ height: number;
331
+ };
332
+ type Position = {
333
+ /**
334
+ * The comment position on the x axis
335
+ */
336
+ x: number;
337
+ /**
338
+ * The comment position on the y axis
339
+ */
340
+ y: number;
341
+ };
342
+ type User = {
343
+ /**
344
+ * The user ID. This is used to identify the user in your database.
345
+ * @description This is optional, but it is recommended to establish a connection to the user in your DB.
346
+ */
347
+ id?: string;
348
+ /**
349
+ * The name of the user. This is used to display the user's name in the UI.
350
+ */
351
+ name: string;
352
+ /**
353
+ * The avatar of the user if supported. This will fall back to user initials.
354
+ */
355
+ avatar?: string;
356
+ };
357
+ type CommentContext = {
358
+ draftComments: CommentWithStatus<"draft">[];
359
+ comments: CommentWithStatus<"published">[];
360
+ resolvedComments: CommentWithStatus<"resolved">[];
361
+ allComments: CommentType[];
362
+ /**
363
+ * The comments at the current location, grouped by session id.
364
+ * @description Contains one key per session that has at least one comment at
365
+ * the current location, including sessions other than the active session.
366
+ * The list order follows the loaded comment state. The entry of an active
367
+ * draft session follows the `draftVisibility` config. The record is empty
368
+ * when the provider has no `locationId`. Comments without a `locationId`
369
+ * are excluded.
370
+ */
371
+ commentsBySession: Record<string, CommentType[]>;
372
+ config: Config | undefined;
373
+ changeOverlayState: (state: CommentOverlayState) => void;
374
+ overlayState: CommentOverlayState;
375
+ updateComment: (id: string, newComment: Pick<CommentType, "content">) => Promise<{
376
+ error: KnownError | null;
377
+ }>;
378
+ deleteComment: (id: string) => Promise<{
379
+ error: KnownError | null;
380
+ }>;
381
+ focusOnComment: (id: string | null) => void;
382
+ registerComment: (position: Position, indicatorPosition?: Indicator) => void;
383
+ getActiveComment: () => CommentType | undefined;
384
+ toggleOverlay: () => void;
385
+ currentUser: User | null;
386
+ /**
387
+ * Resolve a published comment. Requires a resolving session and permission
388
+ */
389
+ resolveComment: (id: string) => Promise<{
390
+ error: KnownError | null;
391
+ }>;
392
+ /**
393
+ * Reopen a resolved comment. Requires a resolving session and permission
394
+ */
395
+ reopenComment: (id: string) => Promise<{
396
+ error: KnownError | null;
397
+ }>;
398
+ /**
399
+ * Mirrors the `canResolveSession` prop of the session provider
400
+ */
401
+ canResolve: boolean;
402
+ updateCommentVisibility: (visibility: Partial<CommentVisibility>) => void;
403
+ triggerError: (error: KnownError) => void;
404
+ events: CommentEvents;
405
+ };
406
+ type CommentWithStatus<T extends Status> = Omit<CommentType, "status"> & {
407
+ status: T;
408
+ };
4
409
 
5
410
  /**
6
411
  * Actions that can be performed on a comment
@@ -21,6 +426,7 @@ type CommentAction = {
21
426
  } | {
22
427
  type: "REGISTER";
23
428
  sessionId: string;
429
+ locationId: string;
24
430
  position: Position;
25
431
  indicatorPosition?: Indicator | null;
26
432
  } | {
@@ -40,4 +446,71 @@ type CommentAction = {
40
446
  visibility: Partial<CommentVisibility>;
41
447
  };
42
448
 
43
- export { type CommentAction, CommentOverlayState, CommentType, CommentVisibility, Indicator, Position };
449
+ /**
450
+ * Discriminator for the kind of session.
451
+ * Adding "rework" later is a change to this union only.
452
+ */
453
+ type SessionType = "feedback";
454
+ type SessionStatus = "draft" | "confirmed" | "resolving" | "resolved";
455
+ /**
456
+ * The pending operation of the session provider
457
+ */
458
+ type SessionOperationState = "idle" | "creating" | "confirming" | "startingResolution" | "completingResolution";
459
+ type FeedbackSession = {
460
+ id: string;
461
+ type: SessionType;
462
+ status: SessionStatus;
463
+ createdAt: Date;
464
+ /** The id of the user who created the session, never a user object */
465
+ createdBy: string;
466
+ /** Ids of the users that took part in the session. May be empty */
467
+ participantIds: string[];
468
+ confirmedAt?: Date;
469
+ resolvedAt?: Date;
470
+ projectId?: string | undefined;
471
+ /** Reserved for a future rework session. No behavior depends on it. */
472
+ sourceSessionId?: string | undefined;
473
+ };
474
+ /**
475
+ * The session as held by the provider, with its persisted comments
476
+ */
477
+ type ActiveFeedbackSession = FeedbackSession & {
478
+ comments: ConfirmedComment[];
479
+ };
480
+ /**
481
+ * Accepted by `initialSession`. `comments` is optional for convenience
482
+ */
483
+ type InitialFeedbackSession = FeedbackSession & {
484
+ comments?: ConfirmedComment[];
485
+ };
486
+ /**
487
+ * Input passed to `onCreateSession`
488
+ */
489
+ type CreateSessionInput = {
490
+ type: SessionType;
491
+ user: User;
492
+ projectId?: string | undefined;
493
+ };
494
+ /**
495
+ * Event map for session-related events
496
+ */
497
+ type SessionEventMap = {
498
+ /** Triggered when a new draft session was created */
499
+ onSessionCreated: (session: ActiveFeedbackSession) => void;
500
+ /** Triggered when the session was confirmed */
501
+ onSessionConfirmed: (session: ActiveFeedbackSession) => void;
502
+ /** Triggered when the session moved to resolving */
503
+ onSessionResolving: (session: ActiveFeedbackSession) => void;
504
+ /** Triggered when the session was resolved */
505
+ onSessionResolved: (session: ActiveFeedbackSession) => void;
506
+ };
507
+
508
+ /**
509
+ * The session events object exposed via useSession()
510
+ * Each method accepts a callback and returns an unsubscribe function
511
+ */
512
+ type SessionEvents = {
513
+ [K in keyof SessionEventMap]: (callback: SessionEventMap[K]) => Unsubscribe;
514
+ };
515
+
516
+ export type { ActiveFeedbackSession, CommentAction, CommentContext, CommentEventMap, CommentEvents, CommentOverlayProps, CommentOverlayState, CommentState, CommentType, CommentVisibility, CommentWithStatus, Config, ConfirmedComment, ConfirmedStatus, CreateSessionInput, DraftVisibility, FeedbackSession, Indicator, InitialFeedbackSession, KnownError, OverlayErrorCode, Position, RealtimeSubscription, SessionEventMap, SessionEvents, SessionOperationState, SessionStatus, SessionType, Status, Unsubscribe, User };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kendew-agency/react-feedback-layer",
3
- "version": "0.4.0-rc.0",
3
+ "version": "0.4.0-rc.2",
4
4
  "description": "Drop-in React feedback layer for collecting contextual user feedback",
5
5
  "license": "MIT",
6
6
  "author": "Kendew Agency",
@@ -58,6 +58,7 @@
58
58
  "@types/react": "^18.2.0",
59
59
  "@types/react-dom": "^18.2.0",
60
60
  "@vitejs/plugin-react": "^4.2.0",
61
+ "fake-indexeddb": "^6.2.8",
61
62
  "fast-check": "^4.10.2",
62
63
  "jsdom": "^31.0.1",
63
64
  "react": "^18.2.0",