/**
* Spaxel Dashboard - Contextual Help System
*
* Provides a searchable help overlay with fuzzy search for 30+ help articles.
* Each article has a title, explanation, and link to relevant dashboard section.
*/
(function() {
'use strict';
// ===== State =====
let helpArticles = [];
let helpOverlayVisible = false;
let searchQuery = '';
// ===== DOM Elements =====
let helpOverlay = null;
let searchInput = null;
let articlesList = null;
/**
* Initialize the help system
*/
async function init() {
// Load articles from JSON file
await loadArticles();
// Create help overlay
createHelpOverlay();
// Add help button to expert mode header
addHelpButton();
// Add keyboard shortcut (Ctrl/Cmd + ?)
document.addEventListener('keydown', handleKeyboardShortcut);
console.log('[Help] Module initialized with', helpArticles.length, 'articles');
}
/**
* Load help articles from JSON file
*/
async function loadArticles() {
try {
const response = await fetch('help_articles.json');
if (!response.ok) {
throw new Error('Failed to load help articles');
}
helpArticles = await response.json();
} catch (err) {
console.error('[Help] Failed to load articles:', err);
// Use fallback articles
helpArticles = getFallbackArticles();
}
}
/**
* Get fallback articles if JSON file fails to load
*/
function getFallbackArticles() {
return [
{
id: 'sensing-link',
title: 'What is a sensing link?',
content: 'A sensing link is the path between two spaxel nodes (one transmitting, one receiving). Motion in the space between them changes the WiFi signal, which spaxel detects.',
category: 'Basics',
action: null
},
{
id: 'detection-quality',
title: 'Why is my detection quality low?',
content: 'Low quality usually means interference from other WiFi devices, an obstacle in the sensing zone, or the node is too far from your router. Click "Diagnose" on the link to find the specific cause.',
category: 'Troubleshooting',
action: { label: 'View Fleet Status', url: '#/fleet' }
}
];
}
/**
* Create the help overlay
*/
function createHelpOverlay() {
helpOverlay = document.createElement('div');
helpOverlay.id = 'help-overlay';
helpOverlay.className = 'help-overlay';
helpOverlay.style.display = 'none';
helpOverlay.innerHTML = `
Help & Documentation
🔍
`;
document.body.appendChild(helpOverlay);
// Get references to key elements
searchInput = document.getElementById('help-search-input');
articlesList = document.getElementById('help-articles');
// Add search listener
searchInput.addEventListener('input', handleSearch);
}
/**
* Add help button to expert mode header
* Note: The button already exists in HTML (id="help-btn"), this just sets up the click handler
*/
function addHelpButton() {
// The help button already exists in the HTML with id="help-btn"
// Just ensure the click handler is properly set up
const existingBtn = document.getElementById('help-btn');
if (existingBtn) {
// Remove any existing onclick handler and add our own
existingBtn.removeAttribute('onclick');
existingBtn.addEventListener('click', () => HelpOverlay.toggle());
} else {
// Fallback: try again after a delay if DOM isn't ready
setTimeout(addHelpButton, 100);
}
}
/**
* Handle keyboard shortcut (Ctrl+? or Cmd+?)
*/
function handleKeyboardShortcut(e) {
if ((e.ctrlKey || e.metaKey) && e.key === '?') {
e.preventDefault();
HelpOverlay.toggle();
} else if (e.key === 'Escape' && helpOverlayVisible) {
HelpOverlay.close();
}
}
/**
* Handle search input
*/
function handleSearch(e) {
searchQuery = e.target.value.toLowerCase().trim();
renderArticles();
}
/**
* Render articles list based on search query
*/
function renderArticles() {
if (!articlesList) {
return;
}
// Filter articles based on search query
const filtered = helpArticles.filter(article => {
if (!searchQuery) {
return true;
}
const searchText = `${article.title} ${article.content} ${article.category}`.toLowerCase();
return searchText.includes(searchQuery) || fuzzyMatch(searchQuery, searchText);
});
// Render filtered articles
if (filtered.length === 0) {
articlesList.innerHTML = `
No articles found for "${searchQuery}"
Try different keywords or browse categories below.
`;
} else {
articlesList.innerHTML = filtered.map(article => renderArticle(article)).join('');
}
}
/**
* Fuzzy match helper
*/
function fuzzyMatch(query, text) {
if (query.length < 3) {
return false;
}
// Simple character-by-character fuzzy matching
let queryIdx = 0;
let textIdx = 0;
let matches = 0;
while (queryIdx < query.length && textIdx < text.length) {
if (query[queryIdx] === text[textIdx]) {
matches++;
queryIdx++;
}
textIdx++;
}
// Require at least 80% of query characters to match
return matches >= query.length * 0.8;
}
/**
* Render a single article
*/
function renderArticle(article) {
let actionHTML = '';
if (article.action) {
actionHTML = `
`;
}
return `