📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
import _map from 'lodash/map'
import {
CaretDownIcon,
CaretRightIcon,
SidebarSimpleIcon,
SidebarIcon,
GearIcon,
XIcon,
ListIcon,
} from '@phosphor-icons/react'
import React, {
useState,
useMemo,
useCallback,
useEffect,
useRef,
memo,
} from 'react'
import { useTranslation } from 'react-i18next'
import { Link } from '~/ui/Link'
import { LinkProps } from 'react-router'
import { PROJECT_TABS } from '~/lib/constants'
import { Text } from '~/ui/Text'
import Tooltip from '~/ui/Tooltip'
import { cn } from '~/utils/generic'
import { getFaviconHost, getFaviconUrl } from '~/utils/referrers'
import routes from '~/utils/routes'
const SIDEBAR_COLLAPSED_KEY = 'project-sidebar-collapsed'
const SIDEBAR_GROUPS_KEY = 'project-sidebar-groups'
const getGroupExpandedState = (groupId: string): boolean => {
if (typeof window === 'undefined') return true
try {
const stored = localStorage.getItem(SIDEBAR_GROUPS_KEY)
if (stored) {
const states = JSON.parse(stored)
return states[groupId] !== false
}
} catch {
// Ignore parse errors
}
return true
}
const setGroupExpandedState = (groupId: string, isExpanded: boolean) => {
if (typeof window === 'undefined') return
try {
const stored = localStorage.getItem(SIDEBAR_GROUPS_KEY)
const states = stored ? JSON.parse(stored) : {}
states[groupId] = isExpanded
localStorage.setItem(SIDEBAR_GROUPS_KEY, JSON.stringify(states))
} catch {
// Ignore errors
}
}
type ProjectTabKey = keyof typeof PROJECT_TABS | 'settings'
const ICON_COLORS: Record<string, string> = {
// Ask AI
ai: 'text-violet-500',
// Web Analytics
traffic: 'text-blue-500',
performance: 'text-amber-500',
seo: 'text-green-500',
journeys: 'text-cyan-500',
funnels: 'text-teal-500',
// Product Analytics
profiles: 'text-fuchsia-500',
sessions: 'text-indigo-500',
replays: 'text-sky-500',
errors: 'text-red-500',
goals: 'text-purple-500',
experiments: 'text-pink-500',
featureFlags: 'text-orange-500',
// CAPTCHA
captcha: 'text-emerald-500',
// Settings
misc: 'text-gray-900 dark:text-gray-50',
}
interface Tab {
id: ProjectTabKey
label: string
icon: React.ComponentType<{ className?: string; weight?: string }>
}
interface TabGroup {
id: string
label: string
tabs: Tab[]
}
interface ProjectSidebarProps {
className?: string
tabs: Tab[]
activeTab: ProjectTabKey
onTabChange: (tabId: keyof typeof PROJECT_TABS) => void
projectId: string
projectName: string
websiteUrl?: string | null
dataLoading?: boolean
searchParams: URLSearchParams
allowedToManage?: boolean
isMobileOpen?: boolean
onMobileClose?: () => void
isEmbedded?: boolean
}
const CollapsibleGroup: React.FC<{
group: TabGroup
activeTab: ProjectTabKey
onTabChange: (tabId: keyof typeof PROJECT_TABS) => void
projectId: string
dataLoading?: boolean
searchParams: URLSearchParams
isCollapsed?: boolean
onMobileClose?: () => void
}> = memo(
({
group,
activeTab,
onTabChange,
projectId,
dataLoading,
searchParams,
isCollapsed,
onMobileClose,
}) => {
const [isExpanded, setIsExpanded] = useState(() =>
getGroupExpandedState(group.id),
)
const hasActiveTab = useMemo(() => {
return group.tabs.some((tab) => tab.id === activeTab)
}, [group.tabs, activeTab])
const prevHasActiveTab = useRef(hasActiveTab)
React.useEffect(() => {
// Only auto-expand when user navigates TO this group (not on initial mount)
if (hasActiveTab && !prevHasActiveTab.current && !isExpanded) {
setIsExpanded(true)
setGroupExpandedState(group.id, true)
}
prevHasActiveTab.current = hasActiveTab
}, [hasActiveTab, group.id]) // eslint-disable-line react-hooks/exhaustive-deps
if (isCollapsed) {
return (
<div className='mb-1'>
<nav className='flex flex-col gap-0.5'>
{_map(group.tabs, (tab) => {
const isCurrent = tab.id === activeTab
const TabIcon = tab.icon
const iconColorClass = ICON_COLORS[tab.id] || 'text-gray-500'
const handleClick = (e: React.MouseEvent) => {
if (tab.id === 'settings') {
onMobileClose?.()
return
}
e.preventDefault()
if (!dataLoading) {
onTabChange(tab.id as keyof typeof PROJECT_TABS)
onMobileClose?.()
}
}
const newSearchParams = new URLSearchParams(
searchParams.toString(),
)
newSearchParams.set('tab', tab.id)
const tabUrl: LinkProps['to'] =
tab.id === 'settings'
? routes.project_settings.replace(':id', projectId)
: { search: newSearchParams.toString() }
return (
<Tooltip
key={tab.id}
text={tab.label}
tooltipNode={
<Link
to={tabUrl}
onClick={handleClick}
className={cn(
'group flex items-center justify-center rounded-md p-2 transition-colors',
{
'bg-gray-100 dark:bg-slate-900': isCurrent,
'hover:bg-gray-100 dark:hover:bg-slate-900/60':
!isCurrent,
'cursor-wait': dataLoading && tab.id !== 'settings',
},
)}
aria-current={isCurrent ? 'page' : undefined}
aria-label={tab.label}
>
<TabIcon
className={cn('size-5 shrink-0', iconColorClass)}
weight='duotone'
aria-hidden='true'
/>
</Link>
}
disableHoverableContent
/>
)
})}
</nav>
</div>
)
}
return (
<div className='mb-1'>
<button
type='button'
onClick={() => {
const newValue = !isExpanded
setIsExpanded(newValue)
setGroupExpandedState(group.id, newValue)
}}
className='group flex w-full items-center justify-between rounded-md px-2 py-1.5 text-left transition-colors hover:bg-gray-100 dark:hover:bg-slate-900/60'
>
<Text
as='span'
size='xs'
colour='secondary'
weight='semibold'
truncate
className='max-w-full'
>
{group.label}
</Text>
{isExpanded ? (
<CaretDownIcon className='size-3.5 text-gray-400' />
) : (
<CaretRightIcon className='size-3.5 text-gray-400' />
)}
</button>
<div
className={cn(
'overflow-hidden transition-[max-height,opacity] duration-150 ease-out-quint motion-reduce:transition-none',
{
'max-h-96 opacity-100': isExpanded,
'max-h-0 opacity-0': !isExpanded,
},
)}
>
<nav className='mt-1 flex flex-col gap-0.5 pl-2'>
{_map(group.tabs, (tab) => {
const isCurrent = tab.id === activeTab
const TabIcon = tab.icon
const iconColorClass = ICON_COLORS[tab.id] || 'text-gray-500'
const handleClick = (e: React.MouseEvent) => {
if (tab.id === 'settings') {
onMobileClose?.()
return // Let the Link handle navigation
}
e.preventDefault()
if (!dataLoading) {
onTabChange(tab.id as keyof typeof PROJECT_TABS)
onMobileClose?.()
}
}
const newSearchParams = new URLSearchParams(
searchParams.toString(),
)
newSearchParams.set('tab', tab.id)
const tabUrl: LinkProps['to'] =
tab.id === 'settings'
? routes.project_settings.replace(':id', projectId)
: { search: newSearchParams.toString() }
return (
<Link
key={tab.id}
to={tabUrl}
onClick={handleClick}
className={cn(
'group flex items-center gap-1.5 rounded-md px-2.5 py-2 transition-colors',
{
'bg-gray-100 dark:bg-slate-900': isCurrent,
'hover:bg-gray-100 dark:hover:bg-slate-900/60':
!isCurrent,
'cursor-wait': dataLoading && tab.id !== 'settings',
},
)}
aria-current={isCurrent ? 'page' : undefined}
>
<TabIcon
className={cn('size-4 shrink-0', iconColorClass)}
weight='duotone'
aria-hidden='true'
/>
<Text
as='span'
size='sm'
weight='medium'
truncate
className='max-w-full'
>
{tab.label}
</Text>
</Link>
)
})}
</nav>
</div>
</div>
)
},
)
CollapsibleGroup.displayName = 'CollapsibleGroup'
const ProjectSidebar: React.FC<ProjectSidebarProps> = ({
tabs,
activeTab,
onTabChange,
projectId,
projectName,
websiteUrl,
dataLoading,
searchParams,
allowedToManage,
isMobileOpen = false,
onMobileClose,
isEmbedded = false,
className,
}) => {
const faviconHost = useMemo(
() => getFaviconHost(websiteUrl || null),
[websiteUrl],
)
const { t } = useTranslation('common')
const shouldCollapseByDefault = searchParams.get('collapsed') === 'true'
const [isCollapsed, setIsCollapsed] = useState(() => {
if (shouldCollapseByDefault) {
return true
}
if (!isEmbedded && typeof window !== 'undefined') {
return localStorage.getItem(SIDEBAR_COLLAPSED_KEY) === 'true'
}
return false
})
const [isScrolled, setIsScrolled] = useState(false)
useEffect(() => {
if (shouldCollapseByDefault) {
setIsCollapsed(true)
}
}, [shouldCollapseByDefault])
useEffect(() => {
if (isEmbedded) return undefined
const handleScroll = () => {
setIsScrolled(window.scrollY > 40)
}
window.addEventListener('scroll', handleScroll, { passive: true })
return () => window.removeEventListener('scroll', handleScroll)
}, [isEmbedded])
const toggleCollapsed = useCallback(() => {
setIsCollapsed((prev) => {
const newValue = !prev
if (!isEmbedded) {
localStorage.setItem(SIDEBAR_COLLAPSED_KEY, String(newValue))
}
return newValue
})
}, [isEmbedded])
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape' && isMobileOpen) {
onMobileClose?.()
}
}
if (isMobileOpen) {
document.addEventListener('keydown', handleEscape)
requestAnimationFrame(() => {
document.body.style.overflow = 'hidden'
})
}
return () => {
document.removeEventListener('keydown', handleEscape)
document.body.style.overflow = ''
}
}, [isMobileOpen, onMobileClose])
const handleTabChange = useCallback(
(tabId: keyof typeof PROJECT_TABS) => {
onTabChange(tabId)
onMobileClose?.()
},
[onTabChange, onMobileClose],
)
const askAiTab = useMemo(() => {
return tabs.find((tab) => PROJECT_TABS.ai && tab.id === PROJECT_TABS.ai)
}, [tabs])
const tabGroups = useMemo<TabGroup[]>(() => {
const groups: TabGroup[] = []
const webAnalyticsTabs = tabs.filter((tab) =>
[
PROJECT_TABS.traffic,
PROJECT_TABS.seo,
PROJECT_TABS.performance,
PROJECT_TABS.journeys,
PROJECT_TABS.funnels,
]
.filter(Boolean)
.includes(tab.id as any),
)
if (webAnalyticsTabs.length > 0) {
groups.push({
id: 'webAnalytics',
label: t('dashboard.webAnalytics'),
tabs: webAnalyticsTabs,
})
}
const productAnalyticsTabs = tabs.filter((tab) =>
[
PROJECT_TABS.profiles,
PROJECT_TABS.sessions,
PROJECT_TABS.replays,
PROJECT_TABS.errors,
PROJECT_TABS.goals,
PROJECT_TABS.experiments,
PROJECT_TABS.featureFlags,
]
.filter(Boolean)
.includes(tab.id as any),
)
if (productAnalyticsTabs.length > 0) {
groups.push({
id: 'productAnalytics',
label: t('dashboard.productAnalytics'),
tabs: productAnalyticsTabs,
})
}
const securityTabs = tabs.filter(
(tab) => PROJECT_TABS.captcha && tab.id === PROJECT_TABS.captcha,
)
if (securityTabs.length > 0) {
groups.push({
id: 'security',
label: t('dashboard.security'),
tabs: securityTabs,
})
}
return groups
}, [tabs, t])
const sidebarContent = (
<aside
className={cn(
'sticky top-2 flex shrink-0 flex-col self-start overflow-hidden',
isMobileOpen
? 'h-screen w-64 border-r border-gray-200 bg-white dark:border-slate-800/60 dark:bg-slate-950'
: cn(
isEmbedded
? 'h-[calc(100vh-1rem)]'
: cn(
isScrolled
? 'h-[calc(100vh-var(--banner-height)-1.5rem)]'
: 'h-[calc(100vh-var(--header-total-height)-1rem)]',
),
isCollapsed ? 'w-14' : 'w-56',
),
className,
)}
>
<div className='flex items-center justify-between px-3 py-2'>
{isCollapsed && !isMobileOpen ? (
<Tooltip
text={projectName}
tooltipNode={
faviconHost ? (
<img
className='size-6 shrink-0 rounded-sm'
src={getFaviconUrl(faviconHost)}
loading='lazy'
alt={projectName}
/>
) : (
<Text
as='h2'
size='lg'
weight='semibold'
className='text-center'
>
{projectName.charAt(0).toUpperCase()}
</Text>
)
}
disableHoverableContent
/>
) : (
<Tooltip
className='max-w-full flex-1'
text={projectName}
tooltipNode={
<div className='flex min-w-0 items-center gap-1'>
{faviconHost ? (
<img
className='size-6 shrink-0 rounded-sm'
src={getFaviconUrl(faviconHost)}
loading='lazy'
alt=''
aria-hidden='true'
/>
) : null}
<Text
as='h2'
size='lg'
weight='semibold'
truncate
className='text-left'
>
{projectName}
</Text>
</div>
}
disableHoverableContent
/>
)}
{isMobileOpen ? (
<button
type='button'
onClick={onMobileClose}
className='ml-2 rounded-md p-1.5 text-gray-500 hover:bg-gray-100 hover:text-gray-700 dark:text-gray-400 dark:hover:bg-slate-900 dark:hover:text-gray-200'
aria-label={t('common.close')}
>
<XIcon className='h-5 w-5' />
</button>
) : null}
</div>
<div className='flex-1 overflow-y-auto px-2 py-3'>
{askAiTab ? (
<div className='mb-1'>
{(() => {
const isCurrent = askAiTab.id === activeTab
const TabIcon = askAiTab.icon
const iconColorClass = ICON_COLORS[askAiTab.id] || 'text-gray-500'
const handleClick = (e: React.MouseEvent) => {
e.preventDefault()
if (!dataLoading) {
handleTabChange(askAiTab.id as keyof typeof PROJECT_TABS)
}
}
const newSearchParams = new URLSearchParams(
searchParams.toString(),
)
newSearchParams.set('tab', askAiTab.id)
if (isCollapsed && !isMobileOpen) {
return (
<Tooltip
text={askAiTab.label}
tooltipNode={
<Link
to={{ search: newSearchParams.toString() }}
onClick={handleClick}
className={cn(
'group flex items-center justify-center rounded-md p-2 transition-colors',
{
'bg-linear-to-r from-violet-500/10 to-purple-500/10 dark:from-violet-500/20 dark:to-purple-500/20':
isCurrent,
'hover:bg-gray-100 dark:hover:bg-slate-900/60':
!isCurrent,
'cursor-wait': dataLoading,
},
)}
aria-current={isCurrent ? 'page' : undefined}
aria-label={askAiTab.label}
>
<TabIcon
className={cn('size-5 shrink-0', iconColorClass)}
weight='duotone'
aria-hidden='true'
/>
</Link>
}
disableHoverableContent
/>
)
}
return (
<Link
to={{ search: newSearchParams.toString() }}
onClick={handleClick}
className={cn(
'group flex items-center gap-2 rounded-md px-2.5 py-2 transition-colors',
{
'bg-linear-to-r from-violet-500/10 to-purple-500/10 dark:from-violet-500/20 dark:to-purple-500/20':
isCurrent,
'hover:bg-gray-100 dark:hover:bg-slate-900/60':
!isCurrent,
'cursor-wait': dataLoading,
},
)}
aria-current={isCurrent ? 'page' : undefined}
>
<TabIcon
className={cn('size-4 shrink-0', iconColorClass)}
weight='duotone'
aria-hidden='true'
/>
<Text
as='span'
size='sm'
weight='medium'
truncate
className='max-w-full'
>
{askAiTab.label}
</Text>
</Link>
)
})()}
</div>
) : null}
{_map(tabGroups, (group) => (
<CollapsibleGroup
key={group.id}
group={group}
activeTab={activeTab}
onTabChange={onTabChange}
projectId={projectId}
dataLoading={dataLoading}
searchParams={searchParams}
isCollapsed={isCollapsed && !isMobileOpen ? true : false}
onMobileClose={onMobileClose}
/>
))}
</div>
<div className='flex flex-col gap-0.5 px-2 py-2'>
{!isMobileOpen ? (
<button
type='button'
onClick={toggleCollapsed}
className={cn(
'group flex w-full items-center rounded-md px-2.5 py-2 text-sm font-medium tracking-normal transition-colors',
'text-gray-900 hover:bg-gray-100 dark:text-gray-50 dark:hover:bg-slate-900/60',
isCollapsed ? 'justify-center' : 'gap-2.5',
)}
aria-label={isCollapsed ? t('common.expand') : t('common.collapse')}
>
{isCollapsed ? (
<SidebarIcon
className={cn('h-5 w-5 shrink-0', ICON_COLORS.misc)}
weight='duotone'
aria-hidden='true'
/>
) : (
<>
<SidebarSimpleIcon
className={cn('h-5 w-5 shrink-0', ICON_COLORS.misc)}
weight='duotone'
aria-hidden='true'
/>
<span className='truncate'>{t('common.collapse')}</span>
</>
)}
</button>
) : null}
{allowedToManage ? (
<Link
to={routes.project_settings.replace(':id', projectId)}
onClick={onMobileClose}
className={cn(
'group flex w-full items-center rounded-md px-2.5 py-2 text-sm font-medium tracking-normal transition-colors',
'text-gray-900 hover:bg-gray-100 dark:text-gray-50 dark:hover:bg-slate-900/60',
isCollapsed && !isMobileOpen ? 'justify-center' : 'gap-2.5',
)}
aria-label={t('common.settings')}
>
<GearIcon
className={cn('h-5 w-5 shrink-0', ICON_COLORS.misc)}
weight='duotone'
aria-hidden='true'
/>
{!isCollapsed || isMobileOpen ? (
<span className='truncate'>{t('common.settings')}</span>
) : null}
</Link>
) : null}
</div>
</aside>
)
if (isMobileOpen) {
return (
<div className='pointer-events-auto fixed inset-0 z-50 md:hidden'>
<div
className='animate-fade-in absolute inset-0 bg-black/50 motion-reduce:animate-none'
onClick={onMobileClose}
aria-hidden='true'
/>
<div className='animate-slide-in-left relative h-full w-fit motion-reduce:animate-none'>
{sidebarContent}
</div>
</div>
)
}
return sidebarContent
}
interface MobileSidebarTriggerProps {
onClick: () => void
activeTabLabel?: string
}
export const MobileSidebarTrigger: React.FC<MobileSidebarTriggerProps> = ({
onClick,
activeTabLabel,
}) => {
const { t } = useTranslation('common')
return (
<div className='relative z-10 mb-4 flex items-center gap-3 md:hidden'>
<button
type='button'
onClick={onClick}
className='flex items-center justify-center rounded-md border border-gray-300 p-2 text-gray-700 transition-[background-color,color,box-shadow,transform] duration-150 ease-out-quint ring-inset hover:bg-white focus:z-10 focus:ring-1 focus:ring-slate-900 focus:outline-hidden active:scale-[0.97] motion-reduce:active:scale-100 dark:border-slate-700/80 dark:text-gray-200 dark:hover:bg-slate-900 dark:focus:ring-slate-300'
aria-label={t('common.openMenu')}
>
<ListIcon className='h-5 w-5' />
</button>
{activeTabLabel ? (
<span className='text-sm font-medium text-gray-700 dark:text-gray-200'>
{activeTabLabel}
</span>
) : null}
</div>
)
}
const MemoizedProjectSidebar = memo(ProjectSidebar, (prevProps, nextProps) => {
return (
prevProps.activeTab === nextProps.activeTab &&
prevProps.projectId === nextProps.projectId &&
prevProps.projectName === nextProps.projectName &&
prevProps.websiteUrl === nextProps.websiteUrl &&
prevProps.dataLoading === nextProps.dataLoading &&
prevProps.allowedToManage === nextProps.allowedToManage &&
prevProps.isMobileOpen === nextProps.isMobileOpen &&
prevProps.isEmbedded === nextProps.isEmbedded &&
prevProps.className === nextProps.className &&
prevProps.searchParams.toString() === nextProps.searchParams.toString() &&
prevProps.tabs === nextProps.tabs &&
prevProps.onTabChange === nextProps.onTabChange &&
prevProps.onMobileClose === nextProps.onMobileClose
)
})
export default MemoizedProjectSidebar