Skip to content

Commit 8620ea2

Browse files
committed
Fix(calendar): the auto-opened panel stops leaking onto other notes (#502)
The calendar panel is one sticky flag per pane, deliberately: whatever you set stays while you browse. The daily/weekly auto-open wrote into that same flag, so visiting one periodic note turned into "the panel is now open on every note" and a close the user made earlier was quietly undone. The fix is provenance, one extra bit: an open the APP made is scoped to the calendar's own territory and closes on the way out, while an open the USER made keeps the documented stickiness. Closing an auto-opened panel by hand sticks, and re-opening it by hand converts it into a deliberate open that survives leaving the date note. The transitions live in lib/calendar-panel-auto with the reported repro pinned as a test, and the built app was driven through the same sequence end to end. Reported by @uNyanda with keystroke-level steps, as usual.
1 parent 5272b25 commit 8620ea2

3 files changed

Lines changed: 154 additions & 13 deletions

File tree

‎packages/app-core/src/components/EditorPane.tsx‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ import {
225225
import { resolveCommentAnchor, selectionToCommentAnchor } from '../lib/comments'
226226
import { ZEN_OPEN_EDITOR_CONTEXT_MENU_EVENT } from '../lib/keyboard-context-menu'
227227
import { armMiddleClickPasteGuard } from '../lib/middle-click-paste-guard'
228+
import {
229+
CALENDAR_PANEL_CLOSED,
230+
calendarPanelOnNote,
231+
calendarPanelOnToggle,
232+
type CalendarPanelState
233+
} from '../lib/calendar-panel-auto'
228234
import {
229235
assetPathFromTab,
230236
assetTitleFromPath,
@@ -800,7 +806,8 @@ export function EditorPane({ pane }: { pane: PaneLeaf }): JSX.Element {
800806
const [outlineOpen, setOutlineOpen] = useState(false)
801807
const [activeOutlineLine, setActiveOutlineLine] = useState<number | null>(null)
802808
const [commentsOpen, setCommentsOpen] = useState(false)
803-
const [calendarOpen, setCalendarOpen] = useState(false)
809+
const [calendarPanel, setCalendarPanel] = useState<CalendarPanelState>(CALENDAR_PANEL_CLOSED)
810+
const calendarOpen = calendarPanel.open
804811
// The calendar panel is a date navigator. It auto-opens while the pane shows
805812
// a daily/weekly note, but stays available (Obsidian-style) on any note as
806813
// long as the daily or weekly feature is enabled.
@@ -986,7 +993,7 @@ export function EditorPane({ pane }: { pane: PaneLeaf }): JSX.Element {
986993
}, [])
987994

988995
const toggleCalendarPanel = useCallback(() => {
989-
setCalendarOpen((open) => !open)
996+
setCalendarPanel(calendarPanelOnToggle)
990997
}, [])
991998

992999

@@ -1042,7 +1049,7 @@ export function EditorPane({ pane }: { pane: PaneLeaf }): JSX.Element {
10421049
setConnectionsOpen(false)
10431050
setOutlineOpen(false)
10441051
setCommentsOpen(false)
1045-
setCalendarOpen(false)
1052+
setCalendarPanel(CALENDAR_PANEL_CLOSED)
10461053
setConnectionPreview(null)
10471054
const panel = useStore.getState().focusedPanel
10481055
if (panel === 'connections' || panel === 'comments' || panel === 'hoverpreview') {
@@ -1053,17 +1060,21 @@ export function EditorPane({ pane }: { pane: PaneLeaf }): JSX.Element {
10531060
return () => window.removeEventListener('zen:close-right-panel', handler)
10541061
}, [isActive, setConnectionPreview, setFocusedPanel])
10551062

1056-
// Auto-show the calendar when this pane lands on a daily/weekly note. On other
1057-
// notes we leave it as-is (Obsidian-style persistence) so it stays open while
1058-
// you browse, and only force it closed when the feature is turned off entirely.
1059-
// Keyed on the note identity (not every render) so a manual `leader c` / icon
1060-
// close sticks until the note changes.
1063+
// Auto-show the calendar when this pane lands on a daily/weekly note. A
1064+
// panel the USER opened keeps Obsidian-style persistence and stays open
1065+
// while they browse; a panel THIS effect opened is scoped to the periodic
1066+
// notes and closes on the way out, so a visit to a daily note can no longer
1067+
// overwrite a close the user made elsewhere (#502). The transitions live in
1068+
// `calendar-panel-auto`; keyed on the note identity (not every render) so a
1069+
// manual `leader c` / icon close sticks until the note changes.
10611070
useEffect(() => {
1062-
if (!calendarAvailable) {
1063-
setCalendarOpen(false)
1064-
return
1065-
}
1066-
if (isDateNote && autoCalendarPanel) setCalendarOpen(true)
1071+
setCalendarPanel((state) =>
1072+
calendarPanelOnNote(state, {
1073+
isDateNote,
1074+
autoEnabled: autoCalendarPanel,
1075+
available: calendarAvailable
1076+
})
1077+
)
10671078
}, [content?.path, isDateNote, autoCalendarPanel, calendarAvailable])
10681079

10691080
useEffect(() => {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { describe, expect, it } from 'vitest'
2+
import {
3+
CALENDAR_PANEL_CLOSED,
4+
calendarPanelOnNote,
5+
calendarPanelOnToggle,
6+
type CalendarPanelState
7+
} from './calendar-panel-auto'
8+
9+
const on = (state: CalendarPanelState, isDateNote: boolean): CalendarPanelState =>
10+
calendarPanelOnNote(state, { isDateNote, autoEnabled: true, available: true })
11+
12+
describe('calendar panel auto-open provenance (#502)', () => {
13+
it('walks the reported repro: a periodic visit no longer reopens a closed panel', () => {
14+
// 1-2: on a non-periodic note the user closes the panel.
15+
let state: CalendarPanelState = { open: true, auto: false }
16+
state = calendarPanelOnToggle(state)
17+
// 3-4: switching between non-periodic notes preserves that.
18+
state = on(state, false)
19+
expect(state.open).toBe(false)
20+
// 5-6: a daily note auto-opens the panel.
21+
state = on(state, true)
22+
expect(state).toEqual({ open: true, auto: true })
23+
// 7: returning to the non-periodic note closes what the auto-open opened.
24+
state = on(state, false)
25+
expect(state.open).toBe(false)
26+
})
27+
28+
it('keeps a panel the user opened themselves open across a periodic visit', () => {
29+
// Open by hand, browse through a daily note, come back: still open. This
30+
// is the sticky behaviour the pane always promised.
31+
let state = calendarPanelOnToggle(CALENDAR_PANEL_CLOSED)
32+
state = on(state, true)
33+
expect(state).toEqual({ open: true, auto: false })
34+
state = on(state, false)
35+
expect(state.open).toBe(true)
36+
})
37+
38+
it('closing an auto-opened panel by hand sticks', () => {
39+
let state = on(CALENDAR_PANEL_CLOSED, true)
40+
state = calendarPanelOnToggle(state)
41+
expect(state.open).toBe(false)
42+
// Still on the same date note nothing re-opens it (the caller keys the
43+
// note-change transition on the note identity), and later non-date notes
44+
// stay closed too.
45+
state = on(state, false)
46+
expect(state.open).toBe(false)
47+
})
48+
49+
it('re-opening by hand on the date note converts the open into a deliberate one', () => {
50+
let state = on(CALENDAR_PANEL_CLOSED, true)
51+
state = calendarPanelOnToggle(state) // closed
52+
state = calendarPanelOnToggle(state) // opened, by the user now
53+
state = on(state, false)
54+
expect(state.open).toBe(true)
55+
})
56+
57+
it('consecutive periodic notes keep the auto provenance until the exit', () => {
58+
let state = on(CALENDAR_PANEL_CLOSED, true)
59+
state = on(state, true)
60+
expect(state).toEqual({ open: true, auto: true })
61+
state = on(state, false)
62+
expect(state.open).toBe(false)
63+
})
64+
65+
it('does nothing automatic when the preference is off', () => {
66+
const state = calendarPanelOnNote(CALENDAR_PANEL_CLOSED, {
67+
isDateNote: true,
68+
autoEnabled: false,
69+
available: true
70+
})
71+
expect(state.open).toBe(false)
72+
})
73+
74+
it('an unavailable calendar forces closed and clears provenance', () => {
75+
const state = calendarPanelOnNote(
76+
{ open: true, auto: true },
77+
{ isDateNote: true, autoEnabled: true, available: false }
78+
)
79+
expect(state).toEqual(CALENDAR_PANEL_CLOSED)
80+
})
81+
})
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Who opened the calendar panel: the user, or the auto-open?
2+
//
3+
// The panel is one sticky flag per pane, Obsidian-style: whatever you set
4+
// stays while you browse. The auto-open for daily/weekly notes wrote into
5+
// that same flag, so a visit to a periodic note turned into "the panel is now
6+
// open on every note" and a close the user made earlier was quietly undone
7+
// (#502). The distinction that fixes it is provenance: an open the APP made
8+
// is scoped to the calendar's own territory and closes on the way out, while
9+
// an open the USER made keeps the documented stickiness. One extra bit,
10+
// `auto`, carries that provenance; these transitions are the only writers.
11+
12+
export interface CalendarPanelState {
13+
open: boolean
14+
/** True while the current `open` came from the auto-open, not the user. */
15+
auto: boolean
16+
}
17+
18+
export const CALENDAR_PANEL_CLOSED: CalendarPanelState = { open: false, auto: false }
19+
20+
/**
21+
* The pane landed on a (possibly different) note.
22+
*
23+
* Auto-open only ever fires on a transition to open: a panel the user already
24+
* has open on a date note is theirs and keeps `auto: false`, so leaving the
25+
* date note keeps it open, which is exactly the browse-through behaviour the
26+
* sticky flag was built for. Leaving the calendar's territory closes only what
27+
* the auto-open itself opened.
28+
*/
29+
export function calendarPanelOnNote(
30+
state: CalendarPanelState,
31+
input: { isDateNote: boolean; autoEnabled: boolean; available: boolean }
32+
): CalendarPanelState {
33+
if (!input.available) return CALENDAR_PANEL_CLOSED
34+
if (input.isDateNote) {
35+
if (input.autoEnabled && !state.open) return { open: true, auto: true }
36+
return state
37+
}
38+
if (state.auto && state.open) return CALENDAR_PANEL_CLOSED
39+
return state
40+
}
41+
42+
/**
43+
* The user pressed the toggle. Whatever the result, it is theirs now: closing
44+
* an auto-opened panel must stick, and re-opening it by hand converts it into
45+
* a deliberate open that survives leaving the date note.
46+
*/
47+
export function calendarPanelOnToggle(state: CalendarPanelState): CalendarPanelState {
48+
return { open: !state.open, auto: false }
49+
}

0 commit comments

Comments
 (0)