@spur.us/monocle-react 1.1.1-canary.v20250521185758 → 1.1.1-canary.v20250707133917

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @spur.us/monocle-react@1.1.1-canary.v20250521185758 build /home/runner/work/javascript/javascript/packages/monocle-react
2
+ > @spur.us/monocle-react@1.1.1-canary.v20250707133917 build /home/runner/work/javascript/javascript/packages/monocle-react
3
3
  > tsup
4
4
 
5
5
  CLI Building entry: {"index":"src/index.ts"}
@@ -10,13 +10,13 @@
10
10
  CLI Cleaning output folder
11
11
  CJS Build start
12
12
  ESM Build start
13
- CJS dist/index.js 5.15 KB
14
- CJS dist/index.js.map 7.72 KB
15
- CJS ⚡️ Build success in 40ms
16
- ESM dist/index.mjs 3.35 KB
17
- ESM dist/index.mjs.map 7.54 KB
18
- ESM ⚡️ Build success in 41ms
13
+ CJS dist/index.js 6.08 KB
14
+ CJS dist/index.js.map 10.07 KB
15
+ CJS ⚡️ Build success in 43ms
16
+ ESM dist/index.mjs 4.27 KB
17
+ ESM dist/index.mjs.map 9.92 KB
18
+ ESM ⚡️ Build success in 46ms
19
19
  DTS Build start
20
- DTS ⚡️ Build success in 2163ms
21
- DTS dist/index.d.ts 806.00 B
22
- DTS dist/index.d.mts 806.00 B
20
+ DTS ⚡️ Build success in 2242ms
21
+ DTS dist/index.d.ts 1.29 KB
22
+ DTS dist/index.d.mts 1.29 KB
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import React$1 from 'react';
1
+ import React from 'react';
2
2
 
3
3
  /**
4
4
  * Props for the MonocleProvider component.
@@ -19,7 +19,25 @@ interface MonocleContextType {
19
19
  isLoading: boolean;
20
20
  error: Error | null;
21
21
  }
22
- declare const MonocleProvider: React$1.ComponentType<MonocleProviderProps>;
22
+ declare const MonocleProvider: React.ComponentType<MonocleProviderProps>;
23
+ /**
24
+ * Hook to access the Monocle context.
25
+ *
26
+ * @returns {MonocleContextType} The Monocle context containing assessment data, loading state, and error information
27
+ * @throws {Error} When used outside of a MonocleProvider
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * function MyComponent() {
32
+ * const { assessment, isLoading, error, refresh } = useMonocle();
33
+ *
34
+ * if (isLoading) return <div>Loading...</div>;
35
+ * if (error) return <div>Error: {error.message}</div>;
36
+ *
37
+ * return <div>Assessment: {assessment}</div>;
38
+ * }
39
+ * ```
40
+ */
23
41
  declare const useMonocle: () => MonocleContextType;
24
42
 
25
43
  export { MonocleProvider, type MonocleProviderProps, useMonocle };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import React$1 from 'react';
1
+ import React from 'react';
2
2
 
3
3
  /**
4
4
  * Props for the MonocleProvider component.
@@ -19,7 +19,25 @@ interface MonocleContextType {
19
19
  isLoading: boolean;
20
20
  error: Error | null;
21
21
  }
22
- declare const MonocleProvider: React$1.ComponentType<MonocleProviderProps>;
22
+ declare const MonocleProvider: React.ComponentType<MonocleProviderProps>;
23
+ /**
24
+ * Hook to access the Monocle context.
25
+ *
26
+ * @returns {MonocleContextType} The Monocle context containing assessment data, loading state, and error information
27
+ * @throws {Error} When used outside of a MonocleProvider
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * function MyComponent() {
32
+ * const { assessment, isLoading, error, refresh } = useMonocle();
33
+ *
34
+ * if (isLoading) return <div>Loading...</div>;
35
+ * if (error) return <div>Error: {error.message}</div>;
36
+ *
37
+ * return <div>Assessment: {assessment}</div>;
38
+ * }
39
+ * ```
40
+ */
23
41
  declare const useMonocle: () => MonocleContextType;
24
42
 
25
43
  export { MonocleProvider, type MonocleProviderProps, useMonocle };
package/dist/index.js CHANGED
@@ -50,7 +50,8 @@ function useMaxAllowedInstancesGuard(name, error, maxCount = 1) {
50
50
  }
51
51
  instanceCounter.set(name, count + 1);
52
52
  return () => {
53
- instanceCounter.set(name, (instanceCounter.get(name) || 1) - 1);
53
+ const currentCount = instanceCounter.get(name) || 0;
54
+ instanceCounter.set(name, Math.max(0, currentCount - 1));
54
55
  };
55
56
  }, []);
56
57
  }
@@ -104,14 +105,32 @@ var MonocleProviderComponent = ({
104
105
  document.head.appendChild(script);
105
106
  });
106
107
  };
107
- const refresh = async () => {
108
+ const refresh = (0, import_react2.useCallback)(async () => {
108
109
  try {
109
110
  setIsLoading(true);
110
111
  setError(null);
111
112
  await loadScript();
112
113
  if (window.MCL) {
113
- const newAssessment = window.MCL.getAssessment();
114
- setAssessment(newAssessment);
114
+ let timeoutId = null;
115
+ await window.MCL.configure({
116
+ onAssessment: (assessment2) => {
117
+ if (timeoutId) {
118
+ clearTimeout(timeoutId);
119
+ }
120
+ setAssessment(assessment2);
121
+ setIsLoading(false);
122
+ }
123
+ });
124
+ const existingAssessment = window.MCL.getAssessment();
125
+ if (existingAssessment) {
126
+ setAssessment(existingAssessment);
127
+ setIsLoading(false);
128
+ } else {
129
+ timeoutId = setTimeout(() => {
130
+ setError(new Error("Assessment timeout - MCL did not respond within 30 seconds"));
131
+ setIsLoading(false);
132
+ }, 3e4);
133
+ }
115
134
  } else {
116
135
  throw new Error("MCL object not found on window");
117
136
  }
@@ -119,16 +138,24 @@ var MonocleProviderComponent = ({
119
138
  setError(
120
139
  err instanceof Error ? err : new Error("Unknown error occurred")
121
140
  );
122
- } finally {
123
141
  setIsLoading(false);
124
142
  }
125
- };
143
+ }, [publishableKey, domain]);
126
144
  (0, import_react2.useEffect)(() => {
127
145
  if (!assessment) {
128
146
  refresh();
129
147
  }
130
- }, [publishableKey]);
131
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MonocleContext.Provider, { value: { assessment, refresh, isLoading, error }, children });
148
+ return () => {
149
+ if (window.MCL) {
150
+ window.MCL.configure({ onAssessment: void 0 });
151
+ }
152
+ };
153
+ }, [publishableKey, domain, assessment, refresh]);
154
+ const contextValue = (0, import_react2.useMemo)(
155
+ () => ({ assessment, refresh, isLoading, error }),
156
+ [assessment, refresh, isLoading, error]
157
+ );
158
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MonocleContext.Provider, { value: contextValue, children });
132
159
  };
133
160
  var MonocleProvider = withMaxAllowedInstancesGuard(
134
161
  MonocleProviderComponent,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
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,IAChE;AAAA,EACF,GAAG,CAAC,CAAC;AACP;AAqBO,SAAS,6BACd,kBACA,MACA,OACwB;AACxB,QAAM,cACJ,iBAAiB,eACjB,iBAAiB,QACjB,QACA;AAEF,QAAM,MAAmB,CAAC,UAAU;AAClC,gCAA4B,MAAM,KAAK;AACvC,WAAO,4CAAC,oBAAkB,GAAG,OAAO;AAAA,EACtC;AAEA,MAAI,cAAc,gCAAgC,WAAW;AAC7D,SAAO;AACT;;;AC5EO,IAAM,SAAS;;;AFgFlB,IAAAC,sBAAA;AArEJ,IAAM,qBAAiB,6BAAyC,IAAI;AAEpE,IAAM,2BAA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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"]}
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, {\n createContext,\n useContext,\n useEffect,\n useState,\n useMemo,\n useCallback,\n} 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 = useCallback(async () => {\n try {\n setIsLoading(true);\n setError(null);\n await loadScript();\n if (window.MCL) {\n let timeoutId: NodeJS.Timeout | null = null;\n \n // Configure MCL with our callback to receive assessment updates\n await window.MCL.configure({\n onAssessment: (assessment: string) => {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n setAssessment(assessment);\n setIsLoading(false);\n },\n });\n\n // Check if assessment is already available\n const existingAssessment = window.MCL.getAssessment();\n if (existingAssessment) {\n setAssessment(existingAssessment);\n setIsLoading(false);\n } else {\n // Set a timeout in case the assessment never comes\n timeoutId = setTimeout(() => {\n setError(new Error('Assessment timeout - MCL did not respond within 30 seconds'));\n setIsLoading(false);\n }, 30000);\n }\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 setIsLoading(false);\n }\n }, [publishableKey, domain]);\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 \n // Cleanup function to reset callback on unmount\n return () => {\n if (window.MCL) {\n window.MCL.configure({ onAssessment: undefined });\n }\n };\n }, [publishableKey, domain, assessment, refresh]);\n\n const contextValue = useMemo(\n () => ({ assessment, refresh, isLoading, error }),\n [assessment, refresh, isLoading, error]\n );\n\n return (\n <MonocleContext.Provider value={contextValue}>\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\n/**\n * Hook to access the Monocle context.\n *\n * @returns {MonocleContextType} The Monocle context containing assessment data, loading state, and error information\n * @throws {Error} When used outside of a MonocleProvider\n *\n * @example\n * ```tsx\n * function MyComponent() {\n * const { assessment, isLoading, error, refresh } = useMonocle();\n *\n * if (isLoading) return <div>Loading...</div>;\n * if (error) return <div>Error: {error.message}</div>;\n *\n * return <div>Assessment: {assessment}</div>;\n * }\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 const currentCount = instanceCounter.get(name) || 0;\n instanceCounter.set(name, Math.max(0, currentCount - 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,gBAOO;;;ACPP,mBAAkB;AAwEP;AAtEX,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,YAAM,eAAe,gBAAgB,IAAI,IAAI,KAAK;AAClD,sBAAgB,IAAI,MAAM,KAAK,IAAI,GAAG,eAAe,CAAC,CAAC;AAAA,IACzD;AAAA,EACF,GAAG,CAAC,CAAC;AACP;AAqBO,SAAS,6BACd,kBACA,MACA,OACwB;AACxB,QAAM,cACJ,iBAAiB,eACjB,iBAAiB,QACjB,QACA;AAEF,QAAM,MAAmB,CAAC,UAAU;AAClC,gCAA4B,MAAM,KAAK;AACvC,WAAO,4CAAC,oBAAkB,GAAG,OAAO;AAAA,EACtC;AAEA,MAAI,cAAc,gCAAgC,WAAW;AAC7D,SAAO;AACT;;;AC7EO,IAAM,SAAS;;;AFyHlB,IAAAC,sBAAA;AAvGJ,IAAM,qBAAiB,6BAAyC,IAAI;AAEpE,IAAM,2BAA2D,CAAC;AAAA,EAChE;AAAA,EACA;AAAA,EACA,SAAS;AACX,MAAM;AACJ,QAAM,CAAC,YAAY,aAAa,QAAI,wBAA6B,MAAS;AAC1E,QAAM,CAAC,WAAW,YA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AAK;AAAA,UACpB,GAAG,GAAK;AAAA,QACV;AAAA,MACF,OAAO;AACL,cAAM,IAAI,MAAM,gCAAgC;AAAA,MAClD;AAAA,IACF,SAAS,KAAK;AACZ;AAAA,QACE,eAAe,QAAQ,MAAM,IAAI,MAAM,wBAAwB;AAAA,MACjE;AACA,mBAAa,KAAK;AAAA,IACpB;AAAA,EACF,GAAG,CAAC,gBAAgB,MAAM,CAAC;AAE3B,+BAAU,MAAM;AAEd,QAAI,CAAC,YAAY;AACf,cAAQ;AAAA,IACV;AAGA,WAAO,MAAM;AACX,UAAI,OAAO,KAAK;AACd,eAAO,IAAI,UAAU,EAAE,cAAc,OAAU,CAAC;AAAA,MAClD;AAAA,IACF;AAAA,EACF,GAAG,CAAC,gBAAgB,QAAQ,YAAY,OAAO,CAAC;AAEhD,QAAM,mBAAe;AAAA,IACnB,OAAO,EAAE,YAAY,SAAS,WAAW,MAAM;AAAA,IAC/C,CAAC,YAAY,SAAS,WAAW,KAAK;AAAA,EACxC;AAEA,SACE,6CAAC,eAAe,UAAf,EAAwB,OAAO,cAC7B,UACH;AAEJ;AAEO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AACF;AAoBO,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","assessment"]}
package/dist/index.mjs CHANGED
@@ -1,5 +1,12 @@
1
1
  // src/contexts/MonocleProvider.tsx
2
- import { createContext, useContext, useEffect, useState } from "react";
2
+ import {
3
+ createContext,
4
+ useContext,
5
+ useEffect,
6
+ useState,
7
+ useMemo,
8
+ useCallback
9
+ } from "react";
3
10
 
4
11
  // src/utils/useMaxAllowedInstancesGuard.tsx
5
12
  import React from "react";
@@ -13,7 +20,8 @@ function useMaxAllowedInstancesGuard(name, error, maxCount = 1) {
13
20
  }
14
21
  instanceCounter.set(name, count + 1);
15
22
  return () => {
16
- instanceCounter.set(name, (instanceCounter.get(name) || 1) - 1);
23
+ const currentCount = instanceCounter.get(name) || 0;
24
+ instanceCounter.set(name, Math.max(0, currentCount - 1));
17
25
  };
18
26
  }, []);
19
27
  }
@@ -67,14 +75,32 @@ var MonocleProviderComponent = ({
67
75
  document.head.appendChild(script);
68
76
  });
69
77
  };
70
- const refresh = async () => {
78
+ const refresh = useCallback(async () => {
71
79
  try {
72
80
  setIsLoading(true);
73
81
  setError(null);
74
82
  await loadScript();
75
83
  if (window.MCL) {
76
- const newAssessment = window.MCL.getAssessment();
77
- setAssessment(newAssessment);
84
+ let timeoutId = null;
85
+ await window.MCL.configure({
86
+ onAssessment: (assessment2) => {
87
+ if (timeoutId) {
88
+ clearTimeout(timeoutId);
89
+ }
90
+ setAssessment(assessment2);
91
+ setIsLoading(false);
92
+ }
93
+ });
94
+ const existingAssessment = window.MCL.getAssessment();
95
+ if (existingAssessment) {
96
+ setAssessment(existingAssessment);
97
+ setIsLoading(false);
98
+ } else {
99
+ timeoutId = setTimeout(() => {
100
+ setError(new Error("Assessment timeout - MCL did not respond within 30 seconds"));
101
+ setIsLoading(false);
102
+ }, 3e4);
103
+ }
78
104
  } else {
79
105
  throw new Error("MCL object not found on window");
80
106
  }
@@ -82,16 +108,24 @@ var MonocleProviderComponent = ({
82
108
  setError(
83
109
  err instanceof Error ? err : new Error("Unknown error occurred")
84
110
  );
85
- } finally {
86
111
  setIsLoading(false);
87
112
  }
88
- };
113
+ }, [publishableKey, domain]);
89
114
  useEffect(() => {
90
115
  if (!assessment) {
91
116
  refresh();
92
117
  }
93
- }, [publishableKey]);
94
- return /* @__PURE__ */ jsx2(MonocleContext.Provider, { value: { assessment, refresh, isLoading, error }, children });
118
+ return () => {
119
+ if (window.MCL) {
120
+ window.MCL.configure({ onAssessment: void 0 });
121
+ }
122
+ };
123
+ }, [publishableKey, domain, assessment, refresh]);
124
+ const contextValue = useMemo(
125
+ () => ({ assessment, refresh, isLoading, error }),
126
+ [assessment, refresh, isLoading, error]
127
+ );
128
+ return /* @__PURE__ */ jsx2(MonocleContext.Provider, { value: contextValue, children });
95
129
  };
96
130
  var MonocleProvider = withMaxAllowedInstancesGuard(
97
131
  MonocleProviderComponent,
@@ -1 +1 @@
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,QACA;AAEF,QAAM,MAAmB,CAAC,UAAU;AAClC,gCAA4B,MAAM,KAAK;AACvC,WAAO,oBAAC,oBAAkB,GAAG,OAAO;AAAA,EACtC;AAEA,MAAI,cAAc,gCAAgC,WAAW;AAC7D,SAAO;AACT;;;AC5EO,IAAM,SAAS;;;AFgFlB,gBAAAA,YAAA;AArEJ,IAAM,iBAAiB,cAAyC,IAAI;AAEpE,IAAM,2BAA2D,CAAC;AAAA,EAChE;AAAA,EACA;AAAA,EACA,SAAS;AACX,MAAM;AACJ,QAAM,CAAC,YAAY,aAAa,IA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c,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"]}
1
+ {"version":3,"sources":["../src/contexts/MonocleProvider.tsx","../src/utils/useMaxAllowedInstancesGuard.tsx","../src/constants.ts"],"sourcesContent":["import React, {\n createContext,\n useContext,\n useEffect,\n useState,\n useMemo,\n useCallback,\n} 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 = useCallback(async () => {\n try {\n setIsLoading(true);\n setError(null);\n await loadScript();\n if (window.MCL) {\n let timeoutId: NodeJS.Timeout | null = null;\n \n // Configure MCL with our callback to receive assessment updates\n await window.MCL.configure({\n onAssessment: (assessment: string) => {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n setAssessment(assessment);\n setIsLoading(false);\n },\n });\n\n // Check if assessment is already available\n const existingAssessment = window.MCL.getAssessment();\n if (existingAssessment) {\n setAssessment(existingAssessment);\n setIsLoading(false);\n } else {\n // Set a timeout in case the assessment never comes\n timeoutId = setTimeout(() => {\n setError(new Error('Assessment timeout - MCL did not respond within 30 seconds'));\n setIsLoading(false);\n }, 30000);\n }\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 setIsLoading(false);\n }\n }, [publishableKey, domain]);\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 \n // Cleanup function to reset callback on unmount\n return () => {\n if (window.MCL) {\n window.MCL.configure({ onAssessment: undefined });\n }\n };\n }, [publishableKey, domain, assessment, refresh]);\n\n const contextValue = useMemo(\n () => ({ assessment, refresh, isLoading, error }),\n [assessment, refresh, isLoading, error]\n );\n\n return (\n <MonocleContext.Provider value={contextValue}>\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\n/**\n * Hook to access the Monocle context.\n *\n * @returns {MonocleContextType} The Monocle context containing assessment data, loading state, and error information\n * @throws {Error} When used outside of a MonocleProvider\n *\n * @example\n * ```tsx\n * function MyComponent() {\n * const { assessment, isLoading, error, refresh } = useMonocle();\n *\n * if (isLoading) return <div>Loading...</div>;\n * if (error) return <div>Error: {error.message}</div>;\n *\n * return <div>Assessment: {assessment}</div>;\n * }\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 const currentCount = instanceCounter.get(name) || 0;\n instanceCounter.set(name, Math.max(0, currentCount - 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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B;AAAA,MACjE;AACA,mBAAa,KAAK;AAAA,IACpB;AAAA,EACF,GAAG,CAAC,gBAAgB,MAAM,CAAC;AAE3B,YAAU,MAAM;AAEd,QAAI,CAAC,YAAY;AACf,cAAQ;AAAA,IACV;AAGA,WAAO,MAAM;AACX,UAAI,OAAO,KAAK;AACd,eAAO,IAAI,UAAU,EAAE,cAAc,OAAU,CAAC;AAAA,MAClD;AAAA,IACF;AAAA,EACF,GAAG,CAAC,gBAAgB,QAAQ,YAAY,OAAO,CAAC;AAEhD,QAAM,eAAe;AAAA,IACnB,OAAO,EAAE,YAAY,SAAS,WAAW,MAAM;AAAA,IAC/C,CAAC,YAAY,SAAS,WAAW,KAAK;AAAA,EACxC;AAEA,SACE,gBAAAD,KAAC,eAAe,UAAf,EAAwB,OAAO,cAC7B,UACH;AAEJ;AAEO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AACF;AAoBO,IAAM,aAAa,MAAM;AAC9B,QAAM,UAAU,WAAW,cAAc;AACzC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACpE;AACA,SAAO;AACT;","names":["jsx","assessment"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spur.us/monocle-react",
3
- "version": "1.1.1-canary.v20250521185758",
3
+ "version": "1.1.1-canary.v20250707133917",
4
4
  "description": "Monocle React library",
5
5
  "keywords": [
6
6
  "spur",
@@ -20,7 +20,7 @@
20
20
  "author": "Spur",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "@spur.us/types": "^0.3.0-canary.v20250521185758"
23
+ "@spur.us/types": "^0.3.0-canary.v20250707133917"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "react": "^18.0.0 || ^19.0.0 || ^19.0.0-0",
@@ -1,4 +1,11 @@
1
- import React, { createContext, useContext, useEffect, useState } from 'react';
1
+ import React, {
2
+ createContext,
3
+ useContext,
4
+ useEffect,
5
+ useState,
6
+ useMemo,
7
+ useCallback,
8
+ } from 'react';
2
9
  import { withMaxAllowedInstancesGuard } from '../utils';
3
10
  import { MonocleProviderProps } from '../types';
4
11
  import { DOMAIN } from '../constants';
@@ -50,14 +57,37 @@ const MonocleProviderComponent: React.FC<MonocleProviderProps> = ({
50
57
  });
51
58
  };
52
59
 
53
- const refresh = async () => {
60
+ const refresh = useCallback(async () => {
54
61
  try {
55
62
  setIsLoading(true);
56
63
  setError(null);
57
64
  await loadScript();
58
65
  if (window.MCL) {
59
- const newAssessment = window.MCL.getAssessment();
60
- setAssessment(newAssessment);
66
+ let timeoutId: NodeJS.Timeout | null = null;
67
+
68
+ // Configure MCL with our callback to receive assessment updates
69
+ await window.MCL.configure({
70
+ onAssessment: (assessment: string) => {
71
+ if (timeoutId) {
72
+ clearTimeout(timeoutId);
73
+ }
74
+ setAssessment(assessment);
75
+ setIsLoading(false);
76
+ },
77
+ });
78
+
79
+ // Check if assessment is already available
80
+ const existingAssessment = window.MCL.getAssessment();
81
+ if (existingAssessment) {
82
+ setAssessment(existingAssessment);
83
+ setIsLoading(false);
84
+ } else {
85
+ // Set a timeout in case the assessment never comes
86
+ timeoutId = setTimeout(() => {
87
+ setError(new Error('Assessment timeout - MCL did not respond within 30 seconds'));
88
+ setIsLoading(false);
89
+ }, 30000);
90
+ }
61
91
  } else {
62
92
  throw new Error('MCL object not found on window');
63
93
  }
@@ -65,20 +95,31 @@ const MonocleProviderComponent: React.FC<MonocleProviderProps> = ({
65
95
  setError(
66
96
  err instanceof Error ? err : new Error('Unknown error occurred')
67
97
  );
68
- } finally {
69
98
  setIsLoading(false);
70
99
  }
71
- };
100
+ }, [publishableKey, domain]);
72
101
 
73
102
  useEffect(() => {
74
103
  // Only refresh if the publishableKey changes and we don't already have an assessment
75
104
  if (!assessment) {
76
105
  refresh();
77
106
  }
78
- }, [publishableKey]);
107
+
108
+ // Cleanup function to reset callback on unmount
109
+ return () => {
110
+ if (window.MCL) {
111
+ window.MCL.configure({ onAssessment: undefined });
112
+ }
113
+ };
114
+ }, [publishableKey, domain, assessment, refresh]);
115
+
116
+ const contextValue = useMemo(
117
+ () => ({ assessment, refresh, isLoading, error }),
118
+ [assessment, refresh, isLoading, error]
119
+ );
79
120
 
80
121
  return (
81
- <MonocleContext.Provider value={{ assessment, refresh, isLoading, error }}>
122
+ <MonocleContext.Provider value={contextValue}>
82
123
  {children}
83
124
  </MonocleContext.Provider>
84
125
  );
@@ -90,6 +131,24 @@ export const MonocleProvider = withMaxAllowedInstancesGuard(
90
131
  'Only one instance of MonocleProvider is allowed'
91
132
  );
92
133
 
134
+ /**
135
+ * Hook to access the Monocle context.
136
+ *
137
+ * @returns {MonocleContextType} The Monocle context containing assessment data, loading state, and error information
138
+ * @throws {Error} When used outside of a MonocleProvider
139
+ *
140
+ * @example
141
+ * ```tsx
142
+ * function MyComponent() {
143
+ * const { assessment, isLoading, error, refresh } = useMonocle();
144
+ *
145
+ * if (isLoading) return <div>Loading...</div>;
146
+ * if (error) return <div>Error: {error.message}</div>;
147
+ *
148
+ * return <div>Assessment: {assessment}</div>;
149
+ * }
150
+ * ```
151
+ */
93
152
  export const useMonocle = () => {
94
153
  const context = useContext(MonocleContext);
95
154
  if (!context) {
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import React from 'react';
2
+
1
3
  /**
2
4
  * Props for the MonocleProvider component.
3
5
  * @interface MonocleProviderProps
@@ -32,7 +32,8 @@ export function useMaxAllowedInstancesGuard(
32
32
  instanceCounter.set(name, count + 1);
33
33
 
34
34
  return () => {
35
- instanceCounter.set(name, (instanceCounter.get(name) || 1) - 1);
35
+ const currentCount = instanceCounter.get(name) || 0;
36
+ instanceCounter.set(name, Math.max(0, currentCount - 1));
36
37
  };
37
38
  }, []);
38
39
  }