Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

2.8 KB
Raw Blame
import { useKeyboardShortcut } from '@/hooks/useHotkeys'
import { useSidebar } from '@/components/ui/sidebar'
import { useSearchDialog } from '@/hooks/useSearchDialog'
import { useProjectDialog } from '@/hooks/useProjectDialog'
import { useRouter } from '@tanstack/react-router'
import { route } from '@/constants/routes'
import { PlatformShortcuts, ShortcutAction } from '@/lib/shortcuts'
import { useAgentMode } from '@/hooks/useAgentMode'
import { useAssistantSwitcher } from '@/hooks/useAssistantSwitcher'
import { useMessageZoom } from '@/hooks/useMessageZoom'
import { TEMPORARY_CHAT_ID } from '@/constants/chat'

export function KeyboardShortcutsProvider() {
  // The overlay on narrow windows and the docked sidebar toggle differently.
  const { toggleSidebar } = useSidebar()
  const { setOpen: setSearchOpen } = useSearchDialog()
  const { setOpen: setProjectDialogOpen } = useProjectDialog()
  const router = useRouter()

  // Get shortcut specs from centralized configuration
  const sidebarShortcut = PlatformShortcuts[ShortcutAction.TOGGLE_SIDEBAR]
  const newChatShortcut = PlatformShortcuts[ShortcutAction.NEW_CHAT]
  const newProjectShortcut = PlatformShortcuts[ShortcutAction.NEW_PROJECT]
  const settingsShortcut = PlatformShortcuts[ShortcutAction.GO_TO_SETTINGS]
  const searchShortcut = PlatformShortcuts[ShortcutAction.SEARCH]
  const switchAssistantShortcut =
    PlatformShortcuts[ShortcutAction.SWITCH_ASSISTANT]

  // Toggle Sidebar
  useKeyboardShortcut({
    ...sidebarShortcut,
    callback: () => {
      toggleSidebar()
    },
  })

  // New Chat
  useKeyboardShortcut({
    ...newChatShortcut,
    callback: () => {
      useAgentMode.getState().removeThread(TEMPORARY_CHAT_ID)
      router.navigate({ to: route.home })
    },
  })

  // New Agent Chat — disabled, kept as dead code for future use
  // useKeyboardShortcut({
  //   ...newAgentChatShortcut,
  //   callback: () => {
  //     useAgentMode.getState().setAgentMode(TEMPORARY_CHAT_ID, true)
  //     router.navigate({ to: route.home })
  //   },
  // })

  // New Project
  useKeyboardShortcut({
    ...newProjectShortcut,
    callback: () => {
      setProjectDialogOpen(true)
    },
  })

  // Go to Settings
  useKeyboardShortcut({
    ...settingsShortcut,
    callback: () => {
      router.navigate({ to: route.settings.general })
    },
  })

  // Search
  useKeyboardShortcut({
    ...searchShortcut,
    callback: () => {
      setSearchOpen(true)
    },
  })

  // Switch Assistant — advance to the next assistant on each press
  useKeyboardShortcut({
    ...switchAssistantShortcut,
    callback: () => {
      useAssistantSwitcher.getState().cycleHandler?.()
    },
  })

  // Zoom In / Zoom Out - scales chat message text only
  useMessageZoom()

  // This component doesn't render anything
  return null
}