1 import { createContext, useContext, useState } from 'react';
3 import type { ESLink } from './types';
4 import useSearchLibrary from './useSearchLibrary';
6 function useSearchResultsProvider() {
7 const { encryptedSearch, esStatus } = useSearchLibrary();
8 const { dbExists } = esStatus;
10 const [query, setQuery] = useState<string>('');
11 const [isSearching, setIsSearching] = useState(false);
12 const [results, setResults] = useState<ESLink[]>([]);
14 const searchStarted = (query: string) => {
19 const runSearch = async (query: string) => {
21 await encryptedSearch((results: ESLink[]) => {
24 setIsSearching(false);
37 const SearchResultsContext = createContext<ReturnType<typeof useSearchResultsProvider> | null>(null);
39 export function SearchResultsProvider({ children }: { children: React.ReactNode }) {
40 const providerState = useSearchResultsProvider();
41 return <SearchResultsContext.Provider value={providerState}>{children}</SearchResultsContext.Provider>;
44 export default function useSearchResults() {
45 const state = useContext(SearchResultsContext);
47 throw new Error('Trying to use uninitialized SearchResultsProvider');