(
relPath: string | null,
historyMode: 'push' | 'preserve' = 'push',
opts?: { preview?: boolean }
)
| 2954 | |
| 2955 | export const useStore = create<Store>((set, get) => { |
| 2956 | const selectNoteImpl = async ( |
| 2957 | relPath: string | null, |
| 2958 | historyMode: 'push' | 'preserve' = 'push', |
| 2959 | opts?: { preview?: boolean } |
| 2960 | ): Promise<boolean> => { |
| 2961 | const startedAt = performance.now() |
| 2962 | const state = get() |
| 2963 | const activeLeaf = findLeaf(state.paneLayout, state.activePaneId) |
| 2964 | if (!activeLeaf) return false |
| 2965 | |
| 2966 | // Preview opens reuse the pane's preview slot; permanent opens promote |
| 2967 | // the path if it was previously sitting in that slot. |
| 2968 | const addTabToLeaf = (l: PaneLeaf, path: string): PaneLeaf => |
| 2969 | opts?.preview |
| 2970 | ? leafWithPreviewTab(l, path) |
| 2971 | : leafWithPromotedTab(leafWithAddedTab(l, path), path) |
| 2972 | |
| 2973 | if (!relPath) { |
| 2974 | const nextLayout = |
| 2975 | updateLeaf(state.paneLayout, activeLeaf.id, (l) => ({ |
| 2976 | ...l, |
| 2977 | tabs: [], |
| 2978 | activeTab: null |
| 2979 | })) ?? makeLeaf() |
| 2980 | // If the tree lost the active leaf (shouldn't here, but defensively), |
| 2981 | // pin active to a surviving leaf. |
| 2982 | const ensured = ensureActivePane(nextLayout, state.activePaneId) |
| 2983 | const active = activeFieldsFrom( |
| 2984 | ensured.layout, |
| 2985 | ensured.activePaneId, |
| 2986 | state.noteContents, |
| 2987 | state.noteDirty |
| 2988 | ) |
| 2989 | set({ |
| 2990 | paneLayout: ensured.layout, |
| 2991 | activePaneId: ensured.activePaneId, |
| 2992 | ...active, |
| 2993 | loadingNote: false, |
| 2994 | pendingJumpLocation: null |
| 2995 | }) |
| 2996 | return true |
| 2997 | } |
| 2998 | |
| 2999 | if (isWorkspaceVirtualTabPath(relPath)) { |
| 3000 | if ( |
| 3001 | state.selectedPath && |
| 3002 | state.selectedPath !== relPath && |
| 3003 | !isWorkspaceVirtualTabPath(state.selectedPath) && |
| 3004 | state.noteDirty[state.selectedPath] |
| 3005 | ) { |
| 3006 | await get().persistNote(state.selectedPath) |
| 3007 | } |
| 3008 | const latest = get() |
| 3009 | const leafNow = findLeaf(latest.paneLayout, latest.activePaneId) |
| 3010 | if (!leafNow) return false |
| 3011 | const nextLayout = |
| 3012 | updateLeaf(latest.paneLayout, leafNow.id, (l) => leafWithAddedTab(l, relPath)) ?? |
| 3013 | latest.paneLayout |
no test coverage detected