feat(web): search the whole panel from one place
Finding anything meant knowing which tab it lived in — fine once you have learned the panel, hopeless before. ⌘K now covers cards, repositories, issues, wiki pages and members at once. Pages are matched in the browser, so the palette answers before it has asked the server and stays useful while the forge is down. Each remote source is queried independently and a source that fails is simply absent from the results, rather than taking the others with it. Issues come from one organization-wide request: per repository, the palette could not afford them on every keystroke. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
fcc82d82d6
commit
9acb523510
10 files changed
+926
-3
No files matched your search
@@ -12,6 +12,7 @@ import { registerOverviewRoutes } from './overview.js';
|
||||
import { registerProjectRoutes } from './projects.js';
|
||||
import { registerReleaseRoutes } from './releases.js';
|
||||
import { registerRepositoryRoutes } from './repositories.js';
|
||||
import { registerSearchRoutes } from './search.js';
|
||||
import { registerSettingsRoutes } from './settings.js';
|
||||
import { registerWikiRoutes } from './wiki.js';
|
||||
|
||||
@@ -60,6 +61,7 @@ export function registerApiRoutes(router, { setupOnly = false } = {}) {
|
||||
registerDeploymentRoutes(router);
|
||||
registerDiscordRoutes(router);
|
||||
registerCommunityRoutes(router);
|
||||
registerSearchRoutes(router);
|
||||
registerSettingsRoutes(router);
|
||||
registerLogRoutes(router);
|
||||
return router;
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
import { ACCESS_LEVELS } from '../../config/constants.js';
|
||||
import { env } from '../../config/env.js';
|
||||
import { createLogger } from '../../core/logger.js';
|
||||
import { searchCards } from '../../database/repositories/project-repository.js';
|
||||
import { searchPublished } from '../../database/repositories/wiki-repository.js';
|
||||
import { org } from '../../services/gitea/gitea-admin.js';
|
||||
import { isGiteaConfigured } from '../../services/gitea/gitea-client.js';
|
||||
import { pagePath } from '../services/wiki-seed.js';
|
||||
|
||||
/**
|
||||
* One search across everything the dashboard knows (M19).
|
||||
*
|
||||
* Until now, finding something meant knowing which tab it lived in — which is
|
||||
* fine once you have learned the panel and hopeless before. Each source is
|
||||
* asked independently and a source that fails is simply left out: a forge that
|
||||
* is down must not take the wiki and the boards with it.
|
||||
*/
|
||||
|
||||
const log = createLogger('web:search');
|
||||
const guildId = () => env.discord.guildId;
|
||||
|
||||
/** Below this a search matches half the organization, which helps nobody. */
|
||||
const MIN_TERM = 2;
|
||||
const PER_GROUP = 6;
|
||||
|
||||
/**
|
||||
* Repository lists change by the hour and are asked for on every keystroke, so
|
||||
* they are kept briefly. The stamp only moves on success, which turns a forge
|
||||
* outage into slightly old results rather than none.
|
||||
*/
|
||||
const REPO_TTL_MS = 60_000;
|
||||
let repoCache = { at: 0, repositories: [] };
|
||||
|
||||
async function repositories() {
|
||||
if (!isGiteaConfigured()) return [];
|
||||
if (Date.now() - repoCache.at < REPO_TTL_MS) return repoCache.repositories;
|
||||
|
||||
const list = await org.listRepos({ limit: 50 }).catch((error) => {
|
||||
log.debug('Falling back to the cached repository list', error);
|
||||
return null;
|
||||
});
|
||||
if (list) repoCache = { at: Date.now(), repositories: list };
|
||||
return repoCache.repositories;
|
||||
}
|
||||
|
||||
const contains = (haystack, needle) =>
|
||||
String(haystack ?? '')
|
||||
.toLowerCase()
|
||||
.includes(needle);
|
||||
|
||||
async function searchRepositories(needle) {
|
||||
const list = await repositories();
|
||||
return list
|
||||
.filter(
|
||||
(repository) => contains(repository.full_name, needle) || contains(repository.description, needle),
|
||||
)
|
||||
.slice(0, PER_GROUP)
|
||||
.map((repository) => ({
|
||||
kind: 'repository',
|
||||
title: repository.full_name,
|
||||
subtitle: repository.description || 'Dépôt de l’organisation',
|
||||
href: `#/repos/${repository.full_name}`,
|
||||
badge: repository.private ? 'Privé' : null,
|
||||
}));
|
||||
}
|
||||
|
||||
async function searchIssues(needle) {
|
||||
if (!isGiteaConfigured()) return [];
|
||||
|
||||
// `all` rather than `open`: someone searching for an issue by name usually
|
||||
// wants the one they remember, closed or not.
|
||||
const issues = await org.searchIssues({ q: needle, state: 'all', limit: PER_GROUP }).catch((error) => {
|
||||
log.debug('The forge did not answer the issue search', error);
|
||||
return [];
|
||||
});
|
||||
|
||||
return issues.slice(0, PER_GROUP).map((issue) => ({
|
||||
kind: issue.pull_request ? 'pull' : 'issue',
|
||||
title: issue.title,
|
||||
subtitle: `${issue.repository?.full_name ?? 'Gitea'}#${issue.number}`,
|
||||
href: issue.html_url,
|
||||
external: true,
|
||||
badge: issue.state === 'closed' ? 'Fermée' : 'Ouverte',
|
||||
}));
|
||||
}
|
||||
|
||||
function searchBoardCards(needle) {
|
||||
return searchCards(guildId(), needle, PER_GROUP).map((card) => ({
|
||||
kind: 'card',
|
||||
title: card.title,
|
||||
subtitle: `${card.board_name} · ${card.column_name}`,
|
||||
href: `#/projects/${card.board_id}`,
|
||||
badge: card.column_is_done ? 'Terminée' : (card.issue_ref ?? null),
|
||||
}));
|
||||
}
|
||||
|
||||
function searchWiki(needle) {
|
||||
return searchPublished(needle)
|
||||
.slice(0, PER_GROUP)
|
||||
.map((page) => ({
|
||||
kind: 'wiki',
|
||||
title: page.title,
|
||||
subtitle: page.summary || page.category,
|
||||
href: pagePath(page.slug),
|
||||
external: true,
|
||||
badge: page.category,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Members come from the cache rather than a fetch: pulling the roster from
|
||||
* Discord on every keystroke would be slow and rate-limited, and whoever is
|
||||
* being looked for is almost always already cached.
|
||||
*/
|
||||
function searchMembers(client, needle) {
|
||||
const guild = client?.guilds?.cache?.get(guildId());
|
||||
if (!guild) return [];
|
||||
|
||||
return [...guild.members.cache.values()]
|
||||
.filter(
|
||||
(member) =>
|
||||
contains(member.user.username, needle) ||
|
||||
contains(member.displayName, needle) ||
|
||||
member.id === needle,
|
||||
)
|
||||
.slice(0, PER_GROUP)
|
||||
.map((member) => ({
|
||||
kind: 'member',
|
||||
title: member.displayName,
|
||||
subtitle: `@${member.user.username}`,
|
||||
href: `#/discord?member=${encodeURIComponent(member.user.username)}`,
|
||||
badge: member.user.bot ? 'Bot' : null,
|
||||
}));
|
||||
}
|
||||
|
||||
export function registerSearchRoutes(router) {
|
||||
router.get(
|
||||
'/api/search',
|
||||
async ({ query, context }) => {
|
||||
const term = String(query.get('q') ?? '').trim();
|
||||
if (term.length < MIN_TERM) return { term, results: [], truncated: false };
|
||||
|
||||
const needle = term.toLowerCase();
|
||||
const [repos, issues] = await Promise.all([searchRepositories(needle), searchIssues(term)]);
|
||||
|
||||
// Local sources first: they are exact, instant, and what the panel owns.
|
||||
const results = [
|
||||
...searchBoardCards(term),
|
||||
...repos,
|
||||
...issues,
|
||||
...searchWiki(term),
|
||||
...searchMembers(context.client, needle),
|
||||
];
|
||||
|
||||
return { term, results, truncated: results.length >= PER_GROUP * 5 };
|
||||
},
|
||||
{ access: ACCESS_LEVELS.STAFF },
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user