fp/services/next/app/components/timestamps-list.tsx

69 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-01-20 16:16:14 +00:00
import React, { useContext, useState, useEffect } from "react";
2024-07-10 22:11:18 +00:00
import { IVod } from "@/app/lib/vods";
2024-01-20 16:16:14 +00:00
import {
ITimestamp,
deleteTimestamp
2024-07-10 22:11:18 +00:00
} from "@/app/lib/timestamps";
2024-01-20 16:16:14 +00:00
import {
formatTimestamp,
formatUrlTimestamp,
2024-07-10 22:11:18 +00:00
} from "@/app/lib/dates";
2024-01-20 16:16:14 +00:00
import Link from 'next/link';
import { faClock, faLink, faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { isWithinInterval, subHours, Interval } from 'date-fns';
import { useRouter } from 'next/navigation';
export interface ITimestampsProps {
vod: IVod;
timestamps: ITimestamp[];
setTimestamps: Function;
}
2025-01-11 03:10:04 +00:00
function isCreatedByMeRecently(authData: any, ts: ITimestamp) {
2024-01-20 16:16:14 +00:00
if (!authData?.user) return false;
2025-01-11 03:10:04 +00:00
if (authData.user.id !== ts.creator_id) return false;
2024-01-20 16:16:14 +00:00
const last24H: Interval = { start: subHours(new Date(), 24), end: new Date() };
2025-01-11 03:10:04 +00:00
return isWithinInterval(new Date(ts.created_at), last24H);
2024-01-20 16:16:14 +00:00
}
export function TimestampsList({ vod, timestamps, setTimestamps }: ITimestampsProps): React.JSX.Element {
// const throttledTimestampFetch = throttle(getRawTimestampsForVod);
const hasTimestamps = timestamps.length > 0;
return (
<div className="timestamps mb-5">
{hasTimestamps && (
timestamps.map((ts: ITimestamp) => (
<p key={ts.id}>
2025-01-11 03:10:04 +00:00
{JSON.stringify(ts, null, 2)}<br/><br/><br/>
2024-01-20 16:16:14 +00:00
<Link
2025-01-11 03:10:04 +00:00
href={`?t=${formatUrlTimestamp(ts.time)}`}
2024-01-20 16:16:14 +00:00
>
2025-01-11 03:10:04 +00:00
{formatTimestamp(ts.time)}
2024-01-20 16:16:14 +00:00
</Link>{' '}
2025-01-11 03:10:04 +00:00
{/* <span className="mr-2">{ts.tag.name}</span> */}
{isCreatedByMeRecently({}, ts) && (
2024-01-20 16:16:14 +00:00
<button
onClick={() => {
2025-01-11 03:10:04 +00:00
deleteTimestamp({}, ts.id); // @todo restore auth
2024-01-20 16:16:14 +00:00
setTimestamps((prevTimestamps: ITimestamp[]) => prevTimestamps.filter((timestamp) => timestamp.id !== ts.id));
}}
className={`button icon`}
>
<FontAwesomeIcon icon={faTrash}></FontAwesomeIcon>
</button>
)}
</p>
))
)}
{!hasTimestamps && <div className="ml-5"><p><i>This VOD has no timestamps</i></p></div>}
</div>
);
}