📈 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
Reorganise sessions UI components
Blue Moused26a909cc3ac
diff --git a/web/app/pages/Project/tabs/Sessions/SessionDetailView.tsx b/web/app/pages/Project/tabs/Sessions/SessionDetailView.tsxindex 7a3573cc7..c341499ff 100644--- a/web/app/pages/Project/tabs/Sessions/SessionDetailView.tsx+++ b/web/app/pages/Project/tabs/Sessions/SessionDetailView.tsx@@ -1,17 +1,8 @@import dayjs from 'dayjs'-import _capitalize from 'lodash/capitalize'import _isEmpty from 'lodash/isEmpty'import _size from 'lodash/size'import _truncate from 'lodash/truncate'-import {- GlobeIcon,- MonitorIcon,- DeviceMobileIcon,- DeviceTabletIcon,- ClockIcon,- LinkIcon,- UserIcon,-} from '@phosphor-icons/react'+import { GlobeIcon, ClockIcon, LinkIcon, UserIcon } from '@phosphor-icons/react'import { useMemo, useState } from 'react'import { useTranslation } from 'react-i18next'import { Link } from '~/ui/Link'@@ -25,6 +16,7 @@ import {import { SessionDetails as SessionDetailsType } from '~/lib/models/Project'import { useCurrentProject } from '~/providers/CurrentProjectProvider'import { useTheme } from '~/providers/ThemeProvider'+import PulsatingCircle from '~/ui/icons/PulsatingCircle'import Loader from '~/ui/Loader'import { Text } from '~/ui/Text'import Tooltip from '~/ui/Tooltip'@@ -32,6 +24,7 @@ import {getLocaleDisplayName,getStringFromTime,getTimeFromSeconds,+ cn,} from '~/utils/generic'import Flag from '~/ui/Flag'@@ -61,6 +54,8 @@ interface SessionDetailViewProps {}pages?: PageflowItem[]timeBucket?: string+ sessionStart?: string+ lastActivity?: string} | nullsessionLoading: booleantimeFormat: '12-hour' | '24-hour'@@ -69,47 +64,89 @@ interface SessionDetailViewProps {websiteUrl?: string | null}+const RECENTLY_ACTIVE_THRESHOLD_SECONDS = 30 * 60+const InfoRow = ({label,value,+ valueClassName,}: {label: stringvalue: React.ReactNode+ valueClassName?: string}) => (- <div className='flex items-center justify-between border-b border-gray-100 py-2 last:border-0 dark:border-slate-700/50'>- <Text size='sm' colour='muted'>+ <div className='grid grid-cols-[minmax(0,1fr)_minmax(0,1.35fr)] items-center gap-3 border-b border-gray-100 py-1.5 last:border-0 dark:border-slate-800/80'>+ <Text size='sm' colour='secondary' className='min-w-0'>{label}</Text><Text+ as='div'size='sm'weight='medium'colour='primary'- className='flex items-center gap-1'+ className={cn(+ 'flex min-w-0 items-center justify-end gap-1 text-right wrap-break-word',+ valueClassName,+ )}>{value}</Text></div>)-const DeviceIcon = ({ device }: { device: string | null }) => {- const deviceLower = device?.toLowerCase() || ''- if (deviceLower === 'mobile') return <DeviceMobileIcon className='h-4 w-4' />- if (deviceLower === 'tablet') return <DeviceTabletIcon className='h-4 w-4' />- return <MonitorIcon className='h-4 w-4' />-}+const PanelSection = ({+ title,+ action,+ children,+}: {+ title: React.ReactNode+ action?: React.ReactNode+ children: React.ReactNode+}) => (+ <section className='border-t border-gray-100 pt-3 first:border-t-0 first:pt-0 dark:border-slate-800/80'>+ <div className='mb-1.5 flex items-center justify-between gap-3'>+ <Text+ as='h3'+ size='xs'+ weight='semibold'+ colour='primary'+ className='uppercase'+ tracking='wide'+ >+ {title}+ </Text>+ {action}+ </div>+ {children}+ </section>+)-const BrowserIcon = ({ browser }: { browser: string | null }) => {- if (!browser) return <GlobeIcon className='h-4 w-4' />+const BrowserIcon = ({+ browser,+ className,+}: {+ browser: string | null+ className?: string+}) => {+ if (!browser) return <GlobeIcon className={cn('h-4 w-4', className)} />const logoUrl = BROWSER_LOGO_MAP[browser as keyof typeof BROWSER_LOGO_MAP]- if (!logoUrl) return <GlobeIcon className='h-4 w-4' />+ if (!logoUrl) return <GlobeIcon className={cn('h-4 w-4', className)} />- return <img src={logoUrl} className='h-4 w-4' alt='' />+ return <img src={logoUrl} className={cn('h-4 w-4', className)} alt='' />}-const OSIcon = ({ os, theme }: { os: string | null; theme: string }) => {- if (!os) return <GlobeIcon className='h-4 w-4' />+const OSIcon = ({+ os,+ theme,+ className,+}: {+ os: string | null+ theme: string+ className?: string+}) => {+ if (!os) return <GlobeIcon className={cn('h-4 w-4', className)} />const logoUrlLight = OS_LOGO_MAP[os as keyof typeof OS_LOGO_MAP]const logoUrlDark = OS_LOGO_MAP_DARK[os as keyof typeof OS_LOGO_MAP_DARK]@@ -117,9 +154,25 @@ const OSIcon = ({ os, theme }: { os: string | null; theme: string }) => {let logoUrl = theme === 'dark' ? logoUrlDark : logoUrlLightlogoUrl ||= logoUrlLight- if (!logoUrl) return <GlobeIcon className='h-4 w-4' />+ if (!logoUrl) return <GlobeIcon className={cn('h-4 w-4', className)} />++ return <img src={logoUrl} className={cn('h-4 w-4', className)} alt='' />+}- return <img src={logoUrl} className='h-4 w-4' alt='' />+const formatCompactElapsed = (seconds: number) => {+ const safeSeconds = Math.max(0, seconds)+ const minute = 60+ const hour = 60 * minute+ const day = 24 * hour+ const month = 30 * day+ const year = 365 * day++ if (safeSeconds < minute) return 'now'+ if (safeSeconds < hour) return `${Math.floor(safeSeconds / minute)}m`+ if (safeSeconds < day) return `${Math.floor(safeSeconds / hour)}h`+ if (safeSeconds < month) return `${Math.floor(safeSeconds / day)}d`+ if (safeSeconds < year) return `${Math.floor(safeSeconds / month)}mo`+ return `${Math.floor(safeSeconds / year)}y`}export const SessionDetailView = ({@@ -201,39 +254,244 @@ export const SessionDetailView = ({return <Loader />}+ const countryName = details.cc+ ? countries.getName(details.cc, language) || details.cc+ : null+ const locationSummary =+ [countryName, details.rg, details.ct].filter(Boolean).join(', ') ||+ t('project.unknown')+ const osLabel = details.os+ ? `${details.os}${details.osv ? ` ${details.osv}` : ''}`+ : null+ const browserLabel = details.br+ ? `${details.br}${details.brv ? ` ${details.brv}` : ''}`+ : null+ const platformSummary =+ [osLabel, browserLabel].filter(Boolean).join(', ') || t('project.unknown')+ const firstEventAt = activeSession.pages?.[0]?.created+ const lastEventAt =+ activeSession.pages && activeSession.pages.length > 0+ ? activeSession.pages[activeSession.pages.length - 1].created+ : null+ const sessionStartAt = activeSession.sessionStart || firstEventAt+ const lastActivityAt =+ activeSession.lastActivity || lastEventAt || sessionStartAt || null+ const secondsSinceLastActivity = lastActivityAt+ ? Math.max(0, dayjs().diff(dayjs(lastActivityAt), 'second'))+ : null+ const lastSeenTime =+ secondsSinceLastActivity !== null+ ? formatCompactElapsed(secondsSinceLastActivity)+ : null+ const sessionOnlineFor = sessionStartAt+ ? formatCompactElapsed(dayjs().diff(dayjs(sessionStartAt), 'second'))+ : sessionDuration > 0+ ? formatCompactElapsed(sessionDuration)+ : null+ const lastSeenDate = lastActivityAt+ ? new Date(lastActivityAt).toLocaleDateString(language, {+ day: 'numeric',+ month: 'short',+ year: 'numeric',+ hour: 'numeric',+ minute: 'numeric',+ second: 'numeric',+ hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',+ })+ : null+ const lastSeenLabel = lastSeenTime+ ? lastSeenTime === 'now'+ ? 'seen now'+ : `seen ${lastSeenTime} ago`+ : t('project.endOfSession')+ const isRecentlyActive =+ !details.isLive &&+ secondsSinceLastActivity !== null &&+ secondsSinceLastActivity < RECENTLY_ACTIVE_THRESHOLD_SECONDS+ const campaignRows = [+ { label: t('project.mapping.so'), value: details.so },+ { label: t('project.mapping.me'), value: details.me },+ { label: t('project.mapping.ca'), value: details.ca },+ { label: t('project.mapping.te'), value: details.te },+ { label: t('project.mapping.co'), value: details.co },+ ].filter(({ value }) => value)+ const durationValue = details.isLive ? (+ <>+ <ClockIcon className='h-4 w-4' />+ {sessionOnlineFor || t('dashboard.live')}+ </>+ ) : sessionDuration > 0 ? (+ <>+ <ClockIcon className='h-4 w-4' />+ {getStringFromTime(getTimeFromSeconds(sessionDuration))}+ </>+ ) : (+ 'N/A'+ )+ const statusNode = details.isLive ? (+ <Tooltip+ asChild+ text={+ lastSeenDate+ ? t('project.lastSeenAt', { time: lastSeenDate })+ : t('project.online')+ }+ tooltipNode={+ <span className='grid grid-cols-[34px_minmax(0,1fr)] items-center gap-2'>+ <span className='flex h-6 items-center justify-center'>+ <PulsatingCircle type='small' />+ </span>+ <Text as='span' size='sm' colour='secondary' className='truncate'>+ {sessionOnlineFor+ ? `${t('project.online')} ${sessionOnlineFor}`+ : t('project.online')}+ </Text>+ </span>+ }+ />+ ) : isRecentlyActive ? (+ <Tooltip+ asChild+ text={+ lastSeenDate+ ? t('project.lastSeenAt', { time: lastSeenDate })+ : t('project.endOfSession')+ }+ tooltipNode={+ <span className='grid grid-cols-[34px_minmax(0,1fr)] items-center gap-2'>+ <span className='flex h-6 items-center justify-center'>+ <span className='h-2.5 w-2.5 rounded-full bg-amber-500 ring-2 ring-white dark:ring-slate-900' />+ </span>+ <Text as='span' size='sm' colour='secondary' className='truncate'>+ {lastSeenLabel}+ </Text>+ </span>+ }+ />+ ) : (+ <Tooltip+ asChild+ text={+ lastSeenDate+ ? t('project.lastSeenAt', { time: lastSeenDate })+ : t('project.endOfSession')+ }+ tooltipNode={+ <span className='grid grid-cols-[34px_minmax(0,1fr)] items-center gap-2'>+ <span className='flex h-6 items-center justify-center'>+ <span className='h-2.5 w-2.5 rounded-full bg-gray-400 ring-2 ring-white dark:bg-slate-500 dark:ring-slate-900' />+ </span>+ <Text as='span' size='sm' colour='secondary' className='truncate'>+ {lastSeenLabel}+ </Text>+ </span>+ }+ />+ )+return (- <div className='space-y-3'>- <div className='flex flex-col gap-3 lg:flex-row'>- <div className='space-y-4 lg:w-[380px]'>- <div className='rounded-lg border border-gray-200 bg-white px-4 py-5 dark:border-slate-800/60 dark:bg-slate-900/25'>- <Text- as='h3'- size='xs'- weight='semibold'- colour='primary'- className='mb-2 uppercase'- tracking='wide'- >- {t('project.sessionInfo')}- </Text>- <div>- <InfoRow- label={t('dashboard.sessionDuration')}- value={- details.isLive ? (- <span className='flex items-center font-semibold text-red-500'>- <span className='mr-1.5 h-2.5 w-2.5 animate-pulse rounded-full bg-red-500' />- {t('dashboard.live').toUpperCase()}- </span>- ) : sessionDuration > 0 ? (+ <div className='grid gap-3 lg:grid-cols-[minmax(0,1fr)_360px] lg:items-start xl:grid-cols-[minmax(0,1fr)_390px]'>+ <section className='order-2 min-w-0 rounded-lg border border-gray-200 bg-white px-4 py-4 lg:order-1 dark:border-slate-800/60 dark:bg-slate-900/25'>+ <Text+ as='h3'+ size='xs'+ weight='semibold'+ tracking='wide'+ className='mb-3 uppercase'+ >+ {t('project.pageflow')}+ </Text>+ <Pageflow+ pages={activeSession?.pages || []}+ timeFormat={timeFormat}+ zoomedTimeRange={zoomedTimeRange}+ sdur={activeSession?.details?.sdur}+ isLive={activeSession?.details?.isLive}+ websiteUrl={websiteUrl}+ />+ </section>++ <aside className='order-1 lg:sticky lg:top-14 lg:order-2 lg:self-start'>+ <div className='h-fit space-y-3 rounded-lg border border-gray-200 bg-white px-4 py-4 dark:border-slate-800/60 dark:bg-slate-900/25'>+ <div>+ <div className='min-w-0 space-y-1.5'>+ {statusNode}+ <div className='grid grid-cols-[34px_minmax(0,1fr)] items-center gap-2'>+ <span className='flex h-6 items-center justify-center'>+ {details.cc ? (+ <Flag+ className='rounded-xs'+ country={details.cc}+ size={20}+ alt=''+ aria-hidden='true'+ />+ ) : (+ <GlobeIcon className='h-5 w-5 text-gray-500 dark:text-gray-400' />+ )}+ </span>+ <Text+ as='span'+ size='base'+ weight='medium'+ colour='primary'+ className='truncate leading-6'+ title={locationSummary}+ >+ {locationSummary}+ </Text>+ </div>+ <div className='grid grid-cols-[34px_minmax(0,1fr)] items-center gap-2'>+ <span className='relative flex h-6 w-[34px] items-center justify-center'>+ {details.os || details.br ? (<>- <ClockIcon className='h-4 w-4' />- {getStringFromTime(getTimeFromSeconds(sessionDuration))}+ <OSIcon+ os={details.os}+ theme={theme}+ className='absolute left-1 h-4 w-4'+ />+ <BrowserIcon+ browser={details.br}+ className='absolute right-1 h-4 w-4'+ /></>) : (- 'N/A'- )- }+ <OSIcon+ os={details.os}+ theme={theme}+ className='h-5 w-5 text-gray-500 dark:text-gray-400'+ />+ )}+ </span>+ <Text+ as='span'+ size='base'+ weight='medium'+ colour='primary'+ className='truncate leading-6'+ title={platformSummary}+ >+ {platformSummary}+ </Text>+ </div>+ </div>++ {details.profileId ? (+ <Link+ to={`/projects/${projectId}?tab=${PROJECT_TABS.profiles}&profileId=${encodeURIComponent(details.profileId)}`}+ className='mt-3 flex w-full items-center justify-center gap-2 rounded-md border border-gray-300 bg-white px-4 py-1.5 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-50 dark:border-slate-700 dark:bg-slate-900 dark:text-gray-200 dark:hover:bg-slate-800'+ >+ <UserIcon className='h-4 w-4' />+ {t('project.goToProfile')}+ </Link>+ ) : null}+ </div>++ <PanelSection title={t('project.sessionInfo')}>+ <div>+ <InfoRow+ label={t('dashboard.sessionDuration')}+ value={durationValue}/><InfoRowlabel={t('project.mapping.ref')}@@ -242,14 +500,15 @@ export const SessionDetailView = ({<Tooltiptext={details.ref}tooltipNode={- <div className='flex items-center gap-1'>- <LinkIcon className='h-4 w-4' />+ <div className='flex min-w-0 items-center justify-end gap-1'>+ <LinkIcon className='h-4 w-4 shrink-0' /><span+ className='truncate'title={- _size(details.ref) > 25 ? details.ref : undefined+ _size(details.ref) > 34 ? details.ref : undefined}>- {_truncate(details.ref, { length: 25 })}+ {_truncate(details.ref, { length: 34 })}</span></div>}@@ -259,183 +518,39 @@ export const SessionDetailView = ({)}/>- </div>-- <Text- as='h3'- size='xs'- weight='semibold'- colour='primary'- className='mt-5 mb-2 uppercase'- tracking='wide'- >- {t('project.locationAndDevice')}- </Text>- <div>- <InfoRow- label={t('project.mapping.cc')}- value={- details.cc ? (- <>- <Flag- className='rounded-xs'- country={details.cc}- size={16}- alt=''- aria-hidden='true'- />- - {[- details.rg || details.ct- ? details.cc?.toUpperCase()- : countries.getName(details.cc, language) ||- details.cc,- details.rg,- details.ct,- ]- .filter(Boolean)- .join(' > ')}- </>- ) : (- '-'- )- }- /><InfoRowlabel={t('project.mapping.lc')}value={details.lc ? getLocaleDisplayName(details.lc, language) : '-'}/>- <InfoRow- label={t('project.mapping.dv')}- value={- details.dv ? (- <>- <DeviceIcon device={details.dv} />- {_capitalize(details.dv)}- </>- ) : (- '-'- )- }- />- <InfoRow- label={t('project.mapping.br')}- value={- details.br ? (- <>- <BrowserIcon browser={details.br} />- {details.br}- {details.brv ? ` v${details.brv}` : ''}- </>- ) : (- '-'- )- }- />- <InfoRow- label={t('project.mapping.os')}- value={- details.os ? (- <>- <OSIcon os={details.os} theme={theme} />- {details.os}- {details.osv ? ` v${details.osv}` : ''}- </>- ) : (- '-'- )- }- /></div>+ </PanelSection>++ {campaignRows.length > 0 ? (+ <PanelSection title={t('project.campaigns')}>+ <div>+ {campaignRows.map(({ label, value }) => (+ <InfoRow key={label} label={label} value={value} />+ ))}+ </div>+ </PanelSection>+ ) : null}- {details.so ||- details.me ||- details.ca ||- details.te ||- details.co ? (- <>- <Text- as='h3'- size='xs'- weight='semibold'- colour='primary'- className='mt-5 mb-2 uppercase'- tracking='wide'- >- {t('project.campaigns')}- </Text>- <div>- {details.so ? (- <InfoRow- label={t('project.mapping.so')}- value={details.so}- />- ) : null}- {details.me ? (- <InfoRow- label={t('project.mapping.me')}- value={details.me}- />- ) : null}- {details.ca ? (- <InfoRow- label={t('project.mapping.ca')}- value={details.ca}- />- ) : null}- {details.te ? (- <InfoRow- label={t('project.mapping.te')}- value={details.te}- />- ) : null}- {details.co ? (- <InfoRow- label={t('project.mapping.co')}- value={details.co}- />- ) : null}- </div>- </>- ) : null}-- {details.profileId ? (- <Link- to={`/projects/${projectId}?tab=${PROJECT_TABS.profiles}&profileId=${encodeURIComponent(details.profileId)}`}- className='mt-2 flex w-full items-center justify-center gap-2 rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-50 dark:border-slate-600 dark:bg-slate-900 dark:text-gray-200 dark:hover:bg-slate-800'- >- <UserIcon className='h-4 w-4' />- {t('project.goToProfile')}- </Link>- ) : null}- </div>- </div>-- <div className='flex-1'>{!_isEmpty(activeSession?.chart) ? (- <div className='rounded-lg border border-gray-200 bg-white px-4 py-5 dark:border-slate-800/60 dark:bg-slate-900/25'>- <div className='mb-2 flex items-center justify-between'>- <Text- as='h3'- size='xs'- weight='semibold'- colour='primary'- className='uppercase'- tracking='wide'- >- {t('project.sessionActivity')}- </Text>- {zoomedTimeRange && !isTouchDevice ? (+ <PanelSection+ title={t('project.sessionActivity')}+ action={+ zoomedTimeRange && !isTouchDevice ? (<buttononClick={resetZoom}- className='rounded border bg-white px-2 py-1 text-xs text-gray-800 hover:bg-gray-100 dark:border-slate-700 dark:bg-slate-900 dark:text-gray-200 hover:dark:bg-slate-700'+ className='rounded border border-gray-200 bg-white px-2 py-1 text-xs text-gray-800 transition-colors hover:bg-gray-100 dark:border-slate-700 dark:bg-slate-900 dark:text-gray-200 hover:dark:bg-slate-800'>{t('project.resetZoom')}</button>- ) : null}- </div>+ ) : null+ }+ ><SessionChartchart={activeSession?.chart}timeBucket={activeSession?.timeBucket}@@ -443,32 +558,12 @@ export const SessionDetailView = ({rotateXAxis={rotateXAxis}dataNames={dataNames}onZoom={isTouchDevice ? undefined : setZoomedTimeRange}- className='h-[300px] [&_svg]:overflow-visible!'+ className='h-[190px] [&_svg]:overflow-visible!'/>- </div>+ </PanelSection>) : null}</div>- </div>-- <div className='rounded-lg border border-gray-200 bg-white px-4 py-5 dark:border-slate-800/60 dark:bg-slate-900/25'>- <Text- as='h3'- size='xs'- weight='semibold'- tracking='wide'- className='mb-4 uppercase'- >- {t('project.pageflow')}- </Text>- <Pageflow- pages={activeSession?.pages || []}- timeFormat={timeFormat}- zoomedTimeRange={zoomedTimeRange}- sdur={activeSession?.details?.sdur}- isLive={activeSession?.details?.isLive}- websiteUrl={websiteUrl}- />- </div>+ </aside></div>)}diff --git a/web/app/pages/Project/tabs/Sessions/SessionsView.tsx b/web/app/pages/Project/tabs/Sessions/SessionsView.tsxindex e75c3eff3..bb0b61aa9 100644--- a/web/app/pages/Project/tabs/Sessions/SessionsView.tsx+++ b/web/app/pages/Project/tabs/Sessions/SessionsView.tsx@@ -104,6 +104,8 @@ interface ActiveSession {}pages?: PageflowItem[]timeBucket?: string+ sessionStart?: string+ lastActivity?: string}interface SessionsViewProps {@@ -186,6 +188,9 @@ const SessionsViewInner = ({if (deferredData.sessionDetails) {const apiDetails = deferredData.sessionDetails.detailsconst matchingSession = sessions.find((s) => s.psid === activePSID)+ const pages = deferredData.sessionDetails.pages+ const lastPage =+ pages && pages.length > 0 ? pages[pages.length - 1] : nullconst details: SessionDetailsType = {cc: apiDetails.cc,os: apiDetails.os,@@ -211,8 +216,13 @@ const SessionsViewInner = ({return {details,chart: deferredData.sessionDetails.chart,- pages: deferredData.sessionDetails.pages,+ pages,timeBucket: deferredData.sessionDetails.timeBucket,+ sessionStart: matchingSession?.sessionStart || apiDetails.created,+ lastActivity:+ matchingSession?.lastActivity ||+ lastPage?.created ||+ apiDetails.created,}}return null