'use client'
import { useState, useEffect } from 'react'
import { DateTime } from 'luxon'
const TIMEZONE = 'America/Chicago'
export function CurrentDate({ dayString }: { dayString: string }) {
const [formattedDate, setFormattedDate] = useState<string | null>(null)
useEffect(() => {
const calculateNextDayDate = (): string => {
const daysOfWeek = [
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday',
]
const now = DateTime.now().setZone(TIMEZONE)
const currentDayIndex = now.weekday - 1
const targetDayIndex = daysOfWeek.indexOf(dayString.toLowerCase())
if (targetDayIndex === -1) {
throw new Error('Invalid day string')
}
const daysUntilTarget = targetDayIndex - currentDayIndex
const targetDate = now.plus({ days: daysUntilTarget })
return `${targetDate.month}/${targetDate.day}`
}
const updateFormattedDate = () => {
const date = calculateNextDayDate()
setFormattedDate(date)
}
updateFormattedDate()
const intervalId = setInterval(updateFormattedDate, 60000)
return () => clearInterval(intervalId)
}, [dayString])
if (!formattedDate) {
return null
}
return <>({formattedDate})</>
}