opscms-meta-plugin 0.0.0-stage → 0.4.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/CHANGELOG.md +180 -0
- package/LICENSE +21 -0
- package/README.md +338 -2
- package/dist/chunk-HCQSD757.mjs +142 -0
- package/dist/chunk-QEVRQKSQ.mjs +26 -0
- package/dist/components/MetaCatalogSync/index.d.mts +10 -0
- package/dist/components/MetaCatalogSync/index.d.ts +10 -0
- package/dist/components/MetaCatalogSync/index.js +195 -0
- package/dist/components/MetaCatalogSync/index.mjs +108 -0
- package/dist/components/MetaConnectPanel/index.d.mts +20 -0
- package/dist/components/MetaConnectPanel/index.d.ts +20 -0
- package/dist/components/MetaConnectPanel/index.js +723 -0
- package/dist/components/MetaConnectPanel/index.mjs +554 -0
- package/dist/components/MetaLoginButton/index.d.mts +5 -0
- package/dist/components/MetaLoginButton/index.d.ts +5 -0
- package/dist/components/MetaLoginButton/index.js +82 -0
- package/dist/components/MetaLoginButton/index.mjs +58 -0
- package/dist/components/MetaPixelSelect/index.d.mts +11 -0
- package/dist/components/MetaPixelSelect/index.d.ts +11 -0
- package/dist/components/MetaPixelSelect/index.js +202 -0
- package/dist/components/MetaPixelSelect/index.mjs +115 -0
- package/dist/components/ThreadsConnectPanel/index.d.mts +10 -0
- package/dist/components/ThreadsConnectPanel/index.d.ts +10 -0
- package/dist/components/ThreadsConnectPanel/index.js +163 -0
- package/dist/components/ThreadsConnectPanel/index.mjs +56 -0
- package/dist/index.d.mts +376 -0
- package/dist/index.d.ts +376 -0
- package/dist/index.js +3561 -0
- package/dist/index.mjs +3501 -0
- package/icon.svg +1 -0
- package/package.json +109 -4
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import {
|
|
3
|
+
clearedMetaUserConnection
|
|
4
|
+
} from "../../chunk-QEVRQKSQ.mjs";
|
|
5
|
+
import {
|
|
6
|
+
ConnectButton,
|
|
7
|
+
DetailList,
|
|
8
|
+
EmptyState,
|
|
9
|
+
ErrorState,
|
|
10
|
+
FieldWrapper,
|
|
11
|
+
LoadingState,
|
|
12
|
+
MetaAppInfoBadge,
|
|
13
|
+
StyledSelect,
|
|
14
|
+
SuccessState,
|
|
15
|
+
openMetaOAuthPopup,
|
|
16
|
+
useMetaAppInfo,
|
|
17
|
+
useOAuthRedirectMessage
|
|
18
|
+
} from "../../chunk-HCQSD757.mjs";
|
|
19
|
+
|
|
20
|
+
// src/components/MetaConnectPanel/index.tsx
|
|
21
|
+
import { useCallback as useCallback2, useEffect as useEffect2, useRef as useRef2, useState } from "react";
|
|
22
|
+
import { useDocumentInfo as useDocumentInfo2, useForm as useForm2 } from "@payloadcms/ui";
|
|
23
|
+
|
|
24
|
+
// src/components/MetaConnectPanel/TenantDefaults.tsx
|
|
25
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
26
|
+
import { useAuth, useDocumentInfo, useForm, useFormFields } from "@payloadcms/ui";
|
|
27
|
+
function relationId(value) {
|
|
28
|
+
if (value == null || value === "") return null;
|
|
29
|
+
if (typeof value === "object") {
|
|
30
|
+
const id = value.id;
|
|
31
|
+
if (typeof id === "string" || typeof id === "number") return id;
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
var MetaTenantDefaults = () => {
|
|
38
|
+
const { user } = useAuth();
|
|
39
|
+
const { id } = useDocumentInfo();
|
|
40
|
+
const { dispatchFields } = useForm();
|
|
41
|
+
const siteValue = useFormFields(([fields]) => fields?.site?.value);
|
|
42
|
+
const organizationValue = useFormFields(([fields]) => fields?.organization?.value);
|
|
43
|
+
const seeded = useRef(false);
|
|
44
|
+
const lastSite = useRef(null);
|
|
45
|
+
const lastOrg = useRef(null);
|
|
46
|
+
const setSite = useCallback((value) => {
|
|
47
|
+
dispatchFields({ type: "UPDATE", path: "site", value });
|
|
48
|
+
}, [dispatchFields]);
|
|
49
|
+
const setOrganization = useCallback((value) => {
|
|
50
|
+
dispatchFields({ type: "UPDATE", path: "organization", value });
|
|
51
|
+
}, [dispatchFields]);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (id || seeded.current) return;
|
|
54
|
+
const account = user;
|
|
55
|
+
if (!account) return;
|
|
56
|
+
const ownSite = relationId(account.site);
|
|
57
|
+
const ownOrg = relationId(account.organization);
|
|
58
|
+
if (relationId(organizationValue) == null && ownOrg != null) setOrganization(ownOrg);
|
|
59
|
+
if (relationId(siteValue) == null && ownSite != null) {
|
|
60
|
+
setSite(ownSite);
|
|
61
|
+
seeded.current = true;
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (relationId(siteValue) != null || ownOrg == null || account.role === "super-admin") {
|
|
65
|
+
seeded.current = true;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
let cancelled = false;
|
|
69
|
+
fetch(`/api/sites?where[organization][equals]=${encodeURIComponent(String(ownOrg))}&limit=2&depth=0`).then((res) => res.json()).then((body) => {
|
|
70
|
+
if (cancelled) return;
|
|
71
|
+
const docs = body?.docs ?? [];
|
|
72
|
+
if (docs.length === 1 && docs[0]?.id != null) {
|
|
73
|
+
setSite(docs[0].id);
|
|
74
|
+
const siteOrg = relationId(docs[0].organization) ?? ownOrg;
|
|
75
|
+
setOrganization(siteOrg);
|
|
76
|
+
}
|
|
77
|
+
}).catch(() => {
|
|
78
|
+
}).finally(() => {
|
|
79
|
+
seeded.current = true;
|
|
80
|
+
});
|
|
81
|
+
return () => {
|
|
82
|
+
cancelled = true;
|
|
83
|
+
};
|
|
84
|
+
}, [id, user, siteValue, organizationValue, setSite, setOrganization]);
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
const siteId = relationId(siteValue);
|
|
87
|
+
if (siteId == null || lastSite.current === siteId) return;
|
|
88
|
+
lastSite.current = siteId;
|
|
89
|
+
let cancelled = false;
|
|
90
|
+
fetch(`/api/sites/${encodeURIComponent(String(siteId))}?depth=0`).then((res) => res.json()).then((doc) => {
|
|
91
|
+
const orgId = relationId(doc?.organization);
|
|
92
|
+
if (!cancelled && orgId != null) setOrganization(orgId);
|
|
93
|
+
}).catch(() => {
|
|
94
|
+
});
|
|
95
|
+
return () => {
|
|
96
|
+
cancelled = true;
|
|
97
|
+
};
|
|
98
|
+
}, [siteValue, setOrganization]);
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
const orgId = relationId(organizationValue);
|
|
101
|
+
if (orgId == null) return;
|
|
102
|
+
if (lastOrg.current == null) {
|
|
103
|
+
lastOrg.current = orgId;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (String(lastOrg.current) === String(orgId)) return;
|
|
107
|
+
lastOrg.current = orgId;
|
|
108
|
+
const siteId = relationId(siteValue);
|
|
109
|
+
if (siteId == null) return;
|
|
110
|
+
let cancelled = false;
|
|
111
|
+
fetch(`/api/sites/${encodeURIComponent(String(siteId))}?depth=0`).then((res) => res.json()).then((doc) => {
|
|
112
|
+
const siteOrg = relationId(doc?.organization);
|
|
113
|
+
if (cancelled || siteOrg == null || String(siteOrg) === String(orgId)) return;
|
|
114
|
+
lastSite.current = null;
|
|
115
|
+
dispatchFields({ type: "UPDATE", path: "site", value: null });
|
|
116
|
+
}).catch(() => {
|
|
117
|
+
});
|
|
118
|
+
return () => {
|
|
119
|
+
cancelled = true;
|
|
120
|
+
};
|
|
121
|
+
}, [organizationValue, siteValue, dispatchFields]);
|
|
122
|
+
return null;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// src/components/MetaConnectPanel/index.tsx
|
|
126
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
127
|
+
var stepStyle = { marginTop: "1.25rem" };
|
|
128
|
+
var stepTitle = { fontWeight: 600, marginBottom: "0.35rem" };
|
|
129
|
+
var hintStyle = { fontSize: "0.8125rem", lineHeight: 1.45, margin: "0 0 0.5rem" };
|
|
130
|
+
var rowStyle = { display: "flex", gap: "0.75rem", alignItems: "flex-end" };
|
|
131
|
+
var nameInputStyle = { width: "100%", padding: "0.5rem 0.6rem" };
|
|
132
|
+
var MetaConnectPanelField = () => {
|
|
133
|
+
const { id } = useDocumentInfo2();
|
|
134
|
+
const { dispatchFields } = useForm2();
|
|
135
|
+
const redirectMsg = useOAuthRedirectMessage("meta_oauth_success", "meta_oauth_error");
|
|
136
|
+
const appInfo = useMetaAppInfo();
|
|
137
|
+
const [doc, setDoc] = useState(null);
|
|
138
|
+
const [loadingDoc, setLoadingDoc] = useState(false);
|
|
139
|
+
const [pages, setPages] = useState(null);
|
|
140
|
+
const [loadingPages, setLoadingPages] = useState(false);
|
|
141
|
+
const [selectedPageId, setSelectedPageId] = useState("");
|
|
142
|
+
const [saving, setSaving] = useState(false);
|
|
143
|
+
const [actionError, setActionError] = useState(null);
|
|
144
|
+
const [facebookName, setFacebookName] = useState(null);
|
|
145
|
+
const [catalogs, setCatalogs] = useState(null);
|
|
146
|
+
const [businesses, setBusinesses] = useState([]);
|
|
147
|
+
const [pixels, setPixels] = useState(null);
|
|
148
|
+
const [numbers, setNumbers] = useState(null);
|
|
149
|
+
const [assetsError, setAssetsError] = useState(null);
|
|
150
|
+
const [selectedCatalogId, setSelectedCatalogId] = useState("");
|
|
151
|
+
const [selectedPixelId, setSelectedPixelId] = useState("");
|
|
152
|
+
const [selectedNumberId, setSelectedNumberId] = useState("");
|
|
153
|
+
const [newCatalogName, setNewCatalogName] = useState("");
|
|
154
|
+
const [newPixelName, setNewPixelName] = useState("");
|
|
155
|
+
const [createBusinessId, setCreateBusinessId] = useState("");
|
|
156
|
+
const [stepMessage, setStepMessage] = useState(null);
|
|
157
|
+
const [autoPageFailed, setAutoPageFailed] = useState(false);
|
|
158
|
+
const autoOnce = useRef2({ page: false, catalog: false, pixel: false, number: false });
|
|
159
|
+
const loadDoc = useCallback2(() => {
|
|
160
|
+
if (!id) return;
|
|
161
|
+
setLoadingDoc(true);
|
|
162
|
+
fetch(`/api/meta-config/${id}?depth=0`).then((res) => res.json()).then((data) => setDoc(data)).catch(() => {
|
|
163
|
+
}).finally(() => setLoadingDoc(false));
|
|
164
|
+
}, [id]);
|
|
165
|
+
const isConnected = doc?.authMethod === "oauth";
|
|
166
|
+
const hasPage = Boolean(doc?.facebookPageId);
|
|
167
|
+
useEffect2(() => {
|
|
168
|
+
loadDoc();
|
|
169
|
+
}, [loadDoc, redirectMsg.success]);
|
|
170
|
+
useEffect2(() => {
|
|
171
|
+
if (!id || !isConnected) return;
|
|
172
|
+
fetch(`/api/meta-oauth/me?configId=${id}`).then((res) => res.json()).then((data) => {
|
|
173
|
+
if (data.name) setFacebookName(data.name);
|
|
174
|
+
}).catch(() => {
|
|
175
|
+
});
|
|
176
|
+
}, [id, isConnected]);
|
|
177
|
+
const loadPages = useCallback2(() => {
|
|
178
|
+
if (!id) return;
|
|
179
|
+
setLoadingPages(true);
|
|
180
|
+
setActionError(null);
|
|
181
|
+
fetch(`/api/meta-oauth/pages?configId=${id}`).then((res) => res.json()).then((data) => {
|
|
182
|
+
if (data.error) {
|
|
183
|
+
setActionError(data.error);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
setPages(data.pages ?? []);
|
|
187
|
+
}).catch(() => setActionError("Failed to load Pages")).finally(() => setLoadingPages(false));
|
|
188
|
+
}, [id]);
|
|
189
|
+
useEffect2(() => {
|
|
190
|
+
if (!id || !isConnected || hasPage || pages !== null || loadingPages) return;
|
|
191
|
+
loadPages();
|
|
192
|
+
}, [id, isConnected, hasPage, pages, loadingPages, loadPages]);
|
|
193
|
+
const loadAssets = useCallback2(() => {
|
|
194
|
+
if (!id) return;
|
|
195
|
+
setAssetsError(null);
|
|
196
|
+
Promise.all([
|
|
197
|
+
fetch(`/api/meta-catalog/catalogs?configId=${id}`).then((res) => res.json()),
|
|
198
|
+
fetch(`/api/meta-oauth/pixels?configId=${id}`).then((res) => res.json()),
|
|
199
|
+
fetch(`/api/meta-whatsapp/numbers?configId=${id}`).then((res) => res.json())
|
|
200
|
+
]).then(([catalogData, pixelData, numberData]) => {
|
|
201
|
+
if (catalogData.error) setAssetsError(catalogData.error);
|
|
202
|
+
setCatalogs(catalogData.catalogs ?? []);
|
|
203
|
+
setBusinesses(catalogData.businesses ?? pixelData.businesses ?? []);
|
|
204
|
+
setPixels(pixelData.pixels ?? []);
|
|
205
|
+
setNumbers(numberData.numbers ?? []);
|
|
206
|
+
if (!catalogData.error && (pixelData.error || numberData.error)) {
|
|
207
|
+
setAssetsError(pixelData.error || numberData.error);
|
|
208
|
+
}
|
|
209
|
+
}).catch(() => setAssetsError("Could not load catalogs, Pixels, and WhatsApp numbers from this Facebook login."));
|
|
210
|
+
}, [id]);
|
|
211
|
+
useEffect2(() => {
|
|
212
|
+
if (!id || !hasPage) return;
|
|
213
|
+
loadAssets();
|
|
214
|
+
}, [id, hasPage, doc?.facebookPageId, loadAssets]);
|
|
215
|
+
const startConnect = () => {
|
|
216
|
+
if (!id) return;
|
|
217
|
+
setActionError(null);
|
|
218
|
+
openMetaOAuthPopup(`/api/meta-oauth/start?configId=${id}&popup=1`, (result) => {
|
|
219
|
+
if (result.error) setActionError(result.error);
|
|
220
|
+
loadDoc();
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
const disconnect = () => {
|
|
224
|
+
if (!id) return;
|
|
225
|
+
setSaving(true);
|
|
226
|
+
setActionError(null);
|
|
227
|
+
postJson("/api/meta-oauth/disconnect", { configId: id }).then((data) => {
|
|
228
|
+
if (data.error) {
|
|
229
|
+
setActionError(data.error);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const cleared = clearedMetaUserConnection();
|
|
233
|
+
for (const [path, value] of Object.entries(cleared)) {
|
|
234
|
+
dispatchFields({ type: "UPDATE", path, value: value ?? "" });
|
|
235
|
+
}
|
|
236
|
+
autoOnce.current = { page: false, catalog: false, pixel: false, number: false };
|
|
237
|
+
setAutoPageFailed(false);
|
|
238
|
+
setPages(null);
|
|
239
|
+
setCatalogs(null);
|
|
240
|
+
setPixels(null);
|
|
241
|
+
setNumbers(null);
|
|
242
|
+
setFacebookName(null);
|
|
243
|
+
setSelectedPageId("");
|
|
244
|
+
setSelectedCatalogId("");
|
|
245
|
+
setSelectedPixelId("");
|
|
246
|
+
setSelectedNumberId("");
|
|
247
|
+
loadDoc();
|
|
248
|
+
}).catch(() => setActionError("Failed to disconnect")).finally(() => setSaving(false));
|
|
249
|
+
};
|
|
250
|
+
const postJson = (url, body) => fetch(url, {
|
|
251
|
+
method: "POST",
|
|
252
|
+
headers: { "Content-Type": "application/json" },
|
|
253
|
+
body: JSON.stringify(body)
|
|
254
|
+
}).then((res) => res.json());
|
|
255
|
+
const selectPage = (pageId) => {
|
|
256
|
+
const chosen = pageId || selectedPageId;
|
|
257
|
+
if (!id || !chosen) return;
|
|
258
|
+
setSaving(true);
|
|
259
|
+
setActionError(null);
|
|
260
|
+
postJson("/api/meta-oauth/select-page", { configId: id, pageId: chosen }).then((data) => {
|
|
261
|
+
if (data.error) {
|
|
262
|
+
setActionError(data.error);
|
|
263
|
+
if (pageId) setAutoPageFailed(true);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
dispatchFields({ type: "UPDATE", path: "facebookPageId", value: data.facebookPageId });
|
|
267
|
+
dispatchFields({ type: "UPDATE", path: "facebookPageName", value: data.facebookPageName });
|
|
268
|
+
dispatchFields({ type: "UPDATE", path: "instagramBusinessAccountId", value: data.instagramBusinessAccountId || "" });
|
|
269
|
+
dispatchFields({ type: "UPDATE", path: "instagramUsername", value: data.instagramUsername || "" });
|
|
270
|
+
dispatchFields({ type: "UPDATE", path: "connectionStatus", value: "connected" });
|
|
271
|
+
dispatchFields({ type: "UPDATE", path: "authMethod", value: "oauth" });
|
|
272
|
+
loadDoc();
|
|
273
|
+
setPages(null);
|
|
274
|
+
}).catch(() => setActionError("Failed to select Page")).finally(() => setSaving(false));
|
|
275
|
+
};
|
|
276
|
+
const rememberCatalog = (catalogId, businessId) => {
|
|
277
|
+
dispatchFields({ type: "UPDATE", path: "catalogEnabled", value: true });
|
|
278
|
+
dispatchFields({ type: "UPDATE", path: "catalogId", value: catalogId });
|
|
279
|
+
dispatchFields({ type: "UPDATE", path: "catalogSourceCollection", value: "catalogue-items" });
|
|
280
|
+
if (businessId) dispatchFields({ type: "UPDATE", path: "businessManagerId", value: businessId });
|
|
281
|
+
};
|
|
282
|
+
const useCatalog = (catalogId) => {
|
|
283
|
+
const choice = catalogId || selectedCatalogId || doc?.catalogId || "";
|
|
284
|
+
const catalog = catalogs?.find((item) => item.id === choice);
|
|
285
|
+
if (!id || !catalog) return;
|
|
286
|
+
setSaving(true);
|
|
287
|
+
setStepMessage(null);
|
|
288
|
+
postJson("/api/meta-catalog/select", { configId: id, catalogId: catalog.id, businessId: catalog.businessId }).then((data) => {
|
|
289
|
+
if (data.error) {
|
|
290
|
+
setAssetsError(data.error);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
rememberCatalog(catalog.id, catalog.businessId);
|
|
294
|
+
setStepMessage(`Catalog \u201C${catalog.name}\u201D is the one this site syncs to.`);
|
|
295
|
+
loadDoc();
|
|
296
|
+
}).catch(() => setAssetsError("Failed to save the catalog")).finally(() => setSaving(false));
|
|
297
|
+
};
|
|
298
|
+
const createCatalog = () => {
|
|
299
|
+
if (!id || !newCatalogName.trim()) return;
|
|
300
|
+
setSaving(true);
|
|
301
|
+
setStepMessage(null);
|
|
302
|
+
postJson("/api/meta-catalog/create", {
|
|
303
|
+
configId: id,
|
|
304
|
+
name: newCatalogName.trim(),
|
|
305
|
+
businessId: createBusinessId || businesses[0]?.id
|
|
306
|
+
}).then((data) => {
|
|
307
|
+
if (data.error) {
|
|
308
|
+
setAssetsError(data.error);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
rememberCatalog(data.catalogId, data.businessId);
|
|
312
|
+
setNewCatalogName("");
|
|
313
|
+
setStepMessage(`Created catalog \u201C${data.name}\u201D and selected it for this site.`);
|
|
314
|
+
loadAssets();
|
|
315
|
+
loadDoc();
|
|
316
|
+
}).catch(() => setAssetsError("Failed to create the catalog")).finally(() => setSaving(false));
|
|
317
|
+
};
|
|
318
|
+
const rememberPixel = (pixelId, businessId) => {
|
|
319
|
+
dispatchFields({ type: "UPDATE", path: "pixelEnabled", value: true });
|
|
320
|
+
dispatchFields({ type: "UPDATE", path: "pixelId", value: pixelId });
|
|
321
|
+
if (businessId) dispatchFields({ type: "UPDATE", path: "businessManagerId", value: businessId });
|
|
322
|
+
};
|
|
323
|
+
const usePixel = (pixelId) => {
|
|
324
|
+
const choice = pixelId || selectedPixelId || doc?.pixelId || "";
|
|
325
|
+
const pixel = pixels?.find((item) => item.id === choice);
|
|
326
|
+
if (!id || !choice) return;
|
|
327
|
+
setSaving(true);
|
|
328
|
+
setStepMessage(null);
|
|
329
|
+
postJson("/api/meta-oauth/select-pixel", { configId: id, pixelId: choice }).then((data) => {
|
|
330
|
+
if (data.error) {
|
|
331
|
+
setAssetsError(data.error);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
rememberPixel(data.pixelId, pixel?.businessId);
|
|
335
|
+
setStepMessage(`Pixel \u201C${data.name}\u201D is the one this site uses.`);
|
|
336
|
+
loadDoc();
|
|
337
|
+
}).catch(() => setAssetsError("Failed to save the Pixel")).finally(() => setSaving(false));
|
|
338
|
+
};
|
|
339
|
+
const createPixel = () => {
|
|
340
|
+
if (!id || !newPixelName.trim()) return;
|
|
341
|
+
setSaving(true);
|
|
342
|
+
setStepMessage(null);
|
|
343
|
+
postJson("/api/meta-oauth/pixels", {
|
|
344
|
+
configId: id,
|
|
345
|
+
name: newPixelName.trim(),
|
|
346
|
+
businessId: createBusinessId || businesses[0]?.id
|
|
347
|
+
}).then((data) => {
|
|
348
|
+
if (data.error) {
|
|
349
|
+
setAssetsError(data.error);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
rememberPixel(data.pixelId, data.businessId);
|
|
353
|
+
setNewPixelName("");
|
|
354
|
+
setStepMessage(`Created Pixel \u201C${newPixelName.trim()}\u201D and selected it for this site.`);
|
|
355
|
+
loadAssets();
|
|
356
|
+
loadDoc();
|
|
357
|
+
}).catch(() => setAssetsError("Failed to create the Pixel")).finally(() => setSaving(false));
|
|
358
|
+
};
|
|
359
|
+
const useNumber = (phoneNumberId) => {
|
|
360
|
+
const choice = phoneNumberId || selectedNumberId || doc?.whatsappPhoneNumberId || "";
|
|
361
|
+
if (!id || !choice) return;
|
|
362
|
+
setSaving(true);
|
|
363
|
+
setStepMessage(null);
|
|
364
|
+
postJson("/api/meta-whatsapp/select", { configId: id, phoneNumberId: choice }).then((data) => {
|
|
365
|
+
if (data.error) {
|
|
366
|
+
setAssetsError(data.error);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
dispatchFields({ type: "UPDATE", path: "whatsappEnabled", value: true });
|
|
370
|
+
dispatchFields({ type: "UPDATE", path: "whatsappPhoneNumberId", value: data.phoneNumberId });
|
|
371
|
+
dispatchFields({ type: "UPDATE", path: "whatsappBusinessAccountId", value: data.wabaId });
|
|
372
|
+
loadDoc();
|
|
373
|
+
}).catch(() => setAssetsError("Failed to save the WhatsApp number")).finally(() => setSaving(false));
|
|
374
|
+
};
|
|
375
|
+
useEffect2(() => {
|
|
376
|
+
if (autoOnce.current.page || !isConnected || hasPage || !pages || pages.length !== 1) return;
|
|
377
|
+
autoOnce.current.page = true;
|
|
378
|
+
selectPage(pages[0].id);
|
|
379
|
+
}, [isConnected, hasPage, pages]);
|
|
380
|
+
useEffect2(() => {
|
|
381
|
+
if (autoOnce.current.catalog || !hasPage || doc?.catalogId || !catalogs || catalogs.length !== 1) return;
|
|
382
|
+
autoOnce.current.catalog = true;
|
|
383
|
+
useCatalog(catalogs[0].id);
|
|
384
|
+
}, [hasPage, doc?.catalogId, catalogs]);
|
|
385
|
+
useEffect2(() => {
|
|
386
|
+
if (autoOnce.current.pixel || !hasPage || doc?.pixelId || !pixels || pixels.length !== 1) return;
|
|
387
|
+
autoOnce.current.pixel = true;
|
|
388
|
+
usePixel(pixels[0].id);
|
|
389
|
+
}, [hasPage, doc?.pixelId, pixels]);
|
|
390
|
+
useEffect2(() => {
|
|
391
|
+
if (autoOnce.current.number || !hasPage || doc?.whatsappPhoneNumberId || !numbers || numbers.length !== 1) return;
|
|
392
|
+
autoOnce.current.number = true;
|
|
393
|
+
useNumber(numbers[0].id);
|
|
394
|
+
}, [hasPage, doc?.whatsappPhoneNumberId, numbers]);
|
|
395
|
+
if (!id) {
|
|
396
|
+
return /* @__PURE__ */ jsxs(FieldWrapper, { path: "metaConnectPanel", label: "Connect to Meta Business", children: [
|
|
397
|
+
/* @__PURE__ */ jsx(MetaAppInfoBadge, { appInfo }),
|
|
398
|
+
/* @__PURE__ */ jsx(EmptyState, { message: "Save the document first, then Connect will be available." })
|
|
399
|
+
] });
|
|
400
|
+
}
|
|
401
|
+
const pagePicker = pages === null ? /* @__PURE__ */ jsx(ConnectButton, { onClick: loadPages, disabled: loadingPages, children: loadingPages ? "Loading Pages\u2026" : "Load My Pages" }) : pages.length === 0 ? /* @__PURE__ */ jsx(EmptyState, { message: "No Pages found for this Facebook account. Make sure you admin at least one Facebook Page." }) : /* @__PURE__ */ jsxs("div", { style: rowStyle, children: [
|
|
402
|
+
/* @__PURE__ */ jsx("div", { style: { flex: 1 }, children: /* @__PURE__ */ jsx(
|
|
403
|
+
StyledSelect,
|
|
404
|
+
{
|
|
405
|
+
path: "metaConnectPanel_pageSelect",
|
|
406
|
+
value: selectedPageId,
|
|
407
|
+
options: pages.map((page) => ({ label: page.name, value: page.id })),
|
|
408
|
+
placeholder: "Select a Page",
|
|
409
|
+
onChange: setSelectedPageId
|
|
410
|
+
}
|
|
411
|
+
) }),
|
|
412
|
+
/* @__PURE__ */ jsx(ConnectButton, { onClick: () => selectPage(), disabled: !selectedPageId || saving, children: saving ? "Saving\u2026" : "Use This Page" })
|
|
413
|
+
] });
|
|
414
|
+
return /* @__PURE__ */ jsxs(FieldWrapper, { path: "metaConnectPanel", label: "Connect to Meta Business", children: [
|
|
415
|
+
/* @__PURE__ */ jsx(MetaAppInfoBadge, { appInfo }),
|
|
416
|
+
redirectMsg.error && /* @__PURE__ */ jsx(ErrorState, { message: redirectMsg.error }),
|
|
417
|
+
redirectMsg.success && !isConnected && /* @__PURE__ */ jsx(SuccessState, { message: "Connected. Loading the Pages on this Facebook login\u2026" }),
|
|
418
|
+
actionError && /* @__PURE__ */ jsx(ErrorState, { message: actionError }),
|
|
419
|
+
loadingDoc ? /* @__PURE__ */ jsx(LoadingState, { message: "Loading connection status\u2026" }) : !isConnected ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
420
|
+
/* @__PURE__ */ jsx(EmptyState, { message: "Log in with the Facebook account that manages this site. The Page, catalog, Pixel, and WhatsApp number fill in from that login. After that they stay read-only." }),
|
|
421
|
+
/* @__PURE__ */ jsx(ConnectButton, { onClick: startConnect, disabled: !appInfo.configured, children: "Log in with Facebook" })
|
|
422
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
423
|
+
/* @__PURE__ */ jsx(SuccessState, { message: facebookName ? `Signed in as ${facebookName}` : "Signed in with Facebook" }),
|
|
424
|
+
hasPage && /* @__PURE__ */ jsx(
|
|
425
|
+
DetailList,
|
|
426
|
+
{
|
|
427
|
+
items: [
|
|
428
|
+
{ label: "Facebook login", value: facebookName || "\u2014" },
|
|
429
|
+
{ label: "Facebook Page", value: `${doc?.facebookPageName} (${doc?.facebookPageId})` },
|
|
430
|
+
{
|
|
431
|
+
label: "Instagram",
|
|
432
|
+
value: doc?.instagramUsername ? `@${doc.instagramUsername}` : "No Instagram Business account linked to this Page"
|
|
433
|
+
},
|
|
434
|
+
{ label: "Catalog", value: doc?.catalogId || "Not chosen yet" },
|
|
435
|
+
{ label: "Pixel", value: doc?.pixelId || "Not chosen yet" },
|
|
436
|
+
{ label: "WhatsApp", value: doc?.whatsappEnabled && doc?.whatsappPhoneNumberId ? doc.whatsappPhoneNumberId : "Not chosen yet" }
|
|
437
|
+
]
|
|
438
|
+
}
|
|
439
|
+
),
|
|
440
|
+
!hasPage && /* @__PURE__ */ jsxs("div", { style: stepStyle, children: [
|
|
441
|
+
/* @__PURE__ */ jsx("div", { style: stepTitle, children: "1. Facebook Page" }),
|
|
442
|
+
pages?.length === 1 && !autoPageFailed ? /* @__PURE__ */ jsx(LoadingState, { message: "Saving the Page from this Facebook login\u2026" }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
443
|
+
/* @__PURE__ */ jsx("p", { style: hintStyle, children: "Pick the Page this site publishes as. Its Instagram account is detected from that Page. Once saved, it stays read-only." }),
|
|
444
|
+
pagePicker
|
|
445
|
+
] })
|
|
446
|
+
] }),
|
|
447
|
+
hasPage && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
448
|
+
assetsError && /* @__PURE__ */ jsx(ErrorState, { message: assetsError }),
|
|
449
|
+
stepMessage && /* @__PURE__ */ jsx(SuccessState, { message: stepMessage }),
|
|
450
|
+
catalogs === null ? /* @__PURE__ */ jsx(LoadingState, { message: "Loading catalogs, Pixels, and WhatsApp numbers\u2026" }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
451
|
+
!doc?.catalogId && /* @__PURE__ */ jsxs("div", { style: stepStyle, children: [
|
|
452
|
+
/* @__PURE__ */ jsx("div", { style: stepTitle, children: "2. Catalog" }),
|
|
453
|
+
/* @__PURE__ */ jsx("p", { style: hintStyle, children: "Commerce catalogue items sync to this catalog. Create one if this Facebook login does not have one yet." }),
|
|
454
|
+
businesses.length > 1 && /* @__PURE__ */ jsx("div", { style: { marginBottom: "0.75rem" }, children: /* @__PURE__ */ jsx(
|
|
455
|
+
StyledSelect,
|
|
456
|
+
{
|
|
457
|
+
path: "metaConnectPanel_business",
|
|
458
|
+
value: createBusinessId || businesses[0]?.id || "",
|
|
459
|
+
options: businesses.map((business) => ({ label: business.name, value: business.id })),
|
|
460
|
+
placeholder: "Business that will own a new catalog or Pixel",
|
|
461
|
+
onChange: setCreateBusinessId
|
|
462
|
+
}
|
|
463
|
+
) }),
|
|
464
|
+
catalogs.length > 0 && /* @__PURE__ */ jsxs("div", { style: { ...rowStyle, marginBottom: "0.75rem" }, children: [
|
|
465
|
+
/* @__PURE__ */ jsx("div", { style: { flex: 1 }, children: /* @__PURE__ */ jsx(
|
|
466
|
+
StyledSelect,
|
|
467
|
+
{
|
|
468
|
+
path: "metaConnectPanel_catalog",
|
|
469
|
+
value: selectedCatalogId || doc?.catalogId || "",
|
|
470
|
+
options: catalogs.map((catalog) => ({
|
|
471
|
+
label: `${catalog.name} (${catalog.businessName})`,
|
|
472
|
+
value: catalog.id
|
|
473
|
+
})),
|
|
474
|
+
placeholder: "Select a catalog",
|
|
475
|
+
onChange: setSelectedCatalogId
|
|
476
|
+
}
|
|
477
|
+
) }),
|
|
478
|
+
/* @__PURE__ */ jsx(ConnectButton, { onClick: () => useCatalog(), disabled: !(selectedCatalogId || doc?.catalogId) || saving, children: "Use this catalog" })
|
|
479
|
+
] }),
|
|
480
|
+
/* @__PURE__ */ jsxs("div", { style: rowStyle, children: [
|
|
481
|
+
/* @__PURE__ */ jsx(
|
|
482
|
+
"input",
|
|
483
|
+
{
|
|
484
|
+
style: nameInputStyle,
|
|
485
|
+
value: newCatalogName,
|
|
486
|
+
placeholder: "New catalog name",
|
|
487
|
+
onChange: (event) => setNewCatalogName(event.target.value)
|
|
488
|
+
}
|
|
489
|
+
),
|
|
490
|
+
/* @__PURE__ */ jsx(ConnectButton, { onClick: createCatalog, disabled: !newCatalogName.trim() || saving, children: saving ? "Saving\u2026" : "Create catalog" })
|
|
491
|
+
] })
|
|
492
|
+
] }),
|
|
493
|
+
!doc?.pixelId && /* @__PURE__ */ jsxs("div", { style: stepStyle, children: [
|
|
494
|
+
/* @__PURE__ */ jsx("div", { style: stepTitle, children: "3. Pixel" }),
|
|
495
|
+
/* @__PURE__ */ jsx("p", { style: hintStyle, children: "Used for the Conversions API. Create one if this login does not have a Pixel yet." }),
|
|
496
|
+
(pixels ?? []).length > 0 && /* @__PURE__ */ jsxs("div", { style: { ...rowStyle, marginBottom: "0.75rem" }, children: [
|
|
497
|
+
/* @__PURE__ */ jsx("div", { style: { flex: 1 }, children: /* @__PURE__ */ jsx(
|
|
498
|
+
StyledSelect,
|
|
499
|
+
{
|
|
500
|
+
path: "metaConnectPanel_pixel",
|
|
501
|
+
value: selectedPixelId || doc?.pixelId || "",
|
|
502
|
+
options: (pixels ?? []).map((pixel) => ({ label: `${pixel.name} (${pixel.id})`, value: pixel.id })),
|
|
503
|
+
placeholder: "Select a Pixel",
|
|
504
|
+
onChange: setSelectedPixelId
|
|
505
|
+
}
|
|
506
|
+
) }),
|
|
507
|
+
/* @__PURE__ */ jsx(ConnectButton, { onClick: () => usePixel(), disabled: !(selectedPixelId || doc?.pixelId) || saving, children: "Use this Pixel" })
|
|
508
|
+
] }),
|
|
509
|
+
/* @__PURE__ */ jsxs("div", { style: rowStyle, children: [
|
|
510
|
+
/* @__PURE__ */ jsx(
|
|
511
|
+
"input",
|
|
512
|
+
{
|
|
513
|
+
style: nameInputStyle,
|
|
514
|
+
value: newPixelName,
|
|
515
|
+
placeholder: "New Pixel name",
|
|
516
|
+
onChange: (event) => setNewPixelName(event.target.value)
|
|
517
|
+
}
|
|
518
|
+
),
|
|
519
|
+
/* @__PURE__ */ jsx(ConnectButton, { onClick: createPixel, disabled: !newPixelName.trim() || saving, children: saving ? "Saving\u2026" : "Create Pixel" })
|
|
520
|
+
] })
|
|
521
|
+
] }),
|
|
522
|
+
!doc?.whatsappPhoneNumberId && /* @__PURE__ */ jsxs("div", { style: stepStyle, children: [
|
|
523
|
+
/* @__PURE__ */ jsx("div", { style: stepTitle, children: "4. Official WhatsApp" }),
|
|
524
|
+
/* @__PURE__ */ jsx("p", { style: hintStyle, children: "Choose the number this site sends alerts from. Once saved, it stays read-only. Leave it unset to keep the existing WhatsApp bridge." }),
|
|
525
|
+
numbers === null ? /* @__PURE__ */ jsx(LoadingState, { message: "Looking up WhatsApp numbers." }) : numbers.length === 0 ? /* @__PURE__ */ jsx(EmptyState, { message: "No WhatsApp Business numbers on this Facebook login. Disconnect, then log in again, if this account should include WhatsApp." }) : /* @__PURE__ */ jsxs("div", { style: rowStyle, children: [
|
|
526
|
+
/* @__PURE__ */ jsx("div", { style: { flex: 1 }, children: /* @__PURE__ */ jsx(
|
|
527
|
+
StyledSelect,
|
|
528
|
+
{
|
|
529
|
+
path: "metaConnectPanel_whatsapp",
|
|
530
|
+
value: selectedNumberId || doc?.whatsappPhoneNumberId || "",
|
|
531
|
+
options: (numbers ?? []).map((number) => ({
|
|
532
|
+
label: `${number.displayPhone}${number.verifiedName ? ` \xB7 ${number.verifiedName}` : ""} (${number.wabaName})`,
|
|
533
|
+
value: number.id
|
|
534
|
+
})),
|
|
535
|
+
placeholder: "Select a WhatsApp number",
|
|
536
|
+
onChange: setSelectedNumberId
|
|
537
|
+
}
|
|
538
|
+
) }),
|
|
539
|
+
/* @__PURE__ */ jsx(ConnectButton, { onClick: () => useNumber(), disabled: !(selectedNumberId || doc?.whatsappPhoneNumberId) || saving, children: saving ? "Saving\u2026" : "Use for notifications" })
|
|
540
|
+
] })
|
|
541
|
+
] })
|
|
542
|
+
] })
|
|
543
|
+
] }),
|
|
544
|
+
/* @__PURE__ */ jsxs("div", { style: { ...stepStyle, display: "flex", flexDirection: "column", gap: "0.5rem", alignItems: "flex-start" }, children: [
|
|
545
|
+
/* @__PURE__ */ jsx("p", { style: hintStyle, children: "Disconnect removes this site\u2019s Facebook login and the Page, catalog, Pixel, and WhatsApp saved from it. You can log in again afterwards." }),
|
|
546
|
+
/* @__PURE__ */ jsx(ConnectButton, { onClick: disconnect, disabled: saving, children: saving ? "Disconnecting\u2026" : "Disconnect" })
|
|
547
|
+
] })
|
|
548
|
+
] })
|
|
549
|
+
] });
|
|
550
|
+
};
|
|
551
|
+
export {
|
|
552
|
+
MetaConnectPanelField,
|
|
553
|
+
MetaTenantDefaults
|
|
554
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/components/MetaLoginButton/index.tsx
|
|
22
|
+
var MetaLoginButton_exports = {};
|
|
23
|
+
__export(MetaLoginButton_exports, {
|
|
24
|
+
MetaLoginButton: () => MetaLoginButton,
|
|
25
|
+
default: () => MetaLoginButton_default
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(MetaLoginButton_exports);
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
30
|
+
var MESSAGES = {
|
|
31
|
+
denied: "Facebook sign-in was cancelled.",
|
|
32
|
+
state: "That sign-in expired or did not start here. Please try again.",
|
|
33
|
+
no_email: "Facebook did not share an email address, so we cannot tell which account is yours. Sign in with your email and password instead.",
|
|
34
|
+
no_account: "No account here uses that Facebook email. Accounts are made by invitation: ask your administrator to invite the email you use on Facebook.",
|
|
35
|
+
suspended: "This account has been suspended. Contact the platform administrator.",
|
|
36
|
+
not_allowed: "The platform owner account signs in with email and password only.",
|
|
37
|
+
two_factor: "This account uses two-factor sign-in. Sign in with your email, password and authenticator code instead.",
|
|
38
|
+
conflict: "This account is already connected to a different Facebook account.",
|
|
39
|
+
not_configured: "Facebook sign-in is not set up on this server.",
|
|
40
|
+
failed: "Facebook sign-in could not be completed. Please try again."
|
|
41
|
+
};
|
|
42
|
+
var MetaLoginButton = () => {
|
|
43
|
+
const [enabled, setEnabled] = (0, import_react.useState)(false);
|
|
44
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
45
|
+
(0, import_react.useEffect)(() => {
|
|
46
|
+
const code = new URLSearchParams(window.location.search).get("fb_error");
|
|
47
|
+
if (code) setError(MESSAGES[code] ?? MESSAGES.failed);
|
|
48
|
+
let cancelled = false;
|
|
49
|
+
fetch("/api/meta-login/status", { credentials: "same-origin" }).then((response) => response.ok ? response.json() : { enabled: false }).then((data) => {
|
|
50
|
+
if (!cancelled) setEnabled(data.enabled === true);
|
|
51
|
+
}).catch(() => void 0);
|
|
52
|
+
return () => {
|
|
53
|
+
cancelled = true;
|
|
54
|
+
};
|
|
55
|
+
}, []);
|
|
56
|
+
if (!enabled && !error) return null;
|
|
57
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { marginTop: "1.25rem", display: "grid", gap: "0.75rem" }, children: [
|
|
58
|
+
error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { role: "alert", style: { margin: 0, padding: "0.6rem 0.8rem", borderLeft: "3px solid var(--theme-error-500, #d33)", background: "var(--theme-error-100, rgba(221,51,51,0.1))" }, children: error }),
|
|
59
|
+
enabled && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
60
|
+
"a",
|
|
61
|
+
{
|
|
62
|
+
href: "/api/meta-login/start",
|
|
63
|
+
style: {
|
|
64
|
+
display: "block",
|
|
65
|
+
textAlign: "center",
|
|
66
|
+
padding: "0.65rem 1rem",
|
|
67
|
+
borderRadius: 4,
|
|
68
|
+
background: "#1877f2",
|
|
69
|
+
color: "#fff",
|
|
70
|
+
fontWeight: 600,
|
|
71
|
+
textDecoration: "none"
|
|
72
|
+
},
|
|
73
|
+
children: "Continue with Facebook"
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
] });
|
|
77
|
+
};
|
|
78
|
+
var MetaLoginButton_default = MetaLoginButton;
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
MetaLoginButton
|
|
82
|
+
});
|