UNPKG

@user-intent-detector/react

Version:

React bindings for the user-intent-detector core library, providing hooks and components to detect user intent like exit, conversion, hesitation, and confusion.

71 lines 2.33 kB
//packages/react/src/useUserIntent.ts import { useEffect, useState } from 'react'; import { UserIntentTracker } from '@user-intent-detector/core'; export function useUserIntent(options = {}) { const [intentData, setIntentData] = useState({ intent: 'browsing', confidence: 0.9, // Align with core's initial confidence idleTime: 0, timestamp: Date.now(), }); useEffect(() => { const tracker = new UserIntentTracker({ idleTimeout: options.idleTimeout ?? 10000, debug: options.debug ?? false, }); const handleIntentChange = (newIntent) => { setIntentData(newIntent); switch (newIntent.intent) { case 'browsing': options.onBrowsing?.(newIntent); break; case 'idle': options.onIdle?.(newIntent); break; case 'exiting': options.onExit?.(newIntent); break; case 'confused': options.onConfused?.(newIntent); break; case 'hesitation': options.onHesitation?.(newIntent); break; case 'engaged': options.onEngaged?.(newIntent); break; } }; try { tracker.onChange(handleIntentChange); tracker.start(); } catch (error) { if (options.debug) { console.error('Error starting UserIntentTracker:', error); } } return () => { try { tracker.destroy(); // Use destroy for complete cleanup tracker.removeCallback(handleIntentChange); } catch (error) { if (options.debug) { console.error('Error destroying UserIntentTracker:', error); } } }; }, [ options.idleTimeout, options.debug, options.onBrowsing, options.onIdle, options.onExit, options.onConfused, options.onHesitation, options.onEngaged, ]); return intentData; } //# sourceMappingURL=useUserIntent.js.map