|
| 1 | +import { useJson } from "./useJson"; |
| 2 | +import Fuse from "fuse.js"; |
| 3 | +import { JsonSearchEntry } from "~/utilities/search"; |
| 4 | +import { |
| 5 | + createContext, |
| 6 | + useCallback, |
| 7 | + useContext, |
| 8 | + useEffect, |
| 9 | + useReducer, |
| 10 | + useRef, |
| 11 | +} from "react"; |
| 12 | + |
| 13 | +export type InitializeIndexEvent = { |
| 14 | + type: "initialize-index"; |
| 15 | + payload: { json: unknown; fuseOptions: Fuse.IFuseOptions<JsonSearchEntry> }; |
| 16 | +}; |
| 17 | + |
| 18 | +export type SearchEvent = { |
| 19 | + type: "search"; |
| 20 | + payload: { query: string }; |
| 21 | +}; |
| 22 | + |
| 23 | +export type SearchSendWorkerEvent = InitializeIndexEvent | SearchEvent; |
| 24 | + |
| 25 | +export type IndexInitializedEvent = { |
| 26 | + type: "index-initialized"; |
| 27 | +}; |
| 28 | + |
| 29 | +export type SearchResultsEvent = { |
| 30 | + type: "search-results"; |
| 31 | + payload: { results: Fuse.FuseResult<JsonSearchEntry>[] }; |
| 32 | +}; |
| 33 | + |
| 34 | +export type SearchReceiveWorkerEvent = |
| 35 | + | IndexInitializedEvent |
| 36 | + | SearchResultsEvent; |
| 37 | + |
| 38 | +export type JsonSearchApi = { |
| 39 | + search: (query: string) => void; |
| 40 | +}; |
| 41 | + |
| 42 | +const JsonSearchStateContext = createContext<JsonSearchState>( |
| 43 | + {} as JsonSearchState |
| 44 | +); |
| 45 | + |
| 46 | +const JsonSearchApiContext = createContext<JsonSearchApi>({} as JsonSearchApi); |
| 47 | + |
| 48 | +export type JsonSearchState = { |
| 49 | + status: "initializing" | "idle" | "searching"; |
| 50 | + query?: string; |
| 51 | + results?: Fuse.FuseResult<JsonSearchEntry>[]; |
| 52 | +}; |
| 53 | + |
| 54 | +type SearchAction = { |
| 55 | + type: "search"; |
| 56 | + payload: { query: string }; |
| 57 | +}; |
| 58 | + |
| 59 | +type JsonSearchAction = SearchReceiveWorkerEvent | SearchAction; |
| 60 | + |
| 61 | +function reducer( |
| 62 | + state: JsonSearchState, |
| 63 | + action: JsonSearchAction |
| 64 | +): JsonSearchState { |
| 65 | + switch (action.type) { |
| 66 | + case "index-initialized": |
| 67 | + return { |
| 68 | + ...state, |
| 69 | + status: "idle", |
| 70 | + results: undefined, |
| 71 | + }; |
| 72 | + case "search-results": |
| 73 | + return { |
| 74 | + ...state, |
| 75 | + status: "idle", |
| 76 | + results: action.payload.results, |
| 77 | + }; |
| 78 | + case "search": |
| 79 | + return { |
| 80 | + ...state, |
| 81 | + status: "searching", |
| 82 | + query: action.payload.query, |
| 83 | + }; |
| 84 | + default: |
| 85 | + return state; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export function JsonSearchProvider({ |
| 90 | + children, |
| 91 | +}: { |
| 92 | + children: React.ReactNode; |
| 93 | +}) { |
| 94 | + const [json] = useJson(); |
| 95 | + |
| 96 | + const [state, dispatch] = useReducer< |
| 97 | + React.Reducer<JsonSearchState, JsonSearchAction> |
| 98 | + >(reducer, { status: "initializing" }); |
| 99 | + |
| 100 | + const search = useCallback( |
| 101 | + (query: string) => { |
| 102 | + dispatch({ type: "search", payload: { query } }); |
| 103 | + }, |
| 104 | + [dispatch] |
| 105 | + ); |
| 106 | + |
| 107 | + const handleWorkerMessage = useCallback( |
| 108 | + (e: MessageEvent<SearchReceiveWorkerEvent>) => dispatch(e.data), |
| 109 | + [dispatch] |
| 110 | + ); |
| 111 | + |
| 112 | + const workerRef = useRef<Worker | null>(); |
| 113 | + |
| 114 | + useEffect(() => { |
| 115 | + if (typeof window === "undefined" || typeof window.Worker === "undefined") { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if (workerRef.current) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const worker = new Worker("/entry.worker.js"); |
| 124 | + worker.onmessage = handleWorkerMessage; |
| 125 | + |
| 126 | + workerRef.current = worker; |
| 127 | + |
| 128 | + workerRef.current.postMessage({ |
| 129 | + type: "initialize-index", |
| 130 | + payload: { |
| 131 | + json, |
| 132 | + fuseOptions: { |
| 133 | + includeScore: true, |
| 134 | + includeMatches: true, |
| 135 | + minMatchCharLength: 1, |
| 136 | + isCaseSensitive: false, |
| 137 | + threshold: 0.6, |
| 138 | + distance: 200, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }); |
| 142 | + }, [json, workerRef.current]); |
| 143 | + |
| 144 | + useEffect(() => { |
| 145 | + if (state.status !== "searching") { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + workerRef.current?.postMessage({ |
| 150 | + type: "search", |
| 151 | + payload: { query: state.query }, |
| 152 | + }); |
| 153 | + }, [state.status, workerRef.current]); |
| 154 | + |
| 155 | + return ( |
| 156 | + <JsonSearchStateContext.Provider value={state}> |
| 157 | + <JsonSearchApiContext.Provider value={{ search }}> |
| 158 | + {children} |
| 159 | + </JsonSearchApiContext.Provider> |
| 160 | + </JsonSearchStateContext.Provider> |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +export function useJsonSearchState(): JsonSearchState { |
| 165 | + return useContext(JsonSearchStateContext); |
| 166 | +} |
| 167 | + |
| 168 | +export function useJsonSearchApi(): JsonSearchApi { |
| 169 | + return useContext(JsonSearchApiContext); |
| 170 | +} |
0 commit comments