142 lines
5.6 KiB
TypeScript
Executable File
142 lines
5.6 KiB
TypeScript
Executable File
import { Head } from '@inertiajs/react';
|
||
import { useMemo, useState } from 'react';
|
||
import { DayDialog } from '@/components/planner/day-dialog';
|
||
import { MarkerPanel } from '@/components/planner/marker-panel';
|
||
import {
|
||
MonthCalendar,
|
||
type DayInfo,
|
||
} from '@/components/planner/month-calendar';
|
||
import { YearSelect } from '@/components/planner/year-select';
|
||
import { Button } from '@/components/ui/button';
|
||
import { usePlanner } from '@/hooks/use-planner';
|
||
import { addDays } from '@/lib/calendar';
|
||
import type { PlannerPageProps } from '@/types/planner';
|
||
|
||
export default function Planner({
|
||
year,
|
||
years,
|
||
markers,
|
||
periods,
|
||
}: PlannerPageProps) {
|
||
const planner = usePlanner({ markers, periods });
|
||
const [inspectDate, setInspectDate] = useState<string | null>(null);
|
||
|
||
const dayMap = useMemo(() => {
|
||
const markerById = new Map(markers.map((m) => [m.id, m]));
|
||
const map = new Map<string, DayInfo[]>();
|
||
|
||
for (const period of periods) {
|
||
const marker = markerById.get(period.marker_id);
|
||
|
||
if (!marker) {
|
||
continue;
|
||
}
|
||
|
||
let date = period.start_date;
|
||
let guard = 0;
|
||
|
||
while (date <= period.end_date && guard < 400) {
|
||
const list = map.get(date) ?? [];
|
||
list.push({ marker, period });
|
||
map.set(date, list);
|
||
date = addDays(date, 1);
|
||
guard++;
|
||
}
|
||
}
|
||
|
||
return map;
|
||
}, [markers, periods]);
|
||
|
||
const brushColor = planner.activeMarker?.color ?? null;
|
||
|
||
return (
|
||
<>
|
||
<Head title={`План отпусков — ${year}`} />
|
||
|
||
<div className="space-y-4">
|
||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||
<h1 className="text-xl font-semibold">
|
||
План отпусков{' '}
|
||
<span className="text-muted-foreground">{year}</span>
|
||
</h1>
|
||
<YearSelect year={year} years={years} />
|
||
</div>
|
||
|
||
<MarkerPanel
|
||
markers={planner.markers}
|
||
periods={planner.periods}
|
||
year={year}
|
||
activeMarkerId={planner.activeMarkerId}
|
||
onSelect={planner.setActiveMarkerId}
|
||
/>
|
||
|
||
<div className="flex min-h-8 flex-wrap items-center gap-2 text-sm text-muted-foreground">
|
||
{planner.activeMarker ? (
|
||
<>
|
||
<span
|
||
className="size-3 rounded-full"
|
||
style={{
|
||
backgroundColor: planner.activeMarker.color,
|
||
}}
|
||
/>
|
||
<span>
|
||
Кисть «{planner.activeMarker.name}»: клик — один
|
||
день, протяжка — период. На сенсорном экране:
|
||
два касания — начало и конец.
|
||
</span>
|
||
{planner.touchAnchor && (
|
||
<Button
|
||
variant="outline"
|
||
size="sm"
|
||
className="h-7 rounded-full"
|
||
onClick={planner.cancelTouchAnchor}
|
||
>
|
||
Отменить выбор
|
||
</Button>
|
||
)}
|
||
</>
|
||
) : markers.length > 0 ? (
|
||
<span>
|
||
Выберите маркер, чтобы отмечать даты, или нажмите на
|
||
день, чтобы посмотреть отметки.
|
||
</span>
|
||
) : (
|
||
<span>
|
||
Добавьте первый маркер — цвет и имя человека, —
|
||
чтобы начать планирование.
|
||
</span>
|
||
)}
|
||
</div>
|
||
|
||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
|
||
{Array.from({ length: 12 }, (_, month0) => (
|
||
<MonthCalendar
|
||
key={`${year}-${month0}`}
|
||
year={year}
|
||
month0={month0}
|
||
dayMap={dayMap}
|
||
selection={planner.selection}
|
||
brushColor={brushColor}
|
||
onDayPointerDown={planner.dayPointerDown}
|
||
onDayPointerEnter={planner.dayPointerEnter}
|
||
onDayTouchTap={(date) =>
|
||
planner.dayPointerDown(date, 'touch')
|
||
}
|
||
onDayInspect={setInspectDate}
|
||
/>
|
||
))}
|
||
</div>
|
||
</div>
|
||
|
||
<DayDialog
|
||
date={inspectDate}
|
||
entries={inspectDate ? (dayMap.get(inspectDate) ?? []) : []}
|
||
markers={planner.markers}
|
||
onClose={() => setInspectDate(null)}
|
||
onDelete={planner.deletePeriod}
|
||
onReschedule={planner.reschedulePeriod}
|
||
/>
|
||
</>
|
||
);
|
||
}
|