include createdAt on individual video page

This commit is contained in:
MarconLP 2023-04-12 21:07:48 +02:00
parent 173f1d976d
commit 9ea79c2de3
No known key found for this signature in database
GPG key ID: F4CAFFDFA3451D5E
3 changed files with 73 additions and 64 deletions

View file

@ -6,6 +6,7 @@ import ReactPlayer from "react-player";
import { useRouter } from "next/router";
import Link from "next/link";
import Image from "next/image";
import { getTime } from "~/utils/getTime";
const VideoList: NextPage = () => {
const router = useRouter();
@ -67,11 +68,19 @@ const VideoList: NextPage = () => {
<div className="mb-10 mt-4 w-full max-w-[1800px] pl-[24px]">
<div>
{video?.video?.title ? (
<div className="mb-4 flex flex-col">
<span className="text-[18px] text-lg font-medium">
{video?.video?.title}
</span>
<span className="text-[18px] text-sm text-gray-800">
{getTime(video?.video?.createdAt)}
</span>
</div>
) : (
<div className="h-6 w-[300px] animate-pulse rounded bg-slate-200"></div>
<div className="mb-4 flex flex-col">
<div className="h-5 w-[300px] animate-pulse rounded bg-slate-200"></div>
<div className="mt-2 h-4 w-[50px] animate-pulse rounded bg-slate-200"></div>
</div>
)}
</div>
<div className="mt-2 flex flex-row items-center">

View file

@ -7,6 +7,7 @@ import { useSession } from "next-auth/react";
import { useRouter } from "next/router";
import Image from "next/image";
import VideoUploadModal from "~/components/VideoUploadModal";
import { getTime } from "~/utils/getTime";
const VideoList: NextPage = () => {
const router = useRouter();
@ -83,66 +84,6 @@ const VideoCardSkeleton = () => {
};
const VideoCard = ({ title, id, createdAt }: VideoCardProps) => {
const getTime = (timestamp: Date): string => {
const delta = Math.round(
(+new Date() - new Date(timestamp).getTime()) / 1000
);
const minute = 60,
hour = minute * 60,
day = hour * 24;
let timeString = "";
if (delta < 60) {
timeString = "Just now";
} else if (delta < 2 * minute) {
timeString = "1 min";
} else if (delta < hour) {
timeString = Math.floor(delta / minute).toString() + " mins";
} else if (Math.floor(delta / hour) === 1) {
timeString = "1 hour ago";
} else if (delta < day) {
timeString = Math.floor(delta / hour).toString() + " hours ago";
} else if (delta < day * 2) {
timeString = "yesterday";
} else if (delta < day * 7) {
timeString = Math.floor(delta / day).toString() + " days ago";
} else {
const date = new Date(timestamp);
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
timeString =
`${
months[date.getMonth()] || ""
}} ${date.getDate()} ${date.getFullYear()} ` +
`at ${
date.getHours().toString().length === 1
? "0" + date.getHours().toString()
: date.getHours()
}:${
date.getMinutes().toString().length === 1
? "0" + date.getMinutes().toString()
: date.getMinutes()
}`;
}
return timeString;
};
return (
<Link href={`/share/${id}`}>
<div className="h-[240px] w-[250px] cursor-pointer overflow-hidden rounded-lg border border-[#6c668533] text-sm font-normal">

59
src/utils/getTime.ts Normal file
View file

@ -0,0 +1,59 @@
export const getTime = (timestamp: Date): string => {
const delta = Math.round(
(+new Date() - new Date(timestamp).getTime()) / 1000
);
const minute = 60,
hour = minute * 60,
day = hour * 24;
let timeString = "";
if (delta < 60) {
timeString = "Just now";
} else if (delta < 2 * minute) {
timeString = "1 min";
} else if (delta < hour) {
timeString = Math.floor(delta / minute).toString() + " mins";
} else if (Math.floor(delta / hour) === 1) {
timeString = "1 hour ago";
} else if (delta < day) {
timeString = Math.floor(delta / hour).toString() + " hours ago";
} else if (delta < day * 2) {
timeString = "yesterday";
} else if (delta < day * 7) {
timeString = Math.floor(delta / day).toString() + " days ago";
} else {
const date = new Date(timestamp);
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
timeString =
`${
months[date.getMonth()] || ""
}} ${date.getDate()} ${date.getFullYear()} ` +
`at ${
date.getHours().toString().length === 1
? "0" + date.getHours().toString()
: date.getHours()
}:${
date.getMinutes().toString().length === 1
? "0" + date.getMinutes().toString()
: date.getMinutes()
}`;
}
return timeString;
};