You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

slash.tsx 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 { feedbackCommand } from './feedback'
  11. import { docsCommand } from './docs'
  12. import { communityCommand } from './community'
  13. import { accountCommand } from './account'
  14. import i18n from '@/i18n-config/i18next-config'
  15. export const slashAction: ActionItem = {
  16. key: '/',
  17. shortcut: '/',
  18. title: i18n.t('app.gotoAnything.actions.slashTitle'),
  19. description: i18n.t('app.gotoAnything.actions.slashDesc'),
  20. action: (result) => {
  21. if (result.type !== 'command') return
  22. const { command, args } = result.data
  23. executeCommand(command, args)
  24. },
  25. search: async (query, _searchTerm = '') => {
  26. // Delegate all search logic to the command registry system
  27. return slashCommandRegistry.search(query, i18n.language)
  28. },
  29. }
  30. // Register/unregister default handlers for slash commands with external dependencies.
  31. export const registerSlashCommands = (deps: Record<string, any>) => {
  32. // Register command handlers to the registry system with their respective dependencies
  33. slashCommandRegistry.register(themeCommand, { setTheme: deps.setTheme })
  34. slashCommandRegistry.register(languageCommand, { setLocale: deps.setLocale })
  35. slashCommandRegistry.register(feedbackCommand, {})
  36. slashCommandRegistry.register(docsCommand, {})
  37. slashCommandRegistry.register(communityCommand, {})
  38. slashCommandRegistry.register(accountCommand, {})
  39. }
  40. export const unregisterSlashCommands = () => {
  41. // Remove command handlers from registry system (automatically calls each command's unregister method)
  42. slashCommandRegistry.unregister('theme')
  43. slashCommandRegistry.unregister('language')
  44. slashCommandRegistry.unregister('feedback')
  45. slashCommandRegistry.unregister('docs')
  46. slashCommandRegistry.unregister('community')
  47. slashCommandRegistry.unregister('account')
  48. }
  49. export const SlashCommandProvider = () => {
  50. const theme = useTheme()
  51. useEffect(() => {
  52. registerSlashCommands({
  53. setTheme: theme.setTheme,
  54. setLocale: setLocaleOnClient,
  55. })
  56. return () => unregisterSlashCommands()
  57. }, [theme.setTheme])
  58. return null
  59. }