|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect } from 'react'; |
| 4 | + |
| 5 | +type WatsonAssistantInstance = { |
| 6 | + render: () => Promise<void>; |
| 7 | + destroy: () => void; |
| 8 | +}; |
| 9 | + |
| 10 | +// Extend Window interface for WatsonX |
| 11 | +declare global { |
| 12 | + interface Window { |
| 13 | + watsonAssistantChatOptions?: { |
| 14 | + integrationID: string; |
| 15 | + region: string; |
| 16 | + serviceInstanceID: string; |
| 17 | + clientVersion?: string; |
| 18 | + onLoad: (instance: WatsonAssistantInstance) => Promise<void>; |
| 19 | + }; |
| 20 | + watsonAssistantInstance?: { |
| 21 | + // The web chat instance exposes a destroy() method that removes all UI |
| 22 | + destroy: () => void; |
| 23 | + } | null; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// West Midlands locations that should have WatsonX chat |
| 28 | +const WATSON_X_LOCATIONS = [ |
| 29 | + 'birmingham', |
| 30 | + 'sandwell', |
| 31 | + 'walsall', |
| 32 | + 'wolverhampton', |
| 33 | + 'coventry', |
| 34 | + 'dudley', |
| 35 | + 'solihull' |
| 36 | +]; |
| 37 | + |
| 38 | +interface WatsonXChatProps { |
| 39 | + locationSlug?: string; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * WatsonX Chat component for West Midlands locations |
| 44 | + * Injects the IBM WatsonX Assistant chat widget |
| 45 | + * |
| 46 | + * @param locationSlug - Optional location slug to determine if chat should be shown |
| 47 | + * If not provided, chat is always shown (for west-midlands hub page) |
| 48 | + */ |
| 49 | +export default function WatsonXChat({ locationSlug }: WatsonXChatProps) { |
| 50 | + // Determine if we should show the chat widget |
| 51 | + const shouldShowChat = !locationSlug || WATSON_X_LOCATIONS.includes(locationSlug); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + // Only run on client side |
| 55 | + if (typeof window === 'undefined') return; |
| 56 | + |
| 57 | + // Don't initialize if this location shouldn't have the chat |
| 58 | + if (!shouldShowChat) { |
| 59 | + // Clean up any existing instance when navigating away from WM locations |
| 60 | + if (window.watsonAssistantInstance) { |
| 61 | + try { |
| 62 | + window.watsonAssistantInstance.destroy(); |
| 63 | + } catch (error) { |
| 64 | + console.error('Error destroying Watson Assistant instance', error); |
| 65 | + } |
| 66 | + window.watsonAssistantInstance = null; |
| 67 | + } |
| 68 | + const existingScript = document.querySelector( |
| 69 | + 'script[src*="WatsonAssistantChatEntry.js"]' |
| 70 | + ); |
| 71 | + if (existingScript) { |
| 72 | + existingScript.remove(); |
| 73 | + } |
| 74 | + delete window.watsonAssistantChatOptions; |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Check if already initialized |
| 79 | + if (window.watsonAssistantChatOptions) return; |
| 80 | + |
| 81 | + // Set up Watson Assistant options |
| 82 | + window.watsonAssistantChatOptions = { |
| 83 | + integrationID: '83b099b7-08a1-4118-bba3-341fbec366d1', |
| 84 | + region: 'eu-gb', |
| 85 | + serviceInstanceID: 'a3a6beaa-5967-4039-8390-d48ace365d86', |
| 86 | + onLoad: async (instance) => { |
| 87 | + // Keep a reference so we can destroy it when the component unmounts |
| 88 | + window.watsonAssistantInstance = instance; |
| 89 | + await instance.render(); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + // Load the Watson Assistant script |
| 94 | + const script = document.createElement('script'); |
| 95 | + script.src = `https://web-chat.global.assistant.watson.appdomain.cloud/versions/${ |
| 96 | + window.watsonAssistantChatOptions.clientVersion || 'latest' |
| 97 | + }/WatsonAssistantChatEntry.js`; |
| 98 | + script.async = true; |
| 99 | + document.head.appendChild(script); |
| 100 | + |
| 101 | + // Cleanup on unmount |
| 102 | + return () => { |
| 103 | + // Destroy the existing chat instance so the widget disappears |
| 104 | + if (window.watsonAssistantInstance) { |
| 105 | + try { |
| 106 | + window.watsonAssistantInstance.destroy(); |
| 107 | + } catch (error) { |
| 108 | + console.error('Error destroying Watson Assistant instance', error); |
| 109 | + } |
| 110 | + window.watsonAssistantInstance = null; |
| 111 | + } |
| 112 | + |
| 113 | + // Remove the script if component unmounts |
| 114 | + const existingScript = document.querySelector( |
| 115 | + 'script[src*="WatsonAssistantChatEntry.js"]' |
| 116 | + ); |
| 117 | + if (existingScript) { |
| 118 | + existingScript.remove(); |
| 119 | + } |
| 120 | + |
| 121 | + // Clean up the global options |
| 122 | + delete window.watsonAssistantChatOptions; |
| 123 | + }; |
| 124 | + }, [shouldShowChat, locationSlug]); |
| 125 | + |
| 126 | + // This component doesn't render anything visible |
| 127 | + return null; |
| 128 | +} |
0 commit comments