@matija2209/payload-plugin-admin-feedback 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/README.md +23 -0
- package/dist/client/index.cjs +106 -76
- package/dist/client/index.d.cts +31 -5
- package/dist/client/index.d.ts +31 -5
- package/dist/client/index.js +106 -77
- package/dist/index.cjs +168 -45
- package/dist/index.d.cts +28 -2
- package/dist/index.d.ts +28 -2
- package/dist/index.js +168 -45
- package/package.json +8 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.1.0
|
|
4
|
+
|
|
5
|
+
### Security
|
|
6
|
+
|
|
7
|
+
- **Anonymous reports closed** — `admin-feedback` `create` now needs a signed-in user by default (was open to anyone). The `/submit` and `/upload` endpoints check the collection's `create` access, so a host rule applies to them too.
|
|
8
|
+
- **Screenshot delete is token-gated** — `DELETE /upload/:id` used to delete any document of the upload collection for any signed-in user. It now needs the short-lived signed token returned by `/upload` (same user, one hour, not yet attached to a report).
|
|
9
|
+
- **E-mail HTML escaped** — message, page path, selector, selected text and names are escaped before they go into the notification e-mail.
|
|
10
|
+
- `createdBy` always comes from the session; a value in the request body is ignored. Server errors no longer echo internal messages to the client.
|
|
11
|
+
- `/upload` enforces `screenshot.maxFileSizeBytes` and `screenshot.allowedMimeTypes` on the server, and returns 404 when `allowScreenshotUpload` is `false`.
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- `access` option: per-operation access for the `admin-feedback` collection.
|
|
16
|
+
- `collectionOverrides` option: last word on the generated collection (labels, admin group, fields). Keep the slug `admin-feedback`.
|
|
17
|
+
- `adminWidget` option: the plugin registers `AdminFeedbackProvider` in `admin.components.providers`, so no layout edit is needed. It shows only for users whose permissions allow creating feedback.
|
|
18
|
+
- `adminBaseUrl` and `emailDateLocale` options for the e-mail.
|
|
19
|
+
- `AdminFeedbackWidget` props (`title`, `submitLabel`, `uploadLabel`, screenshot limits, `apiRoute`); `FrontendFeedbackWidget` `apiRoute`.
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
|
|
23
|
+
- New peer dependency `@payloadcms/ui` (already present in every Payload admin).
|
|
24
|
+
- `meta.createdAt` is kept on updates instead of being reset.
|
|
25
|
+
|
|
26
|
+
### Upgrade notes (from 1.0.x)
|
|
27
|
+
|
|
28
|
+
- If anonymous visitors must report, pass `access: { create: () => true }` explicitly.
|
|
29
|
+
|
|
3
30
|
## 1.0.4
|
|
4
31
|
|
|
5
32
|
### Added
|
package/README.md
CHANGED
|
@@ -94,6 +94,23 @@ If your project stores uploads in a collection named `files` instead of `media`,
|
|
|
94
94
|
|
|
95
95
|
### 2. Add the admin panel widget
|
|
96
96
|
|
|
97
|
+
The simplest way: let the plugin register it.
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
adminFeedbackPlugin({
|
|
101
|
+
emailTo: 'it@example.com',
|
|
102
|
+
adminWidget: { enabled: true, title: 'Report a bug', submitLabel: 'Send report' },
|
|
103
|
+
access: {
|
|
104
|
+
create: ({ req }) => Boolean(req.user),
|
|
105
|
+
read: ({ req }) => req.user?.role === 'admin' || { createdBy: { equals: req.user?.id } },
|
|
106
|
+
},
|
|
107
|
+
})
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
The widget then shows for every signed-in user whose permissions allow creating feedback. Regenerate the import map (`payload generate:importmap`).
|
|
111
|
+
|
|
112
|
+
Or render it yourself in the admin layout:
|
|
113
|
+
|
|
97
114
|
In your Payload admin layout (`src/app/(payload)/layout.tsx`):
|
|
98
115
|
|
|
99
116
|
```tsx
|
|
@@ -151,6 +168,12 @@ export default async function LocaleLayout({ children, params }) {
|
|
|
151
168
|
|
|
152
169
|
## Plugin Options
|
|
153
170
|
|
|
171
|
+
- `access?: { create?, read?, update?, delete? }` per-operation collection access; default: signed-in user for each. The endpoints check `create`.
|
|
172
|
+
- `collectionOverrides?: (collection) => collection` adjust labels, admin group, fields; keep the slug `admin-feedback`
|
|
173
|
+
- `adminWidget?: { enabled?: boolean; title?: string; submitLabel?: string; uploadLabel?: string }` register the admin widget as a provider
|
|
174
|
+
- `adminBaseUrl?: string` absolute admin host for e-mail links (default `config.serverURL`, then `NEXT_PUBLIC_SERVER_URL`)
|
|
175
|
+
- `emailDateLocale?: string` default `'sl-SI'`
|
|
176
|
+
|
|
154
177
|
- `enabled?: boolean` default `true`
|
|
155
178
|
- `emailTo: string | string[]` required recipient address or addresses
|
|
156
179
|
- `fromLabel?: string` label used by the widget and email output
|
package/dist/client/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
var React = require('react');
|
|
5
5
|
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var ui = require('@payloadcms/ui');
|
|
6
7
|
var navigation = require('next/navigation');
|
|
7
8
|
|
|
8
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -1480,53 +1481,110 @@ function FeedbackWidget({
|
|
|
1480
1481
|
) : null
|
|
1481
1482
|
] });
|
|
1482
1483
|
}
|
|
1483
|
-
|
|
1484
|
+
|
|
1485
|
+
// src/client/api.ts
|
|
1486
|
+
var readJson = async (response) => {
|
|
1487
|
+
try {
|
|
1488
|
+
return await response.json();
|
|
1489
|
+
} catch {
|
|
1490
|
+
return { success: false, error: `Request failed (${response.status}).` };
|
|
1491
|
+
}
|
|
1492
|
+
};
|
|
1493
|
+
var errorOf = (body, fallback) => typeof body.error === "string" && body.error ? body.error : fallback;
|
|
1494
|
+
var createFeedbackApi = (apiBase = "/api") => {
|
|
1495
|
+
const base = `${apiBase.replace(/\/+$/, "")}/admin-feedback`;
|
|
1496
|
+
const deleteTokens = /* @__PURE__ */ new Map();
|
|
1497
|
+
return {
|
|
1498
|
+
submit: async (payload) => {
|
|
1499
|
+
const response = await fetch(`${base}/submit`, {
|
|
1500
|
+
method: "POST",
|
|
1501
|
+
credentials: "include",
|
|
1502
|
+
headers: { "content-type": "application/json" },
|
|
1503
|
+
body: JSON.stringify(payload)
|
|
1504
|
+
});
|
|
1505
|
+
const body = await readJson(response);
|
|
1506
|
+
if (!response.ok || body.success !== true) {
|
|
1507
|
+
return { success: false, error: errorOf(body, "Submit failed.") };
|
|
1508
|
+
}
|
|
1509
|
+
deleteTokens.clear();
|
|
1510
|
+
return { success: true };
|
|
1511
|
+
},
|
|
1512
|
+
upload: async (formData) => {
|
|
1513
|
+
const response = await fetch(`${base}/upload`, {
|
|
1514
|
+
method: "POST",
|
|
1515
|
+
credentials: "include",
|
|
1516
|
+
body: formData
|
|
1517
|
+
});
|
|
1518
|
+
const body = await readJson(response);
|
|
1519
|
+
if (!response.ok || body.success !== true || typeof body.mediaId !== "number") {
|
|
1520
|
+
return { success: false, error: errorOf(body, "Upload failed.") };
|
|
1521
|
+
}
|
|
1522
|
+
if (typeof body.deleteToken === "string") {
|
|
1523
|
+
deleteTokens.set(body.mediaId, body.deleteToken);
|
|
1524
|
+
}
|
|
1525
|
+
return { success: true, mediaId: body.mediaId };
|
|
1526
|
+
},
|
|
1527
|
+
deleteScreenshot: async (mediaId) => {
|
|
1528
|
+
const token = deleteTokens.get(mediaId);
|
|
1529
|
+
if (!token) {
|
|
1530
|
+
return { success: false, error: "This screenshot can no longer be removed." };
|
|
1531
|
+
}
|
|
1532
|
+
const response = await fetch(`${base}/upload/${mediaId}`, {
|
|
1533
|
+
method: "DELETE",
|
|
1534
|
+
credentials: "include",
|
|
1535
|
+
headers: { "x-feedback-delete-token": token }
|
|
1536
|
+
});
|
|
1537
|
+
const body = await readJson(response);
|
|
1538
|
+
if (!response.ok || body.success !== true) {
|
|
1539
|
+
return { success: false, error: errorOf(body, "Delete failed.") };
|
|
1540
|
+
}
|
|
1541
|
+
deleteTokens.delete(mediaId);
|
|
1542
|
+
return { success: true };
|
|
1543
|
+
}
|
|
1544
|
+
};
|
|
1545
|
+
};
|
|
1546
|
+
function AdminFeedbackWidget({
|
|
1547
|
+
title = "Admin feedback",
|
|
1548
|
+
submitLabel = "Send",
|
|
1549
|
+
uploadLabel = "Upload image",
|
|
1550
|
+
allowScreenshotUpload = true,
|
|
1551
|
+
screenshotMaxFileSizeBytes,
|
|
1552
|
+
screenshotAllowedMimeTypes,
|
|
1553
|
+
capturePolicy,
|
|
1554
|
+
apiRoute = "/api"
|
|
1555
|
+
}) {
|
|
1556
|
+
const api = React__default.default.useMemo(() => createFeedbackApi(apiRoute), [apiRoute]);
|
|
1484
1557
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
1485
1558
|
FeedbackWidget,
|
|
1486
1559
|
{
|
|
1487
|
-
title
|
|
1488
|
-
submitLabel
|
|
1489
|
-
uploadLabel
|
|
1490
|
-
allowScreenshotUpload
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
});
|
|
1498
|
-
const result = await response.json();
|
|
1499
|
-
if (!result.success) {
|
|
1500
|
-
return { success: false, error: result.error || "Submit failed." };
|
|
1501
|
-
}
|
|
1502
|
-
return { success: true };
|
|
1503
|
-
},
|
|
1504
|
-
onUpload: async (formData) => {
|
|
1505
|
-
const response = await fetch("/api/admin-feedback/upload", {
|
|
1506
|
-
method: "POST",
|
|
1507
|
-
credentials: "include",
|
|
1508
|
-
body: formData
|
|
1509
|
-
});
|
|
1510
|
-
const result = await response.json();
|
|
1511
|
-
if (!result.success || typeof result.mediaId !== "number") {
|
|
1512
|
-
return { success: false, error: result.error || "Upload failed." };
|
|
1513
|
-
}
|
|
1514
|
-
return { success: true, mediaId: result.mediaId };
|
|
1515
|
-
},
|
|
1516
|
-
onDeleteScreenshot: async (mediaId) => {
|
|
1517
|
-
const response = await fetch(`/api/admin-feedback/upload/${mediaId}`, {
|
|
1518
|
-
method: "DELETE",
|
|
1519
|
-
credentials: "include"
|
|
1520
|
-
});
|
|
1521
|
-
const result = await response.json();
|
|
1522
|
-
if (!result.success) {
|
|
1523
|
-
return { success: false, error: result.error || "Delete failed." };
|
|
1524
|
-
}
|
|
1525
|
-
return { success: true };
|
|
1526
|
-
}
|
|
1560
|
+
title,
|
|
1561
|
+
submitLabel,
|
|
1562
|
+
uploadLabel,
|
|
1563
|
+
allowScreenshotUpload,
|
|
1564
|
+
screenshotMaxFileSizeBytes,
|
|
1565
|
+
screenshotAllowedMimeTypes,
|
|
1566
|
+
capturePolicy,
|
|
1567
|
+
onSubmit: api.submit,
|
|
1568
|
+
onUpload: allowScreenshotUpload ? api.upload : void 0,
|
|
1569
|
+
onDeleteScreenshot: api.deleteScreenshot
|
|
1527
1570
|
}
|
|
1528
1571
|
);
|
|
1529
1572
|
}
|
|
1573
|
+
function AdminFeedbackProvider({
|
|
1574
|
+
children,
|
|
1575
|
+
collectionSlug = "admin-feedback",
|
|
1576
|
+
...widgetProps
|
|
1577
|
+
}) {
|
|
1578
|
+
const { permissions, user } = ui.useAuth();
|
|
1579
|
+
const { config } = ui.useConfig();
|
|
1580
|
+
const canCreate = Boolean(
|
|
1581
|
+
user && permissions?.collections?.[collectionSlug]?.create
|
|
1582
|
+
);
|
|
1583
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
1584
|
+
children,
|
|
1585
|
+
canCreate ? /* @__PURE__ */ jsxRuntime.jsx(AdminFeedbackWidget, { ...widgetProps, apiRoute: config.routes?.api || "/api" }) : null
|
|
1586
|
+
] });
|
|
1587
|
+
}
|
|
1530
1588
|
|
|
1531
1589
|
// src/tenant/extractTenantSlug.ts
|
|
1532
1590
|
var extractTenantSlugFromPathname = (pathname, pathMarkers) => {
|
|
@@ -1573,9 +1631,11 @@ function FrontendFeedbackWidget({
|
|
|
1573
1631
|
submitLabel = "Send",
|
|
1574
1632
|
uploadLabel = "Upload image",
|
|
1575
1633
|
tenantPathMarkers = [],
|
|
1576
|
-
tenantFormDataKey = "tenant"
|
|
1634
|
+
tenantFormDataKey = "tenant",
|
|
1635
|
+
apiRoute = "/api"
|
|
1577
1636
|
}) {
|
|
1578
1637
|
const pathname = navigation.usePathname();
|
|
1638
|
+
const api = React__default.default.useMemo(() => createFeedbackApi(apiRoute), [apiRoute]);
|
|
1579
1639
|
const normalizedPathname = normalizePathname(pathname, locales);
|
|
1580
1640
|
if (!pathMatchesAllowlist(normalizedPathname, include)) {
|
|
1581
1641
|
return null;
|
|
@@ -1587,19 +1647,7 @@ function FrontendFeedbackWidget({
|
|
|
1587
1647
|
submitLabel,
|
|
1588
1648
|
uploadLabel,
|
|
1589
1649
|
allowScreenshotUpload: true,
|
|
1590
|
-
onSubmit:
|
|
1591
|
-
const response = await fetch("/api/admin-feedback/submit", {
|
|
1592
|
-
method: "POST",
|
|
1593
|
-
credentials: "include",
|
|
1594
|
-
headers: { "content-type": "application/json" },
|
|
1595
|
-
body: JSON.stringify(payload)
|
|
1596
|
-
});
|
|
1597
|
-
const result = await response.json();
|
|
1598
|
-
if (!result.success) {
|
|
1599
|
-
return { success: false, error: result.error || "Submit failed." };
|
|
1600
|
-
}
|
|
1601
|
-
return { success: true };
|
|
1602
|
-
},
|
|
1650
|
+
onSubmit: api.submit,
|
|
1603
1651
|
onUpload: async (formData) => {
|
|
1604
1652
|
if (tenantPathMarkers.length > 0) {
|
|
1605
1653
|
const tenantSlug = extractTenantSlugFromPathname(pathname, tenantPathMarkers);
|
|
@@ -1607,32 +1655,14 @@ function FrontendFeedbackWidget({
|
|
|
1607
1655
|
formData.set(tenantFormDataKey, tenantSlug);
|
|
1608
1656
|
}
|
|
1609
1657
|
}
|
|
1610
|
-
|
|
1611
|
-
method: "POST",
|
|
1612
|
-
credentials: "include",
|
|
1613
|
-
body: formData
|
|
1614
|
-
});
|
|
1615
|
-
const result = await response.json();
|
|
1616
|
-
if (!result.success || typeof result.mediaId !== "number") {
|
|
1617
|
-
return { success: false, error: result.error || "Upload failed." };
|
|
1618
|
-
}
|
|
1619
|
-
return { success: true, mediaId: result.mediaId };
|
|
1658
|
+
return api.upload(formData);
|
|
1620
1659
|
},
|
|
1621
|
-
onDeleteScreenshot:
|
|
1622
|
-
const response = await fetch(`/api/admin-feedback/upload/${mediaId}`, {
|
|
1623
|
-
method: "DELETE",
|
|
1624
|
-
credentials: "include"
|
|
1625
|
-
});
|
|
1626
|
-
const result = await response.json();
|
|
1627
|
-
if (!result.success) {
|
|
1628
|
-
return { success: false, error: result.error || "Delete failed." };
|
|
1629
|
-
}
|
|
1630
|
-
return { success: true };
|
|
1631
|
-
}
|
|
1660
|
+
onDeleteScreenshot: api.deleteScreenshot
|
|
1632
1661
|
}
|
|
1633
1662
|
);
|
|
1634
1663
|
}
|
|
1635
1664
|
|
|
1665
|
+
exports.AdminFeedbackProvider = AdminFeedbackProvider;
|
|
1636
1666
|
exports.AdminFeedbackWidget = AdminFeedbackWidget;
|
|
1637
1667
|
exports.FeedbackWidget = FeedbackWidget;
|
|
1638
1668
|
exports.FrontendFeedbackWidget = FrontendFeedbackWidget;
|
package/dist/client/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import React from 'react';
|
|
2
2
|
|
|
3
3
|
type CapturePolicy = 'current-tab-first' | 'strict-current-tab' | 'any-surface';
|
|
4
4
|
type SubmitPayload = {
|
|
@@ -44,9 +44,33 @@ type FeedbackWidgetProps = {
|
|
|
44
44
|
onUpload?: (formData: FormData) => Promise<UploadResult>;
|
|
45
45
|
onDeleteScreenshot?: (mediaId: number) => Promise<DeleteResult>;
|
|
46
46
|
};
|
|
47
|
-
declare function FeedbackWidget({ title, submitLabel, uploadLabel, allowScreenshotUpload, screenshotMaxFileSizeBytes, screenshotAllowedMimeTypes, capturePolicy, onSubmit, onUpload, onDeleteScreenshot, }: FeedbackWidgetProps):
|
|
47
|
+
declare function FeedbackWidget({ title, submitLabel, uploadLabel, allowScreenshotUpload, screenshotMaxFileSizeBytes, screenshotAllowedMimeTypes, capturePolicy, onSubmit, onUpload, onDeleteScreenshot, }: FeedbackWidgetProps): React.JSX.Element;
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
type AdminFeedbackWidgetProps = {
|
|
50
|
+
title?: string;
|
|
51
|
+
submitLabel?: string;
|
|
52
|
+
uploadLabel?: string;
|
|
53
|
+
allowScreenshotUpload?: boolean;
|
|
54
|
+
screenshotMaxFileSizeBytes?: number;
|
|
55
|
+
screenshotAllowedMimeTypes?: string[];
|
|
56
|
+
capturePolicy?: 'current-tab-first' | 'strict-current-tab' | 'any-surface';
|
|
57
|
+
/** Payload REST base path (`config.routes.api`), default `/api`. */
|
|
58
|
+
apiRoute?: string;
|
|
59
|
+
};
|
|
60
|
+
declare function AdminFeedbackWidget({ title, submitLabel, uploadLabel, allowScreenshotUpload, screenshotMaxFileSizeBytes, screenshotAllowedMimeTypes, capturePolicy, apiRoute, }: AdminFeedbackWidgetProps): React.JSX.Element;
|
|
61
|
+
|
|
62
|
+
type AdminFeedbackProviderProps = Omit<AdminFeedbackWidgetProps, 'apiRoute'> & {
|
|
63
|
+
children?: React.ReactNode;
|
|
64
|
+
/** Collection slug whose `create` permission decides whether the widget shows. */
|
|
65
|
+
collectionSlug?: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Admin provider the plugin registers in `admin.components.providers` when
|
|
69
|
+
* `adminWidget.enabled` is set. It renders the widget only for a signed-in user
|
|
70
|
+
* whose permissions allow creating feedback, so the login screen and accounts
|
|
71
|
+
* without access never see a button that would fail.
|
|
72
|
+
*/
|
|
73
|
+
declare function AdminFeedbackProvider({ children, collectionSlug, ...widgetProps }: AdminFeedbackProviderProps): React.JSX.Element;
|
|
50
74
|
|
|
51
75
|
type FrontendFeedbackWidgetProps = {
|
|
52
76
|
include: string[];
|
|
@@ -58,7 +82,9 @@ type FrontendFeedbackWidgetProps = {
|
|
|
58
82
|
tenantPathMarkers?: string[];
|
|
59
83
|
/** FormData key for tenant slug on upload (default `tenant`). */
|
|
60
84
|
tenantFormDataKey?: string;
|
|
85
|
+
/** Payload REST base path (`config.routes.api`), default `/api`. */
|
|
86
|
+
apiRoute?: string;
|
|
61
87
|
};
|
|
62
|
-
declare function FrontendFeedbackWidget({ include, locales, title, submitLabel, uploadLabel, tenantPathMarkers, tenantFormDataKey, }: FrontendFeedbackWidgetProps):
|
|
88
|
+
declare function FrontendFeedbackWidget({ include, locales, title, submitLabel, uploadLabel, tenantPathMarkers, tenantFormDataKey, apiRoute, }: FrontendFeedbackWidgetProps): React.JSX.Element | null;
|
|
63
89
|
|
|
64
|
-
export { AdminFeedbackWidget, FeedbackWidget, FrontendFeedbackWidget };
|
|
90
|
+
export { AdminFeedbackProvider, AdminFeedbackWidget, type AdminFeedbackWidgetProps, FeedbackWidget, FrontendFeedbackWidget };
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import React from 'react';
|
|
2
2
|
|
|
3
3
|
type CapturePolicy = 'current-tab-first' | 'strict-current-tab' | 'any-surface';
|
|
4
4
|
type SubmitPayload = {
|
|
@@ -44,9 +44,33 @@ type FeedbackWidgetProps = {
|
|
|
44
44
|
onUpload?: (formData: FormData) => Promise<UploadResult>;
|
|
45
45
|
onDeleteScreenshot?: (mediaId: number) => Promise<DeleteResult>;
|
|
46
46
|
};
|
|
47
|
-
declare function FeedbackWidget({ title, submitLabel, uploadLabel, allowScreenshotUpload, screenshotMaxFileSizeBytes, screenshotAllowedMimeTypes, capturePolicy, onSubmit, onUpload, onDeleteScreenshot, }: FeedbackWidgetProps):
|
|
47
|
+
declare function FeedbackWidget({ title, submitLabel, uploadLabel, allowScreenshotUpload, screenshotMaxFileSizeBytes, screenshotAllowedMimeTypes, capturePolicy, onSubmit, onUpload, onDeleteScreenshot, }: FeedbackWidgetProps): React.JSX.Element;
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
type AdminFeedbackWidgetProps = {
|
|
50
|
+
title?: string;
|
|
51
|
+
submitLabel?: string;
|
|
52
|
+
uploadLabel?: string;
|
|
53
|
+
allowScreenshotUpload?: boolean;
|
|
54
|
+
screenshotMaxFileSizeBytes?: number;
|
|
55
|
+
screenshotAllowedMimeTypes?: string[];
|
|
56
|
+
capturePolicy?: 'current-tab-first' | 'strict-current-tab' | 'any-surface';
|
|
57
|
+
/** Payload REST base path (`config.routes.api`), default `/api`. */
|
|
58
|
+
apiRoute?: string;
|
|
59
|
+
};
|
|
60
|
+
declare function AdminFeedbackWidget({ title, submitLabel, uploadLabel, allowScreenshotUpload, screenshotMaxFileSizeBytes, screenshotAllowedMimeTypes, capturePolicy, apiRoute, }: AdminFeedbackWidgetProps): React.JSX.Element;
|
|
61
|
+
|
|
62
|
+
type AdminFeedbackProviderProps = Omit<AdminFeedbackWidgetProps, 'apiRoute'> & {
|
|
63
|
+
children?: React.ReactNode;
|
|
64
|
+
/** Collection slug whose `create` permission decides whether the widget shows. */
|
|
65
|
+
collectionSlug?: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Admin provider the plugin registers in `admin.components.providers` when
|
|
69
|
+
* `adminWidget.enabled` is set. It renders the widget only for a signed-in user
|
|
70
|
+
* whose permissions allow creating feedback, so the login screen and accounts
|
|
71
|
+
* without access never see a button that would fail.
|
|
72
|
+
*/
|
|
73
|
+
declare function AdminFeedbackProvider({ children, collectionSlug, ...widgetProps }: AdminFeedbackProviderProps): React.JSX.Element;
|
|
50
74
|
|
|
51
75
|
type FrontendFeedbackWidgetProps = {
|
|
52
76
|
include: string[];
|
|
@@ -58,7 +82,9 @@ type FrontendFeedbackWidgetProps = {
|
|
|
58
82
|
tenantPathMarkers?: string[];
|
|
59
83
|
/** FormData key for tenant slug on upload (default `tenant`). */
|
|
60
84
|
tenantFormDataKey?: string;
|
|
85
|
+
/** Payload REST base path (`config.routes.api`), default `/api`. */
|
|
86
|
+
apiRoute?: string;
|
|
61
87
|
};
|
|
62
|
-
declare function FrontendFeedbackWidget({ include, locales, title, submitLabel, uploadLabel, tenantPathMarkers, tenantFormDataKey, }: FrontendFeedbackWidgetProps):
|
|
88
|
+
declare function FrontendFeedbackWidget({ include, locales, title, submitLabel, uploadLabel, tenantPathMarkers, tenantFormDataKey, apiRoute, }: FrontendFeedbackWidgetProps): React.JSX.Element | null;
|
|
63
89
|
|
|
64
|
-
export { AdminFeedbackWidget, FeedbackWidget, FrontendFeedbackWidget };
|
|
90
|
+
export { AdminFeedbackProvider, AdminFeedbackWidget, type AdminFeedbackWidgetProps, FeedbackWidget, FrontendFeedbackWidget };
|
package/dist/client/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
4
|
+
import { useAuth, useConfig } from '@payloadcms/ui';
|
|
4
5
|
import { usePathname } from 'next/navigation';
|
|
5
6
|
|
|
6
7
|
var DEFAULT_COLORS = ["#ef4444", "#f97316", "#eab308", "#22c55e", "#06b6d4", "#3b82f6", "#a855f7"];
|
|
@@ -1474,53 +1475,110 @@ function FeedbackWidget({
|
|
|
1474
1475
|
) : null
|
|
1475
1476
|
] });
|
|
1476
1477
|
}
|
|
1477
|
-
|
|
1478
|
+
|
|
1479
|
+
// src/client/api.ts
|
|
1480
|
+
var readJson = async (response) => {
|
|
1481
|
+
try {
|
|
1482
|
+
return await response.json();
|
|
1483
|
+
} catch {
|
|
1484
|
+
return { success: false, error: `Request failed (${response.status}).` };
|
|
1485
|
+
}
|
|
1486
|
+
};
|
|
1487
|
+
var errorOf = (body, fallback) => typeof body.error === "string" && body.error ? body.error : fallback;
|
|
1488
|
+
var createFeedbackApi = (apiBase = "/api") => {
|
|
1489
|
+
const base = `${apiBase.replace(/\/+$/, "")}/admin-feedback`;
|
|
1490
|
+
const deleteTokens = /* @__PURE__ */ new Map();
|
|
1491
|
+
return {
|
|
1492
|
+
submit: async (payload) => {
|
|
1493
|
+
const response = await fetch(`${base}/submit`, {
|
|
1494
|
+
method: "POST",
|
|
1495
|
+
credentials: "include",
|
|
1496
|
+
headers: { "content-type": "application/json" },
|
|
1497
|
+
body: JSON.stringify(payload)
|
|
1498
|
+
});
|
|
1499
|
+
const body = await readJson(response);
|
|
1500
|
+
if (!response.ok || body.success !== true) {
|
|
1501
|
+
return { success: false, error: errorOf(body, "Submit failed.") };
|
|
1502
|
+
}
|
|
1503
|
+
deleteTokens.clear();
|
|
1504
|
+
return { success: true };
|
|
1505
|
+
},
|
|
1506
|
+
upload: async (formData) => {
|
|
1507
|
+
const response = await fetch(`${base}/upload`, {
|
|
1508
|
+
method: "POST",
|
|
1509
|
+
credentials: "include",
|
|
1510
|
+
body: formData
|
|
1511
|
+
});
|
|
1512
|
+
const body = await readJson(response);
|
|
1513
|
+
if (!response.ok || body.success !== true || typeof body.mediaId !== "number") {
|
|
1514
|
+
return { success: false, error: errorOf(body, "Upload failed.") };
|
|
1515
|
+
}
|
|
1516
|
+
if (typeof body.deleteToken === "string") {
|
|
1517
|
+
deleteTokens.set(body.mediaId, body.deleteToken);
|
|
1518
|
+
}
|
|
1519
|
+
return { success: true, mediaId: body.mediaId };
|
|
1520
|
+
},
|
|
1521
|
+
deleteScreenshot: async (mediaId) => {
|
|
1522
|
+
const token = deleteTokens.get(mediaId);
|
|
1523
|
+
if (!token) {
|
|
1524
|
+
return { success: false, error: "This screenshot can no longer be removed." };
|
|
1525
|
+
}
|
|
1526
|
+
const response = await fetch(`${base}/upload/${mediaId}`, {
|
|
1527
|
+
method: "DELETE",
|
|
1528
|
+
credentials: "include",
|
|
1529
|
+
headers: { "x-feedback-delete-token": token }
|
|
1530
|
+
});
|
|
1531
|
+
const body = await readJson(response);
|
|
1532
|
+
if (!response.ok || body.success !== true) {
|
|
1533
|
+
return { success: false, error: errorOf(body, "Delete failed.") };
|
|
1534
|
+
}
|
|
1535
|
+
deleteTokens.delete(mediaId);
|
|
1536
|
+
return { success: true };
|
|
1537
|
+
}
|
|
1538
|
+
};
|
|
1539
|
+
};
|
|
1540
|
+
function AdminFeedbackWidget({
|
|
1541
|
+
title = "Admin feedback",
|
|
1542
|
+
submitLabel = "Send",
|
|
1543
|
+
uploadLabel = "Upload image",
|
|
1544
|
+
allowScreenshotUpload = true,
|
|
1545
|
+
screenshotMaxFileSizeBytes,
|
|
1546
|
+
screenshotAllowedMimeTypes,
|
|
1547
|
+
capturePolicy,
|
|
1548
|
+
apiRoute = "/api"
|
|
1549
|
+
}) {
|
|
1550
|
+
const api = React.useMemo(() => createFeedbackApi(apiRoute), [apiRoute]);
|
|
1478
1551
|
return /* @__PURE__ */ jsx(
|
|
1479
1552
|
FeedbackWidget,
|
|
1480
1553
|
{
|
|
1481
|
-
title
|
|
1482
|
-
submitLabel
|
|
1483
|
-
uploadLabel
|
|
1484
|
-
allowScreenshotUpload
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
});
|
|
1492
|
-
const result = await response.json();
|
|
1493
|
-
if (!result.success) {
|
|
1494
|
-
return { success: false, error: result.error || "Submit failed." };
|
|
1495
|
-
}
|
|
1496
|
-
return { success: true };
|
|
1497
|
-
},
|
|
1498
|
-
onUpload: async (formData) => {
|
|
1499
|
-
const response = await fetch("/api/admin-feedback/upload", {
|
|
1500
|
-
method: "POST",
|
|
1501
|
-
credentials: "include",
|
|
1502
|
-
body: formData
|
|
1503
|
-
});
|
|
1504
|
-
const result = await response.json();
|
|
1505
|
-
if (!result.success || typeof result.mediaId !== "number") {
|
|
1506
|
-
return { success: false, error: result.error || "Upload failed." };
|
|
1507
|
-
}
|
|
1508
|
-
return { success: true, mediaId: result.mediaId };
|
|
1509
|
-
},
|
|
1510
|
-
onDeleteScreenshot: async (mediaId) => {
|
|
1511
|
-
const response = await fetch(`/api/admin-feedback/upload/${mediaId}`, {
|
|
1512
|
-
method: "DELETE",
|
|
1513
|
-
credentials: "include"
|
|
1514
|
-
});
|
|
1515
|
-
const result = await response.json();
|
|
1516
|
-
if (!result.success) {
|
|
1517
|
-
return { success: false, error: result.error || "Delete failed." };
|
|
1518
|
-
}
|
|
1519
|
-
return { success: true };
|
|
1520
|
-
}
|
|
1554
|
+
title,
|
|
1555
|
+
submitLabel,
|
|
1556
|
+
uploadLabel,
|
|
1557
|
+
allowScreenshotUpload,
|
|
1558
|
+
screenshotMaxFileSizeBytes,
|
|
1559
|
+
screenshotAllowedMimeTypes,
|
|
1560
|
+
capturePolicy,
|
|
1561
|
+
onSubmit: api.submit,
|
|
1562
|
+
onUpload: allowScreenshotUpload ? api.upload : void 0,
|
|
1563
|
+
onDeleteScreenshot: api.deleteScreenshot
|
|
1521
1564
|
}
|
|
1522
1565
|
);
|
|
1523
1566
|
}
|
|
1567
|
+
function AdminFeedbackProvider({
|
|
1568
|
+
children,
|
|
1569
|
+
collectionSlug = "admin-feedback",
|
|
1570
|
+
...widgetProps
|
|
1571
|
+
}) {
|
|
1572
|
+
const { permissions, user } = useAuth();
|
|
1573
|
+
const { config } = useConfig();
|
|
1574
|
+
const canCreate = Boolean(
|
|
1575
|
+
user && permissions?.collections?.[collectionSlug]?.create
|
|
1576
|
+
);
|
|
1577
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1578
|
+
children,
|
|
1579
|
+
canCreate ? /* @__PURE__ */ jsx(AdminFeedbackWidget, { ...widgetProps, apiRoute: config.routes?.api || "/api" }) : null
|
|
1580
|
+
] });
|
|
1581
|
+
}
|
|
1524
1582
|
|
|
1525
1583
|
// src/tenant/extractTenantSlug.ts
|
|
1526
1584
|
var extractTenantSlugFromPathname = (pathname, pathMarkers) => {
|
|
@@ -1567,9 +1625,11 @@ function FrontendFeedbackWidget({
|
|
|
1567
1625
|
submitLabel = "Send",
|
|
1568
1626
|
uploadLabel = "Upload image",
|
|
1569
1627
|
tenantPathMarkers = [],
|
|
1570
|
-
tenantFormDataKey = "tenant"
|
|
1628
|
+
tenantFormDataKey = "tenant",
|
|
1629
|
+
apiRoute = "/api"
|
|
1571
1630
|
}) {
|
|
1572
1631
|
const pathname = usePathname();
|
|
1632
|
+
const api = React.useMemo(() => createFeedbackApi(apiRoute), [apiRoute]);
|
|
1573
1633
|
const normalizedPathname = normalizePathname(pathname, locales);
|
|
1574
1634
|
if (!pathMatchesAllowlist(normalizedPathname, include)) {
|
|
1575
1635
|
return null;
|
|
@@ -1581,19 +1641,7 @@ function FrontendFeedbackWidget({
|
|
|
1581
1641
|
submitLabel,
|
|
1582
1642
|
uploadLabel,
|
|
1583
1643
|
allowScreenshotUpload: true,
|
|
1584
|
-
onSubmit:
|
|
1585
|
-
const response = await fetch("/api/admin-feedback/submit", {
|
|
1586
|
-
method: "POST",
|
|
1587
|
-
credentials: "include",
|
|
1588
|
-
headers: { "content-type": "application/json" },
|
|
1589
|
-
body: JSON.stringify(payload)
|
|
1590
|
-
});
|
|
1591
|
-
const result = await response.json();
|
|
1592
|
-
if (!result.success) {
|
|
1593
|
-
return { success: false, error: result.error || "Submit failed." };
|
|
1594
|
-
}
|
|
1595
|
-
return { success: true };
|
|
1596
|
-
},
|
|
1644
|
+
onSubmit: api.submit,
|
|
1597
1645
|
onUpload: async (formData) => {
|
|
1598
1646
|
if (tenantPathMarkers.length > 0) {
|
|
1599
1647
|
const tenantSlug = extractTenantSlugFromPathname(pathname, tenantPathMarkers);
|
|
@@ -1601,30 +1649,11 @@ function FrontendFeedbackWidget({
|
|
|
1601
1649
|
formData.set(tenantFormDataKey, tenantSlug);
|
|
1602
1650
|
}
|
|
1603
1651
|
}
|
|
1604
|
-
|
|
1605
|
-
method: "POST",
|
|
1606
|
-
credentials: "include",
|
|
1607
|
-
body: formData
|
|
1608
|
-
});
|
|
1609
|
-
const result = await response.json();
|
|
1610
|
-
if (!result.success || typeof result.mediaId !== "number") {
|
|
1611
|
-
return { success: false, error: result.error || "Upload failed." };
|
|
1612
|
-
}
|
|
1613
|
-
return { success: true, mediaId: result.mediaId };
|
|
1652
|
+
return api.upload(formData);
|
|
1614
1653
|
},
|
|
1615
|
-
onDeleteScreenshot:
|
|
1616
|
-
const response = await fetch(`/api/admin-feedback/upload/${mediaId}`, {
|
|
1617
|
-
method: "DELETE",
|
|
1618
|
-
credentials: "include"
|
|
1619
|
-
});
|
|
1620
|
-
const result = await response.json();
|
|
1621
|
-
if (!result.success) {
|
|
1622
|
-
return { success: false, error: result.error || "Delete failed." };
|
|
1623
|
-
}
|
|
1624
|
-
return { success: true };
|
|
1625
|
-
}
|
|
1654
|
+
onDeleteScreenshot: api.deleteScreenshot
|
|
1626
1655
|
}
|
|
1627
1656
|
);
|
|
1628
1657
|
}
|
|
1629
1658
|
|
|
1630
|
-
export { AdminFeedbackWidget, FeedbackWidget, FrontendFeedbackWidget };
|
|
1659
|
+
export { AdminFeedbackProvider, AdminFeedbackWidget, FeedbackWidget, FrontendFeedbackWidget };
|