import { useCallback, type RefObject } from 'react' import type { SidebarSelection, VaultEntry } from '../types' interface UseInboxOrganizeAdvanceOptions { activeTabPath: string | null activeTabPathRef: RefObject autoAdvanceEnabled: boolean entries: VaultEntry[] onSelectNote: (entry: VaultEntry) => void | Promise onToggleOrganized: (path: string) => Promise requestedActiveTabPathRef: RefObject selection: SidebarSelection visibleNotesRef: RefObject } function nextVisibleEntryAfter(entries: VaultEntry[], currentPath: string): VaultEntry | null { const currentIndex = entries.findIndex((entry) => entry.path === currentPath) if (currentIndex < 0) return null const nextEntry = entries[currentIndex + 1] return nextEntry ?? null } function shouldAdvanceAfterOrganize( entry: VaultEntry, path: string, options: Pick, ): boolean { return options.autoAdvanceEnabled && !entry.organized && options.activeTabPath === path && options.selection.kind === 'filter' && options.selection.filter === 'inbox' } function isStillFocusedOnPath( path: string, activeTabPathRef: RefObject, requestedActiveTabPathRef: RefObject, ): boolean { return activeTabPathRef.current === path && requestedActiveTabPathRef.current === path } export function useInboxOrganizeAdvance(options: UseInboxOrganizeAdvanceOptions): (path: string) => Promise { const { activeTabPath, activeTabPathRef, autoAdvanceEnabled, entries, onSelectNote, onToggleOrganized, requestedActiveTabPathRef, selection, visibleNotesRef, } = options return useCallback(async (path: string) => { const entry = entries.find((candidate) => candidate.path === path) if (!entry) return const nextEntry = shouldAdvanceAfterOrganize(entry, path, { activeTabPath, autoAdvanceEnabled, selection, }) ? nextVisibleEntryAfter(visibleNotesRef.current, path) : null const organized = await onToggleOrganized(path) if (!organized || !nextEntry) return if (!isStillFocusedOnPath(path, activeTabPathRef, requestedActiveTabPathRef)) return void onSelectNote(nextEntry) }, [ activeTabPath, activeTabPathRef, autoAdvanceEnabled, entries, onSelectNote, onToggleOrganized, requestedActiveTabPathRef, selection, visibleNotesRef, ]) }