2025-08-10 16:44:01 -04:00
|
|
|
import {emojiKeys} from '../features/emoji.js';
|
2026-01-18 03:32:38 -05:00
|
|
|
import {GET} from '../modules/fetch.js';
|
2023-04-22 11:32:34 -04:00
|
|
|
|
|
|
|
|
const maxMatches = 6;
|
|
|
|
|
|
|
|
|
|
function sortAndReduce(map) {
|
2023-05-17 21:14:31 -04:00
|
|
|
const sortedMap = new Map(Array.from(map.entries()).sort((a, b) => a[1] - b[1]));
|
2023-04-22 11:32:34 -04:00
|
|
|
return Array.from(sortedMap.keys()).slice(0, maxMatches);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function matchEmoji(queryText) {
|
|
|
|
|
const query = queryText.toLowerCase().replaceAll('_', ' ');
|
2025-08-10 16:44:01 -04:00
|
|
|
if (!query) return emojiKeys.slice(0, maxMatches);
|
2023-04-22 11:32:34 -04:00
|
|
|
|
|
|
|
|
// results is a map of weights, lower is better
|
|
|
|
|
const results = new Map();
|
2025-08-10 16:44:01 -04:00
|
|
|
for (const emojiKey of emojiKeys) {
|
|
|
|
|
const index = emojiKey.replaceAll('_', ' ').indexOf(query);
|
|
|
|
|
if (index === -1) continue;
|
|
|
|
|
results.set(emojiKey, index);
|
2023-04-22 11:32:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sortAndReduce(results);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function matchMention(queryText) {
|
|
|
|
|
const query = queryText.toLowerCase();
|
|
|
|
|
|
|
|
|
|
// results is a map of weights, lower is better
|
|
|
|
|
const results = new Map();
|
2023-08-12 04:36:23 -04:00
|
|
|
for (const obj of window.config.mentionValues ?? []) {
|
2023-04-22 11:32:34 -04:00
|
|
|
const index = obj.key.toLowerCase().indexOf(query);
|
|
|
|
|
if (index === -1) continue;
|
|
|
|
|
const existing = results.get(obj);
|
|
|
|
|
results.set(obj, existing ? existing - index : index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sortAndReduce(results);
|
|
|
|
|
}
|
2025-06-05 18:25:07 -04:00
|
|
|
|
2026-03-21 04:55:19 -04:00
|
|
|
export async function matchIssue(owner, repo, issueIndexStr, query, isPull) {
|
|
|
|
|
let url = `${window.config.appSubUrl}/${owner}/${repo}/issues/suggestions?q=${encodeURIComponent(query)}`;
|
|
|
|
|
if (isPull !== undefined) {
|
|
|
|
|
url += `&pull=${isPull ? '1' : '0'}`;
|
2026-03-21 04:28:00 -04:00
|
|
|
}
|
|
|
|
|
const res = await GET(url);
|
2025-06-05 18:25:07 -04:00
|
|
|
|
2026-01-18 03:32:38 -05:00
|
|
|
const issues = await res.json();
|
|
|
|
|
const issueIndex = parseInt(issueIndexStr);
|
2025-06-05 18:25:07 -04:00
|
|
|
|
2026-01-18 03:32:38 -05:00
|
|
|
// filter out issue with same id
|
|
|
|
|
return issues.filter((i) => i.id !== issueIndex);
|
2025-06-05 18:25:07 -04:00
|
|
|
}
|