PluginProbe
Extendify / 2.2.2
Extendify v2.2.2
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 / Library / hooks / usePatterns.js

usePatterns.js in Extendify 2.2.2, at src/Library/hooks/usePatterns.js

50 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { PATTERNS_HOST } from '@constants';
2 import useSWRInfinite from 'swr/infinite';
3
4 const fetcher = (url) => fetch(url).then((res) => res.json());
5
6 export const usePatterns = (incomingParams) => {
7 const params = {
8 siteType: undefined,
9 category: undefined,
10 wpVersion: window.extSharedData.wpVersion,
11 lang: window.extSharedData.wpLanguage || null,
12 showLocalizedCopy: window.extSharedData.showLocalizedCopy || null,
13 ...incomingParams,
14 };
15
16 const getKey = (pageIndex, previousPageData) => {
17 if (!params.category) return null;
18 if (previousPageData && !previousPageData.length) return null;
19
20 const urlParams = new URLSearchParams({ page: pageIndex + 1 });
21
22 Object.entries(params)
23 .filter(([, value]) => value !== undefined)
24 .forEach(([key, value]) => {
25 urlParams.append(key, value);
26 });
27
28 return `${PATTERNS_HOST}/api/patterns?${urlParams.toString()}`;
29 };
30
31 const { data, error, isLoading, isValidating, mutate, size, setSize } =
32 useSWRInfinite(getKey, fetcher, {
33 initialSize: 2,
34 revalidateFirstPage: false,
35 revalidateIfStale: false,
36 revalidateOnFocus: false,
37 revalidateOnReconnect: false,
38 });
39
40 return {
41 data,
42 error,
43 isLoading,
44 isValidating,
45 mutate,
46 size,
47 setSize,
48 };
49 };
50