generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b159e4
commit 176dec2
Showing
2 changed files
with
78 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,84 @@ | ||
import React from 'react'; | ||
import { formatDistanceToNow, format, parseISO, isToday, isYesterday } from 'date-fns'; | ||
import { | ||
parseISO, | ||
isFuture, | ||
format, | ||
differenceInMinutes, | ||
differenceInHours, | ||
differenceInDays | ||
} from 'date-fns'; | ||
import { Tooltip } from '@carbon/react'; | ||
|
||
interface FriendlyDateProps { | ||
date: string | null | undefined; // The date string in ISO format | ||
} | ||
|
||
const FriendlyDate: React.FC<FriendlyDateProps> = ({ date }) => { | ||
const formatDateAtlassianStyle = (date: Date) => { | ||
const now = new Date(); | ||
const minutesDiff = differenceInMinutes(now, date); | ||
const hoursDiff = differenceInHours(now, date); | ||
const daysDiff = differenceInDays(now, date); | ||
|
||
// Past dates | ||
if (minutesDiff < 1) return 'just now'; | ||
if (minutesDiff < 60) return minutesDiff === 1 ? 'a minute ago' : `${minutesDiff} minutes ago`; | ||
if (hoursDiff < 24) return hoursDiff === 1 ? '1 hour ago' : `${hoursDiff} hours ago`; | ||
if (daysDiff === 1) return 'yesterday'; | ||
if (daysDiff < 7) return `${daysDiff} days ago`; | ||
if(daysDiff === 7) return 'a week ago'; | ||
|
||
// Use full date for older dates | ||
return format(date, 'MMMM d, yyyy'); | ||
} | ||
|
||
if(!date) return <span data-testid="friendly-date"></span>; | ||
// Future dates | ||
const formatFutureDateAtlassianStyle = (date: Date) => { | ||
const now = new Date(); | ||
const minutesDiff = differenceInMinutes(date, now); | ||
const hoursDiff = differenceInHours(date, now); | ||
const daysDiff = differenceInDays(date, now); | ||
|
||
if (minutesDiff < 1) return 'shortly'; | ||
if (minutesDiff < 60) return minutesDiff === 1 ? 'in 1 minute' : `in ${minutesDiff} minutes`; | ||
if (hoursDiff < 24) return hoursDiff === 1 ? 'in 1 hour' : `in ${hoursDiff} hours`; | ||
if (daysDiff === 1) return 'tomorrow'; | ||
if (daysDiff < 7) return `in ${daysDiff} days`; | ||
if (daysDiff === 7) return 'in a week'; | ||
|
||
try{ | ||
const parsedDate = parseISO(date); | ||
const cleanDate = format(parsedDate, "MMM dd, yyyy"); | ||
|
||
const formattedDate = isToday(parsedDate) | ||
? "Today" | ||
: isYesterday(parsedDate) | ||
? "Yesterday" | ||
: formatDistanceToNow(parsedDate, { addSuffix: true }); | ||
|
||
return <Tooltip | ||
label={cleanDate} | ||
align="bottom-left" | ||
autoAlign> | ||
<span>{formattedDate}</span> | ||
</Tooltip>; | ||
} catch(e){ | ||
return <span data-testid="friendly-date"></span>; | ||
return format(date, 'MMMM d, yyyy'); | ||
} | ||
|
||
const FriendlyDate: React.FC<FriendlyDateProps> = ({ date }) => { | ||
|
||
if (!date) return <span data-testid="friendly-date"></span>; | ||
|
||
try { | ||
const parsedDate = parseISO(date); | ||
const cleanDate = format(parsedDate, "MMMM dd, yyyy"); | ||
|
||
// Use appropriate formatting for past or future dates | ||
const formattedDate = isFuture(parsedDate) | ||
? formatFutureDateAtlassianStyle(parsedDate) | ||
: formatDateAtlassianStyle(parsedDate); | ||
|
||
if(cleanDate === formattedDate) | ||
console.log(`cleanDate: ${cleanDate} formattedDate: ${formattedDate}`); | ||
return ( | ||
<> | ||
{(cleanDate === formattedDate) ? (<span>{formattedDate}</span>) : ( | ||
<Tooltip | ||
label={cleanDate} // Display full date in tooltip | ||
align="bottom-left" | ||
autoAlign | ||
> | ||
<span>{formattedDate}</span> | ||
</Tooltip> | ||
)} | ||
</> | ||
); | ||
} catch (e) { | ||
return <span data-testid="friendly-date"></span>; // Fallback for invalid dates | ||
} | ||
}; | ||
|
||
export default FriendlyDate; | ||
export default FriendlyDate; |