@foundrykit/guides-plugin 0.0.0-stage → 0.1.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/README.md +92 -2
- package/dist/collections/guides.d.ts +8 -0
- package/dist/collections/guides.js +129 -0
- package/dist/collections/guides.js.map +1 -0
- package/dist/collections/siteGuides.d.ts +7 -0
- package/dist/collections/siteGuides.js +139 -0
- package/dist/collections/siteGuides.js.map +1 -0
- package/dist/components/GuideArticle.d.ts +24 -0
- package/dist/components/GuideArticle.js +79 -0
- package/dist/components/GuideArticle.js.map +1 -0
- package/dist/components/GuideSearch.d.ts +5 -0
- package/dist/components/GuideSearch.js +83 -0
- package/dist/components/GuideSearch.js.map +1 -0
- package/dist/components/GuidesNavLink.d.ts +2 -0
- package/dist/components/GuidesNavLink.js +16 -0
- package/dist/components/GuidesNavLink.js.map +1 -0
- package/dist/components/GuidesView.d.ts +6 -0
- package/dist/components/GuidesView.js +212 -0
- package/dist/components/GuidesView.js.map +1 -0
- package/dist/components/Help.d.ts +12 -0
- package/dist/components/Help.js +112 -0
- package/dist/components/Help.js.map +1 -0
- package/dist/exports/client.d.ts +2 -0
- package/dist/exports/client.js +4 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/rsc.d.ts +1 -0
- package/dist/exports/rsc.js +3 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/hub.d.ts +33 -0
- package/dist/hub.js +40 -0
- package/dist/hub.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +111 -0
- package/dist/index.js.map +1 -0
- package/dist/searchGuides.d.ts +12 -0
- package/dist/searchGuides.js +63 -0
- package/dist/searchGuides.js.map +1 -0
- package/dist/sync.d.ts +29 -0
- package/dist/sync.js +71 -0
- package/dist/sync.js.map +1 -0
- package/package.json +106 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/GuidesNavLink.tsx"],"sourcesContent":["'use client';\n\nimport { usePathname } from 'next/navigation.js';\n\n/** \"Guides\" in the sidebar for every signed-in user, below the collection groups. */\nexport function GuidesNavLink() {\n const active = usePathname()?.startsWith('/admin/guides');\n return (\n <a href='/admin/guides' className={`nav__link${active ? 'active' : ''}`}>\n <span className='nav__link-label'>Guides</span>\n </a>\n );\n}\n"],"names":["usePathname","GuidesNavLink","active","startsWith","a","href","className","span"],"mappings":"AAAA;;AAEA,SAASA,WAAW,QAAQ,qBAAqB;AAEjD,mFAAmF,GACnF,OAAO,SAASC;IACd,MAAMC,SAASF,eAAeG,WAAW;IACzC,qBACE,KAACC;QAAEC,MAAK;QAAgBC,WAAW,CAAC,SAAS,EAAEJ,SAAS,WAAW,IAAI;kBACrE,cAAA,KAACK;YAAKD,WAAU;sBAAkB;;;AAGxC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AdminViewServerProps } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* /admin/guides and /admin/guides/<slug>: browse, search and read this site's guides. Reads the
|
|
4
|
+
* local copy with the user's own access, so it works when the hub is down.
|
|
5
|
+
*/
|
|
6
|
+
export declare function GuidesView(props: AdminViewServerProps): Promise<import("react/jsx-runtime").JSX.Element>;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { DefaultTemplate } from '@payloadcms/next/templates';
|
|
3
|
+
import { Gutter } from '@payloadcms/ui';
|
|
4
|
+
import { redirect } from 'next/navigation.js';
|
|
5
|
+
import { GUIDES_SLUG, SITE_GUIDES_SLUG } from '../hub.js';
|
|
6
|
+
import { GuideArticle } from './GuideArticle.js';
|
|
7
|
+
import { GuideSearch } from './GuideSearch.js';
|
|
8
|
+
const muted = {
|
|
9
|
+
color: 'var(--theme-elevation-600)',
|
|
10
|
+
margin: 0
|
|
11
|
+
};
|
|
12
|
+
const OTHER = 'Other';
|
|
13
|
+
/** Guides grouped under their hub topic, topics in hub order, untopicked guides last. */ const byTopic = (guides)=>{
|
|
14
|
+
const groups = new Map();
|
|
15
|
+
for (const g of guides){
|
|
16
|
+
const title = g.topic?.title || OTHER;
|
|
17
|
+
const group = groups.get(title) ?? {
|
|
18
|
+
order: title === OTHER ? Infinity : g.topic?.order ?? Number.MAX_SAFE_INTEGER,
|
|
19
|
+
guides: []
|
|
20
|
+
};
|
|
21
|
+
group.guides.push(g);
|
|
22
|
+
groups.set(title, group);
|
|
23
|
+
}
|
|
24
|
+
return [
|
|
25
|
+
...groups.entries()
|
|
26
|
+
].sort(([a, x], [b, y])=>x.order - y.order || a.localeCompare(b));
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* /admin/guides and /admin/guides/<slug>: browse, search and read this site's guides. Reads the
|
|
30
|
+
* local copy with the user's own access, so it works when the hub is down.
|
|
31
|
+
*/ export async function GuidesView(props) {
|
|
32
|
+
const { initPageResult, params, searchParams } = props;
|
|
33
|
+
const { req, locale, permissions, visibleEntities } = initPageResult;
|
|
34
|
+
const segments = params?.segments ?? [];
|
|
35
|
+
const slug = segments[1];
|
|
36
|
+
// Payload doesn't send signed-out visitors of custom views to the login page, and guides
|
|
37
|
+
// aren't readable without a user, so the query below would throw and show an error page.
|
|
38
|
+
if (!req.user) {
|
|
39
|
+
redirect(`/admin/login?redirect=${encodeURIComponent(`/admin/${segments.join('/') || 'guides'}`)}`);
|
|
40
|
+
}
|
|
41
|
+
// Authors are whoever the plugin's canAuthor allows, as applied by site-guides access control.
|
|
42
|
+
const canAuthor = Boolean(permissions?.collections?.[SITE_GUIDES_SLUG]?.create);
|
|
43
|
+
const { docs } = await req.payload.find({
|
|
44
|
+
collection: GUIDES_SLUG,
|
|
45
|
+
pagination: false,
|
|
46
|
+
depth: 0,
|
|
47
|
+
sort: [
|
|
48
|
+
'order',
|
|
49
|
+
'title'
|
|
50
|
+
],
|
|
51
|
+
overrideAccess: false,
|
|
52
|
+
req
|
|
53
|
+
});
|
|
54
|
+
const guides = docs;
|
|
55
|
+
const selected = slug ? guides.find((g)=>g.slug === slug) : undefined;
|
|
56
|
+
const hits = guides.map((g)=>({
|
|
57
|
+
slug: g.slug ?? g.hubId,
|
|
58
|
+
title: g.title,
|
|
59
|
+
summary: g.summary ?? '',
|
|
60
|
+
bodyText: g.bodyText ?? ''
|
|
61
|
+
}));
|
|
62
|
+
const recent = [
|
|
63
|
+
...guides
|
|
64
|
+
].sort((a, b)=>(b.hubUpdatedAt ?? '').localeCompare(a.hubUpdatedAt ?? '')).slice(0, 3);
|
|
65
|
+
return /*#__PURE__*/ _jsx(DefaultTemplate, {
|
|
66
|
+
i18n: req.i18n,
|
|
67
|
+
locale: locale,
|
|
68
|
+
params: params,
|
|
69
|
+
payload: req.payload,
|
|
70
|
+
permissions: permissions,
|
|
71
|
+
req: req,
|
|
72
|
+
searchParams: searchParams,
|
|
73
|
+
user: req.user ?? undefined,
|
|
74
|
+
visibleEntities: visibleEntities,
|
|
75
|
+
children: /*#__PURE__*/ _jsxs(Gutter, {
|
|
76
|
+
children: [
|
|
77
|
+
/*#__PURE__*/ _jsxs("header", {
|
|
78
|
+
style: {
|
|
79
|
+
margin: '1.5rem 0',
|
|
80
|
+
display: 'flex',
|
|
81
|
+
flexDirection: 'column',
|
|
82
|
+
gap: '0.5rem'
|
|
83
|
+
},
|
|
84
|
+
children: [
|
|
85
|
+
/*#__PURE__*/ _jsx("h1", {
|
|
86
|
+
style: {
|
|
87
|
+
margin: 0
|
|
88
|
+
},
|
|
89
|
+
children: "Guides"
|
|
90
|
+
}),
|
|
91
|
+
/*#__PURE__*/ _jsx("p", {
|
|
92
|
+
style: muted,
|
|
93
|
+
children: "How-tos for working in the CMS."
|
|
94
|
+
})
|
|
95
|
+
]
|
|
96
|
+
}),
|
|
97
|
+
/*#__PURE__*/ _jsxs("div", {
|
|
98
|
+
style: {
|
|
99
|
+
display: 'grid',
|
|
100
|
+
gap: '2rem',
|
|
101
|
+
gridTemplateColumns: 'minmax(12rem, 16rem) minmax(0, 1fr)',
|
|
102
|
+
paddingBottom: '3rem'
|
|
103
|
+
},
|
|
104
|
+
children: [
|
|
105
|
+
/*#__PURE__*/ _jsxs("nav", {
|
|
106
|
+
"aria-label": "Guides by topic",
|
|
107
|
+
style: {
|
|
108
|
+
display: 'flex',
|
|
109
|
+
flexDirection: 'column',
|
|
110
|
+
gap: '1rem'
|
|
111
|
+
},
|
|
112
|
+
children: [
|
|
113
|
+
canAuthor && /*#__PURE__*/ _jsx("a", {
|
|
114
|
+
href: `/admin/collections/${SITE_GUIDES_SLUG}/create`,
|
|
115
|
+
children: "+ New site guide"
|
|
116
|
+
}),
|
|
117
|
+
byTopic(guides).map(([title, group])=>/*#__PURE__*/ _jsxs("section", {
|
|
118
|
+
style: {
|
|
119
|
+
display: 'flex',
|
|
120
|
+
flexDirection: 'column',
|
|
121
|
+
gap: '0.25rem'
|
|
122
|
+
},
|
|
123
|
+
children: [
|
|
124
|
+
/*#__PURE__*/ _jsx("span", {
|
|
125
|
+
style: {
|
|
126
|
+
color: 'var(--theme-elevation-600)',
|
|
127
|
+
fontSize: '0.75rem',
|
|
128
|
+
textTransform: 'uppercase',
|
|
129
|
+
letterSpacing: '0.05em'
|
|
130
|
+
},
|
|
131
|
+
children: title
|
|
132
|
+
}),
|
|
133
|
+
group.guides.map((g)=>/*#__PURE__*/ _jsx("a", {
|
|
134
|
+
href: `/admin/guides/${g.slug}`,
|
|
135
|
+
"aria-current": g.slug === slug ? 'page' : undefined,
|
|
136
|
+
style: g.slug === slug ? {
|
|
137
|
+
fontWeight: 600
|
|
138
|
+
} : undefined,
|
|
139
|
+
children: g.title
|
|
140
|
+
}, g.id))
|
|
141
|
+
]
|
|
142
|
+
}, title)),
|
|
143
|
+
guides.length === 0 && /*#__PURE__*/ _jsx("p", {
|
|
144
|
+
style: muted,
|
|
145
|
+
children: "No guides yet."
|
|
146
|
+
})
|
|
147
|
+
]
|
|
148
|
+
}),
|
|
149
|
+
/*#__PURE__*/ _jsxs("div", {
|
|
150
|
+
style: {
|
|
151
|
+
display: 'flex',
|
|
152
|
+
flexDirection: 'column',
|
|
153
|
+
gap: '2rem',
|
|
154
|
+
minWidth: 0
|
|
155
|
+
},
|
|
156
|
+
children: [
|
|
157
|
+
/*#__PURE__*/ _jsx(GuideSearch, {
|
|
158
|
+
guides: hits
|
|
159
|
+
}),
|
|
160
|
+
slug ? selected ? /*#__PURE__*/ _jsx(GuideArticle, {
|
|
161
|
+
guide: selected,
|
|
162
|
+
canAuthor: canAuthor
|
|
163
|
+
}) : /*#__PURE__*/ _jsx("p", {
|
|
164
|
+
style: muted,
|
|
165
|
+
children: "Guide not found."
|
|
166
|
+
}) : recent.length > 0 && /*#__PURE__*/ _jsx("div", {
|
|
167
|
+
style: {
|
|
168
|
+
display: 'grid',
|
|
169
|
+
gap: '1rem',
|
|
170
|
+
gridTemplateColumns: 'repeat(auto-fill, minmax(14rem, 1fr))'
|
|
171
|
+
},
|
|
172
|
+
children: recent.map((g)=>/*#__PURE__*/ _jsxs("a", {
|
|
173
|
+
href: `/admin/guides/${g.slug}`,
|
|
174
|
+
style: {
|
|
175
|
+
display: 'block',
|
|
176
|
+
padding: '1rem',
|
|
177
|
+
textDecoration: 'none',
|
|
178
|
+
borderRadius: 'var(--style-radius-m, 6px)',
|
|
179
|
+
border: '1px solid var(--theme-border-color)',
|
|
180
|
+
background: 'var(--theme-elevation-50)'
|
|
181
|
+
},
|
|
182
|
+
children: [
|
|
183
|
+
/*#__PURE__*/ _jsx("span", {
|
|
184
|
+
style: {
|
|
185
|
+
display: 'block',
|
|
186
|
+
fontWeight: 600,
|
|
187
|
+
color: 'var(--theme-text)'
|
|
188
|
+
},
|
|
189
|
+
children: g.title
|
|
190
|
+
}),
|
|
191
|
+
/*#__PURE__*/ _jsx("span", {
|
|
192
|
+
style: {
|
|
193
|
+
...muted,
|
|
194
|
+
display: 'block',
|
|
195
|
+
fontSize: '0.875rem',
|
|
196
|
+
marginTop: '0.25rem'
|
|
197
|
+
},
|
|
198
|
+
children: g.summary
|
|
199
|
+
})
|
|
200
|
+
]
|
|
201
|
+
}, g.id))
|
|
202
|
+
})
|
|
203
|
+
]
|
|
204
|
+
})
|
|
205
|
+
]
|
|
206
|
+
})
|
|
207
|
+
]
|
|
208
|
+
})
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
//# sourceMappingURL=GuidesView.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/GuidesView.tsx"],"sourcesContent":["import { DefaultTemplate } from '@payloadcms/next/templates';\nimport { Gutter } from '@payloadcms/ui';\nimport { redirect } from 'next/navigation.js';\nimport type { AdminViewServerProps } from 'payload';\n\nimport { GUIDES_SLUG, SITE_GUIDES_SLUG } from '../hub.js';\nimport type { GuideHit } from '../searchGuides.js';\nimport { GuideArticle, type LocalGuide } from './GuideArticle.js';\nimport { GuideSearch } from './GuideSearch.js';\n\nconst muted = { color: 'var(--theme-elevation-600)', margin: 0 };\nconst OTHER = 'Other';\n\n/** Guides grouped under their hub topic, topics in hub order, untopicked guides last. */\nconst byTopic = (guides: LocalGuide[]) => {\n const groups = new Map<string, { order: number; guides: LocalGuide[] }>();\n for (const g of guides) {\n const title = g.topic?.title || OTHER;\n const group = groups.get(title) ?? {\n order:\n title === OTHER\n ? Infinity\n : (g.topic?.order ?? Number.MAX_SAFE_INTEGER),\n guides: [],\n };\n group.guides.push(g);\n groups.set(title, group);\n }\n return [...groups.entries()].sort(\n ([a, x], [b, y]) => x.order - y.order || a.localeCompare(b)\n );\n};\n\n/**\n * /admin/guides and /admin/guides/<slug>: browse, search and read this site's guides. Reads the\n * local copy with the user's own access, so it works when the hub is down.\n */\nexport async function GuidesView(props: AdminViewServerProps) {\n const { initPageResult, params, searchParams } = props;\n const { req, locale, permissions, visibleEntities } = initPageResult;\n const segments = (params?.segments as string[] | undefined) ?? [];\n const slug = segments[1];\n\n // Payload doesn't send signed-out visitors of custom views to the login page, and guides\n // aren't readable without a user, so the query below would throw and show an error page.\n if (!req.user) {\n redirect(\n `/admin/login?redirect=${encodeURIComponent(`/admin/${segments.join('/') || 'guides'}`)}`\n );\n }\n // Authors are whoever the plugin's canAuthor allows, as applied by site-guides access control.\n const canAuthor = Boolean(\n (\n permissions?.collections as\n | Record<string, { create?: unknown }>\n | undefined\n )?.[SITE_GUIDES_SLUG]?.create\n );\n\n const { docs } = await req.payload.find({\n collection: GUIDES_SLUG as never,\n pagination: false,\n depth: 0,\n sort: ['order', 'title'],\n overrideAccess: false,\n req,\n });\n const guides = docs as unknown as LocalGuide[];\n const selected = slug ? guides.find(g => g.slug === slug) : undefined;\n const hits: GuideHit[] = guides.map(g => ({\n slug: g.slug ?? g.hubId,\n title: g.title,\n summary: g.summary ?? '',\n bodyText: g.bodyText ?? '',\n }));\n const recent = [...guides]\n .sort((a, b) => (b.hubUpdatedAt ?? '').localeCompare(a.hubUpdatedAt ?? ''))\n .slice(0, 3);\n\n return (\n <DefaultTemplate\n i18n={req.i18n}\n locale={locale}\n params={params}\n payload={req.payload}\n permissions={permissions}\n req={req}\n searchParams={searchParams}\n user={req.user ?? undefined}\n visibleEntities={visibleEntities}\n >\n <Gutter>\n <header\n style={{\n margin: '1.5rem 0',\n display: 'flex',\n flexDirection: 'column',\n gap: '0.5rem',\n }}\n >\n <h1 style={{ margin: 0 }}>Guides</h1>\n <p style={muted}>How-tos for working in the CMS.</p>\n </header>\n <div\n style={{\n display: 'grid',\n gap: '2rem',\n gridTemplateColumns: 'minmax(12rem, 16rem) minmax(0, 1fr)',\n paddingBottom: '3rem',\n }}\n >\n <nav\n aria-label='Guides by topic'\n style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}\n >\n {canAuthor && (\n <a href={`/admin/collections/${SITE_GUIDES_SLUG}/create`}>\n + New site guide\n </a>\n )}\n {byTopic(guides).map(([title, group]) => (\n <section\n key={title}\n style={{\n display: 'flex',\n flexDirection: 'column',\n gap: '0.25rem',\n }}\n >\n <span\n style={{\n color: 'var(--theme-elevation-600)',\n fontSize: '0.75rem',\n textTransform: 'uppercase',\n letterSpacing: '0.05em',\n }}\n >\n {title}\n </span>\n {group.guides.map(g => (\n <a\n key={g.id}\n href={`/admin/guides/${g.slug}`}\n aria-current={g.slug === slug ? 'page' : undefined}\n style={g.slug === slug ? { fontWeight: 600 } : undefined}\n >\n {g.title}\n </a>\n ))}\n </section>\n ))}\n {guides.length === 0 && <p style={muted}>No guides yet.</p>}\n </nav>\n\n <div\n style={{\n display: 'flex',\n flexDirection: 'column',\n gap: '2rem',\n minWidth: 0,\n }}\n >\n <GuideSearch guides={hits} />\n {slug ? (\n selected ? (\n <GuideArticle guide={selected} canAuthor={canAuthor} />\n ) : (\n <p style={muted}>Guide not found.</p>\n )\n ) : (\n recent.length > 0 && (\n <div\n style={{\n display: 'grid',\n gap: '1rem',\n gridTemplateColumns:\n 'repeat(auto-fill, minmax(14rem, 1fr))',\n }}\n >\n {recent.map(g => (\n <a\n key={g.id}\n href={`/admin/guides/${g.slug}`}\n style={{\n display: 'block',\n padding: '1rem',\n textDecoration: 'none',\n borderRadius: 'var(--style-radius-m, 6px)',\n border: '1px solid var(--theme-border-color)',\n background: 'var(--theme-elevation-50)',\n }}\n >\n <span\n style={{\n display: 'block',\n fontWeight: 600,\n color: 'var(--theme-text)',\n }}\n >\n {g.title}\n </span>\n <span\n style={{\n ...muted,\n display: 'block',\n fontSize: '0.875rem',\n marginTop: '0.25rem',\n }}\n >\n {g.summary}\n </span>\n </a>\n ))}\n </div>\n )\n )}\n </div>\n </div>\n </Gutter>\n </DefaultTemplate>\n );\n}\n"],"names":["DefaultTemplate","Gutter","redirect","GUIDES_SLUG","SITE_GUIDES_SLUG","GuideArticle","GuideSearch","muted","color","margin","OTHER","byTopic","guides","groups","Map","g","title","topic","group","get","order","Infinity","Number","MAX_SAFE_INTEGER","push","set","entries","sort","a","x","b","y","localeCompare","GuidesView","props","initPageResult","params","searchParams","req","locale","permissions","visibleEntities","segments","slug","user","encodeURIComponent","join","canAuthor","Boolean","collections","create","docs","payload","find","collection","pagination","depth","overrideAccess","selected","undefined","hits","map","hubId","summary","bodyText","recent","hubUpdatedAt","slice","i18n","header","style","display","flexDirection","gap","h1","p","div","gridTemplateColumns","paddingBottom","nav","aria-label","href","section","span","fontSize","textTransform","letterSpacing","aria-current","fontWeight","id","length","minWidth","guide","padding","textDecoration","borderRadius","border","background","marginTop"],"mappings":";AAAA,SAASA,eAAe,QAAQ,6BAA6B;AAC7D,SAASC,MAAM,QAAQ,iBAAiB;AACxC,SAASC,QAAQ,QAAQ,qBAAqB;AAG9C,SAASC,WAAW,EAAEC,gBAAgB,QAAQ,YAAY;AAE1D,SAASC,YAAY,QAAyB,oBAAoB;AAClE,SAASC,WAAW,QAAQ,mBAAmB;AAE/C,MAAMC,QAAQ;IAAEC,OAAO;IAA8BC,QAAQ;AAAE;AAC/D,MAAMC,QAAQ;AAEd,uFAAuF,GACvF,MAAMC,UAAU,CAACC;IACf,MAAMC,SAAS,IAAIC;IACnB,KAAK,MAAMC,KAAKH,OAAQ;QACtB,MAAMI,QAAQD,EAAEE,KAAK,EAAED,SAASN;QAChC,MAAMQ,QAAQL,OAAOM,GAAG,CAACH,UAAU;YACjCI,OACEJ,UAAUN,QACNW,WACCN,EAAEE,KAAK,EAAEG,SAASE,OAAOC,gBAAgB;YAChDX,QAAQ,EAAE;QACZ;QACAM,MAAMN,MAAM,CAACY,IAAI,CAACT;QAClBF,OAAOY,GAAG,CAACT,OAAOE;IACpB;IACA,OAAO;WAAIL,OAAOa,OAAO;KAAG,CAACC,IAAI,CAC/B,CAAC,CAACC,GAAGC,EAAE,EAAE,CAACC,GAAGC,EAAE,GAAKF,EAAET,KAAK,GAAGW,EAAEX,KAAK,IAAIQ,EAAEI,aAAa,CAACF;AAE7D;AAEA;;;CAGC,GACD,OAAO,eAAeG,WAAWC,KAA2B;IAC1D,MAAM,EAAEC,cAAc,EAAEC,MAAM,EAAEC,YAAY,EAAE,GAAGH;IACjD,MAAM,EAAEI,GAAG,EAA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;gCACfC,KAAK;gCACLkB,UAAU;4BACZ;;8CAEA,KAACrF;oCAAYM,QAAQgD;;gCACpBjB,OACCe,yBACE,KAACrD;oCAAauF,OAAOlC;oCAAUX,WAAWA;mDAE1C,KAAC4B;oCAAEL,OAAO/D;8CAAO;qCAGnB0D,OAAOyB,MAAM,GAAG,mBACd,KAACd;oCACCN,OAAO;wCACLC,SAAS;wCACTE,KAAK;wCACLI,qBACE;oCACJ;8CAECZ,OAAOJ,GAAG,CAAC9C,CAAAA,kBACV,MAACa;4CAECqD,MAAM,CAAC,cAAc,EAAElE,EAAE4B,IAAI,EAAE;4CAC/B2B,OAAO;gDACLC,SAAS;gDACTsB,SAAS;gDACTC,gBAAgB;gDAChBC,cAAc;gDACdC,QAAQ;gDACRC,YAAY;4CACd;;8DAEA,KAACd;oDACCb,OAAO;wDACLC,SAAS;wDACTiB,YAAY;wDACZhF,OAAO;oDACT;8DAECO,EAAEC,KAAK;;8DAEV,KAACmE;oDACCb,OAAO;wDACL,GAAG/D,KAAK;wDACRgE,SAAS;wDACTa,UAAU;wDACVc,WAAW;oDACb;8DAECnF,EAAEgD,OAAO;;;2CA5BPhD,EAAE0E,EAAE;;;;;;;;;AAwC/B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A "Help" button listing the guides for one place in the admin, in a side drawer. Renders
|
|
3
|
+
* nothing when no guide lists that place, so sections without guides look as before.
|
|
4
|
+
*
|
|
5
|
+
* `context` is a help context key (e.g. `media.create`); without one it uses the current
|
|
6
|
+
* collection or global slug, which is how the plugin puts it on every edit page.
|
|
7
|
+
*/
|
|
8
|
+
export declare function Help({ context }: {
|
|
9
|
+
context?: string;
|
|
10
|
+
}): import("react/jsx-runtime").JSX.Element | null;
|
|
11
|
+
/** The edit-page Help button the plugin adds to every collection and global. */
|
|
12
|
+
export declare const HelpButton: () => import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { Button, Drawer, useConfig, useDocumentInfo, useModal } from '@payloadcms/ui';
|
|
4
|
+
import { useEffect, useState } from 'react';
|
|
5
|
+
const muted = {
|
|
6
|
+
color: 'var(--theme-elevation-600)',
|
|
7
|
+
fontSize: '0.875rem',
|
|
8
|
+
margin: '0.25rem 0 0'
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* A "Help" button listing the guides for one place in the admin, in a side drawer. Renders
|
|
12
|
+
* nothing when no guide lists that place, so sections without guides look as before.
|
|
13
|
+
*
|
|
14
|
+
* `context` is a help context key (e.g. `media.create`); without one it uses the current
|
|
15
|
+
* collection or global slug, which is how the plugin puts it on every edit page.
|
|
16
|
+
*/ export function Help({ context }) {
|
|
17
|
+
const { collectionSlug, globalSlug } = useDocumentInfo();
|
|
18
|
+
const docSlug = collectionSlug ?? globalSlug;
|
|
19
|
+
const key = context ?? docSlug;
|
|
20
|
+
const { getEntityConfig } = useConfig();
|
|
21
|
+
const { openModal } = useModal();
|
|
22
|
+
const [guides, setGuides] = useState([]);
|
|
23
|
+
useEffect(()=>{
|
|
24
|
+
if (!key) return;
|
|
25
|
+
const params = new URLSearchParams({
|
|
26
|
+
'where[helpContexts][in]': key,
|
|
27
|
+
depth: '0',
|
|
28
|
+
limit: '10',
|
|
29
|
+
sort: 'order',
|
|
30
|
+
'select[title]': 'true',
|
|
31
|
+
'select[slug]': 'true',
|
|
32
|
+
'select[summary]': 'true'
|
|
33
|
+
});
|
|
34
|
+
let cancelled = false;
|
|
35
|
+
fetch(`/api/guides?${params}`, {
|
|
36
|
+
credentials: 'include'
|
|
37
|
+
}).then((res)=>res.ok ? res.json() : {
|
|
38
|
+
docs: []
|
|
39
|
+
}).then((data)=>!cancelled && setGuides(data.docs ?? [])).catch(()=>!cancelled && setGuides([]));
|
|
40
|
+
return ()=>{
|
|
41
|
+
cancelled = true;
|
|
42
|
+
};
|
|
43
|
+
}, [
|
|
44
|
+
key
|
|
45
|
+
]);
|
|
46
|
+
if (!key || guides.length === 0) return null;
|
|
47
|
+
let label = key;
|
|
48
|
+
if (!context && docSlug) {
|
|
49
|
+
const entity = getEntityConfig(collectionSlug ? {
|
|
50
|
+
collectionSlug
|
|
51
|
+
} : {
|
|
52
|
+
globalSlug: docSlug
|
|
53
|
+
});
|
|
54
|
+
const raw = entity?.labels?.plural ?? entity?.label;
|
|
55
|
+
if (typeof raw === 'string') label = raw;
|
|
56
|
+
}
|
|
57
|
+
const drawerSlug = `guide-help-${key}`;
|
|
58
|
+
return /*#__PURE__*/ _jsxs(_Fragment, {
|
|
59
|
+
children: [
|
|
60
|
+
/*#__PURE__*/ _jsx(Button, {
|
|
61
|
+
buttonStyle: "secondary",
|
|
62
|
+
size: "small",
|
|
63
|
+
margin: false,
|
|
64
|
+
onClick: ()=>openModal(drawerSlug),
|
|
65
|
+
children: "? Help"
|
|
66
|
+
}),
|
|
67
|
+
/*#__PURE__*/ _jsxs(Drawer, {
|
|
68
|
+
slug: drawerSlug,
|
|
69
|
+
title: `Help with ${label}`,
|
|
70
|
+
children: [
|
|
71
|
+
/*#__PURE__*/ _jsx("ul", {
|
|
72
|
+
style: {
|
|
73
|
+
listStyle: 'none',
|
|
74
|
+
margin: 0,
|
|
75
|
+
padding: 0,
|
|
76
|
+
display: 'flex',
|
|
77
|
+
flexDirection: 'column',
|
|
78
|
+
gap: '1rem'
|
|
79
|
+
},
|
|
80
|
+
children: guides.map((g)=>/*#__PURE__*/ _jsxs("li", {
|
|
81
|
+
children: [
|
|
82
|
+
/*#__PURE__*/ _jsx("a", {
|
|
83
|
+
href: `/admin/guides/${g.slug}`,
|
|
84
|
+
style: {
|
|
85
|
+
fontWeight: 600
|
|
86
|
+
},
|
|
87
|
+
children: g.title
|
|
88
|
+
}),
|
|
89
|
+
/*#__PURE__*/ _jsx("p", {
|
|
90
|
+
style: muted,
|
|
91
|
+
children: g.summary
|
|
92
|
+
})
|
|
93
|
+
]
|
|
94
|
+
}, g.slug))
|
|
95
|
+
}),
|
|
96
|
+
/*#__PURE__*/ _jsx("p", {
|
|
97
|
+
style: {
|
|
98
|
+
marginTop: '1.5rem'
|
|
99
|
+
},
|
|
100
|
+
children: /*#__PURE__*/ _jsx("a", {
|
|
101
|
+
href: "/admin/guides",
|
|
102
|
+
children: "All guides →"
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
]
|
|
106
|
+
})
|
|
107
|
+
]
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/** The edit-page Help button the plugin adds to every collection and global. */ export const HelpButton = ()=>/*#__PURE__*/ _jsx(Help, {});
|
|
111
|
+
|
|
112
|
+
//# sourceMappingURL=Help.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/Help.tsx"],"sourcesContent":["'use client';\n\nimport {\n Button,\n Drawer,\n useConfig,\n useDocumentInfo,\n useModal,\n} from '@payloadcms/ui';\nimport { useEffect, useState } from 'react';\n\ntype GuideLink = { slug: string; title: string; summary: string };\n\nconst muted = {\n color: 'var(--theme-elevation-600)',\n fontSize: '0.875rem',\n margin: '0.25rem 0 0',\n};\n\n/**\n * A \"Help\" button listing the guides for one place in the admin, in a side drawer. Renders\n * nothing when no guide lists that place, so sections without guides look as before.\n *\n * `context` is a help context key (e.g. `media.create`); without one it uses the current\n * collection or global slug, which is how the plugin puts it on every edit page.\n */\nexport function Help({ context }: { context?: string }) {\n const { collectionSlug, globalSlug } = useDocumentInfo();\n const docSlug = collectionSlug ?? globalSlug;\n const key = context ?? docSlug;\n const { getEntityConfig } = useConfig();\n const { openModal } = useModal();\n const [guides, setGuides] = useState<GuideLink[]>([]);\n\n useEffect(() => {\n if (!key) return;\n const params = new URLSearchParams({\n 'where[helpContexts][in]': key,\n depth: '0',\n limit: '10',\n sort: 'order',\n 'select[title]': 'true',\n 'select[slug]': 'true',\n 'select[summary]': 'true',\n });\n let cancelled = false;\n fetch(`/api/guides?${params}`, { credentials: 'include' })\n .then(res => (res.ok ? res.json() : { docs: [] }))\n .then(\n (data: { docs?: GuideLink[] }) =>\n !cancelled && setGuides(data.docs ?? [])\n )\n .catch(() => !cancelled && setGuides([]));\n return () => {\n cancelled = true;\n };\n }, [key]);\n\n if (!key || guides.length === 0) return null;\n\n let label = key;\n if (!context && docSlug) {\n const entity = getEntityConfig(\n collectionSlug ? { collectionSlug } : { globalSlug: docSlug }\n ) as { labels?: { plural?: unknown }; label?: unknown } | undefined;\n const raw = entity?.labels?.plural ?? entity?.label;\n if (typeof raw === 'string') label = raw;\n }\n const drawerSlug = `guide-help-${key}`;\n\n return (\n <>\n <Button\n buttonStyle='secondary'\n size='small'\n margin={false}\n onClick={() => openModal(drawerSlug)}\n >\n ? Help\n </Button>\n <Drawer slug={drawerSlug} title={`Help with ${label}`}>\n <ul\n style={{\n listStyle: 'none',\n margin: 0,\n padding: 0,\n display: 'flex',\n flexDirection: 'column',\n gap: '1rem',\n }}\n >\n {guides.map(g => (\n <li key={g.slug}>\n <a href={`/admin/guides/${g.slug}`} style={{ fontWeight: 600 }}>\n {g.title}\n </a>\n <p style={muted}>{g.summary}</p>\n </li>\n ))}\n </ul>\n <p style={{ marginTop: '1.5rem' }}>\n <a href='/admin/guides'>All guides →</a>\n </p>\n </Drawer>\n </>\n );\n}\n\n/** The edit-page Help button the plugin adds to every collection and global. */\nexport const HelpButton = () => <Help />;\n"],"names":["Button","Drawer","useConfig","useDocumentInfo","useModal","useEffect","useState","muted","color","fontSize","margin","Help","context","collectionSlug","globalSlug","docSlug","key","getEntityConfig","openModal","guides","setGuides","params","URLSearchParams","depth","limit","sort","cancelled","fetch","credentials","then","res","ok","json","docs","data","catch","length","label","entity","raw","labels","plural","drawerSlug","buttonStyle","size","onClick","slug","title","ul","style","listStyle","padding","display","flexDirection","gap","map","g","li","a","href","fontWeight","p","summary","marginTop","HelpButton"],"mappings":"AAAA;;AAEA,SACEA,MAAM,EACNC,MA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AAUH,QAAQD;QAC9C,IAAI,OAAOE,QAAQ,UAAUF,QAAQE;IACvC;IACA,MAAMG,aAAa,CAAC,WAAW,EAAE1B,KAAK;IAEtC,qBACE;;0BACE,KAAChB;gBACC2C,aAAY;gBACZC,MAAK;gBACLlC,QAAQ;gBACRmC,SAAS,IAAM3B,UAAUwB;0BAC1B;;0BAGD,MAACzC;gBAAO6C,MAAMJ;gBAAYK,OAAO,CAAC,UAAU,EAAEV,OAAO;;kCACnD,KAACW;wBACCC,OAAO;4BACLC,WAAW;4BACXxC,QAAQ;4BACRyC,SAAS;4BACTC,SAAS;4BACTC,eAAe;4BACfC,KAAK;wBACP;kCAECnC,OAAOoC,GAAG,CAACC,CAAAA,kBACV,MAACC;;kDACC,KAACC;wCAAEC,MAAM,CAAC,cAAc,EAAEH,EAAEV,IAAI,EAAE;wCAAEG,OAAO;4CAAEW,YAAY;wCAAI;kDAC1DJ,EAAET,KAAK;;kDAEV,KAACc;wCAAEZ,OAAO1C;kDAAQiD,EAAEM,OAAO;;;+BAJpBN,EAAEV,IAAI;;kCAQnB,KAACe;wBAAEZ,OAAO;4BAAEc,WAAW;wBAAS;kCAC9B,cAAA,KAACL;4BAAEC,MAAK;sCAAgB;;;;;;;AAKlC;AAEA,8EAA8E,GAC9E,OAAO,MAAMK,aAAa,kBAAM,KAACrD,UAAQ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["export { GuidesNavLink } from '../components/GuidesNavLink.js';\nexport { Help, HelpButton } from '../components/Help.js';\n"],"names":["GuidesNavLink","Help","HelpButton"],"mappings":"AAAA,SAASA,aAAa,QAAQ,iCAAiC;AAC/D,SAASC,IAAI,EAAEC,UAAU,QAAQ,wBAAwB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { GuidesView } from '../components/GuidesView.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exports/rsc.ts"],"sourcesContent":["export { GuidesView } from '../components/GuidesView.js';\n"],"names":["GuidesView"],"mappings":"AAAA,SAASA,UAAU,QAAQ,8BAA8B"}
|
package/dist/hub.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Synced from package.json by `pnpm version-packages` (scripts/sync-plugin-version.mjs); a unit test checks it. */
|
|
2
|
+
export declare const PLUGIN_VERSION = "0.1.1";
|
|
3
|
+
export declare const GUIDES_SLUG = "guides";
|
|
4
|
+
export declare const SITE_GUIDES_SLUG = "site-guides";
|
|
5
|
+
export declare const SIGNATURE_HEADER = "x-guides-signature";
|
|
6
|
+
export type GuidesPluginConfig = {
|
|
7
|
+
/** The guides hub's URL. Default `process.env.GUIDES_HUB_URL`. */
|
|
8
|
+
hubURL?: string;
|
|
9
|
+
/** This site's guides key, from its Sites record in the hub. Default `process.env.GUIDES_HUB_KEY`. */
|
|
10
|
+
siteKey?: string;
|
|
11
|
+
/** Feature keys this site has, reported to the hub on every sync so Umi can check its Sites record. */
|
|
12
|
+
features?: string[];
|
|
13
|
+
/** Who can write site-specific guides here (Umi staff). Default: nobody. */
|
|
14
|
+
canAuthor?: (user: unknown) => boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Cron (Payload's 6-field form, seconds first) for the backstop pull, run by Payload jobs on
|
|
17
|
+
* the `default` queue. Default hourly, `0 0 * * * *`; `false` turns it off.
|
|
18
|
+
*/
|
|
19
|
+
schedule?: string | false;
|
|
20
|
+
disabled?: boolean;
|
|
21
|
+
};
|
|
22
|
+
export type ResolvedConfig = {
|
|
23
|
+
hubURL?: string;
|
|
24
|
+
siteKey?: string;
|
|
25
|
+
features: string[];
|
|
26
|
+
canAuthor: (user: unknown) => boolean;
|
|
27
|
+
schedule: string | false;
|
|
28
|
+
};
|
|
29
|
+
export declare const resolveConfig: (options: GuidesPluginConfig) => ResolvedConfig;
|
|
30
|
+
/** A request to the hub, authenticated as this site. */
|
|
31
|
+
export declare const hubFetch: (config: ResolvedConfig, path: string, init?: RequestInit) => Promise<Response>;
|
|
32
|
+
/** True when the header is the hub's HMAC of this exact body, keyed with this site's guides key. */
|
|
33
|
+
export declare const verifySignature: (key: string, body: string, header: string | null) => boolean;
|
package/dist/hub.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
/** Synced from package.json by `pnpm version-packages` (scripts/sync-plugin-version.mjs); a unit test checks it. */ export const PLUGIN_VERSION = '0.1.1';
|
|
3
|
+
export const GUIDES_SLUG = 'guides';
|
|
4
|
+
export const SITE_GUIDES_SLUG = 'site-guides';
|
|
5
|
+
export const SIGNATURE_HEADER = 'x-guides-signature';
|
|
6
|
+
// Env is read when the hub is called, not when the plugin is registered, so config built before
|
|
7
|
+
// env loads (or a rotated key) still works.
|
|
8
|
+
export const resolveConfig = (options)=>({
|
|
9
|
+
get hubURL () {
|
|
10
|
+
return options.hubURL ?? process.env.GUIDES_HUB_URL;
|
|
11
|
+
},
|
|
12
|
+
get siteKey () {
|
|
13
|
+
return options.siteKey ?? process.env.GUIDES_HUB_KEY;
|
|
14
|
+
},
|
|
15
|
+
features: options.features ?? [],
|
|
16
|
+
canAuthor: options.canAuthor ?? (()=>false),
|
|
17
|
+
schedule: options.schedule ?? '0 0 * * * *'
|
|
18
|
+
});
|
|
19
|
+
/** A request to the hub, authenticated as this site. */ export const hubFetch = async (config, path, init = {})=>{
|
|
20
|
+
if (!config.hubURL || !config.siteKey) {
|
|
21
|
+
throw new Error('Set GUIDES_HUB_URL and GUIDES_HUB_KEY (or the hubURL and siteKey options) to reach the guides hub.');
|
|
22
|
+
}
|
|
23
|
+
return fetch(new URL(path, config.hubURL), {
|
|
24
|
+
...init,
|
|
25
|
+
headers: {
|
|
26
|
+
'content-type': 'application/json',
|
|
27
|
+
authorization: `sites API-Key ${config.siteKey}`,
|
|
28
|
+
...init.headers
|
|
29
|
+
},
|
|
30
|
+
signal: AbortSignal.timeout(10_000)
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
/** True when the header is the hub's HMAC of this exact body, keyed with this site's guides key. */ export const verifySignature = (key, body, header)=>{
|
|
34
|
+
if (!header) return false;
|
|
35
|
+
const expected = Buffer.from(`sha256=${crypto.createHmac('sha256', key).update(body).digest('hex')}`);
|
|
36
|
+
const received = Buffer.from(header);
|
|
37
|
+
return received.length === expected.length && crypto.timingSafeEqual(received, expected);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=hub.js.map
|
package/dist/hub.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hub.ts"],"sourcesContent":["import crypto from 'node:crypto';\n\n/** Synced from package.json by `pnpm version-packages` (scripts/sync-plugin-version.mjs); a unit test checks it. */\nexport const PLUGIN_VERSION = '0.1.1';\n\nexport const GUIDES_SLUG = 'guides';\nexport const SITE_GUIDES_SLUG = 'site-guides';\nexport const SIGNATURE_HEADER = 'x-guides-signature';\n\nexport type GuidesPluginConfig = {\n /** The guides hub's URL. Default `process.env.GUIDES_HUB_URL`. */\n hubURL?: string;\n /** This site's guides key, from its Sites record in the hub. Default `process.env.GUIDES_HUB_KEY`. */\n siteKey?: string;\n /** Feature keys this site has, reported to the hub on every sync so Umi can check its Sites record. */\n features?: string[];\n /** Who can write site-specific guides here (Umi staff). Default: nobody. */\n canAuthor?: (user: unknown) => boolean;\n /**\n * Cron (Payload's 6-field form, seconds first) for the backstop pull, run by Payload jobs on\n * the `default` queue. Default hourly, `0 0 * * * *`; `false` turns it off.\n */\n schedule?: string | false;\n disabled?: boolean;\n};\n\nexport type ResolvedConfig = {\n hubURL?: string;\n siteKey?: string;\n features: string[];\n canAuthor: (user: unknown) => boolean;\n schedule: string | false;\n};\n\n// Env is read when the hub is called, not when the plugin is registered, so config built before\n// env loads (or a rotated key) still works.\nexport const resolveConfig = (options: GuidesPluginConfig): ResolvedConfig => ({\n get hubURL() {\n return options.hubURL ?? process.env.GUIDES_HUB_URL;\n },\n get siteKey() {\n return options.siteKey ?? process.env.GUIDES_HUB_KEY;\n },\n features: options.features ?? [],\n canAuthor: options.canAuthor ?? (() => false),\n schedule: options.schedule ?? '0 0 * * * *',\n});\n\n/** A request to the hub, authenticated as this site. */\nexport const hubFetch = async (\n config: ResolvedConfig,\n path: string,\n init: RequestInit = {}\n) => {\n if (!config.hubURL || !config.siteKey) {\n throw new Error(\n 'Set GUIDES_HUB_URL and GUIDES_HUB_KEY (or the hubURL and siteKey options) to reach the guides hub.'\n );\n }\n return fetch(new URL(path, config.hubURL), {\n ...init,\n headers: {\n 'content-type': 'application/json',\n authorization: `sites API-Key ${config.siteKey}`,\n ...init.headers,\n },\n signal: AbortSignal.timeout(10_000),\n });\n};\n\n/** True when the header is the hub's HMAC of this exact body, keyed with this site's guides key. */\nexport const verifySignature = (\n key: string,\n body: string,\n header: string | null\n) => {\n if (!header) return false;\n const expected = Buffer.from(\n `sha256=${crypto.createHmac('sha256', key).update(body).digest('hex')}`\n );\n const received = Buffer.from(header);\n return (\n received.length === expected.length &&\n crypto.timingSafeEqual(received, expected)\n );\n};\n"],"names":["crypto","PLUGIN_VERSION","GUIDES_SLUG","SITE_GUIDES_SLUG","SIGNATURE_HEADER","resolveConfig","options","hubURL","process","env","GUIDES_HUB_URL","siteKey","GUIDES_HUB_KEY","features","canAuthor","schedule","hubFetch","config","path","init","Error","fetch","URL","headers","authorization","signal","AbortSignal","timeout","verifySignature","key","body","header","expected","Buffer","from","createHmac","update","digest","received","length","timingSafeEqual"],"mappings":"AAAA,OAAOA,YAAY,cAAc;AAEjC,kHAAkH,GAClH,OAAO,MAAMC,iBAAiB,QAAQ;AAEtC,OAAO,MAAMC,cAAc,SAAS;AACpC,OAAO,MAAMC,mBAAmB,cAAc;AAC9C,OAAO,MAAMC,mBAAmB,qBAAqB;AA2BrD,gGAAgG;AAChG,4CAA4C;AAC5C,OAAO,MAAMC,gBAAgB,CAACC,UAAiD,CAAA;QAC7E,IAAIC,UAAS;YACX,OAAOD,QAAQC,MAAM,IAAIC,QAAQC,GAAG,CAACC,cAAc;QACrD;QACA,IAAIC,WAAU;YACZ,OAAOL,QAAQK,OAAO,IAAIH,QAAQC,GAAG,CAACG,cAAc;QACtD;QACAC,UAAUP,QAAQO,QAAQ,IAAI,EAAE;QAChCC,WAAWR,QAAQQ,SAAS,IAAK,CAAA,IAAM,KAAI;QAC3CC,UAAUT,QAAQS,QAAQ,IAAI;IAChC,CAAA,EAAG;AAEH,sDAAsD,GACtD,OAAO,MAAMC,WAAW,OACtBC,QACAC,MACAC,OAAoB,CAAC,CAAC;IAEtB,IAAI,CAACF,OAAOV,MAAM,IAAI,CAACU,OAAON,OAAO,EAAE;QACrC,MAAM,IAAIS,MACR;IAEJ;IACA,OAAOC,MAAM,IAAIC,IAAIJ,MAAMD,OAAOV,MAAM,GAAG;QACzC,GAAGY,IAAI;QACPI,SAAS;YACP,gBAAgB;YAChBC,eAAe,CAAC,cAAc,EAAEP,OAAON,OAAO,EAAE;YAChD,GAAGQ,KAAKI,OAAO;QACjB;QACAE,QAAQC,YAAYC,OAAO,CAAC;IAC9B;AACF,EAAE;AAEF,kGAAkG,GAClG,OAAO,MAAMC,kBAAkB,CAC7BC,KACAC,MACAC;IAEA,IAAI,CAACA,QAAQ,OAAO;IACpB,MAAMC,WAAWC,OAAOC,IAAI,CAC1B,CAAC,OAAO,EAAElC,OAAOmC,UAAU,CAAC,UAAUN,KAAKO,MAAM,CAACN,MAAMO,MAAM,CAAC,QAAQ;IAEzE,MAAMC,WAAWL,OAAOC,IAAI,CAACH;IAC7B,OACEO,SAASC,MAAM,KAAKP,SAASO,MAAM,IACnCvC,OAAOwC,eAAe,CAACF,UAAUN;AAErC,EAAE"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Config } from 'payload';
|
|
2
|
+
import { type GuidesPluginConfig } from './hub.js';
|
|
3
|
+
export type { GuidesPluginConfig, GuidesPluginConfig as PluginConfig, } from './hub.js';
|
|
4
|
+
export { syncGuides } from './sync.js';
|
|
5
|
+
/**
|
|
6
|
+
* CMS guides from the Umi guides hub: a Guides page in the sidebar, a Help button on every edit
|
|
7
|
+
* page, and local search, all read from a local copy the hub keeps in sync. Umi staff
|
|
8
|
+
* (`canAuthor`) write site-specific guides in the Site guides collection; publishing saves
|
|
9
|
+
* them to the hub.
|
|
10
|
+
*/
|
|
11
|
+
export declare const guidesPlugin: (options?: GuidesPluginConfig) => (config: Config) => Config;
|
|
12
|
+
export default guidesPlugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { guidesCollection } from './collections/guides.js';
|
|
2
|
+
import { siteGuidesCollection } from './collections/siteGuides.js';
|
|
3
|
+
import { GUIDES_SLUG, resolveConfig, SITE_GUIDES_SLUG } from './hub.js';
|
|
4
|
+
import { syncGuides } from './sync.js';
|
|
5
|
+
export { syncGuides } from './sync.js';
|
|
6
|
+
const HELP_BUTTON = '@foundrykit/guides-plugin/client#HelpButton';
|
|
7
|
+
const skipHelp = (slug)=>slug === GUIDES_SLUG || slug === SITE_GUIDES_SLUG || slug === 'users' || slug.startsWith('payload-');
|
|
8
|
+
// Payload reads beforeDocumentControls from `edit` on collections but from `elements` on globals
|
|
9
|
+
// (@payloadcms/next views/Document/renderDocumentSlots), so the slot depends on the kind.
|
|
10
|
+
const withHelp = (slot)=>(entry)=>{
|
|
11
|
+
if (skipHelp(entry.slug)) return entry;
|
|
12
|
+
const components = entry.admin?.components ?? {};
|
|
13
|
+
const current = components[slot] ?? {};
|
|
14
|
+
return {
|
|
15
|
+
...entry,
|
|
16
|
+
admin: {
|
|
17
|
+
...entry.admin,
|
|
18
|
+
components: {
|
|
19
|
+
...components,
|
|
20
|
+
[slot]: {
|
|
21
|
+
...current,
|
|
22
|
+
beforeDocumentControls: [
|
|
23
|
+
...current.beforeDocumentControls ?? [],
|
|
24
|
+
HELP_BUTTON
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
const syncTask = (config)=>({
|
|
32
|
+
slug: 'guidesSync',
|
|
33
|
+
label: 'Sync guides from the hub',
|
|
34
|
+
handler: async ({ req })=>({
|
|
35
|
+
output: await syncGuides(req.payload, config)
|
|
36
|
+
}),
|
|
37
|
+
...config.schedule ? {
|
|
38
|
+
schedule: [
|
|
39
|
+
{
|
|
40
|
+
cron: config.schedule,
|
|
41
|
+
queue: 'default'
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
} : {}
|
|
45
|
+
});
|
|
46
|
+
/** Fills an empty local copy on first boot, without holding startup up or failing it. */ const syncIfEmpty = async (payload, config)=>{
|
|
47
|
+
try {
|
|
48
|
+
const { totalDocs } = await payload.count({
|
|
49
|
+
collection: GUIDES_SLUG
|
|
50
|
+
});
|
|
51
|
+
if (totalDocs === 0) await syncGuides(payload, config);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
payload.logger.warn({
|
|
54
|
+
err
|
|
55
|
+
}, 'Guides: first sync from the hub failed; the next webhook or scheduled pull will retry.');
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* CMS guides from the Umi guides hub: a Guides page in the sidebar, a Help button on every edit
|
|
60
|
+
* page, and local search, all read from a local copy the hub keeps in sync. Umi staff
|
|
61
|
+
* (`canAuthor`) write site-specific guides in the Site guides collection; publishing saves
|
|
62
|
+
* them to the hub.
|
|
63
|
+
*/ export const guidesPlugin = (options = {})=>(config)=>{
|
|
64
|
+
if (options.disabled) return config;
|
|
65
|
+
const resolved = resolveConfig(options);
|
|
66
|
+
const components = config.admin?.components ?? {};
|
|
67
|
+
const onInit = config.onInit;
|
|
68
|
+
return {
|
|
69
|
+
...config,
|
|
70
|
+
collections: [
|
|
71
|
+
...(config.collections ?? []).map(withHelp('edit')),
|
|
72
|
+
guidesCollection(resolved),
|
|
73
|
+
siteGuidesCollection(resolved)
|
|
74
|
+
],
|
|
75
|
+
globals: (config.globals ?? []).map(withHelp('elements')),
|
|
76
|
+
admin: {
|
|
77
|
+
...config.admin,
|
|
78
|
+
components: {
|
|
79
|
+
...components,
|
|
80
|
+
afterNavLinks: [
|
|
81
|
+
...components.afterNavLinks ?? [],
|
|
82
|
+
'@foundrykit/guides-plugin/client#GuidesNavLink'
|
|
83
|
+
],
|
|
84
|
+
views: {
|
|
85
|
+
...components.views,
|
|
86
|
+
guides: {
|
|
87
|
+
Component: '@foundrykit/guides-plugin/rsc#GuidesView',
|
|
88
|
+
path: '/guides',
|
|
89
|
+
meta: {
|
|
90
|
+
title: 'Guides'
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
jobs: {
|
|
97
|
+
...config.jobs,
|
|
98
|
+
tasks: [
|
|
99
|
+
...config.jobs?.tasks ?? [],
|
|
100
|
+
syncTask(resolved)
|
|
101
|
+
]
|
|
102
|
+
},
|
|
103
|
+
onInit: async (payload)=>{
|
|
104
|
+
await onInit?.(payload);
|
|
105
|
+
void syncIfEmpty(payload, resolved);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
export default guidesPlugin;
|
|
110
|
+
|
|
111
|
+
//# sourceMappingURL=index.js.map
|