Keep calendar timeline stable when inspecting dates

This commit is contained in:
syntaxbullet
2026-09-05 08:10:51 +02:00
parent 1f017caee5
commit 2f67b775bf
2 changed files with 62 additions and 6 deletions

View File

@@ -44,11 +44,18 @@ export function CalendarHeatmap({
const latestDate =
allDays.findLast((day) => day.state !== "future")?.date ??
allDays.at(-1)?.date;
// A date selected in the editor must stay visible even outside the current window.
const anchor =
controlledDate && allDays.some((day) => day.date === controlledDate)
? controlledDate
: latestDate;
// Inspecting a square changes selection, not the window. Only an external
// date outside the visible preset needs a new anchor to bring it into view.
const [windowAnchor, setWindowAnchor] = useState({ latestDate, date: latestDate });
let anchor = windowAnchor.latestDate === latestDate ? windowAnchor.date : latestDate;
if (months !== "custom" && anchor && controlledDate &&
allDays.some(day => day.date === controlledDate) &&
!visibleCalendarDays(allDays, months, anchor).some(day => day.date === controlledDate)) {
anchor = controlledDate;
}
if (windowAnchor.latestDate !== latestDate || windowAnchor.date !== anchor) {
setWindowAnchor({ latestDate, date: anchor });
}
const [localSelectedDate, setLocalSelectedDate] = useState(anchor);
const selectedDate = controlledDate ?? localSelectedDate;
function setSelectedDate(date: string) {