import { useEffect, useState } from "preact/hooks"; import { t } from "../services/i18n"; import Alert from "./react/Alert"; import { useNoteContext, useTriliumEvent } from "./react/hooks"; import "./search_result.css"; import { SearchNoteList } from "./collections/NoteList"; enum SearchResultState { NO_RESULTS, NOT_EXECUTED, GOT_RESULTS } export default function SearchResult() { const { note, notePath, ntxId } = useNoteContext(); const [ state, setState ] = useState(); const [ highlightedTokens, setHighlightedTokens ] = useState(); function refresh() { if (note?.type !== "search") { setState(undefined); } else if (!note?.searchResultsLoaded) { setState(SearchResultState.NOT_EXECUTED); } else if (note.getChildNoteIds().length === 0) { setState(SearchResultState.NO_RESULTS); } else { setState(SearchResultState.GOT_RESULTS); setHighlightedTokens(note.highlightedTokens); } } useEffect(() => refresh(), [ note ]); useTriliumEvent("searchRefreshed", ({ ntxId: eventNtxId }) => { if (eventNtxId === ntxId) { refresh(); } }); useTriliumEvent("notesReloaded", ({ noteIds }) => { if (note?.noteId && noteIds.includes(note.noteId)) { refresh(); } }); return (
{state === SearchResultState.NOT_EXECUTED && ( {t("search_result.search_not_executed")} )} {state === SearchResultState.NO_RESULTS && ( {t("search_result.no_notes_found")} )} {state === SearchResultState.GOT_RESULTS && ( )}
); }