|
| 1 | +'use client' |
| 2 | +import { useEffect } from 'react' |
| 3 | +import type { ActionItem, CommandSearchResult } from './types' |
| 4 | +import { buildLanguageCommands, buildLanguageRootItem } from './run-language' |
| 5 | +import { buildThemeCommands, buildThemeRootItem } from './run-theme' |
| 6 | +import i18n from '@/i18n-config/i18next-config' |
| 7 | +import { executeCommand, registerCommands, unregisterCommands } from './command-bus' |
| 8 | +import { useTheme } from 'next-themes' |
| 9 | +import { setLocaleOnClient } from '@/i18n-config' |
| 10 | + |
| 11 | +const rootParser = (query: string): CommandSearchResult[] => { |
| 12 | + const q = query.toLowerCase() |
| 13 | + const items: CommandSearchResult[] = [] |
| 14 | + if (!q || 'theme'.includes(q)) |
| 15 | + items.push(buildThemeRootItem()) |
| 16 | + if (!q || 'language'.includes(q) || 'lang'.includes(q)) |
| 17 | + items.push(buildLanguageRootItem()) |
| 18 | + return items |
| 19 | +} |
| 20 | + |
| 21 | +type RunContext = { |
| 22 | + setTheme?: (value: 'light' | 'dark' | 'system') => void |
| 23 | + setLocale?: (locale: string) => Promise<void> |
| 24 | + search?: (query: string) => void |
| 25 | +} |
| 26 | + |
| 27 | +export const commandAction: ActionItem = { |
| 28 | + key: '@run', |
| 29 | + shortcut: '@run', |
| 30 | + title: i18n.t('app.gotoAnything.actions.runTitle'), |
| 31 | + description: i18n.t('app.gotoAnything.actions.runDesc'), |
| 32 | + action: (result) => { |
| 33 | + if (result.type !== 'command') return |
| 34 | + const { command, args } = result.data |
| 35 | + if (command === 'theme.set') { |
| 36 | + executeCommand('theme.set', args) |
| 37 | + return |
| 38 | + } |
| 39 | + if (command === 'i18n.set') { |
| 40 | + executeCommand('i18n.set', args) |
| 41 | + return |
| 42 | + } |
| 43 | + if (command === 'nav.search') |
| 44 | + executeCommand('nav.search', args) |
| 45 | + }, |
| 46 | + search: async (_, searchTerm = '') => { |
| 47 | + const q = searchTerm.trim() |
| 48 | + if (q.startsWith('theme')) |
| 49 | + return buildThemeCommands(q.replace(/^theme\s*/, ''), i18n.language) |
| 50 | + if (q.startsWith('language') || q.startsWith('lang')) |
| 51 | + return buildLanguageCommands(q.replace(/^(language|lang)\s*/, '')) |
| 52 | + |
| 53 | + // root categories |
| 54 | + return rootParser(q) |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | +// Register/unregister default handlers for @run commands with external dependencies. |
| 59 | +export const registerRunCommands = (deps: { |
| 60 | + setTheme?: (value: 'light' | 'dark' | 'system') => void |
| 61 | + setLocale?: (locale: string) => Promise<void> |
| 62 | + search?: (query: string) => void |
| 63 | +}) => { |
| 64 | + registerCommands({ |
| 65 | + 'theme.set': async (args) => { |
| 66 | + deps.setTheme?.(args?.value) |
| 67 | + }, |
| 68 | + 'i18n.set': async (args) => { |
| 69 | + const locale = args?.locale |
| 70 | + if (locale) |
| 71 | + await deps.setLocale?.(locale) |
| 72 | + }, |
| 73 | + 'nav.search': (args) => { |
| 74 | + const q = args?.query |
| 75 | + if (q) |
| 76 | + deps.search?.(q) |
| 77 | + }, |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +export const unregisterRunCommands = () => { |
| 82 | + unregisterCommands(['theme.set', 'i18n.set', 'nav.search']) |
| 83 | +} |
| 84 | + |
| 85 | +export const RunCommandProvider = ({ onNavSearch }: { onNavSearch?: (q: string) => void }) => { |
| 86 | + const theme = useTheme() |
| 87 | + useEffect(() => { |
| 88 | + registerRunCommands({ |
| 89 | + setTheme: theme.setTheme, |
| 90 | + setLocale: setLocaleOnClient, |
| 91 | + search: onNavSearch, |
| 92 | + }) |
| 93 | + return () => unregisterRunCommands() |
| 94 | + }, [theme.setTheme, onNavSearch]) |
| 95 | + |
| 96 | + return null |
| 97 | +} |
0 commit comments