Skip to content

Commit 1b66cab

Browse files
committed
feat(users): add online status duration display in user columns
1 parent b577a47 commit 1b66cab

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

dashboard/src/components/users/columns.tsx

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { Select, SelectContent, SelectItem, SelectTrigger } from '../ui/select'
88
import UsageSliderCompact from './usage-slider-compact'
99
import { cn } from '@/lib/utils'
1010
import useDirDetection from '@/hooks/use-dir-detection'
11+
import { dateUtils } from '@/utils/dateFormatter'
12+
import dayjs from '@/lib/dayjs'
1113

1214
export const setupColumns = ({
1315
t,
@@ -36,14 +38,53 @@ export const setupColumns = ({
3638
</button>
3739
),
3840
cell: ({ row }) => {
41+
const onlineAt = row.original.online_at
42+
43+
const getOnlineTimeText = () => {
44+
if (!onlineAt) {
45+
return null
46+
}
47+
48+
const currentTime = dayjs()
49+
const lastOnlineTime = dateUtils.toDayjs(onlineAt)
50+
const diffInSeconds = currentTime.diff(lastOnlineTime, 'seconds')
51+
52+
const isOnline = diffInSeconds <= 60
53+
54+
if (isOnline) {
55+
return null
56+
} else {
57+
const duration = dayjs.duration(diffInSeconds, 'seconds')
58+
59+
if (duration.days() > 0) {
60+
return `${duration.days()}d`
61+
} else if (duration.hours() > 0) {
62+
return `${duration.hours()}h`
63+
} else if (duration.minutes() > 0) {
64+
return `${duration.minutes()}m`
65+
} else {
66+
return `${duration.seconds()}s`
67+
}
68+
}
69+
}
70+
71+
const onlineTimeText = getOnlineTimeText()
72+
3973
return (
4074
<div className="overflow-hidden text-ellipsis whitespace-nowrap pl-1 font-medium md:pl-2">
4175
<div className="flex items-start gap-x-3 px-1 py-1">
4276
<div className="pt-1">
43-
<OnlineBadge lastOnline={row.original.online_at} />
77+
<OnlineBadge lastOnline={onlineAt} />
4478
</div>
45-
<div className="flex flex-col gap-y-0.5 overflow-hidden text-ellipsis whitespace-nowrap">
46-
<span className="overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium">{row.getValue('username')}</span>
79+
<div className="flex flex-col gap-y-0.5 overflow-hidden text-ellipsis whitespace-nowrap min-w-0 flex-1">
80+
<div className="flex items-center gap-x-1.5 overflow-hidden">
81+
<span className="overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium">{row.getValue('username')}</span>
82+
{onlineTimeText && (
83+
<span className="hidden shrink-0 text-[10px] font-normal text-muted-foreground md:inline">
84+
{onlineTimeText}
85+
</span>
86+
)}
87+
</div>
4788
{row.original.admin?.username && (
4889
<span className="flex items-center gap-x-0.5 overflow-hidden text-xs font-normal text-muted-foreground">
4990
<span className="hidden sm:block">{t('created')}</span>

0 commit comments

Comments
 (0)