File size: 2,090 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
import { useQuery, UseQueryResult, UseQueryOptions } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp'; // eslint-disable-line no-restricted-imports
import type { Design } from '../types';

// Ideally this data should come from the themes API, maybe by a tag that's applied to
// themes? e.g. `link-in-bio` or `no-fold`
const STATIC_PREVIEWS = [
	'bantry',
	'sigler',
	'miller',
	'pollard',
	'paxton',
	'jones',
	'baker',
	'kingsley',
];

export interface UseThemeDesignsQueryOptions {
	filter?: string;
	tier?: 'all' | 'free' | 'premium';
}

export function useThemeDesignsQuery(
	{ filter = 'auto-loading-homepage', tier = 'all' }: UseThemeDesignsQueryOptions = {},
	queryOptions: Omit< UseQueryOptions< unknown, Error, Design[] >, 'queryKey' > = {}
): UseQueryResult< Design[], Error > {
	return useQuery< any, Error, Design[] >( {
		queryKey: [ 'themes', filter, tier ],
		queryFn: () =>
			wpcom.req.get( '/themes', {
				search: '',
				number: 50,
				tier,
				filter,
				apiNamespace: 'wpcom/v2',
			} ),
		// Our theme offering doesn't change that often, we don't need to
		// re-fetch until the next page refresh.
		staleTime: Infinity,
		select: ( response ) => response.themes.map( apiThemeToDesign ),
		...queryOptions,
		meta: {
			// Asks Calypso not to persist the data
			persist: false,
			...queryOptions.meta,
		},
	} );
}

function apiThemeToDesign( { id, name, taxonomies, stylesheet, price, theme_tier }: any ): Design {
	// Designs use a "featured" term in the theme_picks taxonomy. For example: Blank Canvas
	const isFeaturedPicks = !! taxonomies?.theme_picks?.find(
		( { slug }: any ) => slug === 'featured'
	);

	return {
		slug: id,
		title: name,
		recipe: {
			stylesheet,
		},
		categories: taxonomies?.theme_subject ?? [],
		is_featured_picks: isFeaturedPicks,
		showFirst: isFeaturedPicks,
		...( STATIC_PREVIEWS.includes( id ) && { preview: 'static' } ),
		design_type: theme_tier?.slug === 'free' ? 'standard' : 'premium',
		design_tier: theme_tier?.slug,
		price,

		// Deprecated; used for /start flow
		stylesheet,
		theme: id,
	};
}