PluginProbe
Extendify / 2.2.0
Extendify v2.2.0
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / HelpCenter / hooks / useSearchArticles.js

useSearchArticles.js in Extendify 2.2.0, at src/HelpCenter/hooks/useSearchArticles.js

22 lines 623 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { KB_HOST } from '@constants';
2 import useSWRImmutable from 'swr/immutable';
3
4 export const fetcher = async (search) => {
5 if (search.length < 3) return null;
6 const urlParams = new URLSearchParams({
7 lang: window.extSharedData.wpLanguage || null,
8 search,
9 });
10 return await fetch(`${KB_HOST}/api/posts?${urlParams.toString()}`, {
11 method: 'POST',
12 }).then((res) => {
13 if (!res.ok) throw new Error(res.statusText);
14 return res.json();
15 });
16 };
17
18 export const useSearchArticles = (search) => {
19 const { data, error } = useSWRImmutable(search || null, fetcher);
20 return { data, error, loading: !data && !error };
21 };
22