loading up the forgejo repo on tangled to test page performance
at forgejo 48 lines 1.7 kB view raw
1import $ from 'jquery'; 2import {htmlEscape} from 'escape-goat'; 3 4const {appSubUrl} = window.config; 5const looksLikeEmailAddressCheck = /^\S+@\S+$/; 6 7export function initCompSearchUserBox() { 8 const searchUserBox = document.getElementById('search-user-box'); 9 if (!searchUserBox) return; 10 11 const allowEmailInput = searchUserBox.getAttribute('data-allow-email') === 'true'; 12 const allowEmailDescription = searchUserBox.getAttribute('data-allow-email-description') ?? undefined; 13 $(searchUserBox).search({ 14 minCharacters: 2, 15 apiSettings: { 16 url: `${appSubUrl}/user/search_candidates?q={query}`, 17 onResponse(response) { 18 const resultItems = []; 19 const searchQuery = searchUserBox.querySelector('input').value; 20 const searchQueryUppercase = searchQuery.toUpperCase(); 21 for (const item of response.data) { 22 const resultItem = { 23 title: item.login, 24 image: item.avatar_url, 25 description: htmlEscape(item.full_name), 26 }; 27 if (searchQueryUppercase === item.login.toUpperCase()) { 28 resultItems.unshift(resultItem); // add the exact match to the top 29 } else { 30 resultItems.push(resultItem); 31 } 32 } 33 34 if (allowEmailInput && !resultItems.length && looksLikeEmailAddressCheck.test(searchQuery)) { 35 const resultItem = { 36 title: searchQuery, 37 description: allowEmailDescription, 38 }; 39 resultItems.push(resultItem); 40 } 41 42 return {results: resultItems}; 43 }, 44 }, 45 searchFields: ['login', 'full_name'], 46 showNoResults: false, 47 }); 48}