import React, { useState, useEffect } from "react"
import { Skeleton, Button, Popover } from "antd"
import ReactMarkdown from "react-markdown"
import remarkGfm from "remark-gfm"
import { DateTime } from "luxon"
import { Icons } from "@components/Icons"
import EventsModel from "@models/events"
import useCenteredContainer from "@hooks/useCenteredContainer"
import createGoogleCalendarEvent from "@utils/createGoogleCalendarEvent"
import ContrastYIQ from "@utils/contrastYIQ"
import ProcessString from "@utils/processString"
import "./index.less"
const LocationProcessRegexs = [
{
regex: /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi,
fn: (key, result) => (
{result[1]}
),
},
]
const EventCountdown = ({ date, prefix }) => {
const [label, setLabel] = useState(null)
useEffect(() => {
function updateCountdown() {
const nowDate = DateTime.local()
const fromDate = DateTime.fromISO(date)
const diff = fromDate.diff(nowDate, "minutes").values
setLabel(nowDate.plus(diff).toRelative())
}
updateCountdown()
const interval = setInterval(updateCountdown, 1000)
return () => clearInterval(interval)
}, [date])
return (
)
}
const EventStartDate = ({ startDate, started }) => (
}
>
{startDate.toLocaleString(DateTime.DATE_FULL)}
{startDate.toLocaleString(DateTime.TIME_SIMPLE)}
)
const EventHeader = ({ pageConfig, event, contrastColor }) => {
if (pageConfig.header) {
return (
)
}
return (
)
}
const EventPage = (props) => {
useCenteredContainer(false)
const [L_Event, R_Event, E_Event, M_Event] = app.cores.api.useRequest(
EventsModel.data,
props.params["id"],
)
const [contrastColor, setContrastColor] = useState(null)
const [started, setStarted] = useState(false)
const [ended, setEnded] = useState(false)
useEffect(() => {
if (!R_Event) return
// Calculate event status
const now = DateTime.local()
const eventStart = DateTime.fromISO(R_Event.startDate)
const eventEnd = DateTime.fromISO(R_Event.endDate)
const startDiff = eventStart.diff(now, "minutes").values
const endDiff = eventEnd.diff(now, "minutes").values
setStarted(startDiff.minutes < 0)
setEnded(endDiff.minutes < 0)
// Calculate contrast color for header
if (R_Event.pageConfig?.header?.style?.backgroundImage) {
const url = R_Event.pageConfig.header.style.backgroundImage
.replace("url(", "")
.replace(")", "")
.replace(/['"]/gi, "")
ContrastYIQ.fromUrl(url).then(setContrastColor)
}
}, [R_Event])
const handleClickWatchLiveStream = () => {
if (!R_Event?.pageConfig?.livestreamId) return
app.location.push(`/tv/live/${R_Event.pageConfig.livestreamId}`)
}
const handleClickAddToCalendar = () => {
createGoogleCalendarEvent({
title: R_Event.name,
startDate: new Date(R_Event.startDate),
endDate: new Date(R_Event.endDate),
description: `${R_Event.shortDescription} - See details at ${location.href}`,
location: R_Event.location,
})
}
if (E_Event) {
return null
}
if (L_Event) {
return
}
if (!R_Event) {
return null
}
const eventStartedOrEnded = started || ended
const startDate = DateTime.fromISO(R_Event.startDate)
const endDate = DateTime.fromISO(R_Event.endDate)
const { pageConfig } = R_Event
return (
{started && !ended && (
)}
{!started && (
)}
{ProcessString(LocationProcessRegexs)(
R_Event.location,
)}
{!eventStartedOrEnded && (
)}
{started && pageConfig.livestreamId && (
)}
)
}
export default EventPage