export type NavTarget = | 'locking-washers' | 'rivets' | 'home' | 'size' | 'theme' | 'tracking' | 'returns' | 'success'; export interface NavigationAction { type: 'url' | 'hash' | 'event' | 'function'; value: string | (() => void); description: string; } export function detectNavTarget(text: string): NavTarget | null { const lower = text.toLowerCase(); // Existing targets const mentionsLockWashers = lower.includes('lock washer') || lower.includes('lock washers') || lower.includes('locking washer') || lower.includes('locking washers'); const mentionsRivets = lower.includes('rivet') || lower.includes('rivets') || lower.includes('plastic rivet'); // New local dev targets const mentionsHome = lower.includes('home'); const mentionsSize = lower.includes('size'); const mentionsTheme = lower.includes('theme'); // New sendcloud targets const mentionsTracking = lower.includes('track') || lower.includes('tracking') || lower.includes('parcel') || lower.includes('shipping'); const mentionsReturns = lower.includes('return') || lower.includes('returns') || lower.includes('refund'); const mentionsSuccess = lower.includes('success') || lower.includes('customers') || lower.includes('customer success') || lower.includes('testimonials'); if (mentionsLockWashers) return 'locking-washers'; if (mentionsRivets) return 'rivets'; if (mentionsHome) return 'home'; if (mentionsSize) return 'size'; if (mentionsTheme) return 'theme'; if (mentionsTracking) return 'tracking'; if (mentionsReturns) return 'returns'; if (mentionsSuccess) return 'success'; return null; } export function getNavigationAction(target: NavTarget): NavigationAction { const currentDomain = typeof window !== 'undefined' ? window.location.hostname : ''; const isLocalDev = currentDomain === 'localhost' || currentDomain.includes('127.0.0.1'); const isSendcloud = currentDomain === 'www.sendcloud.com' || currentDomain === 'sendcloud.com'; // Local development actions if (isLocalDev) { const localActions: Record = { 'locking-washers': { type: 'url', value: 'https://www.accu.co.uk/shop/precision-components/washers-spacers/locking-washers/external-fine-tooth-locking-washers?page=1', description: 'Navigate to Accu.co.uk locking washers page' }, 'rivets': { type: 'url', value: 'https://www.accu.co.uk/shop/precision-components/rivets/plastic-rivets', description: 'Navigate to Accu.co.uk plastic rivets page' }, 'home': { type: 'url', value: 'http://localhost:8083/chatbot/dev/index.html', description: 'Navigate to home page' }, 'size': { type: 'url', value: 'http://localhost:8083/chatbot/dev/size.html', description: 'Navigate to size control page' }, 'theme': { type: 'url', value: 'http://localhost:8083/chatbot/dev/theme.html', description: 'Navigate to theme customization page' }, 'tracking': { type: 'url', value: 'http://localhost:8083/chatbot/dev/api.html', description: 'Navigate to API testing page' }, 'returns': { type: 'url', value: 'http://localhost:8083/chatbot/dev/api.html', description: 'Navigate to API testing page' }, 'success': { type: 'url', value: 'http://localhost:8083/chatbot/dev/api.html', description: 'Navigate to API testing page' }, } as const; return localActions[target]; } // Sendcloud.com actions if (isSendcloud) { const sendcloudActions: Record = { 'locking-washers': { type: 'url', value: 'https://www.accu.co.uk/shop/precision-components/washers-spacers/locking-washers/external-fine-tooth-locking-washers?page=1', description: 'Navigate to Accu.co.uk locking washers page' }, 'rivets': { type: 'url', value: 'https://www.accu.co.uk/shop/precision-components/rivets/plastic-rivets', description: 'Navigate to Accu.co.uk plastic rivets page' }, 'home': { type: 'url', value: 'https://www.sendcloud.com/', description: 'Navigate to Sendcloud home page' }, 'size': { type: 'url', value: 'https://www.sendcloud.com/', description: 'Navigate to Sendcloud home page' }, 'theme': { type: 'url', value: 'https://www.sendcloud.com/', description: 'Navigate to Sendcloud home page' }, 'tracking': { type: 'url', value: 'https://www.sendcloud.com/parcel-tracking-solution/', description: 'Navigate to Sendcloud parcel tracking solution' }, 'returns': { type: 'url', value: 'https://www.sendcloud.com/returns-management-software/', description: 'Navigate to Sendcloud returns management software' }, 'success': { type: 'url', value: 'https://www.sendcloud.com/customer-success/', description: 'Navigate to Sendcloud customer success page' }, } as const; return sendcloudActions[target]; } // Default actions (fallback) const defaultActions: Record = { 'locking-washers': { type: 'url', value: 'https://www.accu.co.uk/shop/precision-components/washers-spacers/locking-washers/external-fine-tooth-locking-washers?page=1', description: 'Navigate to Accu.co.uk locking washers page' }, 'rivets': { type: 'url', value: 'https://www.accu.co.uk/shop/precision-components/rivets/plastic-rivets', description: 'Navigate to Accu.co.uk plastic rivets page' }, 'home': { type: 'url', value: 'https://www.sendcloud.com/', description: 'Navigate to Sendcloud home page' }, 'size': { type: 'url', value: 'https://www.sendcloud.com/', description: 'Navigate to Sendcloud home page' }, 'theme': { type: 'url', value: 'https://www.sendcloud.com/', description: 'Navigate to Sendcloud home page' }, 'tracking': { type: 'url', value: 'https://www.sendcloud.com/parcel-tracking-solution/', description: 'Navigate to Sendcloud parcel tracking solution' }, 'returns': { type: 'url', value: 'https://www.sendcloud.com/returns-management-software/', description: 'Navigate to Sendcloud returns management software' }, 'success': { type: 'url', value: 'https://www.sendcloud.com/customer-success/', description: 'Navigate to Sendcloud customer success page' }, } as const; return defaultActions[target]; } // Legacy function for backward compatibility export function urlForNavTarget(target: NavTarget): string { const action = getNavigationAction(target); if (action.type === 'url') { return action.value as string; } // For non-URL actions, return a fallback URL return typeof window !== 'undefined' ? window.location.href : ''; } export function getNavigationResponse(target: NavTarget): string { const responses: Record = { 'locking-washers': "I'll take you to our locking washers page. These precision components are perfect for preventing loosening in high-vibration applications.", 'rivets': "Let me show you our plastic rivets selection. These are ideal for lightweight assemblies and non-conductive applications.", 'home': "Taking you to the home page where you can explore all our features and solutions.", 'size': "Navigating to the size control system. Here you can adjust the widget size and positioning to fit your needs.", 'theme': "Switching to the theme customization area. You can personalize the appearance to match your brand.", 'tracking': "I'm taking you to our parcel tracking solution. Turn shipping into an experience with branded tracking pages and automated notifications.", 'returns': "Navigating to our returns management software. Take the hassle out of returns for you and your customers with our automated system.", 'success': "Let me show you our customer success stories. See how businesses are growing with Sendcloud's shipping solutions.", } as const; return responses[target]; } /** * Navigate to a URL in a way that works with SPAs * This function attempts multiple navigation strategies to ensure compatibility * with different SPA frameworks (React Router, Vue Router, Angular Router, etc.) */ export function navigateToUrl(url: string): void { try { const internalLink = findInternalLink(url); if (internalLink) { internalLink.click(); return; } if (isSameOrigin(url)) { const currentUrlObj = new URL(window.location.href); const targetUrlObj = new URL(url); if (targetUrlObj.pathname !== currentUrlObj.pathname || targetUrlObj.hash || targetUrlObj.search !== currentUrlObj.search) { window.location.href = url; return; } else { window.history.pushState({}, '', url); window.dispatchEvent(new PopStateEvent('popstate')); return; } } window.location.href = url; } catch (error) { console.error('[Navigation] Navigation failed:', error); window.location.href = url; } } /** * Find an internal link that matches the target URL * This works for SPAs that have the target route as a link in the DOM */ function findInternalLink(targetUrl: string): HTMLAnchorElement | null { const targetPath = new URL(targetUrl).pathname; // Look for links that match the target path const links = Array.from(document.querySelectorAll('a[href]')); for (const link of links) { const href = link.getAttribute('href'); if (!href) continue; // Check if the link href matches our target if (href === targetUrl || href === targetPath) { console.log('[Navigation] Found matching internal link:', href); return link as HTMLAnchorElement; } // Check for relative paths that match if (href.startsWith('/') && href === targetPath) { console.log('[Navigation] Found matching relative link:', href); return link as HTMLAnchorElement; } } return null; } /** * Check if a URL is on the same origin as the current page */ function isSameOrigin(url: string): boolean { try { const currentOrigin = window.location.origin; const targetOrigin = new URL(url).origin; return currentOrigin === targetOrigin; } catch { return false; } }