@spur.us/monocle-react 1.0.0 → 1.1.0-canary.v20250520214223
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/.turbo/turbo-build.log +10 -10
- package/dist/index.d.mts +16 -7
- package/dist/index.d.ts +16 -7
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/constants.ts +1 -0
- package/src/contexts/MonocleProvider.tsx +4 -7
- package/src/index.ts +2 -0
- package/src/types.ts +12 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @spur.us/monocle-react@1.0.
|
|
2
|
+
> @spur.us/monocle-react@1.1.0-canary.v20250520214223 build /home/runner/work/javascript/javascript/packages/monocle-react
|
|
3
3
|
> tsup
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: {"index":"src/index.ts"}
|
|
@@ -10,13 +10,13 @@
|
|
|
10
10
|
[34mCLI[39m Cleaning output folder
|
|
11
11
|
[34mCJS[39m Build start
|
|
12
12
|
[34mESM[39m Build start
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
[
|
|
18
|
-
[
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m3.35 KB[39m
|
|
14
|
+
[32mESM[39m [1mdist/index.mjs.map [22m[32m7.54 KB[39m
|
|
15
|
+
[32mESM[39m ⚡️ Build success in 43ms
|
|
16
|
+
[32mCJS[39m [1mdist/index.js [22m[32m5.15 KB[39m
|
|
17
|
+
[32mCJS[39m [1mdist/index.js.map [22m[32m7.72 KB[39m
|
|
18
|
+
[32mCJS[39m ⚡️ Build success in 44ms
|
|
19
19
|
[34mDTS[39m Build start
|
|
20
|
-
[32mDTS[39m ⚡️ Build success in
|
|
21
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
22
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 2322ms
|
|
21
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m806.00 B[39m
|
|
22
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m806.00 B[39m
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React$1 from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Props for the MonocleProvider component.
|
|
5
|
+
* @interface MonocleProviderProps
|
|
6
|
+
*/
|
|
7
|
+
interface MonocleProviderProps {
|
|
8
|
+
/** The child components to be wrapped by the MonocleProvider. */
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
/** The publishable key used for authentication with Monocle. */
|
|
11
|
+
publishableKey: string;
|
|
12
|
+
/** Optional base domain for the Monocle API. Defaults to `mcl.spur.us` if not provided */
|
|
13
|
+
domain?: string;
|
|
14
|
+
}
|
|
2
15
|
|
|
3
16
|
interface MonocleContextType {
|
|
4
17
|
assessment: string | undefined;
|
|
@@ -6,11 +19,7 @@ interface MonocleContextType {
|
|
|
6
19
|
isLoading: boolean;
|
|
7
20
|
error: Error | null;
|
|
8
21
|
}
|
|
9
|
-
|
|
10
|
-
children: React.ReactNode;
|
|
11
|
-
publishableKey: string;
|
|
12
|
-
}
|
|
13
|
-
declare const MonocleProvider: React.ComponentType<MonocleProviderProps>;
|
|
22
|
+
declare const MonocleProvider: React$1.ComponentType<MonocleProviderProps>;
|
|
14
23
|
declare const useMonocle: () => MonocleContextType;
|
|
15
24
|
|
|
16
|
-
export { MonocleProvider, useMonocle };
|
|
25
|
+
export { MonocleProvider, type MonocleProviderProps, useMonocle };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React$1 from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Props for the MonocleProvider component.
|
|
5
|
+
* @interface MonocleProviderProps
|
|
6
|
+
*/
|
|
7
|
+
interface MonocleProviderProps {
|
|
8
|
+
/** The child components to be wrapped by the MonocleProvider. */
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
/** The publishable key used for authentication with Monocle. */
|
|
11
|
+
publishableKey: string;
|
|
12
|
+
/** Optional base domain for the Monocle API. Defaults to `mcl.spur.us` if not provided */
|
|
13
|
+
domain?: string;
|
|
14
|
+
}
|
|
2
15
|
|
|
3
16
|
interface MonocleContextType {
|
|
4
17
|
assessment: string | undefined;
|
|
@@ -6,11 +19,7 @@ interface MonocleContextType {
|
|
|
6
19
|
isLoading: boolean;
|
|
7
20
|
error: Error | null;
|
|
8
21
|
}
|
|
9
|
-
|
|
10
|
-
children: React.ReactNode;
|
|
11
|
-
publishableKey: string;
|
|
12
|
-
}
|
|
13
|
-
declare const MonocleProvider: React.ComponentType<MonocleProviderProps>;
|
|
22
|
+
declare const MonocleProvider: React$1.ComponentType<MonocleProviderProps>;
|
|
14
23
|
declare const useMonocle: () => MonocleContextType;
|
|
15
24
|
|
|
16
|
-
export { MonocleProvider, useMonocle };
|
|
25
|
+
export { MonocleProvider, type MonocleProviderProps, useMonocle };
|
package/dist/index.js
CHANGED
|
@@ -64,12 +64,16 @@ function withMaxAllowedInstancesGuard(WrappedComponent, name, error) {
|
|
|
64
64
|
return Hoc;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
// src/constants.ts
|
|
68
|
+
var DOMAIN = "mcl.spur.us";
|
|
69
|
+
|
|
67
70
|
// src/contexts/MonocleProvider.tsx
|
|
68
71
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
69
72
|
var MonocleContext = (0, import_react2.createContext)(null);
|
|
70
73
|
var MonocleProviderComponent = ({
|
|
71
74
|
children,
|
|
72
|
-
publishableKey
|
|
75
|
+
publishableKey,
|
|
76
|
+
domain = DOMAIN
|
|
73
77
|
}) => {
|
|
74
78
|
const [assessment, setAssessment] = (0, import_react2.useState)(void 0);
|
|
75
79
|
const [isLoading, setIsLoading] = (0, import_react2.useState)(true);
|
|
@@ -89,7 +93,7 @@ var MonocleProviderComponent = ({
|
|
|
89
93
|
const script = document.createElement("script");
|
|
90
94
|
script.id = "_mcl";
|
|
91
95
|
script.async = true;
|
|
92
|
-
script.src = `https
|
|
96
|
+
script.src = `https://${domain}/d/mcl.js?tk=${publishableKey}`;
|
|
93
97
|
script.onload = () => {
|
|
94
98
|
resolve();
|
|
95
99
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/contexts/MonocleProvider.tsx","../src/utils/useMaxAllowedInstancesGuard.tsx"],"sourcesContent":["export * from './contexts';\n","import React, { createContext, useContext, useEffect, useState } from 'react';\nimport { withMaxAllowedInstancesGuard } from '../utils';\
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/contexts/MonocleProvider.tsx","../src/utils/useMaxAllowedInstancesGuard.tsx","../src/constants.ts"],"sourcesContent":["export * from './contexts';\n\nexport type { MonocleProviderProps } from './types';\n","import React, { createContext, useContext, useEffect, useState } from 'react';\nimport { withMaxAllowedInstancesGuard } from '../utils';\nimport { MonocleProviderProps } from '../types';\nimport { DOMAIN } from '../constants';\ninterface MonocleContextType {\n assessment: string | undefined;\n refresh: () => void;\n isLoading: boolean;\n error: Error | null;\n}\n\nconst MonocleContext = createContext<MonocleContextType | null>(null);\n\nconst MonocleProviderComponent: React.FC<MonocleProviderProps> = ({\n children,\n publishableKey,\n domain = DOMAIN,\n}) => {\n const [assessment, setAssessment] = useState<string | undefined>(undefined);\n const [isLoading, setIsLoading] = useState(true);\n const [error, setError] = useState<Error | null>(null);\n\n const loadScript = () => {\n return new Promise<void>((resolve, reject) => {\n const existingScript = document.getElementById('_mcl');\n if (existingScript) {\n // If script exists but hasn't loaded yet, wait for it\n if (!window.MCL) {\n existingScript.onload = () => resolve();\n existingScript.onerror = () =>\n reject(new Error('Failed to load Monocle script'));\n } else {\n resolve();\n }\n return;\n }\n\n const script = document.createElement('script');\n script.id = '_mcl';\n script.async = true;\n script.src = `https://${domain}/d/mcl.js?tk=${publishableKey}`;\n script.onload = () => {\n resolve();\n };\n script.onerror = (_e) => {\n console.error('MonocleProvider: Script failed to load');\n reject(new Error('Failed to load Monocle script'));\n };\n document.head.appendChild(script);\n });\n };\n\n const refresh = async () => {\n try {\n setIsLoading(true);\n setError(null);\n await loadScript();\n if (window.MCL) {\n const newAssessment = window.MCL.getAssessment();\n setAssessment(newAssessment);\n } else {\n throw new Error('MCL object not found on window');\n }\n } catch (err) {\n setError(\n err instanceof Error ? err : new Error('Unknown error occurred')\n );\n } finally {\n setIsLoading(false);\n }\n };\n\n useEffect(() => {\n // Only refresh if the publishableKey changes and we don't already have an assessment\n if (!assessment) {\n refresh();\n }\n }, [publishableKey]);\n\n return (\n <MonocleContext.Provider value={{ assessment, refresh, isLoading, error }}>\n {children}\n </MonocleContext.Provider>\n );\n};\n\nexport const MonocleProvider = withMaxAllowedInstancesGuard(\n MonocleProviderComponent,\n 'MonocleProvider',\n 'Only one instance of MonocleProvider is allowed'\n);\n\nexport const useMonocle = () => {\n const context = useContext(MonocleContext);\n if (!context) {\n throw new Error('useMonocle must be used within a MonocleProvider');\n }\n return context;\n};\n","import React from 'react';\n\nconst instanceCounter = new Map<string, number>();\n\n/**\n * A React hook that ensures a component is not instantiated more than a specified number of times.\n * Throws an error if the maximum number of instances is exceeded.\n *\n * @param name - A unique identifier for the component type\n * @param error - The error message to display if the maximum count is exceeded\n * @param maxCount - The maximum number of allowed instances (defaults to 1)\n * @throws Error when the maximum number of instances is exceeded\n *\n * @example\n * ```tsx\n * const MyComponent = () => {\n * useMaxAllowedInstancesGuard('MyComponent', 'Only one instance of MyComponent is allowed');\n * return <div>My Component</div>;\n * };\n * ```\n */\nexport function useMaxAllowedInstancesGuard(\n name: string,\n error: string,\n maxCount = 1\n): void {\n React.useEffect(() => {\n const count = instanceCounter.get(name) || 0;\n if (count === maxCount) {\n throw new Error(error);\n }\n instanceCounter.set(name, count + 1);\n\n return () => {\n instanceCounter.set(name, (instanceCounter.get(name) || 1) - 1);\n };\n }, []);\n}\n\n/**\n * A higher-order component that wraps a component with instance count protection.\n * This HOC ensures that the wrapped component cannot be instantiated more than once\n * (or a specified number of times) in the application.\n *\n * @param WrappedComponent - The component to wrap with instance count protection\n * @param name - A unique identifier for the component type\n * @param error - The error message to display if the maximum count is exceeded\n * @returns A new component with instance count protection\n *\n * @example\n * ```tsx\n * const ProtectedComponent = withMaxAllowedInstancesGuard(\n * MyComponent,\n * 'MyComponent',\n * 'Only one instance of MyComponent is allowed'\n * );\n * ```\n */\nexport function withMaxAllowedInstancesGuard<P extends object>(\n WrappedComponent: React.ComponentType<P>,\n name: string,\n error: string\n): React.ComponentType<P> {\n const displayName =\n WrappedComponent.displayName ||\n WrappedComponent.name ||\n name ||\n 'Component';\n\n const Hoc: React.FC<P> = (props) => {\n useMaxAllowedInstancesGuard(name, error);\n return <WrappedComponent {...props} />;\n };\n\n Hoc.displayName = `withMaxAllowedInstancesGuard(${displayName})`;\n return Hoc;\n}\n","export const DOMAIN = 'mcl.spur.us';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,gBAAsE;;;ACAtE,mBAAkB;AAuEP;AArEX,IAAM,kBAAkB,oBAAI,IAAoB;AAmBzC,SAAS,4BACd,MACA,OACA,WAAW,GACL;AACN,eAAAC,QAAM,UAAU,MAAM;AACpB,UAAM,QAAQ,gBAAgB,IAAI,IAAI,KAAK;AAC3C,QAAI,UAAU,UAAU;AACtB,YAAM,IAAI,MAAM,KAAK;AAAA,IACvB;AACA,oBAAgB,IAAI,MAAM,QAAQ,CAAC;AAEnC,WAAO,MAAM;AACX,sBAAgB,IAAI,OAAO,gBAAgB,IAAI,IAAI,KAAK,KAAK,CAAC;AAAA,IA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A,EACH;AAEA,QAAM,UAAU,YAAY;AAC1B,QAAI;AACF,mBAAa,IAAI;AACjB,eAAS,IAAI;AACb,YAAM,WAAW;AACjB,UAAI,OAAO,KAAK;AACd,cAAM,gBAAgB,OAAO,IAAI,cAAc;AAC/C,sBAAc,aAAa;AAAA,MAC7B,OAAO;AACL,cAAM,IAAI,MAAM,gCAAgC;AAAA,MAClD;AAAA,IACF,SAAS,KAAK;AACZ;AAAA,QACE,eAAe,QAAQ,MAAM,IAAI,MAAM,wBAAwB;AAAA,MACjE;AAAA,IACF,UAAE;AACA,mBAAa,KAAK;AAAA,IACpB;AAAA,EACF;AAEA,+BAAU,MAAM;AAEd,QAAI,CAAC,YAAY;AACf,cAAQ;AAAA,IACV;AAAA,EACF,GAAG,CAAC,cAAc,CAAC;AAEnB,SACE,6CAAC,eAAe,UAAf,EAAwB,OAAO,EAAE,YAAY,SAAS,WAAW,MAAM,GACrE,UACH;AAEJ;AAEO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AACF;AAEO,IAAM,aAAa,MAAM;AAC9B,QAAM,cAAU,0BAAW,cAAc;AACzC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACpE;AACA,SAAO;AACT;","names":["import_react","React","import_jsx_runtime"]}
|
package/dist/index.mjs
CHANGED
|
@@ -27,12 +27,16 @@ function withMaxAllowedInstancesGuard(WrappedComponent, name, error) {
|
|
|
27
27
|
return Hoc;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
// src/constants.ts
|
|
31
|
+
var DOMAIN = "mcl.spur.us";
|
|
32
|
+
|
|
30
33
|
// src/contexts/MonocleProvider.tsx
|
|
31
34
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
32
35
|
var MonocleContext = createContext(null);
|
|
33
36
|
var MonocleProviderComponent = ({
|
|
34
37
|
children,
|
|
35
|
-
publishableKey
|
|
38
|
+
publishableKey,
|
|
39
|
+
domain = DOMAIN
|
|
36
40
|
}) => {
|
|
37
41
|
const [assessment, setAssessment] = useState(void 0);
|
|
38
42
|
const [isLoading, setIsLoading] = useState(true);
|
|
@@ -52,7 +56,7 @@ var MonocleProviderComponent = ({
|
|
|
52
56
|
const script = document.createElement("script");
|
|
53
57
|
script.id = "_mcl";
|
|
54
58
|
script.async = true;
|
|
55
|
-
script.src = `https
|
|
59
|
+
script.src = `https://${domain}/d/mcl.js?tk=${publishableKey}`;
|
|
56
60
|
script.onload = () => {
|
|
57
61
|
resolve();
|
|
58
62
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/contexts/MonocleProvider.tsx","../src/utils/useMaxAllowedInstancesGuard.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useState } from 'react';\nimport { withMaxAllowedInstancesGuard } from '../utils';\
|
|
1
|
+
{"version":3,"sources":["../src/contexts/MonocleProvider.tsx","../src/utils/useMaxAllowedInstancesGuard.tsx","../src/constants.ts"],"sourcesContent":["import React, { createContext, useContext, useEffect, useState } from 'react';\nimport { withMaxAllowedInstancesGuard } from '../utils';\nimport { MonocleProviderProps } from '../types';\nimport { DOMAIN } from '../constants';\ninterface MonocleContextType {\n assessment: string | undefined;\n refresh: () => void;\n isLoading: boolean;\n error: Error | null;\n}\n\nconst MonocleContext = createContext<MonocleContextType | null>(null);\n\nconst MonocleProviderComponent: React.FC<MonocleProviderProps> = ({\n children,\n publishableKey,\n domain = DOMAIN,\n}) => {\n const [assessment, setAssessment] = useState<string | undefined>(undefined);\n const [isLoading, setIsLoading] = useState(true);\n const [error, setError] = useState<Error | null>(null);\n\n const loadScript = () => {\n return new Promise<void>((resolve, reject) => {\n const existingScript = document.getElementById('_mcl');\n if (existingScript) {\n // If script exists but hasn't loaded yet, wait for it\n if (!window.MCL) {\n existingScript.onload = () => resolve();\n existingScript.onerror = () =>\n reject(new Error('Failed to load Monocle script'));\n } else {\n resolve();\n }\n return;\n }\n\n const script = document.createElement('script');\n script.id = '_mcl';\n script.async = true;\n script.src = `https://${domain}/d/mcl.js?tk=${publishableKey}`;\n script.onload = () => {\n resolve();\n };\n script.onerror = (_e) => {\n console.error('MonocleProvider: Script failed to load');\n reject(new Error('Failed to load Monocle script'));\n };\n document.head.appendChild(script);\n });\n };\n\n const refresh = async () => {\n try {\n setIsLoading(true);\n setError(null);\n await loadScript();\n if (window.MCL) {\n const newAssessment = window.MCL.getAssessment();\n setAssessment(newAssessment);\n } else {\n throw new Error('MCL object not found on window');\n }\n } catch (err) {\n setError(\n err instanceof Error ? err : new Error('Unknown error occurred')\n );\n } finally {\n setIsLoading(false);\n }\n };\n\n useEffect(() => {\n // Only refresh if the publishableKey changes and we don't already have an assessment\n if (!assessment) {\n refresh();\n }\n }, [publishableKey]);\n\n return (\n <MonocleContext.Provider value={{ assessment, refresh, isLoading, error }}>\n {children}\n </MonocleContext.Provider>\n );\n};\n\nexport const MonocleProvider = withMaxAllowedInstancesGuard(\n MonocleProviderComponent,\n 'MonocleProvider',\n 'Only one instance of MonocleProvider is allowed'\n);\n\nexport const useMonocle = () => {\n const context = useContext(MonocleContext);\n if (!context) {\n throw new Error('useMonocle must be used within a MonocleProvider');\n }\n return context;\n};\n","import React from 'react';\n\nconst instanceCounter = new Map<string, number>();\n\n/**\n * A React hook that ensures a component is not instantiated more than a specified number of times.\n * Throws an error if the maximum number of instances is exceeded.\n *\n * @param name - A unique identifier for the component type\n * @param error - The error message to display if the maximum count is exceeded\n * @param maxCount - The maximum number of allowed instances (defaults to 1)\n * @throws Error when the maximum number of instances is exceeded\n *\n * @example\n * ```tsx\n * const MyComponent = () => {\n * useMaxAllowedInstancesGuard('MyComponent', 'Only one instance of MyComponent is allowed');\n * return <div>My Component</div>;\n * };\n * ```\n */\nexport function useMaxAllowedInstancesGuard(\n name: string,\n error: string,\n maxCount = 1\n): void {\n React.useEffect(() => {\n const count = instanceCounter.get(name) || 0;\n if (count === maxCount) {\n throw new Error(error);\n }\n instanceCounter.set(name, count + 1);\n\n return () => {\n instanceCounter.set(name, (instanceCounter.get(name) || 1) - 1);\n };\n }, []);\n}\n\n/**\n * A higher-order component that wraps a component with instance count protection.\n * This HOC ensures that the wrapped component cannot be instantiated more than once\n * (or a specified number of times) in the application.\n *\n * @param WrappedComponent - The component to wrap with instance count protection\n * @param name - A unique identifier for the component type\n * @param error - The error message to display if the maximum count is exceeded\n * @returns A new component with instance count protection\n *\n * @example\n * ```tsx\n * const ProtectedComponent = withMaxAllowedInstancesGuard(\n * MyComponent,\n * 'MyComponent',\n * 'Only one instance of MyComponent is allowed'\n * );\n * ```\n */\nexport function withMaxAllowedInstancesGuard<P extends object>(\n WrappedComponent: React.ComponentType<P>,\n name: string,\n error: string\n): React.ComponentType<P> {\n const displayName =\n WrappedComponent.displayName ||\n WrappedComponent.name ||\n name ||\n 'Component';\n\n const Hoc: React.FC<P> = (props) => {\n useMaxAllowedInstancesGuard(name, error);\n return <WrappedComponent {...props} />;\n };\n\n Hoc.displayName = `withMaxAllowedInstancesGuard(${displayName})`;\n return Hoc;\n}\n","export const DOMAIN = 'mcl.spur.us';\n"],"mappings":";AAAA,SAAgB,eAAe,YAAY,WAAW,gBAAgB;;;ACAtE,OAAO,WAAW;AAuEP;AArEX,IAAM,kBAAkB,oBAAI,IAAoB;AAmBzC,SAAS,4BACd,MACA,OACA,WAAW,GACL;AACN,QAAM,UAAU,MAAM;AACpB,UAAM,QAAQ,gBAAgB,IAAI,IAAI,KAAK;AAC3C,QAAI,UAAU,UAAU;AACtB,YAAM,IAAI,MAAM,KAAK;AAAA,IACvB;AACA,oBAAgB,IAAI,MAAM,QAAQ,CAAC;AAEnC,WAAO,MAAM;AACX,sBAAgB,IAAI,OAAO,gBAAgB,IAAI,IAAI,KAAK,KAAK,CAAC;AAAA,IAChE;AAAA,EACF,GAAG,CAAC,CAAC;AACP;AAqBO,SAAS,6BACd,kBACA,MACA,OACwB;AACxB,QAAM,cACJ,iBAAiB,eACjB,iBAAiB,QACjB,QAC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AAM,gBAAgB,OAAO,IAAI,cAAc;AAC/C,sBAAc,aAAa;AAAA,MAC7B,OAAO;AACL,cAAM,IAAI,MAAM,gCAAgC;AAAA,MAClD;AAAA,IACF,SAAS,KAAK;AACZ;AAAA,QACE,eAAe,QAAQ,MAAM,IAAI,MAAM,wBAAwB;AAAA,MACjE;AAAA,IACF,UAAE;AACA,mBAAa,KAAK;AAAA,IACpB;AAAA,EACF;AAEA,YAAU,MAAM;AAEd,QAAI,CAAC,YAAY;AACf,cAAQ;AAAA,IACV;AAAA,EACF,GAAG,CAAC,cAAc,CAAC;AAEnB,SACE,gBAAAA,KAAC,eAAe,UAAf,EAAwB,OAAO,EAAE,YAAY,SAAS,WAAW,MAAM,GACrE,UACH;AAEJ;AAEO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AACF;AAEO,IAAM,aAAa,MAAM;AAC9B,QAAM,UAAU,WAAW,cAAc;AACzC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACpE;AACA,SAAO;AACT;","names":["jsx"]}
|
package/package.json
CHANGED
package/src/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DOMAIN = 'mcl.spur.us';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React, { createContext, useContext, useEffect, useState } from 'react';
|
|
2
2
|
import { withMaxAllowedInstancesGuard } from '../utils';
|
|
3
|
-
|
|
3
|
+
import { MonocleProviderProps } from '../types';
|
|
4
|
+
import { DOMAIN } from '../constants';
|
|
4
5
|
interface MonocleContextType {
|
|
5
6
|
assessment: string | undefined;
|
|
6
7
|
refresh: () => void;
|
|
@@ -10,14 +11,10 @@ interface MonocleContextType {
|
|
|
10
11
|
|
|
11
12
|
const MonocleContext = createContext<MonocleContextType | null>(null);
|
|
12
13
|
|
|
13
|
-
interface MonocleProviderProps {
|
|
14
|
-
children: React.ReactNode;
|
|
15
|
-
publishableKey: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
14
|
const MonocleProviderComponent: React.FC<MonocleProviderProps> = ({
|
|
19
15
|
children,
|
|
20
16
|
publishableKey,
|
|
17
|
+
domain = DOMAIN,
|
|
21
18
|
}) => {
|
|
22
19
|
const [assessment, setAssessment] = useState<string | undefined>(undefined);
|
|
23
20
|
const [isLoading, setIsLoading] = useState(true);
|
|
@@ -41,7 +38,7 @@ const MonocleProviderComponent: React.FC<MonocleProviderProps> = ({
|
|
|
41
38
|
const script = document.createElement('script');
|
|
42
39
|
script.id = '_mcl';
|
|
43
40
|
script.async = true;
|
|
44
|
-
script.src = `https
|
|
41
|
+
script.src = `https://${domain}/d/mcl.js?tk=${publishableKey}`;
|
|
45
42
|
script.onload = () => {
|
|
46
43
|
resolve();
|
|
47
44
|
};
|
package/src/index.ts
CHANGED
package/src/types.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Props for the MonocleProvider component.
|
|
3
|
+
* @interface MonocleProviderProps
|
|
4
|
+
*/
|
|
5
|
+
export interface MonocleProviderProps {
|
|
6
|
+
/** The child components to be wrapped by the MonocleProvider. */
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
/** The publishable key used for authentication with Monocle. */
|
|
9
|
+
publishableKey: string;
|
|
10
|
+
/** Optional base domain for the Monocle API. Defaults to `mcl.spur.us` if not provided */
|
|
11
|
+
domain?: string;
|
|
12
|
+
}
|