ADD time ago display to VideoCard component
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
import { formatTime } from "../../utils/utils.ts";
|
import { formatTime, stringToDate, dateToTimeAgo } from "../../utils/utils.ts";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
@@ -8,6 +8,7 @@ type VideoCardProps = {
|
|||||||
duration: number,
|
duration: number,
|
||||||
thumbnailPath: string | null,
|
thumbnailPath: string | null,
|
||||||
videoPath: string,
|
videoPath: string,
|
||||||
|
createdAt: string,
|
||||||
className?: string
|
className?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ const VideoCard = ({
|
|||||||
duration,
|
duration,
|
||||||
thumbnailPath,
|
thumbnailPath,
|
||||||
videoPath,
|
videoPath,
|
||||||
|
createdAt,
|
||||||
className
|
className
|
||||||
}: VideoCardProps) => {
|
}: VideoCardProps) => {
|
||||||
|
|
||||||
@@ -53,6 +55,10 @@ const VideoCard = ({
|
|||||||
">
|
">
|
||||||
{formatTime(duration)}
|
{formatTime(duration)}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
{dateToTimeAgo(stringToDate(createdAt))}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ const MyClips = () => {
|
|||||||
duration={clip.duration}
|
duration={clip.duration}
|
||||||
thumbnailPath={clip.thumbnailPath}
|
thumbnailPath={clip.thumbnailPath}
|
||||||
videoPath={clip.videoPath}
|
videoPath={clip.videoPath}
|
||||||
|
createdAt={clip.createdAt}
|
||||||
className={"w-40 m-5"}
|
className={"w-40 m-5"}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -12,6 +12,50 @@ function formatTime(seconds: number): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stringToDate(dateString: string): Date {
|
||||||
|
const validIso = dateString.substring(0, 23);
|
||||||
|
const date = new Date(validIso);
|
||||||
|
if (isNaN(date.getTime())) {
|
||||||
|
throw new Error("Invalid date string");
|
||||||
|
}
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
function dateToTimeAgo(date: Date): string {
|
||||||
|
const now = new Date();
|
||||||
|
const secondsAgo = Math.floor((now.getTime() - date.getTime()) / 1000);
|
||||||
|
|
||||||
|
if (secondsAgo < 60) {
|
||||||
|
return `${secondsAgo} seconds ago`;
|
||||||
|
} else if (secondsAgo < 3600) {
|
||||||
|
const minutes = Math.floor(secondsAgo / 60);
|
||||||
|
|
||||||
|
if (minutes === 1) {
|
||||||
|
return "1 minute ago";
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${minutes} minutes ago`;
|
||||||
|
} else if (secondsAgo < 86400) {
|
||||||
|
const hours = Math.floor(secondsAgo / 3600);
|
||||||
|
|
||||||
|
if (hours === 1) {
|
||||||
|
return "1 hour ago";
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${hours} hours ago`;
|
||||||
|
} else {
|
||||||
|
const days = Math.floor(secondsAgo / 86400);
|
||||||
|
|
||||||
|
if (days === 1) {
|
||||||
|
return "1 day ago";
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${days} days ago`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
formatTime
|
formatTime,
|
||||||
|
stringToDate,
|
||||||
|
dateToTimeAgo
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user