Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom job timeline date format #745

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/api/typings/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,34 @@ export type UIConfig = Partial<{
miscLinks: Array<IMiscLink>;
favIcon: FavIcon;
locale: { lng?: string };
dateFormats?: DateFormats;
}>;

export type FavIcon = {
default: string;
alternative: string;
};

export type DateFormats = {
/**
* When timestamp is in same day (today)
*
* @example `hh:mm:ss`
* @see https://date-fns.org/v3.6.0/docs/format
*/
short?: string;

/**
* When timestamp is in same year
*
* @example `MM-dd hh:mm:ss`
* @see https://date-fns.org/v3.6.0/docs/format
*/
common?: string;

/**
* @example `yyyy-MM-dd hh:mm:ss`
* @see https://date-fns.org/v3.6.0/docs/format
*/
full?: string;
}
16 changes: 15 additions & 1 deletion packages/ui/src/components/JobCard/Timeline/Timeline.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatDistance, getYear, isToday, differenceInMilliseconds } from 'date-fns';
import { formatDistance, getYear, isToday, differenceInMilliseconds, format } from 'date-fns';
import enLocale from 'date-fns/locale/en-US';
import { TFunction } from 'i18next';
import React from 'react';
Expand All @@ -7,18 +7,29 @@ import { dateFnsLocale } from '../../../services/i18n';
import s from './Timeline.module.css';
import { AppJob, Status } from '@bull-board/api/typings/app';
import { STATUSES } from '@bull-board/api/src/constants/statuses';
import { useUIConfig } from '../../../hooks/useUIConfig';

type TimeStamp = number | Date;

const formatDate = (ts: TimeStamp, locale: string) => {
const uiConfig = useUIConfig();
const dateFormats = uiConfig.dateFormats || {};

let options: Intl.DateTimeFormatOptions;

if (isToday(ts)) {
if (dateFormats?.short) {
return format(new Date(ts), dateFormats.short)
}
options = {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
};
} else if (getYear(ts) === getYear(new Date())) {
if (dateFormats?.common) {
return format(new Date(ts), dateFormats.common)
}
options = {
month: 'numeric',
day: 'numeric',
Expand All @@ -27,6 +38,9 @@ const formatDate = (ts: TimeStamp, locale: string) => {
second: '2-digit',
};
} else {
if (dateFormats?.full) {
return format(new Date(ts), dateFormats.full)
}
options = {
year: 'numeric',
month: 'numeric',
Expand Down