admin-lib-cms 1.2.7 → 1.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-IXZXII3M.mjs +795 -0
- package/dist/client.d.mts +132 -0
- package/dist/client.d.ts +132 -0
- package/dist/client.js +725 -0
- package/dist/client.mjs +26 -0
- package/dist/index.d.mts +9 -113
- package/dist/index.d.ts +9 -113
- package/dist/index.js +1204 -440
- package/dist/index.mjs +1733 -1744
- package/dist/types.d.mts +5 -1
- package/dist/types.d.ts +5 -1
- package/package.json +9 -1
|
@@ -0,0 +1,795 @@
|
|
|
1
|
+
// src/subscription/SubscriptionProvider.tsx
|
|
2
|
+
import { createContext, useContext, useState, useCallback, useEffect } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
var SubscriptionContext = createContext(void 0);
|
|
5
|
+
function useSubscriptionContext() {
|
|
6
|
+
const ctx = useContext(SubscriptionContext);
|
|
7
|
+
if (!ctx) {
|
|
8
|
+
throw new Error("useSubscriptionContext must be used within a SubscriptionProvider");
|
|
9
|
+
}
|
|
10
|
+
return ctx;
|
|
11
|
+
}
|
|
12
|
+
function useSubscription() {
|
|
13
|
+
return useSubscriptionContext();
|
|
14
|
+
}
|
|
15
|
+
function SubscriptionProvider({
|
|
16
|
+
children,
|
|
17
|
+
apiUrl,
|
|
18
|
+
projectId,
|
|
19
|
+
email,
|
|
20
|
+
validateSubscriptionUrl
|
|
21
|
+
}) {
|
|
22
|
+
const [subscription, setSubscription] = useState(null);
|
|
23
|
+
const [loading, setLoading] = useState(true);
|
|
24
|
+
const [error, setError] = useState(null);
|
|
25
|
+
const fetchSubscription = useCallback(
|
|
26
|
+
async (silent = false) => {
|
|
27
|
+
if (!apiUrl || !projectId) {
|
|
28
|
+
setLoading(false);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (!silent) setLoading(true);
|
|
32
|
+
setError(null);
|
|
33
|
+
try {
|
|
34
|
+
const endpoint = validateSubscriptionUrl || `${apiUrl}/api/validate-subscription`;
|
|
35
|
+
const res = await fetch(endpoint, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: { "Content-Type": "application/json" },
|
|
38
|
+
body: JSON.stringify({ projectId })
|
|
39
|
+
});
|
|
40
|
+
if (!res.ok) throw new Error(`Server error: ${res.status}`);
|
|
41
|
+
const data = await res.json();
|
|
42
|
+
setSubscription({
|
|
43
|
+
subscriptionStatus: data.valid ? data.status : "inactive",
|
|
44
|
+
currentPeriodEnd: data.expiryDate,
|
|
45
|
+
customerEmail: email,
|
|
46
|
+
subscriptionId: data._id,
|
|
47
|
+
stripeCustomerId: data.stripeCustomerId,
|
|
48
|
+
stripePaymentLink: data.stripePaymentLink,
|
|
49
|
+
valid: data.valid
|
|
50
|
+
});
|
|
51
|
+
} catch (err) {
|
|
52
|
+
console.error("SubscriptionProvider Error:", err);
|
|
53
|
+
setError(err.message || "Failed to fetch subscription");
|
|
54
|
+
setSubscription({ subscriptionStatus: "active", valid: true });
|
|
55
|
+
} finally {
|
|
56
|
+
setLoading(false);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
[apiUrl, projectId, email]
|
|
60
|
+
);
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
fetchSubscription();
|
|
63
|
+
const handleFocus = () => {
|
|
64
|
+
fetchSubscription(true);
|
|
65
|
+
};
|
|
66
|
+
window.addEventListener("focus", handleFocus);
|
|
67
|
+
let intervalId;
|
|
68
|
+
if (subscription && !subscription.valid) {
|
|
69
|
+
intervalId = setInterval(() => {
|
|
70
|
+
fetchSubscription(true);
|
|
71
|
+
}, 1e4);
|
|
72
|
+
}
|
|
73
|
+
return () => {
|
|
74
|
+
window.removeEventListener("focus", handleFocus);
|
|
75
|
+
if (intervalId) clearInterval(intervalId);
|
|
76
|
+
};
|
|
77
|
+
}, [fetchSubscription, subscription?.valid]);
|
|
78
|
+
let daysOverdue = 0;
|
|
79
|
+
if (subscription && !subscription.valid && subscription.currentPeriodEnd) {
|
|
80
|
+
const diffTime = Date.now() - new Date(subscription.currentPeriodEnd).getTime();
|
|
81
|
+
if (diffTime > 0) {
|
|
82
|
+
daysOverdue = Math.floor(diffTime / (1e3 * 60 * 60 * 24));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const isSubscriptionActive = subscription ? subscription.valid || daysOverdue < 30 : true;
|
|
86
|
+
const value = {
|
|
87
|
+
subscription,
|
|
88
|
+
loading,
|
|
89
|
+
error,
|
|
90
|
+
refetch: fetchSubscription,
|
|
91
|
+
isSubscriptionActive,
|
|
92
|
+
daysOverdue,
|
|
93
|
+
config: { apiUrl, projectId }
|
|
94
|
+
};
|
|
95
|
+
return /* @__PURE__ */ jsx(SubscriptionContext.Provider, { value, children });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/subscription/SubscriptionPage.tsx
|
|
99
|
+
import { useState as useState2 } from "react";
|
|
100
|
+
import { CreditCard, ExternalLink, Settings, AlertTriangle, CheckCircle2 } from "lucide-react";
|
|
101
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
102
|
+
function SubscriptionPage() {
|
|
103
|
+
const { subscription, loading, config } = useSubscription();
|
|
104
|
+
const [actionLoading, setActionLoading] = useState2(null);
|
|
105
|
+
const status = subscription?.subscriptionStatus || "inactive";
|
|
106
|
+
const isValid = subscription?.valid ?? false;
|
|
107
|
+
const handleSubscribe = () => {
|
|
108
|
+
if (!subscription?.stripePaymentLink) {
|
|
109
|
+
alert("No payment link found. Please contact support.");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const raw = subscription.stripePaymentLink;
|
|
113
|
+
const url = raw.startsWith("http") ? raw : `https://buy.stripe.com/${raw}`;
|
|
114
|
+
window.open(url, "_blank");
|
|
115
|
+
};
|
|
116
|
+
const handleManageBilling = async () => {
|
|
117
|
+
if (!subscription?.stripeCustomerId) {
|
|
118
|
+
alert("No customer ID available. Please contact support.");
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
setActionLoading("billing");
|
|
122
|
+
try {
|
|
123
|
+
const returnUrl = window.location.href;
|
|
124
|
+
const apiUrl = config.apiUrl || "https://dev-soft-admin-sigma.vercel.app";
|
|
125
|
+
const res = await fetch(`${apiUrl}/api/create-billing-session`, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
headers: { "Content-Type": "application/json" },
|
|
128
|
+
body: JSON.stringify({
|
|
129
|
+
customerId: subscription.stripeCustomerId,
|
|
130
|
+
returnUrl
|
|
131
|
+
})
|
|
132
|
+
});
|
|
133
|
+
if (!res.ok) throw new Error(`Server error: ${res.status}`);
|
|
134
|
+
const data = await res.json();
|
|
135
|
+
if (data.url) {
|
|
136
|
+
window.open(data.url, "_blank");
|
|
137
|
+
} else {
|
|
138
|
+
alert("Could not create billing session.");
|
|
139
|
+
}
|
|
140
|
+
} catch (err) {
|
|
141
|
+
alert(`Portal failed: ${err.message}`);
|
|
142
|
+
} finally {
|
|
143
|
+
setActionLoading(null);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
if (loading && !subscription) {
|
|
147
|
+
return /* @__PURE__ */ jsx2("div", { className: "flex h-full min-h-[500px] w-full items-center justify-center bg-slate-50", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-3 text-slate-500", children: [
|
|
148
|
+
/* @__PURE__ */ jsx2("div", { className: "h-8 w-8 animate-spin rounded-full border-4 border-indigo-200 border-t-indigo-600" }),
|
|
149
|
+
/* @__PURE__ */ jsx2("p", { className: "text-sm font-medium", children: "Verifying subscription..." })
|
|
150
|
+
] }) });
|
|
151
|
+
}
|
|
152
|
+
return /* @__PURE__ */ jsx2("div", { className: "flex min-h-screen w-full flex-col items-center justify-center bg-slate-50 p-6", children: /* @__PURE__ */ jsxs("div", { className: "w-full max-w-md overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-xl", children: [
|
|
153
|
+
/* @__PURE__ */ jsxs("div", { className: `p-8 text-center ${isValid ? "bg-emerald-50" : "bg-rose-50"}`, children: [
|
|
154
|
+
/* @__PURE__ */ jsx2("div", { className: "mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-white shadow-sm", children: isValid ? /* @__PURE__ */ jsx2(CheckCircle2, { className: "h-8 w-8 text-emerald-500" }) : /* @__PURE__ */ jsx2(AlertTriangle, { className: "h-8 w-8 text-rose-500" }) }),
|
|
155
|
+
/* @__PURE__ */ jsx2("h2", { className: "text-2xl font-bold text-slate-900 tracking-tight", children: isValid ? "Subscription Active" : "Subscription Inactive" }),
|
|
156
|
+
/* @__PURE__ */ jsx2("p", { className: "mt-2 text-sm text-slate-600", children: isValid ? "Your subscription is currently active and in good standing." : "Your project requires an active subscription to access the administration panel." })
|
|
157
|
+
] }),
|
|
158
|
+
/* @__PURE__ */ jsxs("div", { className: "p-8", children: [
|
|
159
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
|
|
160
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between rounded-xl bg-slate-50 p-4 border border-slate-100", children: [
|
|
161
|
+
/* @__PURE__ */ jsx2("span", { className: "text-sm font-medium text-slate-500", children: "Status" }),
|
|
162
|
+
/* @__PURE__ */ jsx2("span", { className: `inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold capitalize ${isValid ? "bg-emerald-100 text-emerald-700" : "bg-rose-100 text-rose-700"}`, children: status })
|
|
163
|
+
] }),
|
|
164
|
+
subscription?.currentPeriodEnd && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between rounded-xl bg-slate-50 p-4 border border-slate-100", children: [
|
|
165
|
+
/* @__PURE__ */ jsx2("span", { className: "text-sm font-medium text-slate-500", children: "Next Billing Date" }),
|
|
166
|
+
/* @__PURE__ */ jsx2("span", { className: "text-sm font-semibold text-slate-900", children: new Date(subscription.currentPeriodEnd).toLocaleDateString() })
|
|
167
|
+
] })
|
|
168
|
+
] }),
|
|
169
|
+
/* @__PURE__ */ jsxs("div", { className: "mt-8 space-y-3", children: [
|
|
170
|
+
!isValid && /* @__PURE__ */ jsxs(
|
|
171
|
+
"button",
|
|
172
|
+
{
|
|
173
|
+
onClick: handleSubscribe,
|
|
174
|
+
className: "group flex w-full items-center justify-center gap-2 rounded-xl bg-indigo-600 px-4 py-3.5 text-sm font-semibold text-white shadow-sm transition-all hover:bg-indigo-700 hover:shadow",
|
|
175
|
+
children: [
|
|
176
|
+
/* @__PURE__ */ jsx2(CreditCard, { className: "h-4 w-4" }),
|
|
177
|
+
"Pay Now to Restore Access",
|
|
178
|
+
/* @__PURE__ */ jsx2(ExternalLink, { className: "h-3.5 w-3.5 opacity-70 transition-transform group-hover:translate-x-0.5" })
|
|
179
|
+
]
|
|
180
|
+
}
|
|
181
|
+
),
|
|
182
|
+
/* @__PURE__ */ jsxs(
|
|
183
|
+
"button",
|
|
184
|
+
{
|
|
185
|
+
onClick: handleManageBilling,
|
|
186
|
+
disabled: actionLoading === "billing",
|
|
187
|
+
className: "group flex w-full items-center justify-center gap-2 rounded-xl border border-slate-200 bg-white px-4 py-3.5 text-sm font-semibold text-slate-700 shadow-sm transition-all hover:bg-slate-50 hover:text-slate-900 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 disabled:opacity-50",
|
|
188
|
+
children: [
|
|
189
|
+
actionLoading === "billing" ? /* @__PURE__ */ jsx2("div", { className: "h-4 w-4 animate-spin rounded-full border-2 border-slate-300 border-t-slate-600" }) : /* @__PURE__ */ jsx2(Settings, { className: "h-4 w-4 text-slate-400 group-hover:text-slate-600" }),
|
|
190
|
+
"Manage Billing & Invoices"
|
|
191
|
+
]
|
|
192
|
+
}
|
|
193
|
+
)
|
|
194
|
+
] })
|
|
195
|
+
] })
|
|
196
|
+
] }) });
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// src/provider.tsx
|
|
200
|
+
import {
|
|
201
|
+
createContext as createContext2,
|
|
202
|
+
useCallback as useCallback2,
|
|
203
|
+
useContext as useContext2,
|
|
204
|
+
useEffect as useEffect2,
|
|
205
|
+
useMemo,
|
|
206
|
+
useRef,
|
|
207
|
+
useState as useState3
|
|
208
|
+
} from "react";
|
|
209
|
+
import { Fragment, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
210
|
+
var CmsContext = createContext2(null);
|
|
211
|
+
function useCmsContext() {
|
|
212
|
+
const ctx = useContext2(CmsContext);
|
|
213
|
+
if (!ctx) throw new Error("useCms must be used inside <CmsProvider>");
|
|
214
|
+
return ctx;
|
|
215
|
+
}
|
|
216
|
+
function useCmsOptional() {
|
|
217
|
+
return useContext2(CmsContext);
|
|
218
|
+
}
|
|
219
|
+
function CmsProvider(props) {
|
|
220
|
+
if (props.projectId) {
|
|
221
|
+
return /* @__PURE__ */ jsx3(
|
|
222
|
+
SubscriptionProvider,
|
|
223
|
+
{
|
|
224
|
+
apiUrl: props.apiUrl,
|
|
225
|
+
projectId: props.projectId,
|
|
226
|
+
email: props.email,
|
|
227
|
+
validateSubscriptionUrl: props.validateSubscriptionUrl,
|
|
228
|
+
children: /* @__PURE__ */ jsx3(CmsProviderInner, { ...props, children: /* @__PURE__ */ jsx3(SiteGuard, { children: props.children }) })
|
|
229
|
+
}
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
return /* @__PURE__ */ jsx3(CmsProviderInner, { ...props, children: props.children });
|
|
233
|
+
}
|
|
234
|
+
function SiteGuard({ children }) {
|
|
235
|
+
const { daysOverdue, isSubscriptionActive } = useSubscription();
|
|
236
|
+
if (!isSubscriptionActive && daysOverdue >= 60) {
|
|
237
|
+
return /* @__PURE__ */ jsx3("div", { style: { display: "flex", height: "100vh", width: "100%", alignItems: "center", justifyContent: "center", backgroundColor: "#f8fafc", color: "#0f172a", fontFamily: "system-ui, -apple-system, sans-serif" }, children: /* @__PURE__ */ jsxs2("div", { style: { textAlign: "center", padding: "2rem", maxWidth: "32rem" }, children: [
|
|
238
|
+
/* @__PURE__ */ jsx3("h1", { style: { fontSize: "1.5rem", fontWeight: 600, marginBottom: "0.5rem" }, children: "This website is temporarily unavailable" }),
|
|
239
|
+
/* @__PURE__ */ jsx3("p", { style: { color: "#64748b" }, children: "Please contact the site administrator if you believe this is an error." })
|
|
240
|
+
] }) });
|
|
241
|
+
}
|
|
242
|
+
return /* @__PURE__ */ jsx3(Fragment, { children });
|
|
243
|
+
}
|
|
244
|
+
function CmsProviderInner({ config, adapter, children, iconMap, initialOverrides }) {
|
|
245
|
+
const [overrides, setOverrides] = useState3(initialOverrides || {});
|
|
246
|
+
const [savedOverrides, setSavedOverrides] = useState3(initialOverrides || {});
|
|
247
|
+
const [selectedId, setSelectedIdRaw] = useState3(null);
|
|
248
|
+
const [editMode, setEditMode] = useState3(false);
|
|
249
|
+
const [collectionChangedAt, setCollectionChangedAt] = useState3(0);
|
|
250
|
+
const registryRef = useRef(/* @__PURE__ */ new Map());
|
|
251
|
+
const refCountRef = useRef(/* @__PURE__ */ new Map());
|
|
252
|
+
useEffect2(() => {
|
|
253
|
+
if (typeof window === "undefined" || typeof BroadcastChannel === "undefined") return;
|
|
254
|
+
const bc = new BroadcastChannel("admin-cms:collection-changed");
|
|
255
|
+
bc.onmessage = () => setCollectionChangedAt(Date.now());
|
|
256
|
+
return () => bc.close();
|
|
257
|
+
}, []);
|
|
258
|
+
const notifyCollectionChanged = useCallback2(() => {
|
|
259
|
+
setCollectionChangedAt(Date.now());
|
|
260
|
+
if (typeof BroadcastChannel !== "undefined") {
|
|
261
|
+
const bc = new BroadcastChannel("admin-cms:collection-changed");
|
|
262
|
+
bc.postMessage({ at: Date.now() });
|
|
263
|
+
bc.close();
|
|
264
|
+
}
|
|
265
|
+
}, []);
|
|
266
|
+
useEffect2(() => {
|
|
267
|
+
if (typeof window === "undefined") return;
|
|
268
|
+
const params = new URLSearchParams(window.location.search);
|
|
269
|
+
if (params.get("__cms") === "edit") setEditMode(true);
|
|
270
|
+
}, []);
|
|
271
|
+
useEffect2(() => {
|
|
272
|
+
let cancelled = false;
|
|
273
|
+
adapter.getOverrides().then((o) => {
|
|
274
|
+
if (cancelled) return;
|
|
275
|
+
setOverrides(o);
|
|
276
|
+
setSavedOverrides(o);
|
|
277
|
+
});
|
|
278
|
+
return () => {
|
|
279
|
+
cancelled = true;
|
|
280
|
+
};
|
|
281
|
+
}, [adapter]);
|
|
282
|
+
useEffect2(() => {
|
|
283
|
+
if (!editMode || typeof window === "undefined") return;
|
|
284
|
+
const parent = window.parent;
|
|
285
|
+
if (!parent || parent === window) return;
|
|
286
|
+
const send = (msg) => parent.postMessage({ __cms: true, ...msg }, "*");
|
|
287
|
+
send({ type: "cms:ready" });
|
|
288
|
+
const onMsg = (e) => {
|
|
289
|
+
const data = e.data;
|
|
290
|
+
if (!data || !data.__cms) return;
|
|
291
|
+
if (data.type === "cms:set-override") {
|
|
292
|
+
setOverrides((prev) => ({ ...prev, [data.id]: data.value }));
|
|
293
|
+
} else if (data.type === "cms:set-all-overrides") {
|
|
294
|
+
setOverrides(data.overrides);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
window.addEventListener("message", onMsg);
|
|
298
|
+
return () => window.removeEventListener("message", onMsg);
|
|
299
|
+
}, [editMode]);
|
|
300
|
+
const setSelectedId = useCallback2(
|
|
301
|
+
(id) => {
|
|
302
|
+
setSelectedIdRaw(id);
|
|
303
|
+
if (editMode && typeof window !== "undefined" && window.parent && window.parent !== window) {
|
|
304
|
+
window.parent.postMessage(
|
|
305
|
+
{ __cms: true, type: "cms:select", id },
|
|
306
|
+
"*"
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
[editMode]
|
|
311
|
+
);
|
|
312
|
+
const getSafeItems = useCallback2(() => {
|
|
313
|
+
try {
|
|
314
|
+
return JSON.parse(
|
|
315
|
+
JSON.stringify(Array.from(registryRef.current.values()), (_key, value2) => {
|
|
316
|
+
if (typeof value2 === "function") {
|
|
317
|
+
return value2.name || void 0;
|
|
318
|
+
}
|
|
319
|
+
return value2;
|
|
320
|
+
})
|
|
321
|
+
);
|
|
322
|
+
} catch {
|
|
323
|
+
return Array.from(registryRef.current.values());
|
|
324
|
+
}
|
|
325
|
+
}, []);
|
|
326
|
+
const registerEditable = useCallback2((meta) => {
|
|
327
|
+
refCountRef.current.set(meta.id, (refCountRef.current.get(meta.id) || 0) + 1);
|
|
328
|
+
registryRef.current.set(meta.id, meta);
|
|
329
|
+
if (typeof window !== "undefined" && window.parent && window.parent !== window) {
|
|
330
|
+
window.parent.postMessage(
|
|
331
|
+
{
|
|
332
|
+
__cms: true,
|
|
333
|
+
type: "cms:register",
|
|
334
|
+
items: getSafeItems()
|
|
335
|
+
},
|
|
336
|
+
"*"
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
}, [getSafeItems]);
|
|
340
|
+
const unregisterEditable = useCallback2((id) => {
|
|
341
|
+
const count = (refCountRef.current.get(id) || 1) - 1;
|
|
342
|
+
if (count <= 0) {
|
|
343
|
+
refCountRef.current.delete(id);
|
|
344
|
+
registryRef.current.delete(id);
|
|
345
|
+
} else {
|
|
346
|
+
refCountRef.current.set(id, count);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (typeof window !== "undefined" && window.parent && window.parent !== window) {
|
|
350
|
+
window.parent.postMessage(
|
|
351
|
+
{
|
|
352
|
+
__cms: true,
|
|
353
|
+
type: "cms:register",
|
|
354
|
+
items: getSafeItems()
|
|
355
|
+
},
|
|
356
|
+
"*"
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
}, [getSafeItems]);
|
|
360
|
+
const setOverride = useCallback2((id, value2) => {
|
|
361
|
+
setOverrides((prev) => ({ ...prev, [id]: value2 }));
|
|
362
|
+
}, []);
|
|
363
|
+
const save = useCallback2(async () => {
|
|
364
|
+
await adapter.setOverrides(overrides);
|
|
365
|
+
setSavedOverrides(overrides);
|
|
366
|
+
}, [adapter, overrides]);
|
|
367
|
+
const reset = useCallback2(
|
|
368
|
+
async (id) => {
|
|
369
|
+
if (id) {
|
|
370
|
+
setOverrides((prev) => {
|
|
371
|
+
const next = { ...prev };
|
|
372
|
+
delete next[id];
|
|
373
|
+
return next;
|
|
374
|
+
});
|
|
375
|
+
} else {
|
|
376
|
+
setOverrides({});
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
[]
|
|
380
|
+
);
|
|
381
|
+
const isDirty = useMemo(
|
|
382
|
+
() => JSON.stringify(overrides) !== JSON.stringify(savedOverrides),
|
|
383
|
+
[overrides, savedOverrides]
|
|
384
|
+
);
|
|
385
|
+
const getRegistered = useCallback2(
|
|
386
|
+
() => Array.from(registryRef.current.values()),
|
|
387
|
+
[]
|
|
388
|
+
);
|
|
389
|
+
const value = useMemo(() => ({
|
|
390
|
+
config,
|
|
391
|
+
adapter,
|
|
392
|
+
overrides,
|
|
393
|
+
editMode,
|
|
394
|
+
selectedId,
|
|
395
|
+
setSelectedId,
|
|
396
|
+
setOverride,
|
|
397
|
+
registerEditable,
|
|
398
|
+
unregisterEditable,
|
|
399
|
+
getRegistered,
|
|
400
|
+
save,
|
|
401
|
+
reset,
|
|
402
|
+
isDirty,
|
|
403
|
+
iconMap,
|
|
404
|
+
uploadImage: adapter.uploadImage?.bind(adapter),
|
|
405
|
+
collectionChangedAt,
|
|
406
|
+
notifyCollectionChanged
|
|
407
|
+
}), [config, adapter, overrides, editMode, selectedId, setSelectedId, setOverride, registerEditable, unregisterEditable, getRegistered, save, reset, isDirty, iconMap, collectionChangedAt, notifyCollectionChanged]);
|
|
408
|
+
return /* @__PURE__ */ jsx3(CmsContext.Provider, { value, children });
|
|
409
|
+
}
|
|
410
|
+
function useSubmit(formId) {
|
|
411
|
+
const ctx = useContext2(CmsContext);
|
|
412
|
+
return async (data) => {
|
|
413
|
+
const submission = {
|
|
414
|
+
id: crypto.randomUUID(),
|
|
415
|
+
formId,
|
|
416
|
+
submittedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
417
|
+
data,
|
|
418
|
+
read: false
|
|
419
|
+
};
|
|
420
|
+
if (ctx?.adapter) {
|
|
421
|
+
await ctx.adapter.saveSubmission(submission);
|
|
422
|
+
} else {
|
|
423
|
+
const KEY = "admin-cms:submissions";
|
|
424
|
+
const list = (() => {
|
|
425
|
+
try {
|
|
426
|
+
return JSON.parse(localStorage.getItem(KEY) ?? "[]");
|
|
427
|
+
} catch {
|
|
428
|
+
return [];
|
|
429
|
+
}
|
|
430
|
+
})();
|
|
431
|
+
list.unshift(submission);
|
|
432
|
+
localStorage.setItem(KEY, JSON.stringify(list));
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// src/editable.tsx
|
|
438
|
+
import { createElement, isValidElement, useEffect as useEffect3, useState as useState4, Fragment as Fragment2 } from "react";
|
|
439
|
+
import { Fragment as Fragment3, jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
440
|
+
function useRegister(id, type, label, section, pageId, defaultValue, itemSchema, fixedLength, search, collection, max, displayField, reorderable, sortable) {
|
|
441
|
+
const ctx = useCmsOptional();
|
|
442
|
+
useEffect3(() => {
|
|
443
|
+
if (!ctx) return;
|
|
444
|
+
ctx.registerEditable({ id, type, label, section, pageId, defaultValue, itemSchema, fixedLength, search, collection, max, displayField, reorderable, sortable });
|
|
445
|
+
return () => ctx.unregisterEditable(id);
|
|
446
|
+
}, [ctx?.registerEditable, ctx?.unregisterEditable, id, type, label, section, pageId, defaultValue, itemSchema, fixedLength, search, collection, max, displayField, reorderable, sortable]);
|
|
447
|
+
return ctx;
|
|
448
|
+
}
|
|
449
|
+
function editableClasses(active, selected) {
|
|
450
|
+
if (!active) return "";
|
|
451
|
+
return [
|
|
452
|
+
"outline-dashed outline-2 outline-offset-2 cursor-pointer transition-colors",
|
|
453
|
+
selected ? "outline-blue-500 bg-blue-500/5" : "outline-blue-400/60 hover:outline-blue-500"
|
|
454
|
+
].join(" ");
|
|
455
|
+
}
|
|
456
|
+
function EditableText({
|
|
457
|
+
id,
|
|
458
|
+
label,
|
|
459
|
+
section,
|
|
460
|
+
pageId,
|
|
461
|
+
children,
|
|
462
|
+
as = "span",
|
|
463
|
+
className,
|
|
464
|
+
type = "text",
|
|
465
|
+
lineClassNames,
|
|
466
|
+
renderLine
|
|
467
|
+
}) {
|
|
468
|
+
const ctx = useRegister(id, type, label, section, pageId, children);
|
|
469
|
+
const value = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? String(ctx.overrides[id] ?? "") : children;
|
|
470
|
+
const active = !!ctx?.editMode;
|
|
471
|
+
const selected = ctx?.selectedId === id;
|
|
472
|
+
const isEmpty = !value || String(value).trim() === "";
|
|
473
|
+
const renderValue = active && isEmpty ? `[Empty: ${label || id}]` : value;
|
|
474
|
+
const onClick = (e) => {
|
|
475
|
+
if (!active || !ctx) return;
|
|
476
|
+
e.preventDefault();
|
|
477
|
+
e.stopPropagation();
|
|
478
|
+
ctx.setSelectedId(id);
|
|
479
|
+
};
|
|
480
|
+
let finalChildren = renderValue;
|
|
481
|
+
if (typeof renderValue === "string" && renderValue.includes("\n")) {
|
|
482
|
+
finalChildren = renderValue.split("\n").map((line, i, arr) => {
|
|
483
|
+
if (renderLine) {
|
|
484
|
+
return /* @__PURE__ */ jsxs3(Fragment2, { children: [
|
|
485
|
+
renderLine(line, i),
|
|
486
|
+
i < arr.length - 1 && /* @__PURE__ */ jsx4("br", {})
|
|
487
|
+
] }, i);
|
|
488
|
+
}
|
|
489
|
+
const lineClass = lineClassNames?.[i];
|
|
490
|
+
return /* @__PURE__ */ jsxs3(Fragment2, { children: [
|
|
491
|
+
lineClass ? /* @__PURE__ */ jsx4("span", { className: lineClass, children: line }) : line,
|
|
492
|
+
i < arr.length - 1 && /* @__PURE__ */ jsx4("br", {})
|
|
493
|
+
] }, i);
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
return createElement(
|
|
497
|
+
as,
|
|
498
|
+
{
|
|
499
|
+
className: [
|
|
500
|
+
className,
|
|
501
|
+
editableClasses(active, selected),
|
|
502
|
+
active && isEmpty ? "opacity-50" : ""
|
|
503
|
+
].filter(Boolean).join(" "),
|
|
504
|
+
"data-cms-id": id,
|
|
505
|
+
onClick: active ? onClick : void 0
|
|
506
|
+
},
|
|
507
|
+
finalChildren
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
function EditableImage({
|
|
511
|
+
id,
|
|
512
|
+
label,
|
|
513
|
+
section,
|
|
514
|
+
pageId,
|
|
515
|
+
src,
|
|
516
|
+
alt,
|
|
517
|
+
className,
|
|
518
|
+
width,
|
|
519
|
+
height
|
|
520
|
+
}) {
|
|
521
|
+
const ctx = useRegister(id, "image", label, section, pageId, src);
|
|
522
|
+
const value = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? String(ctx.overrides[id] ?? src) : src;
|
|
523
|
+
const active = !!ctx?.editMode;
|
|
524
|
+
const selected = ctx?.selectedId === id;
|
|
525
|
+
const onClick = (e) => {
|
|
526
|
+
if (!active || !ctx) return;
|
|
527
|
+
e.preventDefault();
|
|
528
|
+
e.stopPropagation();
|
|
529
|
+
ctx.setSelectedId(id);
|
|
530
|
+
};
|
|
531
|
+
return /* @__PURE__ */ jsx4(
|
|
532
|
+
"img",
|
|
533
|
+
{
|
|
534
|
+
src: value || void 0,
|
|
535
|
+
alt,
|
|
536
|
+
width,
|
|
537
|
+
height,
|
|
538
|
+
"data-cms-id": id,
|
|
539
|
+
onClick: active ? onClick : void 0,
|
|
540
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" ")
|
|
541
|
+
}
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
function EditableFile({
|
|
545
|
+
id,
|
|
546
|
+
label,
|
|
547
|
+
section,
|
|
548
|
+
pageId,
|
|
549
|
+
src,
|
|
550
|
+
defaultUrl = "",
|
|
551
|
+
className,
|
|
552
|
+
children
|
|
553
|
+
}) {
|
|
554
|
+
const initialUrl = src || defaultUrl;
|
|
555
|
+
const ctx = useRegister(id, "file", label, section, pageId, initialUrl);
|
|
556
|
+
const value = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? String(ctx.overrides[id] ?? initialUrl) : initialUrl;
|
|
557
|
+
const active = !!ctx?.editMode;
|
|
558
|
+
const selected = ctx?.selectedId === id;
|
|
559
|
+
const onClick = (e) => {
|
|
560
|
+
if (!active || !ctx) return;
|
|
561
|
+
e.preventDefault();
|
|
562
|
+
e.stopPropagation();
|
|
563
|
+
ctx.setSelectedId(id);
|
|
564
|
+
};
|
|
565
|
+
if (children) {
|
|
566
|
+
return /* @__PURE__ */ jsx4(
|
|
567
|
+
"div",
|
|
568
|
+
{
|
|
569
|
+
"data-cms-id": id,
|
|
570
|
+
onClick: active ? onClick : void 0,
|
|
571
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" "),
|
|
572
|
+
children: children(value)
|
|
573
|
+
}
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
return /* @__PURE__ */ jsx4(
|
|
577
|
+
"div",
|
|
578
|
+
{
|
|
579
|
+
"data-cms-id": id,
|
|
580
|
+
onClick: active ? onClick : void 0,
|
|
581
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" "),
|
|
582
|
+
children: /* @__PURE__ */ jsx4(
|
|
583
|
+
"iframe",
|
|
584
|
+
{
|
|
585
|
+
src: value,
|
|
586
|
+
className: "w-full h-[500px] rounded-xl border-0",
|
|
587
|
+
title: label || id
|
|
588
|
+
}
|
|
589
|
+
)
|
|
590
|
+
}
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
function EditableLink({
|
|
594
|
+
id,
|
|
595
|
+
label,
|
|
596
|
+
section,
|
|
597
|
+
pageId,
|
|
598
|
+
href,
|
|
599
|
+
children,
|
|
600
|
+
text,
|
|
601
|
+
className,
|
|
602
|
+
target
|
|
603
|
+
}) {
|
|
604
|
+
let defaultText = text ?? "";
|
|
605
|
+
if (!defaultText) {
|
|
606
|
+
if (typeof children === "string") {
|
|
607
|
+
defaultText = children;
|
|
608
|
+
} else if (Array.isArray(children)) {
|
|
609
|
+
const strChild = children.find((c) => typeof c === "string");
|
|
610
|
+
if (typeof strChild === "string") {
|
|
611
|
+
defaultText = strChild.trim();
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
const ctx = useRegister(id, "link", label, section, pageId, { href, text: defaultText });
|
|
616
|
+
const override = ctx?.overrides[id];
|
|
617
|
+
const hrefValue = override?.href ?? href;
|
|
618
|
+
const textValue = override?.text ?? defaultText;
|
|
619
|
+
const active = !!ctx?.editMode;
|
|
620
|
+
const selected = ctx?.selectedId === id;
|
|
621
|
+
const onClick = (e) => {
|
|
622
|
+
if (!active || !ctx) return;
|
|
623
|
+
e.preventDefault();
|
|
624
|
+
e.stopPropagation();
|
|
625
|
+
ctx.setSelectedId(id);
|
|
626
|
+
};
|
|
627
|
+
const isExternal = hrefValue.startsWith("http");
|
|
628
|
+
const computedTarget = target || (isExternal ? "_blank" : void 0);
|
|
629
|
+
const computedRel = computedTarget === "_blank" ? "noopener noreferrer" : void 0;
|
|
630
|
+
let content;
|
|
631
|
+
if (typeof children === "function") {
|
|
632
|
+
content = children(textValue);
|
|
633
|
+
} else if (override && typeof override.text === "string" && override.text !== "") {
|
|
634
|
+
if (Array.isArray(children)) {
|
|
635
|
+
let replaced = false;
|
|
636
|
+
content = children.map((c) => {
|
|
637
|
+
if (typeof c === "string" && !replaced) {
|
|
638
|
+
replaced = true;
|
|
639
|
+
return override.text;
|
|
640
|
+
}
|
|
641
|
+
return c;
|
|
642
|
+
});
|
|
643
|
+
} else if (isValidElement(children)) {
|
|
644
|
+
content = /* @__PURE__ */ jsxs3(Fragment3, { children: [
|
|
645
|
+
override.text,
|
|
646
|
+
" ",
|
|
647
|
+
children
|
|
648
|
+
] });
|
|
649
|
+
} else {
|
|
650
|
+
content = override.text;
|
|
651
|
+
}
|
|
652
|
+
} else if (typeof children !== "undefined") {
|
|
653
|
+
content = children;
|
|
654
|
+
} else {
|
|
655
|
+
content = textValue;
|
|
656
|
+
}
|
|
657
|
+
const isEmpty = !content || typeof content === "string" && content.trim() === "";
|
|
658
|
+
const renderContent = active && isEmpty ? `[Empty Link: ${label || id}]` : content;
|
|
659
|
+
return /* @__PURE__ */ jsx4(
|
|
660
|
+
"a",
|
|
661
|
+
{
|
|
662
|
+
href: hrefValue,
|
|
663
|
+
target: computedTarget,
|
|
664
|
+
rel: computedRel,
|
|
665
|
+
"data-cms-id": id,
|
|
666
|
+
onClick: active ? onClick : void 0,
|
|
667
|
+
className: [
|
|
668
|
+
className,
|
|
669
|
+
editableClasses(active, selected),
|
|
670
|
+
active && isEmpty ? "opacity-50" : ""
|
|
671
|
+
].filter(Boolean).join(" "),
|
|
672
|
+
children: renderContent
|
|
673
|
+
}
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
function EditableList({
|
|
677
|
+
id,
|
|
678
|
+
label,
|
|
679
|
+
section,
|
|
680
|
+
pageId,
|
|
681
|
+
initialItems,
|
|
682
|
+
renderItem,
|
|
683
|
+
renderList,
|
|
684
|
+
itemSchema,
|
|
685
|
+
fixedLength,
|
|
686
|
+
className,
|
|
687
|
+
listClassName,
|
|
688
|
+
search,
|
|
689
|
+
reorderable,
|
|
690
|
+
sortable
|
|
691
|
+
}) {
|
|
692
|
+
const ctx = useRegister(id, "list", label, section, pageId, initialItems, itemSchema, fixedLength, search, void 0, void 0, void 0, reorderable, sortable);
|
|
693
|
+
const rawValue = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? ctx.overrides[id] : initialItems;
|
|
694
|
+
const rawList = Array.isArray(rawValue) ? rawValue : initialItems;
|
|
695
|
+
const value = Array.isArray(rawList) ? rawList.map((item, idx) => {
|
|
696
|
+
const def = Array.isArray(initialItems) ? initialItems[idx] : void 0;
|
|
697
|
+
if (def && typeof def === "object" && item && typeof item === "object") {
|
|
698
|
+
return { ...def, ...item };
|
|
699
|
+
}
|
|
700
|
+
return item;
|
|
701
|
+
}) : initialItems;
|
|
702
|
+
const active = !!ctx?.editMode;
|
|
703
|
+
const selected = ctx?.selectedId === id;
|
|
704
|
+
const onClick = (e) => {
|
|
705
|
+
if (!active || !ctx) return;
|
|
706
|
+
e.preventDefault();
|
|
707
|
+
e.stopPropagation();
|
|
708
|
+
ctx.setSelectedId(id);
|
|
709
|
+
};
|
|
710
|
+
return /* @__PURE__ */ jsxs3(
|
|
711
|
+
"div",
|
|
712
|
+
{
|
|
713
|
+
"data-cms-id": id,
|
|
714
|
+
onClick: active ? onClick : void 0,
|
|
715
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" "),
|
|
716
|
+
children: [
|
|
717
|
+
renderList ? renderList(value) : /* @__PURE__ */ jsx4("div", { className: listClassName, children: renderItem && value.map(renderItem) }),
|
|
718
|
+
active && value.length === 0 && /* @__PURE__ */ jsxs3("div", { style: { padding: "2rem", textAlign: "center", fontSize: "0.875rem", color: "#737373", backgroundColor: "rgba(245, 245, 245, 0.5)", border: "2px dashed #d4d4d4", borderRadius: "0.75rem", width: "100%", cursor: "pointer" }, children: [
|
|
719
|
+
"Empty List ",
|
|
720
|
+
label ? `(${label})` : "",
|
|
721
|
+
" \u2014 Click to add items"
|
|
722
|
+
] })
|
|
723
|
+
]
|
|
724
|
+
}
|
|
725
|
+
);
|
|
726
|
+
}
|
|
727
|
+
function EditableReference({
|
|
728
|
+
id,
|
|
729
|
+
label,
|
|
730
|
+
section,
|
|
731
|
+
pageId,
|
|
732
|
+
collection,
|
|
733
|
+
max,
|
|
734
|
+
displayField,
|
|
735
|
+
className,
|
|
736
|
+
children
|
|
737
|
+
}) {
|
|
738
|
+
const ctx = useRegister(id, "reference", label, section, pageId, max === 1 ? "" : [], void 0, void 0, void 0, collection, max, displayField);
|
|
739
|
+
const active = !!ctx?.editMode;
|
|
740
|
+
const selected = ctx?.selectedId === id;
|
|
741
|
+
const value = ctx && Object.prototype.hasOwnProperty.call(ctx.overrides, id) ? ctx.overrides[id] : void 0;
|
|
742
|
+
const [resolvedItems, setResolvedItems] = useState4([]);
|
|
743
|
+
useEffect3(() => {
|
|
744
|
+
if (!ctx) return;
|
|
745
|
+
const ids = Array.isArray(value) ? value : value ? [value] : [];
|
|
746
|
+
if (ids.length === 0) {
|
|
747
|
+
setResolvedItems([]);
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
ctx.adapter.listEntries(collection).then((res) => {
|
|
751
|
+
setResolvedItems(res.filter((r) => ids.includes(r.id)));
|
|
752
|
+
}).catch(() => {
|
|
753
|
+
});
|
|
754
|
+
}, [value, collection, ctx]);
|
|
755
|
+
const onClick = (e) => {
|
|
756
|
+
if (!active || !ctx) return;
|
|
757
|
+
e.preventDefault();
|
|
758
|
+
e.stopPropagation();
|
|
759
|
+
ctx.setSelectedId(id);
|
|
760
|
+
};
|
|
761
|
+
const childContent = typeof children === "function" ? children(resolvedItems) : children;
|
|
762
|
+
return /* @__PURE__ */ jsxs3(
|
|
763
|
+
"div",
|
|
764
|
+
{
|
|
765
|
+
"data-cms-id": id,
|
|
766
|
+
onClick: active ? onClick : void 0,
|
|
767
|
+
className: [className, editableClasses(active, selected)].filter(Boolean).join(" "),
|
|
768
|
+
children: [
|
|
769
|
+
childContent,
|
|
770
|
+
active && (!resolvedItems || resolvedItems.length === 0) && /* @__PURE__ */ jsxs3("div", { style: { padding: "2rem", textAlign: "center", fontSize: "0.875rem", color: "#737373", backgroundColor: "rgba(245, 245, 245, 0.5)", border: "2px dashed #d4d4d4", borderRadius: "0.75rem", width: "100%", cursor: "pointer" }, children: [
|
|
771
|
+
"Empty Reference ",
|
|
772
|
+
label ? `(${label})` : "",
|
|
773
|
+
" \u2014 Click to select"
|
|
774
|
+
] })
|
|
775
|
+
]
|
|
776
|
+
}
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
export {
|
|
781
|
+
useSubscriptionContext,
|
|
782
|
+
useSubscription,
|
|
783
|
+
SubscriptionProvider,
|
|
784
|
+
SubscriptionPage,
|
|
785
|
+
useCmsContext,
|
|
786
|
+
useCmsOptional,
|
|
787
|
+
CmsProvider,
|
|
788
|
+
useSubmit,
|
|
789
|
+
EditableText,
|
|
790
|
+
EditableImage,
|
|
791
|
+
EditableFile,
|
|
792
|
+
EditableLink,
|
|
793
|
+
EditableList,
|
|
794
|
+
EditableReference
|
|
795
|
+
};
|