Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
import { LucideIcon } from 'lucide-react'
import { route } from '@/constants/routes'
import {
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
} from '@/components/ui/sidebar'
import { Kbd, KbdGroup } from '@/components/ui/kbd'
import { useTranslation } from '@/i18n/react-i18next-compat'
import { Link, useNavigate } from '@tanstack/react-router'
import { PlatformMetaKey } from '@/containers/PlatformMetaKey'
import React, { useRef } from 'react'
import {
SearchIcon,
type SearchIconHandle,
} from '@/components/animated-icon/search'
import {
FolderPlusIcon,
type FolderPlusIconHandle,
} from '@/components/animated-icon/folder-plus'
import {
MessageCircleIcon,
type MessageCircleIconHandle,
} from '@/components/animated-icon/message-circle'
import {
SettingsIcon,
type SettingsIconHandle,
} from '@/components/animated-icon/settings'
import { BlocksIcon, type BlocksIconHandle } from '../animated-icon/blocks'
import {
BotIcon,
type BotIconHandle,
} from '@/components/animated-icon/bot'
import AddProjectDialog from '@/containers/dialogs/AddProjectDialog'
import { SearchDialog } from '@/containers/dialogs/SearchDialog'
import { useThreadManagement } from '@/hooks/useThreadManagement'
import { useSearchDialog } from '@/hooks/useSearchDialog'
import { useProjectDialog } from '@/hooks/useProjectDialog'
import { useAgentMode } from '@/hooks/useAgentMode'
import { TEMPORARY_CHAT_ID } from '@/constants/chat'
import { PlatformShortcuts, ShortcutAction } from '@/lib/shortcuts'
import { useSidebar } from '@/components/ui/sidebar'
type AnimatedIconHandle =
| SearchIconHandle
| FolderPlusIconHandle
| MessageCircleIconHandle
| SettingsIconHandle
| BlocksIconHandle
| BotIconHandle
type NavMainItem = {
title: string
url?: string
icon?: LucideIcon | React.ComponentType<{ className?: string }>
animatedIcon?: React.ForwardRefExoticComponent<
{
className?: string
size?: number
} & React.RefAttributes<AnimatedIconHandle>
>
isActive?: boolean
shortcut?: React.ReactNode
onClick?: () => void
/** Emitted as `data-testid` on the rendered control; see e2e/README.md. */
testId?: string
}
const getNavMainItems = (
onNewProject: () => void,
onSearch: () => void,
onNewChat: () => void,
onJanClaw: () => void
): NavMainItem[] => [
{
title: 'common:newChat',
animatedIcon: MessageCircleIcon,
onClick: onNewChat,
testId: 'new-chat-button',
shortcut: (
<KbdGroup className="ml-auto shrink-0 scale-90 gap-0">
<Kbd className="bg-transparent size-3">
<PlatformMetaKey />
</Kbd>
<Kbd className="bg-transparent size-3 uppercase">{PlatformShortcuts[ShortcutAction.NEW_CHAT].key}</Kbd>
</KbdGroup>
),
},
{
title: 'common:newAgentChat',
animatedIcon: BotIcon,
onClick: onJanClaw,
shortcut: (
<KbdGroup className="ml-auto shrink-0 scale-90 gap-0">
<Kbd className="bg-transparent size-3">
<PlatformMetaKey />
</Kbd>
<Kbd className="bg-transparent size-3 uppercase">{PlatformShortcuts[ShortcutAction.NEW_AGENT_CHAT].key}</Kbd>
</KbdGroup>
),
},
{
title: 'common:projects.new',
animatedIcon: FolderPlusIcon,
onClick: onNewProject,
shortcut: (
<KbdGroup className="ml-auto shrink-0 scale-90 gap-0">
<Kbd className="bg-transparent size-3">
<PlatformMetaKey />
</Kbd>
<Kbd className="bg-transparent size-3 uppercase">{PlatformShortcuts[ShortcutAction.NEW_PROJECT].key}</Kbd>
</KbdGroup>
),
},
{
title: 'common:search',
animatedIcon: SearchIcon,
onClick: onSearch,
shortcut: (
<KbdGroup className="ml-auto shrink-0 scale-90 gap-0">
<Kbd className="bg-transparent size-3">
<PlatformMetaKey />
</Kbd>
<Kbd className="bg-transparent size-3 uppercase">{PlatformShortcuts[ShortcutAction.SEARCH].key} </Kbd>
</KbdGroup>
),
},
{
title: 'common:hub',
url: route.hub.index,
animatedIcon: BlocksIcon,
},
{
title: 'common:settings',
url: route.settings.general,
animatedIcon: SettingsIcon,
},
]
function NavMainItemWithAnimatedIcon({
item,
label,
}: {
item: NavMainItem
label: string
}) {
const iconRef = useRef<AnimatedIconHandle>(null)
const AnimatedIcon = item.animatedIcon!
const content = (
<>
<AnimatedIcon ref={iconRef} className="size-4 shrink-0 text-foreground/70" size={16} />
<span className="min-w-0 truncate leading-5">{label}</span>
{item.shortcut}
</>
)
return (
<SidebarMenuItem>
<SidebarMenuButton
asChild={!!item.url}
isActive={item.isActive}
data-testid={item.testId}
onMouseEnter={() => iconRef.current?.startAnimation()}
onMouseLeave={() => iconRef.current?.stopAnimation()}
onClick={item.onClick}
>
{item.url ? <Link to={item.url}>{content}</Link> : content}
</SidebarMenuButton>
</SidebarMenuItem>
)
}
export function NavMain({
afterSearch,
beforeSettings,
}: {
afterSearch?: React.ReactNode
beforeSettings?: React.ReactNode
}) {
const { t } = useTranslation()
const navigate = useNavigate()
const { closeMobileForNavigation } = useSidebar()
const { addFolder } = useThreadManagement()
const { open: searchOpen, setOpen: setSearchOpen } = useSearchDialog()
const { open: projectDialogOpen, setOpen: setProjectDialogOpen } =
useProjectDialog()
const navMainItems = getNavMainItems(
() => setProjectDialogOpen(true),
() => setSearchOpen(true),
() => {
useAgentMode.getState().removeThread(TEMPORARY_CHAT_ID)
// Already on home the path does not change, so close the overlay here.
closeMobileForNavigation()
navigate({ to: route.home })
},
() => {
useAgentMode.getState().setAgentMode(TEMPORARY_CHAT_ID, true)
navigate({ to: route.home })
}
).filter((item) => item.title !== 'common:newAgentChat')
const handleCreateProject = async (name: string, assistantId?: string) => {
const newProject = await addFolder(name, assistantId)
setProjectDialogOpen(false)
navigate({
to: '/project/$projectId',
params: { projectId: newProject.id },
})
}
return (
<>
<SidebarMenu>
{navMainItems.map((item) => {
if (item.animatedIcon) {
return (
<React.Fragment key={item.title}>
{item.title === 'common:settings' && beforeSettings}
<NavMainItemWithAnimatedIcon
item={item}
label={t(item.title)}
/>
{item.title === 'common:search' && afterSearch}
</React.Fragment>
)
}
const Icon = item.icon
return (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton
asChild={!!item.url}
isActive={item.isActive}
data-testid={item.testId}
onClick={item.onClick}
>
{item.url ? (
<Link to={item.url}>
{Icon && <Icon className="size-4 shrink-0 text-foreground/70" />}
<span className="min-w-0 truncate leading-5">{t(item.title)}</span>
{item.shortcut}
</Link>
) : (
<>
{Icon && <Icon className="size-4 shrink-0 text-foreground/70" />}
<span className="min-w-0 truncate leading-5">{t(item.title)}</span>
{item.shortcut}
</>
)}
</SidebarMenuButton>
</SidebarMenuItem>
)
})}
</SidebarMenu>
<AddProjectDialog
open={projectDialogOpen}
onOpenChange={setProjectDialogOpen}
editingKey={null}
onSave={handleCreateProject}
/>
<SearchDialog open={searchOpen} onOpenChange={setSearchOpen} />
</>
)
}