您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

slash.tsx 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use client'
  2. import { useEffect } from 'react'
  3. import type { ActionItem } from '../types'
  4. import { slashCommandRegistry } from './registry'
  5. import { executeCommand } from './command-bus'
  6. import { useTheme } from 'next-themes'
  7. import { setLocaleOnClient } from '@/i18n-config'
  8. import { themeCommand } from './theme'
  9. import { languageCommand } from './language'
  10. import i18n from '@/i18n-config/i18next-config'
  11. export const slashAction: ActionItem = {
  12. key: '/',
  13. shortcut: '/',
  14. title: i18n.t('app.gotoAnything.actions.slashTitle'),
  15. description: i18n.t('app.gotoAnything.actions.slashDesc'),
  16. action: (result) => {
  17. if (result.type !== 'command') return
  18. const { command, args } = result.data
  19. executeCommand(command, args)
  20. },
  21. search: async (query, _searchTerm = '') => {
  22. // Delegate all search logic to the command registry system
  23. return slashCommandRegistry.search(query, i18n.language)
  24. },
  25. }
  26. // Register/unregister default handlers for slash commands with external dependencies.
  27. export const registerSlashCommands = (deps: Record<string, any>) => {
  28. // Register command handlers to the registry system with their respective dependencies
  29. slashCommandRegistry.register(themeCommand, { setTheme: deps.setTheme })
  30. slashCommandRegistry.register(languageCommand, { setLocale: deps.setLocale })
  31. }
  32. export const unregisterSlashCommands = () => {
  33. // Remove command handlers from registry system (automatically calls each command's unregister method)
  34. slashCommandRegistry.unregister('theme')
  35. slashCommandRegistry.unregister('language')
  36. }
  37. export const SlashCommandProvider = () => {
  38. const theme = useTheme()
  39. useEffect(() => {
  40. registerSlashCommands({
  41. setTheme: theme.setTheme,
  42. setLocale: setLocaleOnClient,
  43. })
  44. return () => unregisterSlashCommands()
  45. }, [theme.setTheme])
  46. return null
  47. }