File size: 3,002 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
import { useQuery, UseQueryResult, QueryOptions } from '@tanstack/react-query';
import { stringify } from 'qs';
import wpcomRequest from 'wpcom-proxy-request';
import type { StarterDesigns } from './types';
import type {
	Category,
	Design,
	DesignRecipe,
	SoftwareSet,
	StyleVariation,
	PreviewData,
	DesignType,
} from '@automattic/design-picker/src/types';

interface StarterDesignsQueryParams {
	seed?: string;
	goals?: string[];
	_locale: string;
}

interface Options extends QueryOptions< StarterDesignsResponse > {
	enabled?: boolean;
	select?: ( response: StarterDesigns ) => StarterDesigns;
}

interface StarterDesignsResponse {
	filters: { subject: Record< string, Category > };
	static: { designs: StarterDesign[] };
}

export type ThemeTier = {
	slug: string;
	feature: string;
	platform: string;
};

interface StarterDesign {
	slug: string;
	title: string;
	description: string;
	recipe: DesignRecipe;
	categories: Category[];
	price?: string;
	style_variations?: StyleVariation[];
	software_sets?: SoftwareSet[];
	is_virtual: boolean;
	preview_data: PreviewData | null;
	design_type?: DesignType;
	theme_type?: string;
	screenshot?: string;
	theme_tier: ThemeTier;
	demo_uri?: string;
}

export function useStarterDesignsQuery(
	queryParams: StarterDesignsQueryParams,
	{ select, ...queryOptions }: Options = {}
): UseQueryResult< StarterDesigns > {
	return useQuery( {
		queryKey: [ 'starter-designs', queryParams ],
		queryFn: () => fetchStarterDesigns( queryParams ),
		select: ( response: StarterDesignsResponse ) => {
			const allDesigns = {
				filters: {
					subject: response.filters?.subject || {},
				},
				designs: response.static?.designs?.map( apiStarterDesignsToDesign ),
			};

			return select ? select( allDesigns ) : allDesigns;
		},
		refetchOnMount: 'always',
		staleTime: Infinity,
		...queryOptions,
	} );
}

function fetchStarterDesigns(
	queryParams: StarterDesignsQueryParams
): Promise< StarterDesignsResponse > {
	return wpcomRequest< StarterDesignsResponse >( {
		apiNamespace: 'wpcom/v2',
		path: '/starter-designs',
		query: stringify( queryParams ),
	} );
}

function apiStarterDesignsToDesign( design: StarterDesign ): Design {
	const {
		slug,
		title,
		description,
		recipe,
		categories,
		price,
		style_variations,
		software_sets,
		preview_data,
		design_type,
		screenshot,
		theme_tier,
		demo_uri,
	} = design;

	const is_externally_managed = design.theme_type === 'managed-external';
	const is_bundled_with_woo = ( design.software_sets || [] ).some(
		( { slug } ) => slug === 'woo-on-plans'
	);

	return {
		slug,
		title,
		description,
		recipe,
		categories,
		is_externally_managed,
		is_bundled_with_woo,
		price,
		software_sets,
		design_type: design_type ?? ( theme_tier?.slug === 'free' ? 'standard' : 'premium' ),
		style_variations,
		is_virtual: design.is_virtual && !! design.recipe?.pattern_ids?.length,
		...( preview_data && { preview_data } ),
		theme: '',
		screenshot,
		design_tier: theme_tier?.slug,
		demo_uri,
	};
}