@matija2209/payload-plugin-admin-feedback 1.0.0 → 1.0.3

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,643 @@
1
+ import { definePlugin, headersWithCors } from 'payload';
2
+
3
+ // src/index.ts
4
+ var trim = (value) => {
5
+ if (typeof value !== "string") {
6
+ return null;
7
+ }
8
+ const nextValue = value.trim();
9
+ return nextValue.length > 0 ? nextValue : null;
10
+ };
11
+ var corsHeaders = (req, init) => headersWithCors({
12
+ headers: new Headers(init),
13
+ req
14
+ });
15
+ var getCreatorId = (createdBy) => {
16
+ if (!createdBy) {
17
+ return null;
18
+ }
19
+ if (typeof createdBy === "object" && createdBy !== null && "id" in createdBy) {
20
+ const value = createdBy.id;
21
+ return value !== void 0 && value !== null ? String(value) : null;
22
+ }
23
+ if (typeof createdBy === "number" || typeof createdBy === "string") {
24
+ return String(createdBy);
25
+ }
26
+ return null;
27
+ };
28
+ var getRelationId = (value) => {
29
+ if (!value) {
30
+ return null;
31
+ }
32
+ if (typeof value === "object" && value !== null && "id" in value) {
33
+ const relationId = value.id;
34
+ return relationId !== void 0 && relationId !== null ? String(relationId) : null;
35
+ }
36
+ if (typeof value === "number" || typeof value === "string") {
37
+ return String(value);
38
+ }
39
+ return null;
40
+ };
41
+
42
+ // src/collections/admin-feedback/endpoints/deleteFeedback.ts
43
+ var createDeleteFeedbackEndpoint = (mediaCollectionSlug) => ({
44
+ path: "/delete/:id",
45
+ method: "delete",
46
+ handler: async (req) => {
47
+ try {
48
+ if (!req.user) {
49
+ return Response.json(
50
+ { success: false, error: "Unauthorized" },
51
+ { status: 401, headers: corsHeaders(req) }
52
+ );
53
+ }
54
+ const id = req.routeParams?.id;
55
+ if (!id) {
56
+ return Response.json(
57
+ { success: false, error: "Missing feedback ID." },
58
+ { status: 400, headers: corsHeaders(req) }
59
+ );
60
+ }
61
+ const existing = await req.payload.findByID({
62
+ collection: "admin-feedback",
63
+ id
64
+ });
65
+ if (String(req.user.id) !== getCreatorId(existing.createdBy)) {
66
+ return Response.json(
67
+ { success: false, error: "You can only delete your own feedback." },
68
+ { status: 403, headers: corsHeaders(req) }
69
+ );
70
+ }
71
+ const screenshotId = getRelationId(existing.screenshot);
72
+ if (screenshotId) {
73
+ try {
74
+ await req.payload.delete({
75
+ collection: mediaCollectionSlug,
76
+ id: screenshotId
77
+ });
78
+ } catch (error) {
79
+ req.payload.logger.warn(
80
+ `Failed to delete feedback screenshot ${screenshotId}: ${error instanceof Error ? error.message : "unknown error"}`
81
+ );
82
+ }
83
+ }
84
+ await req.payload.delete({
85
+ collection: "admin-feedback",
86
+ id
87
+ });
88
+ return Response.json({ success: true }, { headers: corsHeaders(req) });
89
+ } catch (error) {
90
+ return Response.json(
91
+ { success: false, error: error instanceof Error ? error.message : "Unknown error" },
92
+ { status: 500, headers: corsHeaders(req) }
93
+ );
94
+ }
95
+ }
96
+ });
97
+
98
+ // src/collections/admin-feedback/endpoints/deleteUpload.ts
99
+ var createDeleteUploadEndpoint = (mediaCollectionSlug) => ({
100
+ path: "/upload/:id",
101
+ method: "delete",
102
+ handler: async (req) => {
103
+ try {
104
+ if (!req.user) {
105
+ return Response.json(
106
+ { success: false, error: "Unauthorized" },
107
+ { status: 401, headers: corsHeaders(req) }
108
+ );
109
+ }
110
+ const id = req.routeParams?.id;
111
+ if (!id) {
112
+ return Response.json(
113
+ { success: false, error: "Missing media ID." },
114
+ { status: 400, headers: corsHeaders(req) }
115
+ );
116
+ }
117
+ await req.payload.delete({
118
+ collection: mediaCollectionSlug,
119
+ id
120
+ });
121
+ return Response.json({ success: true }, { headers: corsHeaders(req) });
122
+ } catch (error) {
123
+ return Response.json(
124
+ { success: false, error: error instanceof Error ? error.message : "Unknown error" },
125
+ { status: 500, headers: corsHeaders(req) }
126
+ );
127
+ }
128
+ }
129
+ });
130
+
131
+ // src/collections/admin-feedback/endpoints/submit.ts
132
+ var createSubmitEndpoint = () => ({
133
+ path: "/submit",
134
+ method: "post",
135
+ handler: async (req) => {
136
+ try {
137
+ const body = await req.json();
138
+ const message = trim(body?.message);
139
+ if (!message) {
140
+ return Response.json(
141
+ { success: false, error: "Message is required." },
142
+ { status: 400, headers: corsHeaders(req) }
143
+ );
144
+ }
145
+ const doc = await req.payload.create({
146
+ collection: "admin-feedback",
147
+ data: {
148
+ message,
149
+ pagePath: trim(body?.pagePath) || "/",
150
+ selector: trim(body?.selector) || void 0,
151
+ selectedText: trim(body?.selectedText) || void 0,
152
+ screenshot: body?.screenshotId || void 0,
153
+ createdBy: req.user?.id,
154
+ meta: {
155
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
156
+ userAgent: trim(body?.meta?.userAgent),
157
+ locale: trim(body?.meta?.locale),
158
+ viewport: trim(body?.meta?.viewport)
159
+ }
160
+ }
161
+ });
162
+ return Response.json({ success: true, id: doc.id }, { headers: corsHeaders(req) });
163
+ } catch (error) {
164
+ return Response.json(
165
+ { success: false, error: error instanceof Error ? error.message : "Unknown error" },
166
+ { status: 500, headers: corsHeaders(req) }
167
+ );
168
+ }
169
+ }
170
+ });
171
+
172
+ // src/collections/admin-feedback/endpoints/updateFeedback.ts
173
+ var createUpdateFeedbackEndpoint = () => ({
174
+ path: "/update/:id",
175
+ method: "patch",
176
+ handler: async (req) => {
177
+ try {
178
+ if (!req.user) {
179
+ return Response.json(
180
+ { success: false, error: "Unauthorized" },
181
+ { status: 401, headers: corsHeaders(req) }
182
+ );
183
+ }
184
+ const id = req.routeParams?.id;
185
+ if (!id) {
186
+ return Response.json(
187
+ { success: false, error: "Missing feedback ID." },
188
+ { status: 400, headers: corsHeaders(req) }
189
+ );
190
+ }
191
+ const existing = await req.payload.findByID({
192
+ collection: "admin-feedback",
193
+ id
194
+ });
195
+ if (String(req.user.id) !== getCreatorId(existing.createdBy)) {
196
+ return Response.json(
197
+ { success: false, error: "You can only edit your own feedback." },
198
+ { status: 403, headers: corsHeaders(req) }
199
+ );
200
+ }
201
+ const body = await req.json();
202
+ const updateData = {};
203
+ const newMessage = trim(body?.message);
204
+ if (newMessage) {
205
+ updateData.message = newMessage;
206
+ }
207
+ if (body && "screenshotId" in body) {
208
+ updateData.screenshot = body.screenshotId || null;
209
+ }
210
+ const updated = await req.payload.update({
211
+ collection: "admin-feedback",
212
+ id: existing.id,
213
+ data: updateData
214
+ });
215
+ return Response.json({ success: true, doc: updated }, { headers: corsHeaders(req) });
216
+ } catch (error) {
217
+ return Response.json(
218
+ { success: false, error: error instanceof Error ? error.message : "Unknown error" },
219
+ { status: 500, headers: corsHeaders(req) }
220
+ );
221
+ }
222
+ }
223
+ });
224
+
225
+ // src/collections/admin-feedback/endpoints/upload.ts
226
+ var createUploadEndpoint = (mediaCollectionSlug) => ({
227
+ path: "/upload",
228
+ method: "post",
229
+ handler: async (req) => {
230
+ try {
231
+ if (!req.user) {
232
+ return Response.json(
233
+ { success: false, error: "Unauthorized" },
234
+ { status: 401, headers: corsHeaders(req) }
235
+ );
236
+ }
237
+ const formData = await req.formData();
238
+ const file = formData.get("file");
239
+ const altRaw = formData.get("alt");
240
+ const alt = typeof altRaw === "string" ? altRaw.trim() : "";
241
+ if (!(file instanceof File)) {
242
+ return Response.json(
243
+ { success: false, error: "Missing file." },
244
+ { status: 400, headers: corsHeaders(req) }
245
+ );
246
+ }
247
+ if (file.size === 0) {
248
+ return Response.json(
249
+ { success: false, error: "File is empty." },
250
+ { status: 400, headers: corsHeaders(req) }
251
+ );
252
+ }
253
+ const media = await req.payload.create({
254
+ collection: mediaCollectionSlug,
255
+ data: {
256
+ alt: alt || file.name
257
+ },
258
+ file: {
259
+ data: Buffer.from(await file.arrayBuffer()),
260
+ mimetype: file.type || "application/octet-stream",
261
+ name: file.name,
262
+ size: file.size
263
+ }
264
+ });
265
+ return Response.json(
266
+ { success: true, mediaId: Number(media.id) },
267
+ { headers: corsHeaders(req) }
268
+ );
269
+ } catch (error) {
270
+ return Response.json(
271
+ { success: false, error: error instanceof Error ? error.message : "Unknown error" },
272
+ { status: 500, headers: corsHeaders(req) }
273
+ );
274
+ }
275
+ }
276
+ });
277
+
278
+ // src/collections/admin-feedback/endpoints/index.ts
279
+ var createAdminFeedbackEndpoints = (mediaCollectionSlug) => [
280
+ createSubmitEndpoint(),
281
+ createUploadEndpoint(mediaCollectionSlug),
282
+ createDeleteUploadEndpoint(mediaCollectionSlug),
283
+ createUpdateFeedbackEndpoint(),
284
+ createDeleteFeedbackEndpoint(mediaCollectionSlug)
285
+ ];
286
+
287
+ // src/server/sendFeedbackEmail.ts
288
+ async function sendFeedbackEmail(payload, options, data) {
289
+ if (!options.emailTo) {
290
+ return;
291
+ }
292
+ const to = Array.isArray(options.emailTo) ? options.emailTo : [options.emailTo];
293
+ const sourceLabel = options.fromLabel || "Admin Feedback";
294
+ const createdBy = data.createdByName || data.createdByEmail || "Unknown";
295
+ let formattedDate = "Unknown";
296
+ try {
297
+ formattedDate = data.createdAt ? new Date(data.createdAt).toLocaleString("sl-SI", {
298
+ day: "numeric",
299
+ month: "long",
300
+ year: "numeric",
301
+ hour: "2-digit",
302
+ minute: "2-digit"
303
+ }) : (/* @__PURE__ */ new Date()).toLocaleString();
304
+ } catch (e) {
305
+ formattedDate = data.createdAt || "Unknown";
306
+ }
307
+ const serverUrl = process.env.NEXT_PUBLIC_SERVER_URL || "http://localhost:34572";
308
+ const adminUrl = data.id ? `${serverUrl}/admin/collections/admin-feedback/${data.id}` : null;
309
+ const from = options.fromAddress ? `${options.fromName || options.fromLabel || "Admin Feedback"} <${options.fromAddress}>` : void 0;
310
+ const screenshotHtml = data.screenshotUrl && data.screenshotUrl !== "-" ? `
311
+ <div style="margin-top: 24px;">
312
+ <p style="margin: 0 0 8px 0; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: #8b8b8b; font-weight: 600;">Screenshot Preview</p>
313
+ <div style="border-radius: 12px; overflow: hidden; border: 1px solid #d6e3e7; background: #00455a;">
314
+ <img src="${data.screenshotUrl}" alt="Feedback Screenshot" style="width: 100%; height: auto; display: block;" />
315
+ </div>
316
+ <p style="margin: 8px 0 0 0; font-size: 12px; color: #8b8b8b;">
317
+ Full URL: <a href="${data.screenshotUrl}" style="color: #00455a; text-decoration: none;">${data.screenshotUrl}</a>
318
+ </p>
319
+ </div>
320
+ ` : "";
321
+ const html = `
322
+ <!DOCTYPE html>
323
+ <html>
324
+ <head>
325
+ <meta charset="utf-8">
326
+ <style>
327
+ body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; color: #353535; background-color: #f7f7f7; margin: 0; padding: 40px 20px; }
328
+ .container { max-width: 600px; margin: 0 auto; background: #ffffff; border-radius: 20px; overflow: hidden; border: 1px solid #d6e3e7; box-shadow: 0 12px 36px rgba(0, 69, 90, 0.08); }
329
+ .header { background: #00455a; color: #fbfbfb; padding: 32px 24px; text-align: center; }
330
+ .header h1 { margin: 0; font-size: 24px; font-weight: 700; }
331
+ .content { padding: 36px 30px; }
332
+ .section { margin-bottom: 24px; }
333
+ .label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: #8b8b8b; font-weight: 700; margin-bottom: 4px; }
334
+ .value { font-size: 16px; color: #353535; margin: 0; }
335
+ .message-box { background: #f1f7f8; border-left: 4px solid #00455a; padding: 16px 20px; border-radius: 8px; margin-top: 8px; }
336
+ .footer { background: #fbfbfb; padding: 24px; text-align: center; border-top: 1px solid #e3edef; color: #8b8b8b; font-size: 13px; }
337
+ .button { display: inline-block; background: #00455a; color: #fbfbfb !important; padding: 12px 24px; border-radius: 999px; text-decoration: none; font-weight: 600; margin-top: 20px; }
338
+ </style>
339
+ </head>
340
+ <body>
341
+ <div class="container">
342
+ <div class="header">
343
+ <h1>New Admin Feedback</h1>
344
+ <p style="margin: 8px 0 0 0; opacity: 0.8; font-size: 14px;">[${sourceLabel}] Received via Feedback Widget</p>
345
+ </div>
346
+ <div class="content">
347
+ <div class="section">
348
+ <p class="label">Message</p>
349
+ <div class="message-box">
350
+ <p class="value" style="white-space: pre-wrap;">${data.message}</p>
351
+ </div>
352
+ </div>
353
+
354
+ <div style="display: flex; flex-wrap: wrap; gap: 20px; border-top: 1px solid #e3edef; padding-top: 24px;">
355
+ <div style="flex: 1; min-width: 200px; margin-bottom: 16px;">
356
+ <p class="label">Page Path</p>
357
+ <p class="value" style="word-break: break-all;">${data.pagePath}</p>
358
+ </div>
359
+ <div style="flex: 1; min-width: 200px; margin-bottom: 16px;">
360
+ <p class="label">Created By</p>
361
+ <p class="value">${createdBy}</p>
362
+ </div>
363
+ </div>
364
+
365
+ <div style="display: flex; flex-wrap: wrap; gap: 20px;">
366
+ <div style="flex: 1; min-width: 200px; margin-bottom: 16px;">
367
+ <p class="label">Selector</p>
368
+ <p class="value" style="font-family: monospace; font-size: 13px;">${data.selector || "-"}</p>
369
+ </div>
370
+ <div style="flex: 1; min-width: 200px; margin-bottom: 16px;">
371
+ <p class="label">Created At</p>
372
+ <p class="value">${formattedDate}</p>
373
+ </div>
374
+ </div>
375
+
376
+ ${data.selectedText && data.selectedText !== "-" ? `
377
+ <div class="section">
378
+ <p class="label">Selected Text</p>
379
+ <p class="value" style="font-style: italic; color: #555;">"${data.selectedText}"</p>
380
+ </div>
381
+ ` : ""}
382
+
383
+ ${screenshotHtml}
384
+
385
+ ${adminUrl ? `
386
+ <div style="text-align: center; margin-top: 32px;">
387
+ <a href="${adminUrl}" class="button">View in Admin Panel</a>
388
+ </div>
389
+ ` : ""}
390
+ </div>
391
+ <div class="footer">
392
+ <p>This automated notification was sent from your Store Admin.</p>
393
+ </div>
394
+ </div>
395
+ </body>
396
+ </html>
397
+ `;
398
+ const emailMessage = {
399
+ to,
400
+ subject: `[${sourceLabel}] New feedback from ${createdBy}`,
401
+ html
402
+ };
403
+ if (from) {
404
+ emailMessage.from = from;
405
+ }
406
+ await payload.sendEmail(emailMessage);
407
+ }
408
+
409
+ // src/collections/admin-feedback/hooks/afterChange.ts
410
+ var createAfterChangeHook = (options) => {
411
+ return async ({ doc, operation, req }) => {
412
+ if (operation !== "create") {
413
+ return doc;
414
+ }
415
+ const screenshotDoc = typeof doc.screenshot === "object" && doc.screenshot !== null ? doc.screenshot : null;
416
+ const createdByDoc = typeof doc.createdBy === "object" && doc.createdBy !== null ? doc.createdBy : null;
417
+ const screenshotUrl = typeof screenshotDoc?.url === "string" ? screenshotDoc.url : typeof screenshotDoc?.filename === "string" ? screenshotDoc.filename : null;
418
+ try {
419
+ await sendFeedbackEmail(req.payload, options, {
420
+ id: doc.id,
421
+ message: String(doc.message || ""),
422
+ pagePath: String(doc.pagePath || "/"),
423
+ selector: typeof doc.selector === "string" ? doc.selector : null,
424
+ selectedText: typeof doc.selectedText === "string" ? doc.selectedText : null,
425
+ screenshotUrl,
426
+ createdByEmail: createdByDoc && typeof createdByDoc.email === "string" ? createdByDoc.email : null,
427
+ createdByName: createdByDoc && typeof createdByDoc.first_name === "string" ? `${createdByDoc.first_name} ${String(createdByDoc.last_name || "")}`.trim() : null,
428
+ createdAt: typeof doc.createdAt === "string" ? doc.createdAt : null
429
+ });
430
+ } catch (error) {
431
+ req.payload.logger.error(
432
+ `Failed to send admin feedback email: ${error instanceof Error ? error.message : "unknown error"}`
433
+ );
434
+ }
435
+ return doc;
436
+ };
437
+ };
438
+
439
+ // src/collections/admin-feedback/hooks/beforeChange.ts
440
+ var normalizeFeedbackBeforeChange = ({ data, req, operation }) => {
441
+ if (!data) {
442
+ return data;
443
+ }
444
+ const nextData = { ...data };
445
+ const message = trim(nextData.message);
446
+ const pagePath = trim(nextData.pagePath);
447
+ nextData.message = message || "";
448
+ nextData.pagePath = pagePath || "/";
449
+ nextData.selector = trim(nextData.selector);
450
+ nextData.selectedText = trim(nextData.selectedText);
451
+ const existingMeta = typeof nextData.meta === "object" && nextData.meta !== null ? nextData.meta : {};
452
+ nextData.meta = {
453
+ ...existingMeta,
454
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
455
+ userAgent: trim(existingMeta.userAgent),
456
+ locale: trim(existingMeta.locale),
457
+ viewport: trim(existingMeta.viewport)
458
+ };
459
+ if (operation === "create" && req.user && !nextData.createdBy) {
460
+ nextData.createdBy = req.user.id;
461
+ }
462
+ return nextData;
463
+ };
464
+
465
+ // src/collections/admin-feedback/hooks/index.ts
466
+ var createAdminFeedbackHooks = (options) => ({
467
+ beforeChange: [normalizeFeedbackBeforeChange],
468
+ afterChange: [createAfterChangeHook(options)]
469
+ });
470
+
471
+ // src/collections/admin-feedback/index.ts
472
+ var createAdminFeedbackCollection = (options, mediaCollectionSlug) => {
473
+ return {
474
+ slug: "admin-feedback",
475
+ admin: {
476
+ group: {
477
+ sl: "System",
478
+ en: "System",
479
+ ru: "System"
480
+ },
481
+ useAsTitle: "message",
482
+ defaultColumns: ["createdAt", "pagePath", "status"]
483
+ },
484
+ access: {
485
+ read: ({ req }) => Boolean(req.user),
486
+ create: () => true,
487
+ update: ({ req }) => Boolean(req.user),
488
+ delete: ({ req }) => Boolean(req.user)
489
+ },
490
+ endpoints: createAdminFeedbackEndpoints(mediaCollectionSlug),
491
+ hooks: createAdminFeedbackHooks(options),
492
+ fields: [
493
+ {
494
+ name: "message",
495
+ type: "textarea",
496
+ required: true,
497
+ maxLength: options.maxMessageLength || 3e3
498
+ },
499
+ {
500
+ name: "pagePath",
501
+ type: "text",
502
+ required: true
503
+ },
504
+ {
505
+ name: "selector",
506
+ type: "text"
507
+ },
508
+ {
509
+ name: "selectedText",
510
+ type: "text"
511
+ },
512
+ {
513
+ name: "screenshot",
514
+ type: "upload",
515
+ relationTo: mediaCollectionSlug,
516
+ required: false,
517
+ admin: {
518
+ description: "Uploaded screenshot preview"
519
+ }
520
+ },
521
+ {
522
+ name: "createdBy",
523
+ type: "relationship",
524
+ relationTo: "users",
525
+ admin: {
526
+ position: "sidebar",
527
+ readOnly: true
528
+ }
529
+ },
530
+ {
531
+ name: "status",
532
+ type: "select",
533
+ defaultValue: "new",
534
+ options: [
535
+ { label: "New", value: "new" },
536
+ { label: "Triaged", value: "triaged" },
537
+ { label: "Resolved", value: "resolved" }
538
+ ],
539
+ admin: {
540
+ position: "sidebar"
541
+ }
542
+ },
543
+ {
544
+ name: "meta",
545
+ type: "json",
546
+ admin: {
547
+ readOnly: true
548
+ }
549
+ }
550
+ ]
551
+ };
552
+ };
553
+
554
+ // src/client/routeMatcher.ts
555
+ var escapeRegex = (input) => input.replace(/[.+^${}()|[\]\\]/g, "\\$&");
556
+ var toRegExp = (pattern) => {
557
+ const escaped = pattern.split("*").map((segment) => escapeRegex(segment)).join(".*");
558
+ return new RegExp(`^${escaped}$`);
559
+ };
560
+ var pathMatchesAllowlist = (pathname, patterns) => {
561
+ if (patterns.length === 0) {
562
+ return false;
563
+ }
564
+ return patterns.some((pattern) => toRegExp(pattern).test(pathname));
565
+ };
566
+
567
+ // src/index.ts
568
+ var PLUGIN_SLUG = "payload-plugin-admin-feedback";
569
+ var DEFAULT_MEDIA_COLLECTION_SLUG = "media";
570
+ var DEFAULT_ORDER = 10;
571
+ var isUploadCollection = (collection) => Boolean(collection.upload);
572
+ var getCollectionLabel = (slug) => `Collection "${slug}"`;
573
+ var resolveMediaCollectionSlug = (config, options) => {
574
+ const collections = config.collections || [];
575
+ const targetCollection = collections.find((collection) => collection.slug === options.mediaCollectionSlug);
576
+ if (!targetCollection) {
577
+ if (!options.strictMediaCollection && options.mediaCollectionSlug !== DEFAULT_MEDIA_COLLECTION_SLUG) {
578
+ const defaultCollection = collections.find(
579
+ (collection) => collection.slug === DEFAULT_MEDIA_COLLECTION_SLUG
580
+ );
581
+ if (defaultCollection && isUploadCollection(defaultCollection)) {
582
+ return defaultCollection.slug;
583
+ }
584
+ }
585
+ throw new Error(
586
+ `${PLUGIN_SLUG}: ${getCollectionLabel(
587
+ options.mediaCollectionSlug
588
+ )} was not found. Add an upload-enabled collection or set mediaCollectionSlug explicitly.`
589
+ );
590
+ }
591
+ if (!isUploadCollection(targetCollection)) {
592
+ throw new Error(
593
+ `${PLUGIN_SLUG}: ${getCollectionLabel(
594
+ targetCollection.slug
595
+ )} is not upload-enabled. Configure an upload collection and set mediaCollectionSlug to that slug.`
596
+ );
597
+ }
598
+ return targetCollection.slug;
599
+ };
600
+ var resolveOptions = (pluginOptions) => ({
601
+ enabled: true,
602
+ allowScreenshotUpload: true,
603
+ maxMessageLength: 3e3,
604
+ mediaCollectionSlug: DEFAULT_MEDIA_COLLECTION_SLUG,
605
+ strictMediaCollection: true,
606
+ ...pluginOptions,
607
+ frontend: {
608
+ enabled: true,
609
+ include: [],
610
+ ...pluginOptions.frontend
611
+ },
612
+ screenshot: {
613
+ enabled: true,
614
+ maxFileSizeBytes: 5 * 1024 * 1024,
615
+ allowedMimeTypes: ["image/png", "image/jpeg", "image/webp"],
616
+ capturePolicy: "current-tab-first",
617
+ ...pluginOptions.screenshot
618
+ }
619
+ });
620
+ var adminFeedbackPlugin = definePlugin({
621
+ slug: PLUGIN_SLUG,
622
+ order: DEFAULT_ORDER,
623
+ plugin: ({ config, ...pluginOptions }) => {
624
+ const options = resolveOptions(pluginOptions);
625
+ if (!options.enabled) {
626
+ return config;
627
+ }
628
+ if (!options.emailTo || Array.isArray(options.emailTo) && options.emailTo.length === 0) {
629
+ throw new Error("adminFeedbackPlugin requires emailTo option.");
630
+ }
631
+ if (pluginOptions.email && !config.email) {
632
+ config.email = pluginOptions.email;
633
+ }
634
+ const resolvedMediaCollectionSlug = resolveMediaCollectionSlug(config, options);
635
+ const feedbackCollection = createAdminFeedbackCollection(options, resolvedMediaCollectionSlug);
636
+ return {
637
+ ...config,
638
+ collections: [...config.collections || [], feedbackCollection]
639
+ };
640
+ }
641
+ });
642
+
643
+ export { adminFeedbackPlugin, pathMatchesAllowlist };
@@ -0,0 +1,54 @@
1
+ import { Config } from 'payload';
2
+
3
+ type FrontendDisplayConfig = {
4
+ enabled?: boolean;
5
+ include?: string[];
6
+ };
7
+ type ScreenshotConfig = {
8
+ enabled?: boolean;
9
+ maxFileSizeBytes?: number;
10
+ allowedMimeTypes?: string[];
11
+ capturePolicy?: 'current-tab-first' | 'strict-current-tab' | 'any-surface';
12
+ };
13
+ type AdminFeedbackPluginOptions = {
14
+ enabled?: boolean;
15
+ emailTo: string | string[];
16
+ fromLabel?: string;
17
+ fromName?: string;
18
+ fromAddress?: string;
19
+ email?: Config['email'];
20
+ allowScreenshotUpload?: boolean;
21
+ maxMessageLength?: number;
22
+ mediaCollectionSlug?: string;
23
+ strictMediaCollection?: boolean;
24
+ screenshot?: ScreenshotConfig;
25
+ frontend?: FrontendDisplayConfig;
26
+ frontendRouteMatcher?: (pathname: string) => boolean;
27
+ };
28
+ type FeedbackStatus = 'new' | 'triaged' | 'resolved';
29
+ type FeedbackScreenshotInfo = {
30
+ id: number;
31
+ url?: string;
32
+ filename?: string;
33
+ alt?: string;
34
+ };
35
+ type FeedbackCreatedByInfo = {
36
+ id: number;
37
+ email?: string;
38
+ };
39
+ type FeedbackListItem = {
40
+ id: number;
41
+ message: string;
42
+ pagePath: string;
43
+ selector?: string | null;
44
+ selectedText?: string | null;
45
+ status: FeedbackStatus;
46
+ createdAt: string;
47
+ updatedAt: string;
48
+ createdBy?: FeedbackCreatedByInfo | number | null;
49
+ screenshot?: FeedbackScreenshotInfo | number | null;
50
+ };
51
+
52
+ declare const pathMatchesAllowlist: (pathname: string, patterns: string[]) => boolean;
53
+
54
+ export { type AdminFeedbackPluginOptions as A, type FeedbackListItem as F, pathMatchesAllowlist as p };