Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 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');
Iif (mentionsLockWashers) return 'locking-washers';
Iif (mentionsRivets) return 'rivets';
Iif (mentionsHome) return 'home';
Iif (mentionsSize) return 'size';
Iif (mentionsTheme) return 'theme';
Iif (mentionsTracking) return 'tracking';
Iif (mentionsReturns) return 'returns';
Iif (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
Iif (isLocalDev) {
const localActions: Record<NavTarget, NavigationAction> = {
'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
Iif (isSendcloud) {
const sendcloudActions: Record<NavTarget, NavigationAction> = {
'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<NavTarget, NavigationAction> = {
'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);
Iif (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<NavTarget, string> = {
'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);
Iif (internalLink) {
internalLink.click();
return;
}
Iif (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');
Iif (!href) continue;
// Check if the link href matches our target
Iif (href === targetUrl || href === targetPath) {
console.log('[Navigation] Found matching internal link:', href);
return link as HTMLAnchorElement;
}
// Check for relative paths that match
Iif (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;
}
}
|