File size: 4,790 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
import {
useQuery,
UseQueryResult,
UseQueryOptions,
QueryKey,
QueryFunction,
useQueries,
} from '@tanstack/react-query';
import {
extractSearchInformation,
normalizePluginsList,
normalizePluginData,
} from 'calypso/lib/plugins/utils';
import wpcom from 'calypso/lib/wp';
import { BASE_STALE_TIME } from 'calypso/state/constants';
import type { Plugin } from 'calypso/state/plugins/installed/types';
type Type = 'all' | 'featured' | 'launched';
const pluginsApiBase = '/marketplace/products';
const featuredPluginsApiBase = '/plugins/featured';
const pluginsApiNamespace = 'wpcom/v2';
const WPCOM_PLUGINS_CACHE_VERSION = 2;
const getCacheKey = ( key: string ): QueryKey => [
WPCOM_PLUGINS_CACHE_VERSION.toString(),
'wpcom-plugins',
key,
];
const fetchWPCOMPlugins = ( type: Type, searchTerm?: string, tag?: string ) => {
const [ search, author ] = extractSearchInformation( searchTerm );
return wpcom.req.get(
{
path: pluginsApiBase,
apiNamespace: pluginsApiNamespace,
},
{
type: type,
...( search && { q: search } ),
...( author && { author } ),
...( tag && ! search && { tag } ),
}
);
};
export const getWPCOMPluginsQueryParams = (
type: Type,
searchTerm?: string,
tag?: string
): { queryKey: QueryKey; queryFn: QueryFunction< Plugin[] > } => {
const queryKey = getCacheKey( type + searchTerm + tag + '-normalized' );
const queryFn = () =>
fetchWPCOMPlugins( type, searchTerm, tag ).then( ( data: { results: Plugin[] } ) =>
normalizePluginsList( data.results )
);
return { queryKey, queryFn };
};
/**
* Returns marketplace plugins list filtered by searchterm and type.
* @param {Type} type Optional The query type
* @param {string} searchTerm Optional The term to search for
* @param {string} tag Optional The tag to search for
* @param {{enabled: boolean, staleTime: number, refetchOnMount: boolean}} {} Optional options to pass to the underlying query engine
* @returns {{ data, error, isLoading: boolean ...}} Returns various parameters piped from `useQuery`
*/
export const useWPCOMPluginsList = (
type: Type,
searchTerm?: string,
tag?: string,
{
enabled = true,
staleTime = BASE_STALE_TIME,
refetchOnMount = true,
}: Omit< UseQueryOptions< Plugin[] >, 'queryKey' > = {}
): UseQueryResult< Plugin[] > => {
return useQuery( {
...getWPCOMPluginsQueryParams( type, searchTerm, tag ),
enabled: enabled,
staleTime: staleTime,
refetchOnMount: refetchOnMount,
} );
};
const fetchWPCOMPlugin = ( slug: string ) =>
wpcom.req.get( {
path: `${ pluginsApiBase }/${ slug }`,
apiNamespace: pluginsApiNamespace,
} );
export const getWPCOMPluginQueryParams = (
slug: string
): { queryKey: QueryKey; queryFn: QueryFunction } => {
const queryKey = getCacheKey( slug + '-normalized' );
const queryFn = () =>
fetchWPCOMPlugin( slug ).then( ( data: any ) =>
normalizePluginData( { detailsFetched: Date.now() }, data )
);
return { queryKey, queryFn };
};
/**
* Returns a marketplace plugin data
* @param {Type} slug The plugin slug to query
* @param {{enabled: boolean, staleTime: number, refetchOnMount: boolean}} {} Optional options to pass to the underlying query engine
* @returns {{ data, error, isLoading: boolean ...}} Returns various parameters piped from `useQuery`
*/
export const useWPCOMPlugin = (
slug: string,
{
enabled = true,
staleTime = BASE_STALE_TIME,
refetchOnMount = true,
}: Omit< UseQueryOptions, 'queryKey' > = {}
): UseQueryResult< any > => {
return useQuery( {
...getWPCOMPluginQueryParams( slug ),
enabled: enabled,
staleTime: staleTime,
refetchOnMount: refetchOnMount,
} );
};
export const useWPCOMPlugins = ( slugs: Array< string > ): Array< UseQueryResult< any > > => {
return useQueries( {
queries: slugs.map( ( slug ) => {
return getWPCOMPluginQueryParams( slug );
} ),
} );
};
export const getWPCOMFeaturedPluginsQueryParams = (): {
queryKey: QueryKey;
queryFn: QueryFunction;
} => {
const queryKey = [ 'plugins-featured-list-normalized' ];
const queryFn = () =>
wpcom.req
.get( {
path: featuredPluginsApiBase,
apiNamespace: pluginsApiNamespace,
} )
.then( normalizePluginsList );
return { queryKey, queryFn };
};
/**
* Returns the featured list of plugins from WPCOM
* @param {{enabled: boolean, staleTime: number, refetchOnMount: boolean}} {} Optional options to pass to the underlying query engine
* @returns {{ data, error, isLoading: boolean ...}} Returns various parameters piped from `useQuery`
*/
export const useWPCOMFeaturedPlugins = ( {
enabled = true,
staleTime = BASE_STALE_TIME,
refetchOnMount = true,
}: Omit< UseQueryOptions, 'queryKey' > = {} ): UseQueryResult => {
return useQuery( {
...getWPCOMFeaturedPluginsQueryParams(),
enabled,
staleTime,
refetchOnMount,
} );
};
|