Files
it-tools/src/tools/index.ts
T
Sebastian DanielssonandClaude Opus 5.5 4a80283786 feat(settings): add an offline mode that hides internet-only tools
"offline": true in tools-settings.json hides tools that need internet
access (menus and routes). Tools declare it with requiresInternet, either
always or unless an intranet mirror is configured. The settings helper
lives in a module without top-level await so eagerly loaded tool
index.ts files can use it without delaying app boot.

Refs sharevb/it-tools#571

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-25 18:51:17 +01:00

88 lines
3.4 KiB
TypeScript

import type { ExternalTool, ToolCategory, ToolWithCategory, ToolsFilter } from './tools.types';
import { translate as t } from '@/plugins/i18n.plugin';
import { appBaseUrl as base } from '@/utils/base-url';
const modules = import.meta.glob<true, string, ToolWithCategory>('./*/index.ts', { eager: true, import: 'tool' });
// Both config files are optional; fetch them in parallel so app boot waits on at
// most one network round-trip instead of two sequential ones. tools-settings.json is
// imported dynamically for the same reason: a static import would evaluate (and fetch)
// it before this module's own fetches start.
const [filterConfig, externalTools, { toolsSettings, isOfflineMode }] = await Promise.all([
fetch(`${base}tools-filter.json`)
.then((response) => (response.ok ? (response.json() as Promise<ToolsFilter>) : ({} as ToolsFilter)))
.catch(() => ({}) as ToolsFilter),
fetch(`${base}external-tools.json`)
.then((response) => (response.ok ? (response.json() as Promise<ExternalTool[]>) : ([] as ExternalTool[])))
.catch(() => [] as ExternalTool[]),
import('@/tools-settings'),
]);
const allModules: ToolWithCategory[] = Object.values(modules);
// markdown-it (and its dependency tree) is only needed when a deployment actually
// configures external tools, so it stays out of the startup bundle.
if (externalTools.length > 0) {
const { default: markdownit } = await import('markdown-it');
allModules.push(
...externalTools.map((externalTool) => {
const html = markdownit().render(
externalTool.markdownContent ||
(externalTool.href ? `${t('tools.external-link-goto')} [${externalTool.href}](${externalTool.href})` : ''),
);
return {
icon: defineAsyncComponent(() => import('@vicons/tabler/es/ExternalLink')),
...externalTool,
component: () => import('@/components/ExternalToolContent.vue'),
externalHTMLContent: html,
} as ToolWithCategory;
}),
);
}
const makeRegExp = (regex: string | undefined) => (regex ? new RegExp(regex, 'i') : null);
const filters = {
excludeCategoryFilterRegex: makeRegExp(filterConfig.excludeCategoryFilterRegex),
includeCategoryFilterRegex: makeRegExp(filterConfig.includeCategoryFilterRegex),
excludeToolsFilterRegex: makeRegExp(filterConfig.excludeToolsFilterRegex),
includeToolsFilterRegex: makeRegExp(filterConfig.includeToolsFilterRegex),
};
const requiresInternet = ({ requiresInternet }: ToolWithCategory) =>
typeof requiresInternet === 'function' ? requiresInternet(toolsSettings) : Boolean(requiresInternet);
const filteredModules = allModules.filter((tool) => {
if (isOfflineMode && requiresInternet(tool)) {
return false;
}
const category = tool.category || 'Development';
if (filters.includeToolsFilterRegex?.test(tool.path)) {
return true;
}
if (filters.includeCategoryFilterRegex?.test(category)) {
return true;
}
if (filters.excludeToolsFilterRegex?.test(tool.path)) {
return false;
}
if (filters.excludeCategoryFilterRegex?.test(category)) {
return false;
}
return true;
});
export const toolsByCategory = filteredModules.reduce((la, moduleDef) => {
let found = la.find((l) => l.name === moduleDef.category);
if (!found) {
found = {
name: moduleDef.category,
components: [],
};
la.push(found);
}
found.components.push(moduleDef);
return la;
}, [] as ToolCategory[]);
export const tools = toolsByCategory.flatMap(({ components }) => components);