Skip to content

Commit b6feddb

Browse files
committed
Refactor UsersTable component to enhance time column rendering
This commit introduces a new renderTimeCell function to streamline the rendering of time-related cells in the UsersTable. It replaces the inline date formatting logic for the "last_seen" and "first_seen" columns with this dedicated function, improving code readability and maintainability. Additionally, constants for time column width and class are defined, and logic is added to conditionally apply styles based on column type, enhancing the overall layout and user experience.
1 parent e553dd3 commit b6feddb

1 file changed

Lines changed: 29 additions & 30 deletions

File tree

client/src/app/[site]/users/components/UsersTable.tsx

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ import { OperatingSystem } from "../../components/shared/icons/OperatingSystem";
4040
import { DeviceIcon } from "../../components/shared/icons/Device";
4141

4242
const columnHelper = createColumnHelper<UsersResponse>();
43+
const TIME_COLUMN_WIDTH = "10rem";
44+
const TIME_COLUMN_WIDTH_CLASS = "w-40";
45+
const TIME_COLUMN_IDS = new Set(["last_seen", "first_seen"]);
46+
47+
const isTimeColumn = (columnId: string) => TIME_COLUMN_IDS.has(columnId);
4348

4449
const handleFilterClick = (e: React.MouseEvent, parameter: FilterParameter, value: string | undefined) => {
4550
e.stopPropagation();
@@ -79,6 +84,21 @@ export function UsersTable() {
7984

8085
return formatRelative(date);
8186
};
87+
88+
const renderTimeCell = (dateStr: string) => {
89+
const date = DateTime.fromSQL(dateStr, {
90+
zone: "utc",
91+
}).setZone(getTimezone());
92+
const formattedDate = formatDateTime(date, { month: "short", day: "numeric", hour: "numeric", minute: "2-digit" });
93+
const relativeTime = formatRelativeTime(dateStr);
94+
95+
return (
96+
<div className={`grid ${TIME_COLUMN_WIDTH_CLASS} whitespace-nowrap`}>
97+
<span className="col-start-1 row-start-1 group-hover:invisible">{relativeTime}</span>
98+
<span className="col-start-1 row-start-1 invisible group-hover:visible">{formattedDate}</span>
99+
</div>
100+
);
101+
};
82102
const { site } = useParams();
83103

84104
const [pagination, setPagination] = useState({
@@ -245,37 +265,11 @@ export function UsersTable() {
245265
}),
246266
columnHelper.accessor("last_seen", {
247267
header: ({ column }) => <SortHeader column={column}>{t("Last Seen")}</SortHeader>,
248-
cell: info => {
249-
const date = DateTime.fromSQL(info.getValue(), {
250-
zone: "utc",
251-
}).setZone(getTimezone());
252-
const formattedDate = formatDateTime(date, { month: "short", day: "numeric", hour: "numeric", minute: "2-digit" });
253-
const relativeTime = formatRelativeTime(info.getValue());
254-
255-
return (
256-
<div className="whitespace-nowrap">
257-
<span className="group-hover:hidden">{relativeTime}</span>
258-
<span className="hidden group-hover:inline">{formattedDate}</span>
259-
</div>
260-
);
261-
},
268+
cell: info => renderTimeCell(info.getValue()),
262269
}),
263270
columnHelper.accessor("first_seen", {
264271
header: ({ column }) => <SortHeader column={column}>{t("First Seen")}</SortHeader>,
265-
cell: info => {
266-
const date = DateTime.fromSQL(info.getValue(), {
267-
zone: "utc",
268-
}).setZone(getTimezone());
269-
const formattedDate = formatDateTime(date, { month: "short", day: "numeric", hour: "numeric", minute: "2-digit" });
270-
const relativeTime = formatRelativeTime(info.getValue());
271-
272-
return (
273-
<div className="whitespace-nowrap">
274-
<span className="group-hover:hidden">{relativeTime}</span>
275-
<span className="hidden group-hover:inline">{formattedDate}</span>
276-
</div>
277-
);
278-
},
272+
cell: info => renderTimeCell(info.getValue()),
279273
}),
280274
];
281275

@@ -362,7 +356,9 @@ export function UsersTable() {
362356
scope="col"
363357
className="h-8 px-2 text-left align-middle font-medium text-xs text-neutral-500 dark:text-neutral-400 whitespace-nowrap first:rounded-l-xl last:rounded-r-xl"
364358
style={{
365-
minWidth: header.id === "user_id" ? "100px" : "auto",
359+
minWidth:
360+
header.id === "user_id" ? "100px" : isTimeColumn(header.id) ? TIME_COLUMN_WIDTH : "auto",
361+
width: isTimeColumn(header.id) ? TIME_COLUMN_WIDTH : undefined,
366362
}}
367363
>
368364
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
@@ -399,7 +395,10 @@ export function UsersTable() {
399395
return (
400396
<tr key={row.id} className="border-b border-neutral-100 dark:border-neutral-800 group hover:bg-neutral-50 dark:hover:bg-neutral-850">
401397
{row.getVisibleCells().map(cell => (
402-
<td key={cell.id} className="p-3 relative">
398+
<td
399+
key={cell.id}
400+
className={isTimeColumn(cell.column.id) ? `p-3 relative ${TIME_COLUMN_WIDTH_CLASS}` : "p-3 relative"}
401+
>
403402
{/* <Link
404403
href={href}
405404
className="absolute inset-0 z-10"

0 commit comments

Comments
 (0)