fix(i18n): tool search in current language and handle switching

Fix #486
This commit is contained in:
ShareVB committed 2026-07-27 13:59:21 +02:00
1 parent be4d22587f
commit e5a2017f7d
5 files changed
+168 -107

No files matched your search

@@ -11,85 +11,89 @@ import DiceIcon from '~icons/mdi/dice-5';
import InfoIcon from '~icons/mdi/information-outline';
import { useFlexSearch } from '@/composable/flexSearch';
import { translate as t } from '@/plugins/i18n.plugin';
const maxSearchResultsPerCategory = import.meta.env.VITE_MAX_SEARCH_RESULT || 25;
export const useCommandPaletteStore = defineStore('command-palette', () => {
const toolStore = useToolStore();
const styleStore = useStyleStore();
const router = useRouter();
const searchPrompt = ref('');
export const useCommandPaletteStore = (locale: string) =>
defineStore(`command-palette-${locale}`, () => {
const toolStore = useToolStore();
const styleStore = useStyleStore();
const router = useRouter();
const searchPrompt = ref('');
const toolsOptions = toolStore.tools.map(tool => ({
...tool,
to: tool.path,
toolCategory: tool.category,
category: 'Tools',
}));
const toolsOptions = toolStore.tools.map((tool) => ({
...tool,
to: tool.path,
toolCategory: tool.category,
category: t('tools.command-palette.store.texts.tools'),
}));
const searchOptions: PaletteOption[] = [
...toolsOptions,
{
name: 'Random tool',
description: 'Get a random tool from the list.',
action: () => {
const { path } = _.sample(toolStore.tools)!;
router.push(path);
const searchOptions: PaletteOption[] = [
...toolsOptions,
{
name: t('tools.command-palette.store.texts.random-tool'),
description: t('tools.command-palette.store.texts.get-a-random-tool-from-the-list'),
action: () => {
const { path } = _.sample(toolStore.tools)!;
router.push(path);
},
icon: DiceIcon,
category: t('tools.command-palette.store.texts.tools-0'),
keywords: ['random', 'tool', 'pick', 'choose', 'select'],
closeOnSelect: true,
},
icon: DiceIcon,
category: 'Tools',
keywords: ['random', 'tool', 'pick', 'choose', 'select'],
closeOnSelect: true,
},
{
name: 'Toggle dark mode',
description: 'Toggle dark mode on or off.',
action: () => styleStore.toggleDark(),
icon: SunIcon,
category: 'Actions',
keywords: ['dark', 'theme', 'toggle', 'mode', 'light', 'system'],
},
{
name: 'Github repository',
href: 'https://github.com/sharevb/it-tools',
category: 'External',
description: 'View the source code of it-tools on Github.',
keywords: ['github', 'repo', 'repository', 'source', 'code'],
icon: GithubIcon,
},
{
name: 'Report a bug or an issue',
description: 'Report a bug or an issue to help improve it-tools.',
href: 'https://github.com/sharevb/it-tools/issues/new/choose',
category: 'Actions',
keywords: ['report', 'issue', 'bug', 'problem', 'error'],
icon: BugIcon,
},
{
name: 'About',
description: 'Learn more about IT-Tools.',
to: '/about',
category: 'Pages',
keywords: ['about', 'learn', 'more', 'info', 'information'],
icon: InfoIcon,
},
];
{
name: t('tools.command-palette.store.texts.toggle-dark-mode'),
description: t('tools.command-palette.store.texts.toggle-dark-mode-on-or-off'),
action: () => styleStore.toggleDark(),
icon: SunIcon,
category: t('tools.command-palette.store.texts.actions'),
keywords: ['dark', 'theme', 'toggle', 'mode', 'light', 'system'],
},
{
name: t('tools.command-palette.store.texts.github-repository'),
href: 'https://github.com/sharevb/it-tools',
category: t('tools.command-palette.store.texts.external'),
description: t('tools.command-palette.store.texts.view-the-source-code-of-it-tools-on-github'),
keywords: ['github', 'repo', 'repository', 'source', 'code'],
icon: GithubIcon,
},
{
name: t('tools.command-palette.store.texts.report-a-bug-or-an-issue'),
description: t('tools.command-palette.store.texts.report-a-bug-or-an-issue-to-help-improve-it-tools'),
href: 'https://github.com/sharevb/it-tools/issues/new/choose',
category: t('tools.command-palette.store.texts.actions-0'),
keywords: ['report', 'issue', 'bug', 'problem', 'error'],
icon: BugIcon,
},
{
name: t('tools.pomodoro-timer.PomodoroApp.text.about'),
description: t('tools.command-palette.store.texts.learn-more-about-it-tools'),
to: '/about',
category: t('tools.command-palette.store.texts.pages'),
keywords: ['about', 'learn', 'more', 'info', 'information'],
icon: InfoIcon,
},
];
const { searchResult } = useFlexSearch({
search: searchPrompt,
data: searchOptions,
options: {
keys: [{ name: 'name', weight: 2 }, 'description', 'keywords', 'category'],
},
});
const { searchResult } = useFlexSearch({
search: searchPrompt,
data: searchOptions,
options: {
keys: [{ name: 'name', weight: 2 }, 'description', 'keywords', 'category'],
},
});
const filteredSearchResult = computed(() =>
_.mapValues(
_.groupBy(searchResult.value, option => option.category),
categoryOptions => categoryOptions.slice(0, maxSearchResultsPerCategory),
));
const filteredSearchResult = computed(() =>
_.mapValues(
_.groupBy(searchResult.value, (option) => option.category),
(categoryOptions) => categoryOptions.slice(0, maxSearchResultsPerCategory),
),
);
return {
filteredSearchResult,
searchPrompt,
};
});
return {
filteredSearchResult,
searchPrompt,
};
})();
@@ -3,13 +3,17 @@ import { storeToRefs } from 'pinia';
import * as _ from 'es-toolkit/compat';
import { useCommandPaletteStore } from './command-palette.store';
import type { PaletteOption } from './command-palette.types';
import { loadLocaleMessages } from '@/plugins/i18n.plugin';
const isModalOpen = ref(false);
const inputRef = ref();
const router = useRouter();
const isMac = computed(() => window.navigator.userAgent.toLowerCase().includes('mac'));
const commandPaletteStore = useCommandPaletteStore();
const { locale } = useI18n();
await loadLocaleMessages(locale.value);
const commandPaletteStore = useCommandPaletteStore(locale.value);
const { searchPrompt, filteredSearchResult } = storeToRefs(commandPaletteStore);
const keys = useMagicKeys({
@@ -32,7 +36,7 @@ whenever(keys.meta_k, open);
whenever(keys.escape, close);
function open() {
return isModalOpen.value = true;
return (isModalOpen.value = true);
}
function close() {
@@ -67,7 +71,9 @@ function handleKeydown(event: KeyboardEvent) {
}
function getOptionIndex(option: PaletteOption) {
return Object.values(filteredSearchResult.value).flat().findIndex(o => o === option);
return Object.values(filteredSearchResult.value)
.flat()
.findIndex((o) => o === option);
}
function activateOption(option: PaletteOption) {
@@ -108,7 +114,6 @@ function activateOption(option: PaletteOption) {
<div flex-1>
<c-button w-full important:justify-start @click="isModalOpen = true">
<span flex items-center gap-3 op-40>
<icon-mdi-search />
{{ $t('search.label') }}
@@ -118,14 +123,36 @@ function activateOption(option: PaletteOption) {
</span>
</c-button>
<c-modal v-model:open="isModalOpen" class="palette-modal" overflow-y-auto shadow-xl important:max-w-650px pretty-scrollbar important:pa-12px @keydown="handleKeydown">
<c-input-text ref="inputRef" v-model:value="searchPrompt" raw-text :placeholder="$t('search.placeholder')" autofocus clearable />
<c-modal
v-model:open="isModalOpen"
class="palette-modal"
overflow-y-auto
shadow-xl
important:max-w-650px
pretty-scrollbar
important:pa-12px
@keydown="handleKeydown"
>
<c-input-text
ref="inputRef"
v-model:value="searchPrompt"
raw-text
:placeholder="$t('search.placeholder')"
autofocus
clearable
/>
<div v-for="(options, category) in filteredSearchResult" :key="category">
<div ml-3 mt-3 text-sm font-bold text-primary op-60>
{{ category }}
</div>
<command-palette-option v-for="option in options" :key="option.name" :option="option" :selected="selectedOptionIndex === getOptionIndex(option)" @activated="activateOption" />
<command-palette-option
v-for="option in options"
:key="option.name"
:option="option"
:selected="selectedOptionIndex === getOptionIndex(option)"
@activated="activateOption"
/>
</div>
</c-modal>
</div>
@@ -136,8 +163,8 @@ function activateOption(option: PaletteOption) {
font-size: 18px;
::v-deep(.input-wrapper) {
padding: 4px;
padding-left: 18px;
padding: 4px;
padding-left: 18px;
}
}