155 lines
5.7 KiB
TypeScript
Executable File
155 lines
5.7 KiB
TypeScript
Executable File
import { memo } from 'react';
|
|
import type { Selection } from '@/hooks/use-planner';
|
|
import {
|
|
formatRu,
|
|
isWeekend,
|
|
monthGrid,
|
|
todayIso,
|
|
WEEKDAYS_RU,
|
|
MONTHS_RU,
|
|
} from '@/lib/calendar';
|
|
import { stripes, tint } from '@/lib/marker-colors';
|
|
import { cn } from '@/lib/utils';
|
|
import type { Marker, VacationPeriod } from '@/types/planner';
|
|
|
|
export type DayInfo = {
|
|
marker: Marker;
|
|
period: VacationPeriod;
|
|
};
|
|
|
|
type Props = {
|
|
year: number;
|
|
month0: number;
|
|
dayMap: Map<string, DayInfo[]>;
|
|
selection: Selection | null;
|
|
brushColor: string | null;
|
|
onDayPointerDown: (date: string, pointerType: string) => boolean;
|
|
onDayPointerEnter: (date: string) => void;
|
|
onDayTouchTap: (date: string) => void;
|
|
onDayInspect: (date: string) => void;
|
|
};
|
|
|
|
export const MonthCalendar = memo(function MonthCalendar({
|
|
year,
|
|
month0,
|
|
dayMap,
|
|
selection,
|
|
brushColor,
|
|
onDayPointerDown,
|
|
onDayPointerEnter,
|
|
onDayTouchTap,
|
|
onDayInspect,
|
|
}: Props) {
|
|
const weeks = monthGrid(year, month0);
|
|
const today = todayIso();
|
|
const selStart =
|
|
selection && selection.start <= selection.end
|
|
? selection.start
|
|
: selection?.end;
|
|
const selEnd =
|
|
selection && selection.start <= selection.end
|
|
? selection.end
|
|
: selection?.start;
|
|
|
|
return (
|
|
<div className="rounded-lg border bg-card p-3 shadow-xs">
|
|
<div className="mb-2 px-1 text-sm font-semibold">
|
|
{MONTHS_RU[month0]}
|
|
</div>
|
|
|
|
<div className="grid grid-cols-7 gap-1">
|
|
{WEEKDAYS_RU.map((day, i) => (
|
|
<div
|
|
key={day}
|
|
className={cn(
|
|
'pb-1 text-center text-[11px] font-medium text-muted-foreground',
|
|
i >= 5 && 'text-red-500/70 dark:text-red-400/70',
|
|
)}
|
|
>
|
|
{day}
|
|
</div>
|
|
))}
|
|
|
|
{weeks.flat().map((date, i) => {
|
|
if (date === null) {
|
|
return (
|
|
<div key={`empty-${i}`} className="h-9 sm:h-10" />
|
|
);
|
|
}
|
|
|
|
const entries = dayMap.get(date) ?? [];
|
|
const inSelection =
|
|
selStart !== undefined &&
|
|
selEnd !== undefined &&
|
|
date >= selStart &&
|
|
date <= selEnd;
|
|
|
|
let background: string | undefined;
|
|
|
|
if (entries.length === 1) {
|
|
background = tint(entries[0].marker.color);
|
|
} else if (entries.length > 1) {
|
|
background = stripes(
|
|
entries.map((e) => e.marker.color),
|
|
);
|
|
}
|
|
|
|
const markedNames = entries
|
|
.map((e) => e.marker.name)
|
|
.join(', ');
|
|
|
|
return (
|
|
<button
|
|
key={date}
|
|
type="button"
|
|
data-date={date}
|
|
onPointerDown={(e) => {
|
|
if (brushColor && e.pointerType !== 'touch') {
|
|
e.preventDefault();
|
|
onDayPointerDown(date, e.pointerType);
|
|
}
|
|
}}
|
|
onPointerOver={() => onDayPointerEnter(date)}
|
|
onPointerUp={(e) => {
|
|
if (brushColor && e.pointerType === 'touch') {
|
|
onDayTouchTap(date);
|
|
}
|
|
}}
|
|
onClick={() => {
|
|
if (!brushColor) {
|
|
onDayInspect(date);
|
|
}
|
|
}}
|
|
className={cn(
|
|
'relative flex h-9 touch-manipulation items-center justify-center rounded-md text-xs select-none sm:h-10 sm:text-sm',
|
|
entries.length === 0 &&
|
|
'hover:bg-accent active:bg-accent',
|
|
isWeekend(date) &&
|
|
entries.length === 0 &&
|
|
'text-red-500/80 dark:text-red-400/80',
|
|
date === today &&
|
|
'font-bold ring-1 ring-ring ring-inset',
|
|
inSelection &&
|
|
'ring-2 ring-foreground/70 ring-inset',
|
|
)}
|
|
style={background ? { background } : undefined}
|
|
aria-label={`${formatRu(date, true)}${markedNames ? `, отмечено: ${markedNames}` : ''}`}
|
|
>
|
|
{Number(date.slice(-2))}
|
|
{entries.length > 0 && (
|
|
<span
|
|
className="absolute bottom-0.5 left-1/2 h-1 w-4 -translate-x-1/2 rounded-full"
|
|
style={{
|
|
backgroundColor:
|
|
entries[0].marker.color,
|
|
}}
|
|
/>
|
|
)}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
);
|
|
});
|