Spaces:
Paused
Paused
File size: 28,056 Bytes
3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 866c437 3b752e8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | "use client";
import React, { useState, useEffect, useRef } from 'react';
import {
Calendar as CalendarIcon,
Clock,
ChevronLeft,
ChevronRight,
ChevronDown,
Check,
Sparkles,
X,
Zap
} from 'lucide-react';
interface DateTimePickerProps {
value: string; // ISO format: YYYY-MM-DDTHH:mm
onChange: (value: string) => void;
required?: boolean;
label?: string;
className?: string;
}
const MONTH_NAMES = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const WEEKDAY_NAMES = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
export function DateTimePicker({ value, onChange, required, label, className }: DateTimePickerProps) {
const [isOpen, setIsOpen] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
// Parse incoming value or fallback to current time
const parseValue = (isoStr: string) => {
if (!isoStr) {
const now = new Date();
return {
year: now.getFullYear(),
month: now.getMonth(),
day: now.getDate(),
displayHour: now.getHours() % 12 || 12,
minutes: Math.ceil(now.getMinutes() / 5) * 5 % 60,
period: (now.getHours() >= 12 ? 'PM' : 'AM') as 'AM' | 'PM'
};
}
const year = parseInt(isoStr.slice(0, 4), 10) || new Date().getFullYear();
const month = (parseInt(isoStr.slice(5, 7), 10) - 1) || 0;
const day = parseInt(isoStr.slice(8, 10), 10) || 1;
const hours = parseInt(isoStr.slice(11, 13), 10) || 0;
const minutes = parseInt(isoStr.slice(14, 16), 10) || 0;
const period: 'AM' | 'PM' = hours >= 12 ? 'PM' : 'AM';
const displayHour = hours % 12 || 12;
return { year, month, day, displayHour, minutes, period };
};
const initial = parseValue(value);
const [viewYear, setViewYear] = useState(initial.year);
const [viewMonth, setViewMonth] = useState(initial.month);
const [selectedDay, setSelectedDay] = useState(initial.day);
const [selectedHour, setSelectedHour] = useState(initial.displayHour);
const [selectedMinute, setSelectedMinute] = useState(initial.minutes);
const [selectedPeriod, setSelectedPeriod] = useState<'AM' | 'PM'>(initial.period);
// Sync state when incoming prop changes
useEffect(() => {
if (value) {
const p = parseValue(value);
setViewYear(p.year);
setViewMonth(p.month);
setSelectedDay(p.day);
setSelectedHour(p.displayHour);
setSelectedMinute(p.minutes);
setSelectedPeriod(p.period);
}
}, [value]);
// Close on click outside
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
setIsOpen(false);
}
};
if (isOpen) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => document.removeEventListener('mousedown', handleClickOutside);
}, [isOpen]);
// Build ISO string helper
const buildIso = (y: number, m: number, d: number, hDisplay: number, min: number, p: 'AM' | 'PM') => {
let h24 = hDisplay;
if (p === 'PM' && h24 < 12) h24 += 12;
if (p === 'AM' && h24 === 12) h24 = 0;
const pad = (n: number) => String(n).padStart(2, '0');
return `${y}-${pad(m + 1)}-${pad(d)}T${pad(h24)}:${pad(min)}`;
};
const handleApplyDate = (y: number, m: number, d: number, h: number, min: number, p: 'AM' | 'PM') => {
const iso = buildIso(y, m, d, h, min, p);
onChange(iso);
};
const handleSelectDay = (day: number) => {
setSelectedDay(day);
handleApplyDate(viewYear, viewMonth, day, selectedHour, selectedMinute, selectedPeriod);
};
const handleHourChange = (newHour: number) => {
setSelectedHour(newHour);
handleApplyDate(viewYear, viewMonth, selectedDay, newHour, selectedMinute, selectedPeriod);
};
const handleMinuteChange = (newMinute: number) => {
setSelectedMinute(newMinute);
handleApplyDate(viewYear, viewMonth, selectedDay, selectedHour, newMinute, selectedPeriod);
};
const handlePeriodChange = (newPeriod: 'AM' | 'PM') => {
setSelectedPeriod(newPeriod);
handleApplyDate(viewYear, viewMonth, selectedDay, selectedHour, selectedMinute, newPeriod);
};
// Month Navigation
const handlePrevMonth = () => {
if (viewMonth === 0) {
setViewMonth(11);
setViewYear(v => v - 1);
} else {
setViewMonth(v => v - 1);
}
};
const handleNextMonth = () => {
if (viewMonth === 11) {
setViewMonth(0);
setViewYear(v => v + 1);
} else {
setViewMonth(v => v + 1);
}
};
// Quick Presets
const applyPreset = (minutesToAdd: number) => {
const d = new Date(Date.now() + minutesToAdd * 60000);
const iso = new Date(d.getTime() - (d.getTimezoneOffset() * 60000)).toISOString().slice(0, 16);
onChange(iso);
};
const applyTomorrowPreset = (hour: number) => {
const d = new Date();
d.setDate(d.getDate() + 1);
d.setHours(hour, 0, 0, 0);
const iso = new Date(d.getTime() - (d.getTimezoneOffset() * 60000)).toISOString().slice(0, 16);
onChange(iso);
};
// Calendar Grid Calculation
const daysInCurrentMonth = new Date(viewYear, viewMonth + 1, 0).getDate();
const firstDayWeekday = new Date(viewYear, viewMonth, 1).getDay();
const daysInPrevMonth = new Date(viewYear, viewMonth, 0).getDate();
// Check if day is today
const now = new Date();
const isCurrentMonthThisMonth = now.getFullYear() === viewYear && now.getMonth() === viewMonth;
const todayDate = now.getDate();
// Format display string for trigger input
const formatDisplay = () => {
if (!value) return "Select target run date & time...";
try {
const d = new Date(value);
return d.toLocaleString(undefined, {
weekday: 'short',
month: 'short',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
} catch {
return value;
}
};
return (
<div ref={containerRef} className={`relative ${className || ''}`}>
{label && (
<label className="text-[11px] font-bold text-foreground/40 uppercase tracking-wider ml-0.5 mb-1 block">
{label}
</label>
)}
{/* Custom Interactive Trigger Box */}
<button
type="button"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
setIsOpen(!isOpen);
}}
className={`w-full bg-[#101216] hover:bg-[#13161c] border rounded-xl py-2 px-3 text-xs font-semibold flex items-center justify-between cursor-pointer transition-all shadow-xs group text-left ${
isOpen
? 'border-indigo-500 ring-2 ring-indigo-500/20 text-white'
: 'border-white/[0.08] hover:border-indigo-500/40 text-foreground/90'
}`}
>
<div className="flex items-center gap-2 min-w-0">
<div className="w-6 h-6 rounded-lg bg-indigo-500/10 border border-indigo-500/20 flex items-center justify-center text-indigo-400 shrink-0">
<CalendarIcon size={12} />
</div>
<span className="truncate text-xs font-semibold">
{formatDisplay()}
</span>
</div>
<div className="flex items-center gap-1.5 shrink-0 pl-2">
<Clock size={12} className="text-foreground/40 group-hover:text-indigo-400 transition-colors" />
<ChevronDown size={12} className={`text-foreground/40 transition-transform duration-200 ${isOpen ? 'rotate-180 text-indigo-400' : ''}`} />
</div>
</button>
{/* Floating Glassmorphic Calendar & Time Popover (Time BESIDE Calendar) */}
{isOpen && (
<div
onClick={(e) => e.stopPropagation()}
className="absolute left-0 top-full mt-2 z-[160] w-[330px] sm:w-[490px] max-w-[95vw] bg-[#0f1117]/95 backdrop-blur-2xl border border-white/[0.09] rounded-2xl shadow-[0_25px_60px_-15px_rgba(0,0,0,0.85)] p-3.5 animate-in fade-in zoom-in-95 duration-150 overflow-hidden select-none"
>
{/* Glowing Top Accent Beam */}
<div className="absolute top-0 left-1/4 right-1/4 h-[2px] bg-gradient-to-r from-transparent via-indigo-500 to-transparent pointer-events-none" />
{/* Quick Presets Bar */}
<div className="flex items-center gap-1 overflow-x-auto pb-2.5 mb-3 border-b border-white/[0.06] text-[10px] custom-scrollbar">
<button
type="button"
onClick={() => applyPreset(0)}
className="px-2 py-0.5 rounded-md bg-indigo-500/15 text-indigo-300 hover:bg-indigo-500/25 border border-indigo-500/30 font-bold transition-all shrink-0 cursor-pointer"
>
⚡ Now
</button>
<button
type="button"
onClick={() => applyPreset(15)}
className="px-2 py-0.5 rounded-md bg-white/[0.04] text-foreground/70 hover:text-foreground hover:bg-white/[0.08] border border-white/[0.06] font-semibold transition-all shrink-0 cursor-pointer"
>
+15m
</button>
<button
type="button"
onClick={() => applyPreset(60)}
className="px-2 py-0.5 rounded-md bg-white/[0.04] text-foreground/70 hover:text-foreground hover:bg-white/[0.08] border border-white/[0.06] font-semibold transition-all shrink-0 cursor-pointer"
>
+1h
</button>
<button
type="button"
onClick={() => applyPreset(180)}
className="px-2 py-0.5 rounded-md bg-white/[0.04] text-foreground/70 hover:text-foreground hover:bg-white/[0.08] border border-white/[0.06] font-semibold transition-all shrink-0 cursor-pointer"
>
+3h
</button>
<button
type="button"
onClick={() => applyTomorrowPreset(9)}
className="px-2 py-0.5 rounded-md bg-white/[0.04] text-foreground/70 hover:text-foreground hover:bg-white/[0.08] border border-white/[0.06] font-semibold transition-all shrink-0 cursor-pointer"
>
Tmr 9AM
</button>
<button
type="button"
onClick={() => applyTomorrowPreset(18)}
className="px-2 py-0.5 rounded-md bg-white/[0.04] text-foreground/70 hover:text-foreground hover:bg-white/[0.08] border border-white/[0.06] font-semibold transition-all shrink-0 cursor-pointer"
>
Tmr 6PM
</button>
</div>
{/* Main Content: Calendar (Left) & Time Controls (Right - BESIDE Calendar) */}
<div className="grid grid-cols-1 sm:grid-cols-[1fr_165px] gap-3.5">
{/* LEFT: Calendar Days Grid */}
<div className="space-y-2">
{/* Month & Year Navigation Header */}
<div className="flex items-center justify-between px-1">
<span className="font-bold text-xs sm:text-sm text-white tracking-tight">
{MONTH_NAMES[viewMonth]} {viewYear}
</span>
<div className="flex items-center gap-1">
<button
type="button"
onClick={handlePrevMonth}
className="p-1 rounded-lg hover:bg-white/[0.08] text-foreground/60 hover:text-white transition-colors cursor-pointer"
title="Previous Month"
>
<ChevronLeft size={15} />
</button>
<button
type="button"
onClick={() => {
const n = new Date();
setViewMonth(n.getMonth());
setViewYear(n.getFullYear());
handleSelectDay(n.getDate());
}}
className="px-2 py-0.5 rounded-md bg-white/[0.05] text-[10px] font-bold text-indigo-400 hover:text-indigo-300 hover:bg-white/[0.08] border border-white/[0.06] transition-colors cursor-pointer"
>
Today
</button>
<button
type="button"
onClick={handleNextMonth}
className="p-1 rounded-lg hover:bg-white/[0.08] text-foreground/60 hover:text-white transition-colors cursor-pointer"
title="Next Month"
>
<ChevronRight size={15} />
</button>
</div>
</div>
{/* Weekday Row */}
<div className="grid grid-cols-7 gap-1 text-center">
{WEEKDAY_NAMES.map((w, idx) => (
<span key={idx} className="text-[10px] font-bold uppercase tracking-wider text-foreground/35 py-0.5">
{w}
</span>
))}
</div>
{/* Calendar Days */}
<div className="grid grid-cols-7 gap-1 text-center">
{Array.from({ length: firstDayWeekday }).map((_, idx) => {
const d = daysInPrevMonth - firstDayWeekday + idx + 1;
return (
<div key={`prev-${idx}`} className="h-7 flex items-center justify-center text-[11px] text-foreground/20 font-medium">
{d}
</div>
);
})}
{Array.from({ length: daysInCurrentMonth }).map((_, idx) => {
const day = idx + 1;
const isSelected = selectedDay === day && viewMonth === (parseValue(value).month) && viewYear === (parseValue(value).year);
const isToday = isCurrentMonthThisMonth && day === todayDate;
return (
<button
key={`cur-${day}`}
type="button"
onClick={() => handleSelectDay(day)}
className={`h-7 rounded-lg text-xs font-semibold flex items-center justify-center transition-all cursor-pointer relative ${
isSelected
? 'bg-gradient-to-r from-indigo-500 to-purple-600 text-white font-bold shadow-md shadow-indigo-500/40 ring-1 ring-indigo-400/50 scale-105 z-10'
: isToday
? 'bg-indigo-500/10 text-indigo-300 border border-indigo-500/30 hover:bg-indigo-500/20'
: 'text-foreground/80 hover:bg-white/[0.08] hover:text-white'
}`}
>
<span>{day}</span>
{isToday && !isSelected && (
<span className="w-1 h-1 rounded-full bg-indigo-400 absolute bottom-0.5" />
)}
</button>
);
})}
</div>
</div>
{/* RIGHT: Time Selector Controls (Beside Calendar!) */}
<div className="sm:border-l sm:border-white/[0.07] sm:pl-3.5 flex flex-col justify-between pt-2.5 sm:pt-0 border-t sm:border-t-0 border-white/[0.06] space-y-2.5">
<div>
<div className="flex items-center justify-between mb-2">
<span className="text-[10px] font-bold text-foreground/50 uppercase tracking-wider flex items-center gap-1">
<Clock size={11} className="text-indigo-400" />
Time
</span>
<span className="text-[11px] font-mono font-bold text-indigo-300 bg-indigo-500/15 px-2 py-0.5 rounded-md border border-indigo-500/30">
{String(selectedHour).padStart(2, '0')}:{String(selectedMinute).padStart(2, '0')} {selectedPeriod}
</span>
</div>
{/* Hour & Minute Selectors */}
<div className="grid grid-cols-2 gap-1.5 mb-2">
<div className="space-y-0.5">
<label className="text-[9px] font-bold text-foreground/40 uppercase tracking-wider block">Hour</label>
<select
value={selectedHour}
onChange={(e) => handleHourChange(parseInt(e.target.value, 10))}
className="w-full bg-[#151922] border border-white/[0.08] hover:border-indigo-500/40 text-white rounded-lg py-1 px-1.5 text-xs font-bold outline-none cursor-pointer [color-scheme:dark]"
>
{Array.from({ length: 12 }).map((_, i) => {
const h = i + 1;
return <option key={h} value={h}>{String(h).padStart(2, '0')}</option>;
})}
</select>
</div>
<div className="space-y-0.5">
<label className="text-[9px] font-bold text-foreground/40 uppercase tracking-wider block">Minute</label>
<select
value={selectedMinute}
onChange={(e) => handleMinuteChange(parseInt(e.target.value, 10))}
className="w-full bg-[#151922] border border-white/[0.08] hover:border-indigo-500/40 text-white rounded-lg py-1 px-1.5 text-xs font-bold outline-none cursor-pointer [color-scheme:dark]"
>
{Array.from({ length: 12 }).map((_, i) => {
const m = i * 5;
return <option key={m} value={m}>{String(m).padStart(2, '0')}</option>;
})}
</select>
</div>
</div>
{/* AM / PM Segmented Switch */}
<div className="space-y-0.5 mb-2">
<label className="text-[9px] font-bold text-foreground/40 uppercase tracking-wider block">Period</label>
<div className="grid grid-cols-2 p-0.5 bg-[#151922] border border-white/[0.08] rounded-lg">
{(['AM', 'PM'] as const).map((p) => (
<button
key={p}
type="button"
onClick={() => handlePeriodChange(p)}
className={`py-1 text-[10px] font-bold rounded-md transition-all cursor-pointer ${
selectedPeriod === p
? 'bg-gradient-to-r from-indigo-500 to-purple-600 text-white shadow-xs'
: 'text-foreground/50 hover:text-foreground'
}`}
>
{p}
</button>
))}
</div>
</div>
{/* Quick Times Shortcuts */}
<div className="space-y-1">
<label className="text-[9px] font-bold text-foreground/40 uppercase tracking-wider block">Quick Times</label>
<div className="grid grid-cols-2 gap-1 text-[9px] font-bold">
<button
type="button"
onClick={() => {
handleHourChange(9);
handleMinuteChange(0);
handlePeriodChange('AM');
}}
className="py-1 px-1 text-center rounded bg-white/[0.04] hover:bg-indigo-500/15 hover:text-indigo-300 text-foreground/70 border border-white/[0.05] transition-all cursor-pointer"
>
9:00 AM
</button>
<button
type="button"
onClick={() => {
handleHourChange(12);
handleMinuteChange(0);
handlePeriodChange('PM');
}}
className="py-1 px-1 text-center rounded bg-white/[0.04] hover:bg-indigo-500/15 hover:text-indigo-300 text-foreground/70 border border-white/[0.05] transition-all cursor-pointer"
>
12:00 PM
</button>
<button
type="button"
onClick={() => {
handleHourChange(3);
handleMinuteChange(0);
handlePeriodChange('PM');
}}
className="py-1 px-1 text-center rounded bg-white/[0.04] hover:bg-indigo-500/15 hover:text-indigo-300 text-foreground/70 border border-white/[0.05] transition-all cursor-pointer"
>
3:00 PM
</button>
<button
type="button"
onClick={() => {
handleHourChange(6);
handleMinuteChange(0);
handlePeriodChange('PM');
}}
className="py-1 px-1 text-center rounded bg-white/[0.04] hover:bg-indigo-500/15 hover:text-indigo-300 text-foreground/70 border border-white/[0.05] transition-all cursor-pointer"
>
6:00 PM
</button>
</div>
</div>
</div>
</div>
</div>
{/* Action Bar Footer */}
<div className="mt-3 pt-2.5 border-t border-white/[0.07] flex items-center justify-between">
<button
type="button"
onClick={() => setIsOpen(false)}
className="text-xs text-foreground/50 hover:text-foreground font-semibold px-2 py-1 rounded-lg hover:bg-white/[0.05] transition-colors cursor-pointer"
>
Cancel
</button>
<button
type="button"
onClick={() => setIsOpen(false)}
className="px-3 py-1 rounded-xl bg-gradient-to-r from-indigo-500 to-purple-600 hover:from-indigo-600 hover:to-purple-700 text-white text-xs font-bold shadow-md shadow-indigo-500/20 active:scale-95 transition-all flex items-center gap-1.5 cursor-pointer"
>
<Check size={12} />
<span>Set Target Time</span>
</button>
</div>
</div>
)}
</div>
);
}
|