File size: 3,276 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
import { NavigatorScreens, useNavigatorButtons } from '@automattic/onboarding';
import { Navigator } from '@wordpress/components/build-types/navigator/types';
import { Element, useMemo } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { useTranslate } from 'i18n-calypso';
import { MutableRefObject } from 'react';
import type { Category } from '@automattic/design-picker/src/types';
import type { NavigatorScreenObject } from '@automattic/onboarding';

interface CategoryBadgeProps {
	category: Category;
	onClick?: ( category: Category ) => void;
}

const CategoryBadge: React.FC< CategoryBadgeProps > = ( { category, onClick } ) => {
	if ( ! onClick ) {
		return <div className="design-preview__sidebar-badge-category">{ category.name }</div>;
	}

	return (
		<button
			className="design-preview__sidebar-badge-category"
			onClick={ () => onClick( category ) }
		>
			{ category.name }
		</button>
	);
};

interface SidebarProps {
	title?: Element;
	author?: string;
	categories?: Category[];
	description?: string;
	shortDescription?: string;
	pricingBadge?: React.ReactNode;
	screens: NavigatorScreenObject[];
	actionButtons: React.ReactNode;
	navigatorRef: MutableRefObject< Navigator | null >;
	onClickCategory?: ( category: Category ) => void;
	onNavigatorPathChange?: ( path?: string ) => void;
}

const Sidebar: React.FC< SidebarProps > = ( {
	title,
	author,
	categories = [],
	pricingBadge,
	description,
	shortDescription,
	screens,
	actionButtons,
	navigatorRef,
	onClickCategory,
	onNavigatorPathChange,
} ) => {
	const translate = useTranslate();
	const navigatorButtons = useNavigatorButtons( screens );

	const decodedDescription = useMemo(
		() => ( description ? decodeEntities( description ) : undefined ),
		[ description ]
	);

	const decodedShortDescription = useMemo(
		() => ( shortDescription ? decodeEntities( shortDescription ) : undefined ),
		[ shortDescription ]
	);

	return (
		<div className="design-preview__sidebar">
			<NavigatorScreens
				navigatorRef={ navigatorRef }
				screens={ screens }
				onNavigatorPathChange={ onNavigatorPathChange }
			>
				<>
					<div className="design-preview__sidebar-header">
						<div className="design-preview__sidebar-title">
							<h1>{ title }</h1>
						</div>
						{ author && (
							<div className="design-preview__sidebar-author">
								{ translate( 'By %(author)s', { args: { author } } ) }
							</div>
						) }
						{ ( pricingBadge || categories.length > 0 ) && (
							<div className="design-preview__sidebar-badges">
								{ pricingBadge }
								{ categories.map( ( category ) => (
									<CategoryBadge
										key={ category.slug }
										category={ category }
										onClick={ onClickCategory }
									/>
								) ) }
							</div>
						) }
						{ ( decodedDescription || decodedShortDescription ) && (
							<div className="design-preview__sidebar-description">
								<p>{ screens.length !== 1 ? decodedDescription : decodedShortDescription }</p>
							</div>
						) }
					</div>
					{ navigatorButtons }
					{ actionButtons && (
						<div className="design-preview__sidebar-action-buttons">{ actionButtons }</div>
					) }
				</>
			</NavigatorScreens>
		</div>
	);
};

export default Sidebar;