Skip to content

📈 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!

Display session liveness status in the UI

Blue Mousef5667177cd68
diff --git a/web/app/pages/Project/View/components/MetricCards.tsx b/web/app/pages/Project/View/components/MetricCards.tsx
index 734eacb70..7890f9759 100644
--- a/web/app/pages/Project/View/components/MetricCards.tsx
+++ b/web/app/pages/Project/View/components/MetricCards.tsx
@@ -14,7 +14,7 @@ import { nFormatter, getStringFromTime, getTimeFromSeconds } from '~/utils/gener
interface MetricCardProps {
label: string
- value: string | number | undefined | null
+ value: React.ReactNode
goodChangeDirection?: 'up' | 'down'
change?: number
type?: 'percent' | 'string'
diff --git a/web/app/pages/Project/View/components/SessionDetails.tsx b/web/app/pages/Project/View/components/SessionDetails.tsx
index 123445f69..989b69c9b 100644
--- a/web/app/pages/Project/View/components/SessionDetails.tsx
+++ b/web/app/pages/Project/View/components/SessionDetails.tsx
@@ -176,12 +176,22 @@ export const SessionDetails = ({ details }: SessionDetailsProps) => {
/>
<MetricCard
classes={{
- value: 'max-md:text-xl md:text-2xl',
+ value: `max-md:text-xl md:text-2xl ${details.isLive ? '!text-red-500 font-semibold' : ''}`,
label: '[&_span]:!text-slate-800 dark:[&_span]:!text-gray-200',
}}
label={t('dashboard.sessionDuration')}
- value={details.sdur as number}
- valueMapper={(value) => getStringFromTime(getTimeFromSeconds(value))}
+ value={
+ details.isLive ? (
+ <span className='flex items-center'>
+ <span className='mr-1.5 h-2.5 w-2.5 animate-pulse rounded-full bg-red-500' />
+ {t('dashboard.live').toUpperCase()}
+ </span>
+ ) : details.sdur !== undefined ? (
+ getStringFromTime(getTimeFromSeconds(details.sdur))
+ ) : (
+ 'N/A'
+ )
+ }
/>
</div>
)
diff --git a/web/app/pages/Project/View/components/Sessions.tsx b/web/app/pages/Project/View/components/Sessions.tsx
index 682b9c336..83863f18e 100644
--- a/web/app/pages/Project/View/components/Sessions.tsx
+++ b/web/app/pages/Project/View/components/Sessions.tsx
@@ -1,4 +1,6 @@
import { ChevronRightIcon } from '@heroicons/react/24/outline'
+import dayjs from 'dayjs'
+import duration from 'dayjs/plugin/duration'
import _map from 'lodash/map'
import React from 'react'
import { useTranslation } from 'react-i18next'
@@ -6,11 +8,14 @@ import { Link } from 'react-router'
import { ClientOnly } from 'remix-utils/client-only'
import Loader from '~/ui/Loader'
+import { getStringFromTime, getTimeFromSeconds } from '~/utils/generic'
import { Session as SessionType } from '../interfaces/session'
import CCRow from './CCRow'
+dayjs.extend(duration)
+
interface SessionsProps {
sessions: SessionType[]
onClick: (psid: string) => void
@@ -34,14 +39,39 @@ const Session = ({ session, onClick, timeFormat }: SessionProps) => {
t,
i18n: { language },
} = useTranslation('common')
- const date = new Date(session.created).toLocaleDateString(language, {
- day: 'numeric',
- month: 'short',
- year: 'numeric',
- hour: 'numeric',
- minute: 'numeric',
- hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',
- })
+
+ const lastActivityTime = dayjs(session.lastActivity)
+ const sessionStartTime = dayjs(session.sessionStart)
+
+ let sessionDurationString = ''
+
+ if (!session.isLive) {
+ const diffSeconds = lastActivityTime.diff(sessionStartTime, 'seconds')
+
+ sessionDurationString = getStringFromTime(getTimeFromSeconds(diffSeconds))
+ }
+
+ const dateLineString = session.isLive
+ ? sessionStartTime.toDate().toLocaleDateString(language, {
+ day: 'numeric',
+ month: 'short',
+ year: 'numeric',
+ hour: 'numeric',
+ minute: 'numeric',
+ hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',
+ })
+ : `${sessionStartTime.toDate().toLocaleDateString(language, {
+ day: 'numeric',
+ month: 'short',
+ year: 'numeric',
+ hour: 'numeric',
+ minute: 'numeric',
+ hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',
+ })} - ${lastActivityTime.toDate().toLocaleTimeString(language, {
+ hour: 'numeric',
+ minute: 'numeric',
+ hourCycle: timeFormat === '12-hour' ? 'h12' : 'h23',
+ })} (${sessionDurationString})`
const psidUrl = new URL(window.location.href)
psidUrl.searchParams.set('psid', session.psid)
@@ -56,7 +86,6 @@ const Session = ({ session, onClick, timeFormat }: SessionProps) => {
e.preventDefault()
e.stopPropagation()
window.history.pushState({}, '', stringifiedUrl)
-
onClick(session.psid)
}}
to={stringifiedUrl}
@@ -64,10 +93,18 @@ const Session = ({ session, onClick, timeFormat }: SessionProps) => {
<li className='relative mb-4 flex cursor-pointer justify-between gap-x-6 rounded-lg bg-gray-200/60 px-4 py-4 font-mono hover:bg-gray-200 sm:px-6 dark:bg-[#162032] dark:hover:bg-slate-800'>
<div className='flex min-w-0 gap-x-4'>
<div className='min-w-0 flex-auto'>
- <p className='flex items-center gap-x-2 text-sm leading-6 font-semibold text-gray-900 dark:text-gray-50'>
- {date}
+ <p className='flex items-center text-sm leading-6 font-semibold tracking-tighter text-gray-900 dark:text-gray-50'>
+ <span>
+ {dateLineString}
+ {session.isLive ? (
+ <span className='ml-2 inline-flex items-center tracking-normal'>
+ <span className='mr-1.5 h-2 w-2 animate-pulse rounded-full bg-red-500' />
+ <span className='text-xs font-medium text-red-500'>{t('dashboard.live').toUpperCase()}</span>
+ </span>
+ ) : null}
+ </span>
</p>
- <p className='mt-1 flex items-center gap-x-2 text-xs leading-5 text-gray-500 dark:text-gray-300'>
+ <p className='mt-1 flex flex-wrap items-center gap-x-2 text-xs leading-5 text-gray-500 dark:text-gray-300'>
<span className='flex'>
{session.cc ? <CCRow size={18} cc={session.cc} language={language} /> : t('project.unknownCountry')}
</span>
diff --git a/web/app/utils/generic.ts b/web/app/utils/generic.ts
index 326f8911e..135386592 100644
--- a/web/app/utils/generic.ts
+++ b/web/app/utils/generic.ts
@@ -51,8 +51,15 @@ export const secondsTillNextMonth = () => {
return 0 | ((date - now) / 1000)
}
-// Returns an object like { h: 0, m: 0, s: 0 } based on the seconds parameter provided
-export const getTimeFromSeconds = (seconds: any) => {
+type Time = {
+ h: number
+ m: number
+ s: number
+ ms: number
+ negative: boolean
+}
+
+export const getTimeFromSeconds = (seconds: any): Time => {
const negative = seconds < 0
const _seconds = Math.abs(seconds)
const h = 0 | (_seconds / 3600)
@@ -69,7 +76,7 @@ export const getTimeFromSeconds = (seconds: any) => {
}
}
-export const getStringFromTime = (time: any, showMS?: boolean) => {
+export const getStringFromTime = (time: Time, showMS?: boolean) => {
const { h, m, s, ms, negative } = time
if (h === 0 && m === 0 && s === 0 && (!showMS || ms === 0)) {
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index db270458f..c9155ac85 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -823,6 +823,7 @@
"support": "Contact support"
},
"project": {
+ "minuteUnitShort": "Min",
"metadata": "Metadata",
"aiDetailsDesc": "The buttons above allow you to see an estimate of what kind of traffic you can expect within an hour in the next X hours.",
"search": "Search",