@suprsend/react-core
Version:
The react headless library for using SuprSend features like inbox, preferences etc
311 lines (310 loc) • 8.06 kB
JavaScript
import { SuprSend as T } from "@suprsend/web-sdk";
export * from "@suprsend/web-sdk";
import k, { createContext as y, useRef as x, useState as N, useEffect as S, useContext as d, useCallback as C } from "react";
const p = y({
suprsendClient: void 0,
authenticatedUser: void 0,
setAuthenticatedUser: void 0
});
function B({
publicApiKey: e,
distinctId: n,
userToken: o,
host: t,
vapidKey: r,
swFileName: s,
refreshUserToken: u,
children: m,
userAuthenticationHandler: l
}) {
const h = () => new T(e, {
host: t,
vapidKey: r,
swFileName: s
}), c = x(h()), [a, f] = N(null), A = async () => {
const i = c.current, M = i.distinctId, D = await F({
distinctId: n,
userToken: o,
refreshUserToken: u,
suprsendClient: i
});
f(i.isIdentified() ? n : null), (n || M && !n) && (l == null || l({
response: D,
authenticateUser: (E) => R({
...E,
suprsendClient: i,
setAuthenticatedUser: f
})
}));
};
return S(() => (setTimeout(() => {
c.current = h(), A();
}, 0), () => {
const i = c.current;
i != null && i.isIdentified() && (i == null || i.reset());
}), [n]), S(() => {
o && (c.current.userToken = o);
}, [o]), /* @__PURE__ */ k.createElement(p.Provider, { value: {
suprsendClient: c.current,
authenticatedUser: a,
setAuthenticatedUser: f
} }, m);
}
function g() {
const e = d(p);
if (e.suprsendClient === void 0)
throw new Error("useSuprSendClient must be used within a SuprSendProvider");
return e.suprsendClient;
}
async function F({
distinctId: e,
userToken: n,
refreshUserToken: o,
suprsendClient: t
}) {
let r;
return t.isIdentified() && (t == null ? void 0 : t.distinctId) != e ? (r = await (t == null ? void 0 : t.reset()), e && (r = await t.identify(e, n, {
refreshUserToken: o
}))) : r = await t.identify(e, n, {
refreshUserToken: o
}), r;
}
const R = async ({
distinctId: e,
userToken: n,
refreshUserToken: o,
suprsendClient: t,
setAuthenticatedUser: r
}) => {
const s = await F({
distinctId: e,
userToken: n,
refreshUserToken: o,
suprsendClient: t
});
return r && r(t.isIdentified() ? e : null), s;
};
function G() {
const e = g(), n = d(p);
return {
authenticatedUser: n.authenticatedUser,
authenticateUser: (o) => R({
...o,
suprsendClient: e,
setAuthenticatedUser: n.setAuthenticatedUser
})
};
}
const w = y(null);
function J({
tenantId: e,
stores: n,
host: o,
pageSize: t,
children: r
}) {
const s = g(), u = d(p), m = x(), [l, h] = N();
return S(() => {
const c = m.current;
if (c && c.remove(), !u.authenticatedUser) return;
m.current = s.feeds.initialize({
tenantId: e,
stores: n,
host: o,
pageSize: t
});
const a = m.current, f = a == null ? void 0 : a.data;
return h(f), a == null || a.emitter.on("feed.store_update", (A) => {
h(A);
}), a.initializeSocketConnection(), a.fetch(), () => a.remove();
}, [u.authenticatedUser]), /* @__PURE__ */ k.createElement(w.Provider, { value: {
feedClient: m.current,
feedData: l,
stores: n
} }, r);
}
function _() {
const e = d(w);
if (e === void 0)
throw new Error("useFeedClient must be used within a SuprSendProvider and SuprSendFeedProvider");
return e == null ? void 0 : e.feedClient;
}
function V() {
const e = d(w);
return e == null ? void 0 : e.feedData;
}
function K() {
const e = d(w);
if (e === void 0)
throw new Error("useFeed must be used within a SuprSendProvider and SuprSendFeedProvider");
return e;
}
const U = "en", P = y({
locale: U,
translations: {}
}), O = ({
children: e,
locale: n = U,
translations: o = {}
}) => /* @__PURE__ */ k.createElement(P.Provider, { value: {
locale: n,
translations: o
} }, e), b = {
locale: "fr",
translations: {
notifications: "Notifications",
markAllAsRead: "Tout marquer comme lu",
noNotificationsTitle: "Pas encore de notifications",
noNotificationsDescription: "Nous vous informerons lorsque nous aurons quelque chose de nouveau pour vous.",
pinned: "Épinglé",
markAsUnread: "Marquer comme non lu",
markAsRead: "Marquer comme lu",
archive: "Archiver",
expiresIn: "Expire dans",
minute: "minute",
minutes: "minutes",
hour: "heure",
hours: "heures",
day: "jour",
days: "jours",
week: "semaine",
weeks: "semaines",
month: "mois",
months: "mois",
year: "an",
years: "ans"
}
}, q = {
locale: "en",
translations: {
notifications: "Notifications",
markAllAsRead: "Mark all as read",
noNotificationsTitle: "No notifications yet",
noNotificationsDescription: "We'll let you know when we've got something new for you.",
pinned: "Pinned",
markAsUnread: "Mark as unread",
markAsRead: "Mark as read",
archive: "Archive",
expiresIn: "Expires in",
minute: "minute",
minutes: "minutes",
hour: "hour",
hours: "hours",
day: "day",
days: "days",
week: "week",
weeks: "weeks",
month: "month",
months: "months",
year: "year",
years: "years"
}
}, I = {
locale: "es",
translations: {
notifications: "Notificaciones",
markAllAsRead: "Marcar todo como leído",
noNotificationsTitle: "Aún no hay notificaciones",
noNotificationsDescription: "Te avisaremos cuando tengamos algo nuevo para ti.",
pinned: "Fijado",
markAsUnread: "Marcar como no leído",
markAsRead: "Marcar como leído",
archive: "Archivo",
expiresIn: "Vence en",
minute: "minuto",
minutes: "minutos",
hour: "hora",
hours: "horas",
day: "día",
days: "días",
week: "semana",
weeks: "semanas",
month: "mes",
months: "meses",
year: "año",
years: "años"
}
}, L = {
locale: "de",
translations: {
notifications: "Benachrichtigungen",
markAllAsRead: "Alle als gelesen markieren",
noNotificationsTitle: "Noch keine Benachrichtigungen",
noNotificationsDescription: "Wir informieren Sie, wenn wir etwas Neues für Sie haben.",
pinned: "Angepinnt",
markAsUnread: "Als ungelesen markieren",
markAsRead: "Als gelesen markieren",
archive: "Archiv",
expiresIn: "Läuft ab in",
minute: "Minute",
minutes: "Minuten",
hour: "Stunde",
hours: "Stunden",
day: "Tag",
days: "Tage",
week: "Woche",
weeks: "Wochen",
month: "Monat",
months: "Monate",
year: "Jahr",
years: "Jahre"
}
}, W = {
locale: "ar",
translations: {
notifications: "إشعارات",
markAllAsRead: "وضع علامة على الكل كمقروءة",
noNotificationsTitle: "لا توجد إخطارات حتى الآن",
noNotificationsDescription: "سنخبرك عندما نحصل على شيء جديد لك.",
pinned: "مثبت",
markAsUnread: "وضع علامة كغير مقروءة",
markAsRead: "وضع علامة كمقروءة",
archive: "أرشيف",
expiresIn: "تنتهي في",
minute: "دقيقة",
minutes: "دقائق",
hour: "ساعة",
hours: "ساعات",
day: "يوم",
days: "أيام",
week: "أسبوع",
weeks: "أسابيع",
month: "شهر",
months: "أشهر",
year: "سنة",
years: "سنوات"
}
}, v = {
fr: b,
en: q,
es: I,
de: L,
ar: W
}, Q = () => {
const {
locale: e,
translations: n
} = d(P);
return {
t: C((t) => {
var s, u;
const r = e && e in v ? e : U;
return (n == null ? void 0 : n[t]) || ((u = (s = v == null ? void 0 : v[r]) == null ? void 0 : s.translations) == null ? void 0 : u[t]) || t;
}, [e, n]),
locale: e
};
};
export {
U as DEFAULT_LANGUAGE,
P as I18nContext,
J as SuprSendFeedProvider,
O as SuprSendI18nProvider,
B as SuprSendProvider,
G as useAuthenticateUser,
K as useFeed,
_ as useFeedClient,
V as useFeedData,
g as useSuprSendClient,
Q as useTranslations
};
//# sourceMappingURL=index.js.map