(function () { // Ensure required globals exist if (!window.algoliasearch || !window['@algolia/autocomplete-js']) { console.error('[Algolia] Missing algoliasearch or @algolia/autocomplete-js. Check script includes.'); return; } const { autocomplete, getAlgoliaResults } = window['@algolia/autocomplete-js']; // --- 1) Algolia client (your NEW site keys) --- const appId = 'WW70UGRCSD'; const apiKey = '212dac5865d6fe58730a95a02ba562f3'; // search-only key recommended const searchClient = algoliasearch(appId, apiKey); // --- 2) Debounced getItems --- function debouncePromise(fn, wait) { let t; return (...args) => new Promise((resolve) => { clearTimeout(t); t = setTimeout(async () => resolve(fn(...args)), wait); }); } const debouncedToolsResults = debouncePromise((query) => { return getAlgoliaResults({ searchClient, queries: [ { indexName: 'Tools', query, params: { attributesToSnippet: ['name:10', 'shortDescription:40'], snippetEllipsisText: '…', hitsPerPage: 10, }, }, ], }); }, 500); // --- 3) Autocomplete (hidden container, detached UI) --- const instance = autocomplete({ container: '#algolia-hidden-autocomplete', placeholder: 'Search tools (⌘ + K)', detachedMediaQuery: '', // force detached overlay always openOnFocus: true, getSources({ query }) { // Pre-results / quick links when empty if (!query) { const ICONS = { featured: ` `, tools: ` `, categories: ` `, submit: ` `, }; return [ { sourceId: 'quickLinks', getItems() { return [ { key: 'featured', label: 'Featured tools', description: 'Browse our favorite tools.', url: '/featured', icon: ICONS.featured, action: 'link', }, { key: 'tools', label: 'All tools', description: 'Browse all tools.', url: '/tools', icon: ICONS.tools, action: 'link', }, { key: 'categories', label: 'Categories', description: 'Explore tools by category.', url: '/categories', icon: ICONS.categories, action: 'link', }, { key: 'submit', label: 'Submit tool', description: 'Submit your tool.', url: '#', icon: ICONS.submit, action: 'modal', modalId: 'add-site', // matches your attribute: data-open-modal="add-site" }, ]; }, templates: { header() { return null; }, item({ item, html }) { const attrs = item.action === 'modal' ? `href="#" data-open-modal="${item.modalId}"` : `href="${item.url}"`; return html`
${html([item.icon])}
${item.label}
${item.description}
`; }, }, onSelect({ item }) { if (item.action === 'modal') { const el = document.querySelector(`[data-open-modal="${item.modalId}"]`); if (el) el.click(); return; } window.location.href = item.url; }, }, ]; } // Actual Algolia results return [ { sourceId: 'tools', async getItems() { return debouncedToolsResults(query); }, templates: { header({ html }) { return html`Tools
`; }, item({ item, components, html }) { const favicon = item.faviconUrl; // must exist in Algolia record (or it'll hide) const url = item.wfUrl; // must exist in Algolia record const name = item.name || ''; return html`
${favicon ? html`${name}` : ''}
${components.Snippet({ hit: item, attribute: 'name' })}
${item.featured ? html` ` : ''} ${item.claimed ? html`
` : ''}
${components.Snippet({ hit: item, attribute: 'shortDescription' })}
`; }, noResults() { return 'No tools for this query.'; }, }, getItemUrl({ item }) { return item.wfUrl; }, }, ]; }, }); // --- 4) Open triggers (click + CMD+K) --- document.addEventListener('click', (event) => { const t = event.target.closest('[data-open-search]'); if (!t) return; event.preventDefault(); instance.setIsOpen(true); }); document.addEventListener('keydown', (event) => { if (event.metaKey && event.key.toLowerCase() === 'k') { event.preventDefault(); instance.setIsOpen(true); } }); })();