Keep calendar timeline stable when inspecting dates
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user