import { closeActiveDialog, openDialog } from "../../services/dialog"; import ReactBasicWidget from "../react/ReactBasicWidget"; import Modal from "../react/Modal"; import Button from "../react/Button"; import NoteAutocomplete from "../react/NoteAutocomplete"; import { t } from "../../services/i18n"; import { useEffect, useRef, useState } from "preact/hooks"; import note_autocomplete, { Suggestion } from "../../services/note_autocomplete"; import appContext from "../../components/app_context"; import commandRegistry from "../../services/command_registry"; import { refToJQuerySelector } from "../react/react_utils"; const KEEP_LAST_SEARCH_FOR_X_SECONDS = 120; type Mode = "last-search" | "recent-notes" | "commands"; interface JumpToNoteDialogProps { mode: Mode; } function JumpToNoteDialogComponent({ mode }: JumpToNoteDialogProps) { const containerRef = useRef(null); const autocompleteRef = useRef(null); const [ isCommandMode, setIsCommandMode ] = useState(mode === "commands"); const [ text, setText ] = useState(isCommandMode ? "> " : ""); useEffect(() => { setIsCommandMode(text.startsWith(">")); }, [ text ]); async function onItemSelected(suggestion: Suggestion) { if (suggestion.notePath) { appContext.tabManager.getActiveContext()?.setNote(suggestion.notePath); } else if (suggestion.commandId) { closeActiveDialog(); await commandRegistry.executeCommand(suggestion.commandId); } } function onShown() { const $autoComplete = refToJQuerySelector(autocompleteRef); switch (mode) { case "last-search": // Fall-through if there is no text, in order to display the recent notes. if (text) { break; } case "recent-notes": note_autocomplete.showRecentNotes($autoComplete); break; case "commands": note_autocomplete.showAllCommands($autoComplete); break; } $autoComplete .trigger("focus") .trigger("select"); } return ( } onShown={onShown} footer={!isCommandMode &&