0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-06-23 16:21:44 +02:00
gitea/web_src/js/features/comp/SearchRepoBox.ts
silverwind 2ecee0bb0a
Refactor search-box module to await-style chooseFromApi
Move web_src/js/modules/fomantic/search.ts to web_src/js/modules/search.ts
(no Fomantic dependency anymore). Replace the imperative
initSearchBox(el, opts) with chooseFromApi(el, url, parse) that returns
a Promise<SearchResult> resolving with the user's chosen item; callers
loop with `while (box.isConnected) { const pick = await ...; ... }` and
own writing back to whatever input/state they manage.

Cleanup is via a single AbortController whose signal is passed to all
listeners; the in-flight fetch has its own controller so a new keystroke
can cancel just the request without tearing down listeners.

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

21 lines
775 B
TypeScript

import {chooseFromApi} from '../../modules/search.ts';
const {appSubUrl} = window.config;
export async function initCompSearchRepoBox(el: HTMLElement) {
const uid = el.getAttribute('data-uid');
const exclusive = el.getAttribute('data-exclusive');
let url = `${appSubUrl}/repo/search?q={query}&uid=${uid}`;
if (exclusive === 'true') url += `&exclusive=true`;
const input = el.querySelector<HTMLInputElement>('input.prompt')!;
while (el.isConnected) {
const pick = await chooseFromApi(el, url, (response: any) => response.data.map((item: any) => ({
title: item.repository.full_name.split('/')[1],
description: item.repository.full_name,
})));
input.value = pick.title;
input.dispatchEvent(new Event('change', {bubbles: true}));
}
}