@emplorium/sdk 0.1.0

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,85 @@
1
+ import {
2
+ EmploriumClient
3
+ } from "../chunk-DYG422DG.mjs";
4
+
5
+ // src/react/EmploriumProvider.tsx
6
+ import { useEffect, useMemo, useRef } from "react";
7
+
8
+ // src/react/context.ts
9
+ import React from "react";
10
+ var EmploriumContext = React.createContext(null);
11
+
12
+ // src/react/EmploriumProvider.tsx
13
+ import { jsx } from "react/jsx-runtime";
14
+ var EmploriumProvider = ({ config, children, autoOpen }) => {
15
+ var _a;
16
+ const clientRef = useRef(null);
17
+ const accountIdRef = useRef(null);
18
+ const mergedConfig = useMemo(
19
+ () => {
20
+ var _a2;
21
+ return {
22
+ ...config,
23
+ autoOpen: (_a2 = config.autoOpen) != null ? _a2 : autoOpen
24
+ };
25
+ },
26
+ [config, autoOpen]
27
+ );
28
+ if (!clientRef.current || accountIdRef.current !== mergedConfig.accountId) {
29
+ (_a = clientRef.current) == null ? void 0 : _a.destroy();
30
+ clientRef.current = new EmploriumClient(mergedConfig);
31
+ accountIdRef.current = mergedConfig.accountId;
32
+ }
33
+ useEffect(() => {
34
+ const client = clientRef.current;
35
+ if (!client)
36
+ return;
37
+ client.init().then(() => {
38
+ if (mergedConfig.autoOpen) {
39
+ client.open();
40
+ }
41
+ });
42
+ return () => client.destroy();
43
+ }, [mergedConfig]);
44
+ return /* @__PURE__ */ jsx(EmploriumContext.Provider, { value: clientRef.current, children });
45
+ };
46
+
47
+ // src/react/EmploriumWidget.tsx
48
+ import { useEffect as useEffect2 } from "react";
49
+
50
+ // src/react/useEmplorium.ts
51
+ import { useContext } from "react";
52
+ var useEmplorium = () => {
53
+ const client = useContext(EmploriumContext);
54
+ if (!client) {
55
+ throw new Error("useEmplorium must be used within an EmploriumProvider");
56
+ }
57
+ return client;
58
+ };
59
+
60
+ // src/react/EmploriumWidget.tsx
61
+ var EmploriumWidget = ({ autoOpen, onReady }) => {
62
+ const client = useEmplorium();
63
+ useEffect2(() => {
64
+ let active = true;
65
+ client.ready().then(() => {
66
+ if (!active)
67
+ return;
68
+ if (autoOpen) {
69
+ client.open();
70
+ }
71
+ onReady == null ? void 0 : onReady();
72
+ }).catch(() => {
73
+ });
74
+ return () => {
75
+ active = false;
76
+ };
77
+ }, [autoOpen, client, onReady]);
78
+ return null;
79
+ };
80
+ export {
81
+ EmploriumProvider,
82
+ EmploriumWidget,
83
+ useEmplorium
84
+ };
85
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/react/EmploriumProvider.tsx","../../src/react/context.ts","../../src/react/EmploriumWidget.tsx","../../src/react/useEmplorium.ts"],"sourcesContent":["import React, { ReactNode, useEffect, useMemo, useRef } from \"react\";\n\nimport { EmploriumClient } from \"../client\";\nimport { EmploriumConfig } from \"../types\";\nimport { EmploriumContext } from \"./context\";\n\ntype ProviderProps = {\n config: EmploriumConfig;\n children?: ReactNode;\n /**\n * If true, the widget will open once ready.\n */\n autoOpen?: boolean;\n};\n\nexport const EmploriumProvider: React.FC<ProviderProps> = ({ config, children, autoOpen }) => {\n const clientRef = useRef<EmploriumClient | null>(null);\n const accountIdRef = useRef<string | null>(null);\n\n const mergedConfig = useMemo(\n () => ({\n ...config,\n autoOpen: config.autoOpen ?? autoOpen,\n }),\n [config, autoOpen]\n );\n\n if (!clientRef.current || accountIdRef.current !== mergedConfig.accountId) {\n clientRef.current?.destroy();\n clientRef.current = new EmploriumClient(mergedConfig);\n accountIdRef.current = mergedConfig.accountId;\n }\n\n useEffect(() => {\n const client = clientRef.current;\n if (!client) return;\n client.init().then(() => {\n if (mergedConfig.autoOpen) {\n client.open();\n }\n });\n\n return () => client.destroy();\n }, [mergedConfig]);\n\n return (\n <EmploriumContext.Provider value={clientRef.current}>{children}</EmploriumContext.Provider>\n );\n};\n","import React from \"react\";\nimport { EmploriumClient } from \"../client\";\n\nexport const EmploriumContext = React.createContext<EmploriumClient | null>(null);\n","import { useEffect } from \"react\";\n\nimport { useEmplorium } from \"./useEmplorium\";\n\ntype WidgetProps = {\n /**\n * Open the widget when ready.\n */\n autoOpen?: boolean;\n /**\n * Callback when the widget signals readiness.\n */\n onReady?: () => void;\n};\n\nexport const EmploriumWidget: React.FC<WidgetProps> = ({ autoOpen, onReady }) => {\n const client = useEmplorium();\n\n useEffect(() => {\n let active = true;\n client\n .ready()\n .then(() => {\n if (!active) return;\n if (autoOpen) {\n client.open();\n }\n onReady?.();\n })\n .catch(() => {\n /* errors are surfaced through the client's event bus */\n });\n\n return () => {\n active = false;\n };\n }, [autoOpen, client, onReady]);\n\n return null;\n};\n","import { useContext } from \"react\";\n\nimport { EmploriumContext } from \"./context\";\n\nexport const useEmplorium = () => {\n const client = useContext(EmploriumContext);\n if (!client) {\n throw new Error(\"useEmplorium must be used within an EmploriumProvider\");\n }\n return client;\n};\n"],"mappings":";;;;;AAAA,SAA2B,WAAW,SAAS,cAAc;;;ACA7D,OAAO,WAAW;AAGX,IAAM,mBAAmB,MAAM,cAAsC,IAAI;;;AD2C5E;AA/BG,IAAM,oBAA6C,CAAC,EAAE,QAAQ,UAAU,SAAS,MAAM;AAf9F;AAgBE,QAAM,YAAY,OAA+B,IAAI;AACrD,QAAM,eAAe,OAAsB,IAAI;AAE/C,QAAM,eAAe;AAAA,IACnB,MAAG;AApBP,UAAAA;AAoBW;AAAA,QACL,GAAG;AAAA,QACH,WAAUA,MAAA,OAAO,aAAP,OAAAA,MAAmB;AAAA,MAC/B;AAAA;AAAA,IACA,CAAC,QAAQ,QAAQ;AAAA,EACnB;AAEA,MAAI,CAAC,UAAU,WAAW,aAAa,YAAY,aAAa,WAAW;AACzE,oBAAU,YAAV,mBAAmB;AACnB,cAAU,UAAU,IAAI,gBAAgB,YAAY;AACpD,iBAAa,UAAU,aAAa;AAAA,EACtC;AAEA,YAAU,MAAM;AACd,UAAM,SAAS,UAAU;AACzB,QAAI,CAAC;AAAQ;AACb,WAAO,KAAK,EAAE,KAAK,MAAM;AACvB,UAAI,aAAa,UAAU;AACzB,eAAO,KAAK;AAAA,MACd;AAAA,IACF,CAAC;AAED,WAAO,MAAM,OAAO,QAAQ;AAAA,EAC9B,GAAG,CAAC,YAAY,CAAC;AAEjB,SACE,oBAAC,iBAAiB,UAAjB,EAA0B,OAAO,UAAU,SAAU,UAAS;AAEnE;;;AEhDA,SAAS,aAAAC,kBAAiB;;;ACA1B,SAAS,kBAAkB;AAIpB,IAAM,eAAe,MAAM;AAChC,QAAM,SAAS,WAAW,gBAAgB;AAC1C,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AACA,SAAO;AACT;;;ADKO,IAAM,kBAAyC,CAAC,EAAE,UAAU,QAAQ,MAAM;AAC/E,QAAM,SAAS,aAAa;AAE5B,EAAAC,WAAU,MAAM;AACd,QAAI,SAAS;AACb,WACG,MAAM,EACN,KAAK,MAAM;AACV,UAAI,CAAC;AAAQ;AACb,UAAI,UAAU;AACZ,eAAO,KAAK;AAAA,MACd;AACA;AAAA,IACF,CAAC,EACA,MAAM,MAAM;AAAA,IAEb,CAAC;AAEH,WAAO,MAAM;AACX,eAAS;AAAA,IACX;AAAA,EACF,GAAG,CAAC,UAAU,QAAQ,OAAO,CAAC;AAE9B,SAAO;AACT;","names":["_a","useEffect","useEffect"]}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@emplorium/sdk",
3
+ "version": "0.1.0",
4
+ "description": "Official browser SDK for embedding the Emplorium chat widget with programmatic control.",
5
+ "license": "SEE LICENSE IN LICENSE",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.mjs",
8
+ "types": "dist/index.d.ts",
9
+ "sideEffects": false,
10
+ "scripts": {
11
+ "build": "tsup src/index.ts src/react/index.ts --dts --format esm,cjs --sourcemap --clean --external react,react-dom",
12
+ "dev": "tsup src/index.ts src/react/index.ts --watch --external react,react-dom"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.js",
18
+ "types": "./dist/index.d.ts"
19
+ },
20
+ "./react": {
21
+ "import": "./dist/react/index.mjs",
22
+ "require": "./dist/react/index.js",
23
+ "types": "./dist/react/index.d.ts"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "peerDependencies": {
30
+ "react": ">=18 <20",
31
+ "react-dom": ">=18 <20"
32
+ },
33
+ "devDependencies": {
34
+ "react": "^18.3.1",
35
+ "react-dom": "^18.3.1",
36
+ "tsup": "^7.3.0",
37
+ "typescript": "^5.4.0"
38
+ },
39
+ "dependencies": {
40
+ "tslib": "^2.6.2"
41
+ },
42
+ "engines": {
43
+ "node": ">=18"
44
+ }
45
+ }