import { useState } from 'react' import { GripVertical, ArrowUp, ArrowDown, Plus } from 'lucide-react' import Modal from '../shared/Modal' import type { Day } from '../../types' interface DayReorderPopupProps { isOpen: boolean days: Day[] t: (key: string, params?: Record) => string locale: string onReorder: (orderedIds: number[]) => void onAddDay: () => void onClose: () => void } /** * Modal for moving whole days around: drag a row by its grip or use the up/down * arrows, and add a day at the end. Day headers stay untouched — this is the * single surface for ordering. Reorders are applied optimistically by the store, * so the list reflects each move immediately. */ export function DayReorderPopup({ isOpen, days, t, locale, onReorder, onAddDay, onClose }: DayReorderPopupProps) { const [dragIndex, setDragIndex] = useState(null) const [overIndex, setOverIndex] = useState(null) const ordered = [...days].sort((a, b) => (a.day_number ?? 0) - (b.day_number ?? 0)) const label = (day: Day, index: number) => { if (day.title) return day.title if (day.date) { const d = new Date(day.date + 'T00:00:00') return d.toLocaleDateString(locale, { weekday: 'short', day: 'numeric', month: 'short' }) } return t('dayplan.dayN', { n: index + 1 }) } const move = (from: number, to: number) => { if (to < 0 || to >= ordered.length || from === to) return const ids = ordered.map(d => d.id) const [moved] = ids.splice(from, 1) ids.splice(to, 0, moved) onReorder(ids) } const cellBtn = { display: 'grid', placeItems: 'center', width: 28, height: 28, border: '1px solid var(--border-faint)', borderRadius: 7, background: 'none', cursor: 'pointer', color: 'var(--text-muted)', padding: 0, } as const return ( } >

{t('dayplan.reorderHint')}

{ordered.map((day, index) => (
setDragIndex(index)} onDragEnd={() => { setDragIndex(null); setOverIndex(null) }} onDragOver={e => { e.preventDefault(); if (overIndex !== index) setOverIndex(index) }} onDrop={e => { e.preventDefault() if (dragIndex !== null && dragIndex !== index) move(dragIndex, index) setDragIndex(null); setOverIndex(null) }} style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '8px 10px', borderRadius: 9, border: '1px solid var(--border-faint)', background: overIndex === index && dragIndex !== null && dragIndex !== index ? 'var(--bg-hover)' : 'var(--bg-card, white)', opacity: dragIndex === index ? 0.5 : 1, outline: overIndex === index && dragIndex !== null && dragIndex !== index ? '2px dashed var(--border-primary)' : 'none', outlineOffset: -2, }} > {index + 1} {label(day, index)}
))}
) }