MCPcopy Create free account
hub / github.com/triggerdotdev/jsonhero-web / JsonSearchProvider

Function JsonSearchProvider

app/hooks/useJsonSearch.tsx:173–242  ·  view source on GitHub ↗
({
  children,
}: {
  children: React.ReactNode;
})

Source from the content-addressed store, hash-verified

171};
172
173export function JsonSearchProvider({
174 children,
175}: {
176 children: React.ReactNode;
177}) {
178 const [json] = useJson();
179
180 const [state, dispatch] = useReducer<
181 React.Reducer<JsonSearchState, JsonSearchAction>
182 >(wrapReducer("jsonSearch", reducer), { status: "initializing" });
183
184 const search = useCallback(
185 (query: string) => {
186 dispatch({ type: "search", payload: { query } });
187 },
188 [dispatch]
189 );
190
191 const reset = useCallback(() => {
192 dispatch({ type: "reset" });
193 }, [dispatch]);
194
195 const handleWorkerMessage = useCallback(
196 (e: MessageEvent<SearchReceiveWorkerEvent>) => dispatch(e.data),
197 [dispatch]
198 );
199
200 const workerRef = useRef<Worker | null>();
201
202 useEffect(() => {
203 if (typeof window === "undefined" || typeof window.Worker === "undefined") {
204 return;
205 }
206
207 if (workerRef.current) {
208 return;
209 }
210
211 const worker = new Worker("/entry.worker.js");
212 worker.onmessage = handleWorkerMessage;
213
214 workerRef.current = worker;
215
216 workerRef.current.postMessage({
217 type: "initialize-index",
218 payload: {
219 json,
220 },
221 });
222 }, [json, workerRef.current]);
223
224 useEffect(() => {
225 if (state.status !== "searching") {
226 return;
227 }
228
229 workerRef.current?.postMessage({
230 type: "search",

Callers

nothing calls this directly

Calls 2

useJsonFunction · 0.90
wrapReducerFunction · 0.85

Tested by

no test coverage detected