File size: 2,865 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
import { SHOW_ALL_SLUG } from '@automattic/design-picker';
import { useTranslate } from 'i18n-calypso';
import { useEffect, useMemo, useState } from 'react';
import { gatherCategories } from './utils';
import type { Category, Design } from '@automattic/design-picker';

export interface Categorization {
	selection: string | null;
	onSelect: ( selectedSlug: string | null ) => void;
	categories: Category[];
}

interface UseCategorizationOptions {
	defaultSelection: string | null;
	showAllFilter?: boolean;
	sort?: ( a: Category, b: Category ) => number;
}

export function useCategorization(
	designs: Design[],
	{ defaultSelection, showAllFilter, sort }: UseCategorizationOptions
): Categorization {
	const translate = useTranslate();

	const categories = useMemo( () => {
		const result = gatherCategories( designs );
		if ( sort ) {
			result.sort( sort );
		}

		if ( showAllFilter && designs.length ) {
			result.unshift( {
				name: translate( 'Show All' ),
				slug: SHOW_ALL_SLUG,
			} );
		}

		return result;
	}, [ designs, showAllFilter, sort, translate ] );

	const [ selection, onSelect ] = useState< string | null >(
		chooseDefaultSelection( categories, defaultSelection )
	);

	useEffect( () => {
		if ( shouldSetToDefaultSelection( categories, selection ) ) {
			onSelect( chooseDefaultSelection( categories, defaultSelection ) );
		}
	}, [ categories, defaultSelection, selection ] );

	return {
		categories,
		selection,
		onSelect,
	};
}

/**
 *	Check that the current selection still matches one of the category slugs,
 *	and if it doesn't reset the current selection to the default selection.
 *	@param categories the list of available categories
 *	@param currentSelection the slug of the current selected category
 *	@returns whether the current selection should be set to the default selection
 */
function shouldSetToDefaultSelection(
	categories: Category[],
	currentSelection: string | null
): boolean {
	// For an empty list, `null` selection is the only correct one.
	if ( categories.length === 0 && currentSelection === null ) {
		return false;
	}
	return ! categories.some( ( { slug } ) => slug === currentSelection );
}

/**
 * Chooses which category is the one that should be used by default.
 * If `defaultSelection` is a valid category slug then it'll be used, otherwise it'll be whichever
 * category appears first in the list.
 * @param categories the categories from which the default will be selected
 * @param defaultSelection use this category as the default selection if possible
 * @returns the default category or null if none is available
 */
function chooseDefaultSelection(
	categories: Category[],
	defaultSelection: string | null
): string | null {
	if ( defaultSelection && categories.find( ( { slug } ) => slug === defaultSelection ) ) {
		return defaultSelection;
	}

	return categories[ 0 ]?.slug ?? null;
}