Add live topbar search for hosting accounts and sites.
Deploy Ladill Hosting / deploy (push) Successful in 20s
Deploy Ladill Hosting / deploy (push) Successful in 20s
Wire a JSON /search endpoint and Alpine dropdown so customers can find accounts, linked domains, and orders from the hosting app header. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -50,5 +50,50 @@ Alpine.data('afia', (config = {}) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
Alpine.data('topbarSearch', (config = {}) => ({
|
||||
query: '',
|
||||
results: [],
|
||||
open: false,
|
||||
loading: false,
|
||||
active: 0,
|
||||
_abort: null,
|
||||
searchUrl: config.searchUrl || '/search',
|
||||
|
||||
onFocus() {
|
||||
if (this.results.length > 0 || this.query.trim().length >= 2) this.open = true;
|
||||
},
|
||||
|
||||
async search() {
|
||||
const q = this.query.trim();
|
||||
if (q.length < 2) { this.results = []; this.open = false; return; }
|
||||
|
||||
this.loading = true;
|
||||
this.open = true;
|
||||
this.active = 0;
|
||||
|
||||
if (this._abort) this._abort.abort();
|
||||
this._abort = new AbortController();
|
||||
|
||||
try {
|
||||
const res = await fetch(`${this.searchUrl}?q=${encodeURIComponent(q)}`, {
|
||||
signal: this._abort.signal,
|
||||
headers: { Accept: 'application/json', 'X-Requested-With': 'XMLHttpRequest' },
|
||||
});
|
||||
const data = await res.json();
|
||||
this.results = data.results || [];
|
||||
} catch (e) {
|
||||
if (e.name !== 'AbortError') this.results = [];
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
moveDown() { if (this.active < this.results.length - 1) this.active++; },
|
||||
moveUp() { if (this.active > 0) this.active--; },
|
||||
go() {
|
||||
if (this.results[this.active]) window.location.href = this.results[this.active].url;
|
||||
},
|
||||
}));
|
||||
|
||||
window.Alpine = Alpine;
|
||||
Alpine.start();
|
||||
|
||||
Reference in New Issue
Block a user