0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-06-22 20:48:27 +02:00
gitea/web_src/js/features/comp/SearchUserBox.ts
silverwind 26cdb902c9
Tighten search-box types, expand test prefix, drop stale CSS comment
- chooseFromApi is now generic over the response shape; each caller
  declares a typed *SearchResponse, removing four `any` annotations
- e2e test prefix: rc- → repo-collab- for legibility in the test DB
- web_src/css/modules/search.css: drop the stale leading comment

Co-Authored-By: Claude (Opus 4.7) <noreply@anthropic.com>
2026-04-26 21:16:32 +02:00

36 lines
1.6 KiB
TypeScript

import {chooseFromApi, type SearchResult} from '../../modules/search.ts';
const {appSubUrl} = window.config;
const looksLikeEmailAddressCheck = /^\S+@\S+$/;
type UserSearchResponse = {data: Array<{login: string; avatar_url: string; full_name: string}>};
export async function initCompSearchUserBox() {
const box = document.querySelector<HTMLElement>('#search-user-box');
if (!box) return;
const allowEmailInput = box.getAttribute('data-allow-email') === 'true';
const allowEmailDescription = box.getAttribute('data-allow-email-description') ?? undefined;
const includeOrgs = box.getAttribute('data-include-orgs') === 'true';
const url = `${appSubUrl}/user/search_candidates?q={query}&orgs=${includeOrgs}`;
const input = box.querySelector<HTMLInputElement>('input.prompt')!;
while (box.isConnected) {
const pick = await chooseFromApi<UserSearchResponse>(box, url, (response, query) => {
const items: SearchResult[] = [];
const queryUpper = query.toUpperCase();
for (const item of response.data) {
const result: SearchResult = {title: item.login, image: item.avatar_url, description: item.full_name};
if (queryUpper === item.login.toUpperCase()) items.unshift(result); // exact match floats to top
else items.push(result);
}
if (allowEmailInput && !items.length && looksLikeEmailAddressCheck.test(query)) {
items.push({title: query, description: allowEmailDescription});
}
return items;
});
input.value = pick.title;
input.dispatchEvent(new Event('change', {bubbles: true}));
}
}