File size: 3,070 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
import config from '@automattic/calypso-config';
import { useTranslate } from 'i18n-calypso';
import { Plugin } from 'calypso/data/marketplace/types';
import { useESPluginsInfinite } from 'calypso/data/marketplace/use-es-query';
import {
	useWPCOMFeaturedPlugins,
	useWPCOMPluginsList,
} from 'calypso/data/marketplace/use-wpcom-plugins-query';
import { useCategories } from '../categories/use-categories';

interface ESResponse {
	data?: {
		plugins: Plugin[];
		pagination: {
			results: number;
			page: number;
			pages: number;
		};
	};
	isLoading: boolean;
	fetchNextPage?: () => void;
	hasNextPage?: boolean;
}

interface WPCOMResponse {
	data?: Plugin[];
	isLoading: boolean;
	fetchNextPage?: () => void;
}

const usePlugins = ( {
	category,
	search,
	infinite = false,
	locale = '',
	slugs,
}: {
	category: string;
	search?: string;
	infinite?: boolean;
	locale?: string;
	slugs?: string[];
} ) => {
	let plugins = [];
	let isFetching = false;
	let results = 0;

	const categories = useCategories();
	const categoryTags = categories[ category || '' ]?.tags || [ category ];
	const tag = categoryTags.join( ',' );

	const translate = useTranslate();
	const wporgPluginsOptions = {
		locale: locale || ( translate.localeSlug as string ),
		category,
		tag,
		searchTerm: search,
		slugs,
	};

	// This is triggered for searches OR any other category than paid, featured
	const {
		data: { plugins: ESPlugins = [], pagination: ESPagination } = {},
		isLoading: isFetchingES,
		fetchNextPage,
		hasNextPage,
	} = useESPluginsInfinite( wporgPluginsOptions, {
		enabled: !! search || ! [ 'paid', 'featured ' ].includes( category ),
	} ) as ESResponse;

	// This is triggered only for paid plugins lists.
	const { data: dotComPlugins = [], isLoading: isFetchingDotCom } = useWPCOMPluginsList(
		config.isEnabled( 'marketplace-fetch-all-dynamic-products' ) ? 'all' : 'launched',
		search,
		tag,
		{
			enabled: category === 'paid',
		}
	) as WPCOMResponse;

	// This is triggered only for featured plugins list in discover page.
	const { data: featuredPlugins = [], isLoading: isFetchingDotComFeatured } =
		useWPCOMFeaturedPlugins( {
			enabled: category === 'featured',
		} ) as WPCOMResponse;

	switch ( category ) {
		case 'paid':
			plugins = dotComPlugins;
			isFetching = isFetchingDotCom;
			results = dotComPlugins?.length ?? 0;
			break;
		case 'popular':
			plugins = ESPlugins;
			isFetching = isFetchingES;
			results = ESPagination?.results ?? 0;
			break;
		case 'featured':
			plugins = featuredPlugins;
			isFetching = isFetchingDotComFeatured;
			results = featuredPlugins?.length ?? 0;
			break;
		default:
			plugins = ESPlugins;
			isFetching = isFetchingES;
			results = ESPagination?.results ?? 0;

			break;
	}

	function fetchNextPageAndStop() {
		if ( ! infinite || ! hasNextPage ) {
			return;
		}

		fetchNextPage && fetchNextPage();
	}

	return {
		plugins,
		isFetching,
		fetchNextPage: fetchNextPageAndStop,
		pagination: {
			page: ESPagination?.page,
			pages: ESPagination?.pages,
			results,
		},
	};
};

export default usePlugins;