'use client' import { useState, useEffect } from 'react' import api from '@/services/api' interface SchedulePickerProps { initialDate: Date | null onSchedule: (date: Date | null) => void onClose: () => void } interface OptimalTime { hour: number day: string score: number } export default function SchedulePicker({ initialDate, onSchedule, onClose, }: SchedulePickerProps) { const [selectedDate, setSelectedDate] = useState( initialDate ? initialDate.toISOString().slice(0, 10) : new Date(Date.now() + 86400000).toISOString().slice(0, 10) ) const [selectedTime, setSelectedTime] = useState( initialDate ? initialDate.toTimeString().slice(0, 5) : '09:00' ) const [optimalTimes, setOptimalTimes] = useState([]) const [loadingOptimal, setLoadingOptimal] = useState(false) const [error, setError] = useState('') useEffect(() => { loadOptimalTimes() }, []) const loadOptimalTimes = async () => { setLoadingOptimal(true) try { const { data } = await api.get('/v1/posts/optimal-times') setOptimalTimes(data.optimalTimes || data || []) } catch { // Non-critical, ignore } finally { setLoadingOptimal(false) } } const handleConfirm = () => { const dateStr = `${selectedDate}T${selectedTime}:00` const date = new Date(dateStr) if (date <= new Date()) { setError('Please select a time in the future') return } onSchedule(date) } const handleRemoveSchedule = () => { onSchedule(null) } const handleOptimalTimeSelect = (time: OptimalTime) => { // Find the next occurrence of that day at that hour const dayMap: Record = { sunday: 0, monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, } const targetDay = dayMap[time.day.toLowerCase()] ?? 0 const now = new Date() const currentDay = now.getDay() let daysUntil = targetDay - currentDay if (daysUntil <= 0) daysUntil += 7 const targetDate = new Date(now) targetDate.setDate(targetDate.getDate() + daysUntil) setSelectedDate(targetDate.toISOString().slice(0, 10)) setSelectedTime(`${time.hour.toString().padStart(2, '0')}:00`) } // Get minimum date (tomorrow) const minDate = new Date() minDate.setDate(minDate.getDate()) const minDateStr = minDate.toISOString().slice(0, 10) // Quick schedule presets const quickPresets = [ { label: 'In 1 hour', getDate: () => new Date(Date.now() + 3600000) }, { label: 'In 3 hours', getDate: () => new Date(Date.now() + 3 * 3600000) }, { label: 'Tomorrow 9 AM', getDate: () => { const d = new Date() d.setDate(d.getDate() + 1) d.setHours(9, 0, 0, 0) return d }}, { label: 'Tomorrow 12 PM', getDate: () => { const d = new Date() d.setDate(d.getDate() + 1) d.setHours(12, 0, 0, 0) return d }}, { label: 'Tomorrow 6 PM', getDate: () => { const d = new Date() d.setDate(d.getDate() + 1) d.setHours(18, 0, 0, 0) return d }}, ] return (
{/* Header */}

Schedule Post

{/* Quick Presets */}
{quickPresets.map((preset) => ( ))}
{/* Date & Time Inputs */}
{ setSelectedDate(e.target.value) setError('') }} min={minDateStr} className="w-full px-3 py-2.5 text-sm bg-background border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-primary text-foreground" />
{ setSelectedTime(e.target.value) setError('') }} className="w-full px-3 py-2.5 text-sm bg-background border border-border rounded-lg focus:outline-none focus:ring-2 focus:ring-primary text-foreground" />
{/* Optimal Times */} {optimalTimes.length > 0 && (
{optimalTimes.slice(0, 5).map((time, i) => ( ))}
)} {loadingOptimal && (

Loading optimal times...

)} {/* Preview */}

This post will be published on{' '} {new Date(`${selectedDate}T${selectedTime}:00`).toLocaleDateString('en-US', { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric', })} {' '}at{' '} {new Date(`${selectedDate}T${selectedTime}:00`).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', })}

{/* Error */} {error && (

{error}

)}
{/* Footer */}
{initialDate && ( )}
) }