@nitrogenbuilder/connector-payload 0.1.18 → 0.1.20

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.
@@ -0,0 +1,353 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
+ import { useState, useCallback } from 'react';
4
+ import { Collapsible, useField } from '@payloadcms/ui';
5
+ function updateAtPath(root, path, newValue) {
6
+ if (path.length === 0)
7
+ return newValue;
8
+ const [head, ...rest] = path;
9
+ if (Array.isArray(root)) {
10
+ const copy = [...root];
11
+ copy[head] = updateAtPath(copy[head], rest, newValue);
12
+ return copy;
13
+ }
14
+ if (typeof root === 'object' && root !== null) {
15
+ const obj = root;
16
+ return { ...obj, [head]: updateAtPath(obj[head], rest, newValue) };
17
+ }
18
+ return root;
19
+ }
20
+ function deleteAtPath(root, path) {
21
+ if (path.length === 0)
22
+ return root;
23
+ if (path.length === 1) {
24
+ const [head] = path;
25
+ if (Array.isArray(root))
26
+ return root.filter((_, i) => i !== head);
27
+ if (typeof root === 'object' && root !== null) {
28
+ const { [head]: _removed, ...rest } = root;
29
+ return rest;
30
+ }
31
+ return root;
32
+ }
33
+ const [head, ...rest] = path;
34
+ if (Array.isArray(root)) {
35
+ const copy = [...root];
36
+ copy[head] = deleteAtPath(copy[head], rest);
37
+ return copy;
38
+ }
39
+ if (typeof root === 'object' && root !== null) {
40
+ const obj = root;
41
+ return { ...obj, [head]: deleteAtPath(obj[head], rest) };
42
+ }
43
+ return root;
44
+ }
45
+ function renameKeyAtPath(root, path, newKey) {
46
+ if (path.length === 0)
47
+ return root;
48
+ const parentPath = path.slice(0, -1);
49
+ const oldKey = path[path.length - 1];
50
+ function navigate(node, remaining) {
51
+ if (remaining.length === 0) {
52
+ if (typeof node === 'object' && node !== null && !Array.isArray(node)) {
53
+ const obj = node;
54
+ const result = {};
55
+ for (const [k, v] of Object.entries(obj)) {
56
+ result[k === oldKey ? newKey : k] = v;
57
+ }
58
+ return result;
59
+ }
60
+ return node;
61
+ }
62
+ const [head, ...rest] = remaining;
63
+ if (Array.isArray(node)) {
64
+ const copy = [...node];
65
+ copy[head] = navigate(copy[head], rest);
66
+ return copy;
67
+ }
68
+ if (typeof node === 'object' && node !== null) {
69
+ const obj = node;
70
+ return { ...obj, [head]: navigate(obj[head], rest) };
71
+ }
72
+ return node;
73
+ }
74
+ return navigate(root, parentPath);
75
+ }
76
+ function addChildAtPath(root, path) {
77
+ function getAt(node, p) {
78
+ if (p.length === 0)
79
+ return node;
80
+ const [head, ...rest] = p;
81
+ if (Array.isArray(node))
82
+ return getAt(node[head], rest);
83
+ if (typeof node === 'object' && node !== null) {
84
+ return getAt(node[head], rest);
85
+ }
86
+ return undefined;
87
+ }
88
+ const target = getAt(root, path);
89
+ if (Array.isArray(target)) {
90
+ return updateAtPath(root, path, [...target, '']);
91
+ }
92
+ if (typeof target === 'object' && target !== null) {
93
+ const obj = target;
94
+ let key = 'newKey';
95
+ let i = 1;
96
+ while (key in obj)
97
+ key = `newKey${i++}`;
98
+ return updateAtPath(root, path, { ...obj, [key]: '' });
99
+ }
100
+ return root;
101
+ }
102
+ function InlineEdit({ display, onCommit, colorClass, }) {
103
+ const [draft, setDraft] = useState(display);
104
+ const commit = () => onCommit(draft);
105
+ return (_jsx("input", { autoFocus: true, value: draft, onChange: (e) => setDraft(e.target.value), onBlur: commit, onKeyDown: (e) => {
106
+ if (e.key === 'Enter') {
107
+ e.preventDefault();
108
+ commit();
109
+ }
110
+ if (e.key === 'Escape')
111
+ onCommit(display);
112
+ }, onClick: (e) => e.stopPropagation(), className: colorClass, style: {
113
+ background: 'transparent',
114
+ border: 'none',
115
+ borderBottom: '1px solid currentColor',
116
+ color: 'inherit',
117
+ font: 'inherit',
118
+ padding: '0 2px',
119
+ outline: 'none',
120
+ minWidth: '4ch',
121
+ width: `${Math.max(draft.length + 2, 4)}ch`,
122
+ maxWidth: '500px',
123
+ } }));
124
+ }
125
+ function ActionBtn({ label, color, onClick, }) {
126
+ return (_jsx("button", { type: "button", onClick: onClick, style: {
127
+ marginLeft: 6,
128
+ padding: '0 5px',
129
+ fontSize: '10px',
130
+ lineHeight: '16px',
131
+ background: color,
132
+ color: '#fff',
133
+ border: 'none',
134
+ borderRadius: '3px',
135
+ cursor: 'pointer',
136
+ flexShrink: 0,
137
+ opacity: 0.85,
138
+ }, children: label }));
139
+ }
140
+ function JsonNode({ k, value, depth, forceOpen, path, unlocked, callbacks, }) {
141
+ const defaultOpen = forceOpen !== null ? forceOpen : depth < 2;
142
+ const [open, setOpen] = useState(defaultOpen);
143
+ const [editingValue, setEditingValue] = useState(false);
144
+ const [editingKey, setEditingKey] = useState(false);
145
+ const { onUpdate, onDelete, onRenameKey, onAddChild } = callbacks;
146
+ const del = unlocked ? (_jsx(ActionBtn, { label: "\u00D7", color: "#ef4444", onClick: (e) => { e.stopPropagation(); onDelete(path); } })) : null;
147
+ const keyEl = k === undefined ? null : (_jsxs(_Fragment, { children: [_jsxs("span", { className: "njv-key", children: ["\"", unlocked && editingKey ? (_jsx(InlineEdit, { display: k, colorClass: "njv-key", onCommit: (newKey) => {
148
+ setEditingKey(false);
149
+ if (newKey !== k)
150
+ onRenameKey(path, newKey);
151
+ } })) : (_jsx("span", { onClick: unlocked ? (e) => { e.stopPropagation(); setEditingKey(true); } : undefined, style: unlocked ? { cursor: 'text', borderBottom: '1px dotted currentColor' } : undefined, children: k })), "\""] }), _jsx("span", { className: "njv-brace", children: ": " })] }));
152
+ if (value === null) {
153
+ return (_jsxs("div", { className: "njv-line", style: { alignItems: 'center' }, children: [_jsxs("span", { className: "njv-code", style: { paddingLeft: depth * 20 }, children: [keyEl, _jsx("span", { className: "njv-null", title: unlocked ? 'Click to convert to empty string' : undefined, onClick: unlocked ? (e) => { e.stopPropagation(); onUpdate(path, ''); } : undefined, style: unlocked ? { cursor: 'pointer', borderBottom: '1px dotted currentColor' } : undefined, children: "null" })] }), del] }));
154
+ }
155
+ if (typeof value === 'string') {
156
+ return (_jsxs("div", { className: "njv-line", style: { alignItems: 'center' }, children: [_jsxs("span", { className: "njv-code", style: { paddingLeft: depth * 20 }, children: [keyEl, _jsxs("span", { className: "njv-string", children: ["\"", unlocked && editingValue ? (_jsx(InlineEdit, { display: value, colorClass: "njv-string", onCommit: (v) => {
157
+ setEditingValue(false);
158
+ onUpdate(path, v);
159
+ } })) : (_jsx("span", { onClick: unlocked ? (e) => { e.stopPropagation(); setEditingValue(true); } : undefined, style: unlocked ? { cursor: 'text', borderBottom: '1px dotted currentColor' } : undefined, children: value.length > 120 ? value.slice(0, 120) + '…' : value })), "\""] })] }), del] }));
160
+ }
161
+ if (typeof value === 'number') {
162
+ return (_jsxs("div", { className: "njv-line", style: { alignItems: 'center' }, children: [_jsxs("span", { className: "njv-code", style: { paddingLeft: depth * 20 }, children: [keyEl, _jsx("span", { className: "njv-number", children: unlocked && editingValue ? (_jsx(InlineEdit, { display: String(value), colorClass: "njv-number", onCommit: (v) => {
163
+ setEditingValue(false);
164
+ const n = Number(v);
165
+ onUpdate(path, Number.isNaN(n) ? v : n);
166
+ } })) : (_jsx("span", { onClick: unlocked ? (e) => { e.stopPropagation(); setEditingValue(true); } : undefined, style: unlocked ? { cursor: 'text', borderBottom: '1px dotted currentColor' } : undefined, children: value })) })] }), del] }));
167
+ }
168
+ if (typeof value === 'boolean') {
169
+ return (_jsxs("div", { className: "njv-line", style: { alignItems: 'center' }, children: [_jsxs("span", { className: "njv-code", style: { paddingLeft: depth * 20 }, children: [keyEl, _jsx("span", { className: "njv-boolean", onClick: unlocked ? (e) => { e.stopPropagation(); onUpdate(path, !value); } : undefined, title: unlocked ? 'Click to toggle' : undefined, style: unlocked ? { cursor: 'pointer', borderBottom: '1px dotted currentColor' } : undefined, children: String(value) })] }), del] }));
170
+ }
171
+ if (Array.isArray(value)) {
172
+ return (_jsxs(_Fragment, { children: [_jsxs("div", { className: "njv-line", style: { alignItems: 'center' }, children: [_jsxs("span", { className: "njv-code", style: { paddingLeft: depth * 20, cursor: 'pointer', userSelect: 'none' }, onClick: () => setOpen((o) => !o), children: [_jsx("span", { className: "njv-arrow", children: open ? '▼' : '▶' }), keyEl, _jsx("span", { className: "njv-brace", children: "[" }), !open && (_jsxs(_Fragment, { children: [_jsxs("span", { className: "njv-summary", children: [" ", value.length, " items "] }), _jsx("span", { className: "njv-brace", children: "]" })] }))] }), del] }), open && (_jsxs(_Fragment, { children: [value.map((item, i) => (_jsx(JsonNode, { value: item, depth: depth + 1, forceOpen: forceOpen, path: [...path, i], unlocked: unlocked, callbacks: callbacks }, i))), _jsxs("div", { className: "njv-line", style: { alignItems: 'center' }, children: [_jsx("span", { className: "njv-code", style: { paddingLeft: (depth + 1) * 20 }, children: _jsx("span", { className: "njv-brace", children: "]" }) }), unlocked && (_jsx(ActionBtn, { label: "+ item", color: "#22c55e", onClick: (e) => { e.stopPropagation(); onAddChild(path); } }))] })] }))] }));
173
+ }
174
+ if (typeof value === 'object') {
175
+ const entries = Object.entries(value);
176
+ return (_jsxs(_Fragment, { children: [_jsxs("div", { className: "njv-line", style: { alignItems: 'center' }, children: [_jsxs("span", { className: "njv-code", style: { paddingLeft: depth * 20, cursor: 'pointer', userSelect: 'none' }, onClick: () => setOpen((o) => !o), children: [_jsx("span", { className: "njv-arrow", children: open ? '▼' : '▶' }), keyEl, _jsx("span", { className: "njv-brace", children: '{' }), !open && (_jsxs(_Fragment, { children: [_jsxs("span", { className: "njv-summary", children: [" ", entries.length, " keys "] }), _jsx("span", { className: "njv-brace", children: '}' })] }))] }), del] }), open && (_jsxs(_Fragment, { children: [entries.map(([ek, ev]) => (_jsx(JsonNode, { k: ek, value: ev, depth: depth + 1, forceOpen: forceOpen, path: [...path, ek], unlocked: unlocked, callbacks: callbacks }, ek))), _jsxs("div", { className: "njv-line", style: { alignItems: 'center' }, children: [_jsx("span", { className: "njv-code", style: { paddingLeft: (depth + 1) * 20 }, children: _jsx("span", { className: "njv-brace", children: '}' }) }), unlocked && (_jsx(ActionBtn, { label: "+ key", color: "#22c55e", onClick: (e) => { e.stopPropagation(); onAddChild(path); } }))] })] }))] }));
177
+ }
178
+ return null;
179
+ }
180
+ const stylesId = 'nitrogen-json-viewer-styles';
181
+ function ensureStyles() {
182
+ if (typeof document === 'undefined')
183
+ return;
184
+ if (document.getElementById(stylesId))
185
+ return;
186
+ const style = document.createElement('style');
187
+ style.id = stylesId;
188
+ style.textContent = `
189
+ .njv-root {
190
+ --njv-bg: var(--theme-elevation-50, #f8f8f8);
191
+ --njv-border: var(--theme-elevation-150, #ddd);
192
+ --njv-toolbar-bg: var(--theme-elevation-100, #eee);
193
+ --njv-gutter-bg: var(--theme-elevation-50, #f0f0f0);
194
+ --njv-gutter-color: var(--theme-elevation-400, #999);
195
+ --njv-gutter-border: var(--theme-elevation-150, #ddd);
196
+ --njv-key: #0451a5;
197
+ --njv-string: #a31515;
198
+ --njv-number: #098658;
199
+ --njv-boolean: #0000ff;
200
+ --njv-null: #0000ff;
201
+ --njv-brace: var(--theme-elevation-800, #333);
202
+ --njv-arrow: var(--theme-elevation-500, #888);
203
+ --njv-summary: var(--theme-elevation-400, #888);
204
+
205
+ .collapsible__content { padding: 0; }
206
+ }
207
+
208
+ [data-theme="dark"] .njv-root {
209
+ --njv-key: #9cdcfe;
210
+ --njv-string: #ce9178;
211
+ --njv-number: #b5cea8;
212
+ --njv-boolean: #569cd6;
213
+ --njv-null: #569cd6;
214
+ }
215
+
216
+ @media (prefers-color-scheme: dark) {
217
+ .njv-root:not([data-theme="light"] .njv-root) {
218
+ --njv-key: #9cdcfe;
219
+ --njv-string: #ce9178;
220
+ --njv-number: #b5cea8;
221
+ --njv-boolean: #569cd6;
222
+ --njv-null: #569cd6;
223
+ }
224
+ }
225
+
226
+ .njv-key { color: var(--njv-key); }
227
+ .njv-string { color: var(--njv-string); word-break: break-all; white-space: normal; }
228
+ .njv-number { color: var(--njv-number); }
229
+ .njv-boolean { color: var(--njv-boolean); }
230
+ .njv-null { color: var(--njv-null); font-style: italic; }
231
+ .njv-brace { color: var(--njv-brace); }
232
+ .njv-arrow {
233
+ color: var(--njv-arrow);
234
+ display: inline-block;
235
+ width: 14px;
236
+ font-size: 9px;
237
+ margin-right: 4px;
238
+ text-align: center;
239
+ }
240
+ .njv-summary { color: var(--njv-summary); font-style: italic; }
241
+
242
+ .njv-body { counter-reset: njv-line; }
243
+
244
+ .njv-line {
245
+ display: flex;
246
+ align-items: baseline;
247
+ min-height: 24px;
248
+ line-height: 24px;
249
+ counter-increment: njv-line;
250
+ }
251
+
252
+ .njv-line::before {
253
+ content: counter(njv-line);
254
+ flex-shrink: 0;
255
+ width: 44px;
256
+ text-align: right;
257
+ padding-right: 12px;
258
+ color: var(--njv-gutter-color);
259
+ background: var(--njv-gutter-bg);
260
+ border-right: 1px solid var(--njv-gutter-border);
261
+ user-select: none;
262
+ position: sticky;
263
+ left: 0;
264
+ z-index: 1;
265
+ }
266
+
267
+ .njv-code {
268
+ padding-left: 12px;
269
+ white-space: nowrap;
270
+ flex: 1;
271
+ }
272
+
273
+ .njv-toolbar {
274
+ display: flex;
275
+ gap: 8px 12px;
276
+ padding: 0 12px;
277
+ border-bottom: 1px solid var(--njv-border);
278
+ background: var(--njv-toolbar-bg);
279
+ button { margin-block: 12px; }
280
+ }
281
+ `;
282
+ document.head.appendChild(style);
283
+ }
284
+ const NitrogenDataViewerRuntime = ({ path, field }) => {
285
+ const { value, setValue } = useField({ path });
286
+ const [copied, setCopied] = useState(false);
287
+ const [pasted, setPasted] = useState(false);
288
+ const [forceOpen, setForceOpen] = useState(null);
289
+ const [treeKey, setTreeKey] = useState(0);
290
+ const [unlocked, setUnlocked] = useState(false);
291
+ ensureStyles();
292
+ let parsed = [];
293
+ try {
294
+ parsed = typeof value === 'string' ? JSON.parse(value) : value;
295
+ }
296
+ catch {
297
+ parsed = [];
298
+ }
299
+ const jsonString = value != null ? JSON.stringify(value, null, 2) : '[]';
300
+ const moduleCount = Array.isArray(parsed) ? parsed.length : null;
301
+ const handleCopy = useCallback(() => {
302
+ navigator.clipboard.writeText(jsonString).then(() => {
303
+ setCopied(true);
304
+ setTimeout(() => setCopied(false), 2000);
305
+ });
306
+ }, [jsonString]);
307
+ const handlePaste = useCallback(() => {
308
+ navigator.clipboard.readText().then((text) => {
309
+ try {
310
+ const parsedValue = JSON.parse(text);
311
+ setValue(parsedValue);
312
+ setPasted(true);
313
+ setTimeout(() => setPasted(false), 2000);
314
+ }
315
+ catch {
316
+ // ignore invalid JSON
317
+ }
318
+ });
319
+ }, [setValue]);
320
+ const expandAll = useCallback(() => {
321
+ setForceOpen(true);
322
+ setTreeKey((k) => k + 1);
323
+ }, []);
324
+ const collapseAll = useCallback(() => {
325
+ setForceOpen(false);
326
+ setTreeKey((k) => k + 1);
327
+ }, []);
328
+ const callbacks = {
329
+ onUpdate: (p, v) => setValue(updateAtPath(parsed, p, v)),
330
+ onDelete: (p) => setValue(deleteAtPath(parsed, p)),
331
+ onRenameKey: (p, newKey) => setValue(renameKeyAtPath(parsed, p, newKey)),
332
+ onAddChild: (p) => setValue(addChildAtPath(parsed, p)),
333
+ };
334
+ const header = (_jsxs("span", { children: [typeof field.label === 'string' ? field.label : field.name, moduleCount !== null && (_jsxs("span", { style: { opacity: 0.5, fontWeight: 400, marginLeft: 8 }, children: ["(", moduleCount, " item", moduleCount !== 1 ? 's' : '', ")"] }))] }));
335
+ return (_jsxs("div", { className: "njv-root field-type", style: { marginBottom: '1.5rem' }, children: [_jsx(Collapsible, { header: header, initCollapsed: true, children: _jsxs("div", { style: { border: '1px solid var(--njv-border)', borderRadius: '0 0 4px 4px', overflow: 'hidden' }, children: [_jsxs("div", { className: "njv-toolbar", children: [_jsx("button", { type: "button", className: "btn btn--size-small btn--style-secondary", onClick: handleCopy, children: copied ? '✓ Copied' : 'Copy' }), unlocked && (_jsx("button", { type: "button", className: "btn btn--size-small btn--style-secondary", onClick: handlePaste, children: pasted ? '✓ Pasted' : 'Paste' })), _jsx("button", { type: "button", className: "btn btn--size-small btn--style-secondary", onClick: expandAll, children: "Expand All" }), _jsx("button", { type: "button", className: "btn btn--size-small btn--style-secondary", onClick: collapseAll, children: "Collapse All" }), _jsx("button", { type: "button", onClick: () => setUnlocked((u) => !u), style: {
336
+ marginBlock: '12px',
337
+ marginLeft: 'auto',
338
+ padding: '4px 12px',
339
+ fontSize: '12px',
340
+ background: unlocked ? '#ef4444' : '#6b7280',
341
+ color: '#fff',
342
+ border: 'none',
343
+ borderRadius: '4px',
344
+ cursor: 'pointer',
345
+ }, children: unlocked ? 'Lock' : 'Unlock Edit' })] }), _jsx("div", { className: "njv-body", style: {
346
+ maxHeight: '50vh',
347
+ overflow: 'auto',
348
+ fontFamily: 'var(--font-mono, "SF Mono", Menlo, Consolas, monospace)',
349
+ fontSize: 'var(--font-body-size, 13px)',
350
+ backgroundColor: 'var(--njv-bg)',
351
+ }, children: _jsx(JsonNode, { value: parsed ?? [], depth: 0, forceOpen: forceOpen, path: [], unlocked: unlocked, callbacks: callbacks }, treeKey) })] }) }), typeof field.admin?.description === 'string' && (_jsx("p", { style: { fontSize: '12px', color: '#888', marginTop: '4px' }, children: field.admin.description }))] }));
352
+ };
353
+ export default NitrogenDataViewerRuntime;
@@ -1,4 +1,3 @@
1
- import React from "react";
2
- export declare const NitrogenEditButton: React.FC<{
1
+ export declare const NitrogenEditButton: import("react").ComponentType<{
3
2
  collection: string;
4
3
  }>;
@@ -1,86 +1,6 @@
1
- "use client";
2
- import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { useState, useEffect } from "react";
4
- export const NitrogenEditButton = ({ collection }) => {
5
- const [id, setId] = useState(undefined);
6
- const [token, setToken] = useState(undefined);
7
- const [hasDevelopmentUrl, setHasDevelopmentUrl] = useState(false);
8
- const [slug, setSlug] = useState(undefined);
9
- const [frontendUrl, setFrontendUrl] = useState(undefined);
10
- const [instanceUrl, setInstanceUrl] = useState(undefined);
11
- const [authorId, setAuthorId] = useState(undefined);
12
- useEffect(() => {
13
- const segments = window.location.pathname.split("/").filter(Boolean);
14
- const docId = segments.length >= 4 ? segments[3] : undefined;
15
- if (docId) {
16
- setId(docId);
17
- // Fetch the document to get its slug (for View button)
18
- fetch(`/api/${collection}/${docId}`)
19
- .then((res) => res.json())
20
- .then((data) => {
21
- if (data.slug) {
22
- setSlug(data.slug);
23
- }
24
- })
25
- .catch((err) => {
26
- console.error("Failed to fetch document:", err);
27
- });
28
- }
29
- // Fetch the current user's ID for the authorId param
30
- fetch("/api/users/me")
31
- .then((res) => res.json())
32
- .then((data) => {
33
- if (data.user?.id) {
34
- setAuthorId(String(data.user.id));
35
- }
36
- })
37
- .catch((err) => {
38
- console.error("Failed to fetch current user:", err);
39
- });
40
- // Fetch the connector token and URLs from NitrogenSettings global
41
- fetch("/api/globals/nitrogen-settings")
42
- .then((res) => res.json())
43
- .then((data) => {
44
- if (data.connectorToken) {
45
- setToken(data.connectorToken);
46
- }
47
- if (data.developmentUrl) {
48
- setHasDevelopmentUrl(true);
49
- }
50
- if (data.instanceUrl) {
51
- setInstanceUrl(data.instanceUrl.replace(/\/$/, ""));
52
- }
53
- const url = data.frontendUrl;
54
- if (url) {
55
- setFrontendUrl(url.replace(/\/$/, ""));
56
- }
57
- })
58
- .catch((err) => {
59
- console.error("Failed to fetch nitrogen settings:", err);
60
- });
61
- }, [collection]);
62
- if (!id || !token || !authorId)
63
- return null;
64
- const param = collection === "nitrogen-templates" ? "templateId" : "pageId";
65
- const editorBase = instanceUrl ?? "";
66
- const baseHref = `${editorBase}/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&${param}=${id}&authorId=${encodeURIComponent(authorId)}`;
67
- const buttonStyle = {
68
- display: "inline-flex",
69
- alignItems: "center",
70
- gap: "8px",
71
- padding: "8px 16px",
72
- background: "#6366f1",
73
- color: "#fff",
74
- borderRadius: "4px",
75
- textDecoration: "none",
76
- fontSize: "14px",
77
- fontWeight: 500,
78
- };
79
- return (_jsxs("div", { style: { display: "flex", gap: "8px", alignItems: "center" }, children: [slug && frontendUrl && (_jsx("a", { href: `${frontendUrl}/${slug}`, target: "_blank", rel: "noopener noreferrer", style: {
80
- ...buttonStyle,
81
- background: "#10b981",
82
- }, children: "View Page" })), hasDevelopmentUrl ? (_jsxs(_Fragment, { children: [_jsx("a", { href: baseHref, target: "_blank", rel: "noopener noreferrer", style: buttonStyle, children: "Edit (Live)" }), _jsx("a", { href: `${baseHref}&development=true`, target: "_blank", rel: "noopener noreferrer", style: {
83
- ...buttonStyle,
84
- background: "#374151",
85
- }, children: "Edit (Dev)" })] })) : (_jsx("a", { href: baseHref, target: "_blank", rel: "noopener noreferrer", style: buttonStyle, children: "Edit with Nitrogen" }))] }));
86
- };
1
+ 'use client';
2
+ import dynamic from 'next/dynamic';
3
+ export const NitrogenEditButton = dynamic(() => import('./NitrogenEditButtonRuntime').then((module) => module.NitrogenEditButtonRuntime), {
4
+ loading: () => null,
5
+ ssr: false,
6
+ });
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ export declare const NitrogenEditButtonRuntime: React.FC<{
3
+ collection: string;
4
+ }>;
@@ -0,0 +1,75 @@
1
+ 'use client';
2
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useState, useEffect } from 'react';
4
+ import { Button } from '@payloadcms/ui';
5
+ import { nitrogenButtonGroupStyle, nitrogenEditDevButtonVars, nitrogenEditLiveButtonVars, nitrogenSecondaryButtonVars, nitrogenViewButtonVars, } from './nitrogenAdminButtonStyles';
6
+ export const NitrogenEditButtonRuntime = ({ collection }) => {
7
+ const [id, setId] = useState(undefined);
8
+ const [token, setToken] = useState(undefined);
9
+ const [hasDevelopmentUrl, setHasDevelopmentUrl] = useState(false);
10
+ const [slug, setSlug] = useState(undefined);
11
+ const [frontendUrl, setFrontendUrl] = useState(undefined);
12
+ const [instanceUrl, setInstanceUrl] = useState(undefined);
13
+ const [authorId, setAuthorId] = useState(undefined);
14
+ useEffect(() => {
15
+ const segments = window.location.pathname.split('/').filter(Boolean);
16
+ const docId = segments.length >= 4 ? segments[3] : undefined;
17
+ if (docId) {
18
+ setId(docId);
19
+ fetch(`/api/${collection}/${docId}`)
20
+ .then((res) => res.json())
21
+ .then((data) => {
22
+ if (data.slug) {
23
+ setSlug(data.slug);
24
+ }
25
+ })
26
+ .catch((err) => {
27
+ console.error('Failed to fetch document:', err);
28
+ });
29
+ }
30
+ fetch('/api/users/me')
31
+ .then((res) => res.json())
32
+ .then((data) => {
33
+ if (data.user?.id) {
34
+ setAuthorId(String(data.user.id));
35
+ }
36
+ })
37
+ .catch((err) => {
38
+ console.error('Failed to fetch current user:', err);
39
+ });
40
+ fetch('/api/globals/nitrogen-settings')
41
+ .then((res) => res.json())
42
+ .then((data) => {
43
+ if (data.connectorToken) {
44
+ setToken(data.connectorToken);
45
+ }
46
+ if (data.developmentUrl) {
47
+ setHasDevelopmentUrl(true);
48
+ }
49
+ if (data.instanceUrl) {
50
+ setInstanceUrl(data.instanceUrl.replace(/\/$/, ''));
51
+ }
52
+ const url = data.frontendUrl;
53
+ if (url) {
54
+ setFrontendUrl(url.replace(/\/$/, ''));
55
+ }
56
+ })
57
+ .catch((err) => {
58
+ console.error('Failed to fetch nitrogen settings:', err);
59
+ });
60
+ }, [collection]);
61
+ if (!id || !token || !authorId)
62
+ return null;
63
+ const param = collection === 'nitrogen-templates' ? 'templateId' : 'pageId';
64
+ const editorBase = instanceUrl ?? '';
65
+ const baseHref = `${editorBase}/nitrogen-editor?token=${encodeURIComponent(token)}&collection=${encodeURIComponent(collection)}&${param}=${id}&authorId=${encodeURIComponent(authorId)}`;
66
+ return (_jsxs("div", { style: nitrogenButtonGroupStyle, children: [slug && frontendUrl && (_jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: `${frontendUrl}/${slug}`, extraButtonProps: {
67
+ style: nitrogenViewButtonVars,
68
+ }, children: "View Page" })), hasDevelopmentUrl ? (_jsxs(_Fragment, { children: [_jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: baseHref, extraButtonProps: {
69
+ style: nitrogenEditLiveButtonVars,
70
+ }, children: "Edit (Live)" }), _jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: `${baseHref}&development=true`, extraButtonProps: {
71
+ style: nitrogenEditDevButtonVars,
72
+ }, children: "Edit (Dev)" })] })) : (_jsx(Button, { buttonStyle: "secondary", el: "anchor", margin: false, newTab: true, url: baseHref, extraButtonProps: {
73
+ style: nitrogenSecondaryButtonVars,
74
+ }, children: "Edit with Nitrogen" }))] }));
75
+ };
@@ -1,4 +1,3 @@
1
- import React from "react";
2
- export declare const NitrogenViewButton: React.FC<{
1
+ export declare const NitrogenViewButton: import("react").ComponentType<{
3
2
  collection: string;
4
3
  }>;
@@ -1,55 +1,6 @@
1
- "use client";
2
- import { jsx as _jsx } from "react/jsx-runtime";
3
- import { useState, useEffect } from "react";
4
- export const NitrogenViewButton = ({ collection }) => {
5
- const [slug, setSlug] = useState(undefined);
6
- const [frontendUrl, setFrontendUrl] = useState(undefined);
7
- useEffect(() => {
8
- // Get the document ID from the URL
9
- const segments = window.location.pathname.split("/").filter(Boolean);
10
- const docId = segments.length >= 4 ? segments[3] : undefined;
11
- if (!docId)
12
- return;
13
- // Fetch the document to get its slug
14
- fetch(`/api/${collection}/${docId}`)
15
- .then((res) => res.json())
16
- .then((data) => {
17
- if (data.slug) {
18
- setSlug(data.slug);
19
- }
20
- })
21
- .catch((err) => {
22
- console.error("Failed to fetch document:", err);
23
- });
24
- // Fetch nitrogen settings to get the frontend URL
25
- fetch("/api/globals/nitrogen-settings")
26
- .then((res) => res.json())
27
- .then((data) => {
28
- const url = data.developmentUrl || data.frontendUrl;
29
- if (url) {
30
- setFrontendUrl(url.replace(/\/$/, ""));
31
- }
32
- })
33
- .catch((err) => {
34
- console.error("Failed to fetch nitrogen settings:", err);
35
- });
36
- }, [collection]);
37
- console.log('[NitrogenViewButton] mounted', { slug, frontendUrl, collection });
38
- if (!slug || !frontendUrl) {
39
- console.log('[NitrogenViewButton] hidden — slug:', slug, 'frontendUrl:', frontendUrl);
40
- return _jsx("span", { style: { color: 'red', fontSize: '12px' }, children: "View button: waiting for data..." });
41
- }
42
- const href = `${frontendUrl}/${slug}`;
43
- return (_jsx("a", { href: href, target: "_blank", rel: "noopener noreferrer", style: {
44
- display: "inline-flex",
45
- alignItems: "center",
46
- gap: "8px",
47
- padding: "8px 16px",
48
- background: "#10b981",
49
- color: "#fff",
50
- borderRadius: "4px",
51
- textDecoration: "none",
52
- fontSize: "14px",
53
- fontWeight: 500,
54
- }, children: "View Page" }));
55
- };
1
+ 'use client';
2
+ import dynamic from 'next/dynamic';
3
+ export const NitrogenViewButton = dynamic(() => import('./NitrogenViewButtonRuntime').then((module) => module.NitrogenViewButtonRuntime), {
4
+ loading: () => null,
5
+ ssr: false,
6
+ });
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ export declare const NitrogenViewButtonRuntime: React.FC<{
3
+ collection: string;
4
+ }>;
@@ -0,0 +1,43 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { useState, useEffect } from 'react';
4
+ import { Button } from '@payloadcms/ui';
5
+ import { nitrogenViewButtonVars } from './nitrogenAdminButtonStyles';
6
+ export const NitrogenViewButtonRuntime = ({ collection }) => {
7
+ const [slug, setSlug] = useState(undefined);
8
+ const [frontendUrl, setFrontendUrl] = useState(undefined);
9
+ useEffect(() => {
10
+ const segments = window.location.pathname.split('/').filter(Boolean);
11
+ const docId = segments.length >= 4 ? segments[3] : undefined;
12
+ if (!docId)
13
+ return;
14
+ fetch(`/api/${collection}/${docId}`)
15
+ .then((res) => res.json())
16
+ .then((data) => {
17
+ if (data.slug) {
18
+ setSlug(data.slug);
19
+ }
20
+ })
21
+ .catch((err) => {
22
+ console.error('Failed to fetch document:', err);
23
+ });
24
+ fetch('/api/globals/nitrogen-settings')
25
+ .then((res) => res.json())
26
+ .then((data) => {
27
+ const url = data.developmentUrl || data.frontendUrl;
28
+ if (url) {
29
+ setFrontendUrl(url.replace(/\/$/, ''));
30
+ }
31
+ })
32
+ .catch((err) => {
33
+ console.error('Failed to fetch nitrogen settings:', err);
34
+ });
35
+ }, [collection]);
36
+ if (!slug || !frontendUrl) {
37
+ return null;
38
+ }
39
+ const href = `${frontendUrl}/${slug}`;
40
+ return (_jsx(Button, { buttonStyle: "primary", el: "anchor", margin: false, newTab: true, url: href, extraButtonProps: {
41
+ style: nitrogenViewButtonVars,
42
+ }, children: "View Page" }));
43
+ };