-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
tabs.tsx
179 lines (162 loc) · 4.18 KB
/
tabs.tsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useContext, useEffect, useState } from '@wordpress/element';
import { Button } from '@wordpress/components';
import classNames from 'classnames';
import { getNewPath, navigateTo, useQuery } from '@woocommerce/navigation';
/**
* Internal dependencies
*/
import './tabs.scss';
import { DEFAULT_TAB_KEY, MARKETPLACE_PATH } from '../constants';
import { MarketplaceContext } from '../../contexts/marketplace-context';
import { MarketplaceContextType } from '../../contexts/types';
import { getAdminSetting } from '../../../utils/admin-settings';
export interface TabsProps {
additionalClassNames?: Array< string > | undefined;
}
interface Tab {
name: string;
title: string;
href?: string;
showUpdateCount: boolean;
updateCount: number;
}
interface Tabs {
[ key: string ]: Tab;
}
const wccomSettings = getAdminSetting( 'wccomHelper', {} );
const wooUpdateCount = wccomSettings?.wooUpdateCount ?? 0;
const tabs: Tabs = {
search: {
name: 'search',
title: __( 'Search results', 'woocommerce' ),
showUpdateCount: false,
updateCount: 0,
},
discover: {
name: 'discover',
title: __( 'Discover', 'woocommerce' ),
showUpdateCount: false,
updateCount: 0,
},
extensions: {
name: 'extensions',
title: __( 'Browse', 'woocommerce' ),
showUpdateCount: false,
updateCount: 0,
},
themes: {
name: 'themes',
title: __( 'Themes', 'woocommerce' ),
showUpdateCount: false,
updateCount: 0,
},
'my-subscriptions': {
name: 'my-subscriptions',
title: __( 'My subscriptions', 'woocommerce' ),
showUpdateCount: true,
updateCount: wooUpdateCount,
},
};
const setUrlTabParam = ( tabKey: string ) => {
navigateTo( {
url: getNewPath(
{ tab: tabKey === DEFAULT_TAB_KEY ? undefined : tabKey },
MARKETPLACE_PATH,
{}
),
} );
};
const getVisibleTabs = ( selectedTab: string ) => {
if ( selectedTab === '' ) {
return tabs;
}
const currentVisibleTabs = { ...tabs };
if ( selectedTab !== 'search' ) {
delete currentVisibleTabs.search;
}
return currentVisibleTabs;
};
const renderTabs = (
marketplaceContextValue: MarketplaceContextType,
visibleTabs: Tabs
) => {
const { selectedTab, setSelectedTab } = marketplaceContextValue;
const onTabClick = ( tabKey: string ) => {
if ( tabKey === selectedTab ) {
return;
}
setSelectedTab( tabKey );
setUrlTabParam( tabKey );
};
const tabContent = [];
for ( const tabKey in visibleTabs ) {
tabContent.push(
tabs[ tabKey ]?.href ? (
<a
className={ classNames(
'woocommerce-marketplace__tab-button',
'components-button',
`woocommerce-marketplace__tab-${ tabKey }`
) }
href={ tabs[ tabKey ]?.href }
key={ tabKey }
>
{ tabs[ tabKey ]?.title }
</a>
) : (
<Button
className={ classNames(
'woocommerce-marketplace__tab-button',
`woocommerce-marketplace__tab-${ tabKey }`,
{
'is-active': tabKey === selectedTab,
}
) }
onClick={ () => onTabClick( tabKey ) }
key={ tabKey }
>
{ tabs[ tabKey ]?.title }
{ tabs[ tabKey ]?.showUpdateCount &&
tabs[ tabKey ]?.updateCount > 0 && (
<span className="woocommerce-marketplace__update-count">
<span> { tabs[ tabKey ]?.updateCount } </span>
</span>
) }
</Button>
)
);
}
return tabContent;
};
const Tabs = ( props: TabsProps ): JSX.Element => {
const { additionalClassNames } = props;
const marketplaceContextValue = useContext( MarketplaceContext );
const { selectedTab, setSelectedTab } = marketplaceContextValue;
const [ visibleTabs, setVisibleTabs ] = useState( getVisibleTabs( '' ) );
const query: Record< string, string > = useQuery();
useEffect( () => {
if ( query?.tab && tabs[ query.tab ] ) {
setSelectedTab( query.tab );
} else if ( Object.keys( query ).length > 0 ) {
setSelectedTab( DEFAULT_TAB_KEY );
}
}, [ query, setSelectedTab ] );
useEffect( () => {
setVisibleTabs( getVisibleTabs( selectedTab ) );
}, [ selectedTab ] );
return (
<nav
className={ classNames(
'woocommerce-marketplace__tabs',
additionalClassNames || []
) }
>
{ renderTabs( marketplaceContextValue, visibleTabs ) }
</nav>
);
};
export default Tabs;