woocommerce/plugins/woocommerce-admin/client/marketplace/components/tabs/tabs.tsx

157 lines
3.5 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
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';
[wccom-17942] Only showing feedback snackbar when content of in-app marketplace has finished loading. Making sure snackbar is fixed position, so it's visible wherever you are on the page. - `ProductListContextProvider` provides `setIsLoading` function as well as `isLoading`. - `Discover` uses these values from context, instead of keeping a loading state in itself. - `FeedbackModal` calls `maybSetTimeout` when `isLoading` changes. If `isLoading` isn't truthy, and snackbar hasn't already rendered, it sets a timeout of 5 seconds to show it. - Removed wrapping <WooFooterItem> from around Footer component, so it's no longer a child of the WooCommerce Admin `.woocommerce-layout__footer` footer. - Removed the `position: relative` from `.woocommerce-layout__footer`. It needs to be `position: fixed`. - Added FooterContent component to Footer, to allow the layout we want. - Changed use of context. This now only has states for the selected tab and loading state. - We use this context in `Tabs` and `Content` to keep track of which tab is selected, and set the selected tab. - We also use it in `Discover` and `Extensions`, which both report loading state to the context. This allows us to use it to only render the snackbar when loading is complete. - Extensions: moved `productList` and `setProductList` and logic for getting product list from the context provider to a state in this component. We don't need to share the list of products in the context. - Renamed `ProductListContext`, `ProductListContextProvider` and `productListContextValue` to more generic `MarketplaceContext`, `MarketplaceContextProvider` and `marketplaceContextValue`. - Renamed a constant and created constants for API paths. - Only shows snackbar after content has loaded, and after a timeout. We set a date `marketplace_redesign_2023_last_shown_date` in local storage to ensure we only show one snackbar.
2023-08-23 15:49:36 +00:00
import { MarketplaceContext } from '../../contexts/marketplace-context';
import { MarketplaceContextType } from '../../contexts/types';
export interface TabsProps {
additionalClassNames?: Array< string > | undefined;
}
interface Tab {
name: string;
title: string;
href?: string;
}
interface Tabs {
[ key: string ]: Tab;
}
const tabs: Tabs = {
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
search: {
name: 'search',
title: __( 'Search results', 'woocommerce' ),
},
discover: {
name: 'discover',
title: __( 'Discover', 'woocommerce' ),
},
extensions: {
name: 'extensions',
title: __( 'Browse', 'woocommerce' ),
},
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
themes: {
name: 'themes',
title: __( 'Themes', 'woocommerce' ),
},
'my-subscriptions': {
name: 'my-subscriptions',
title: __( 'My subscriptions', 'woocommerce' ),
},
};
const setUrlTabParam = ( tabKey: string ) => {
navigateTo( {
2023-10-04 17:37:41 +00:00
url: getNewPath(
{ tab: tabKey === DEFAULT_TAB_KEY ? undefined : tabKey },
MARKETPLACE_PATH,
{}
),
} );
};
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
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 = [];
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
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 }
</Button>
)
);
}
return tabContent;
};
const Tabs = ( props: TabsProps ): JSX.Element => {
[wccom-17942] Only showing feedback snackbar when content of in-app marketplace has finished loading. Making sure snackbar is fixed position, so it's visible wherever you are on the page. - `ProductListContextProvider` provides `setIsLoading` function as well as `isLoading`. - `Discover` uses these values from context, instead of keeping a loading state in itself. - `FeedbackModal` calls `maybSetTimeout` when `isLoading` changes. If `isLoading` isn't truthy, and snackbar hasn't already rendered, it sets a timeout of 5 seconds to show it. - Removed wrapping <WooFooterItem> from around Footer component, so it's no longer a child of the WooCommerce Admin `.woocommerce-layout__footer` footer. - Removed the `position: relative` from `.woocommerce-layout__footer`. It needs to be `position: fixed`. - Added FooterContent component to Footer, to allow the layout we want. - Changed use of context. This now only has states for the selected tab and loading state. - We use this context in `Tabs` and `Content` to keep track of which tab is selected, and set the selected tab. - We also use it in `Discover` and `Extensions`, which both report loading state to the context. This allows us to use it to only render the snackbar when loading is complete. - Extensions: moved `productList` and `setProductList` and logic for getting product list from the context provider to a state in this component. We don't need to share the list of products in the context. - Renamed `ProductListContext`, `ProductListContextProvider` and `productListContextValue` to more generic `MarketplaceContext`, `MarketplaceContextProvider` and `marketplaceContextValue`. - Renamed a constant and created constants for API paths. - Only shows snackbar after content has loaded, and after a timeout. We set a date `marketplace_redesign_2023_last_shown_date` in local storage to ensure we only show one snackbar.
2023-08-23 15:49:36 +00:00
const { additionalClassNames } = props;
const marketplaceContextValue = useContext( MarketplaceContext );
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
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 );
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
} else if ( Object.keys( query ).length > 0 ) {
setSelectedTab( DEFAULT_TAB_KEY );
}
}, [ query, setSelectedTab ] );
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
useEffect( () => {
setVisibleTabs( getVisibleTabs( selectedTab ) );
}, [ selectedTab ] );
return (
<nav
className={ classNames(
'woocommerce-marketplace__tabs',
additionalClassNames || []
) }
>
Marketplace Themes: Feature Branch (#40159) * Support for themes in in-app marketplace. Contains the changes from: https://github.com/woocommerce/woocommerce/pull/40247 https://github.com/woocommerce/woocommerce/pull/40272 https://github.com/woocommerce/woocommerce/pull/40302 https://github.com/woocommerce/woocommerce/pull/40303 https://github.com/woocommerce/woocommerce/pull/40333 https://github.com/woocommerce/woocommerce/pull/40368 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40375 https://github.com/woocommerce/woocommerce/pull/40389 * `.woocommerce-marketplace__discover`: changed `align-items` `flex-start` to `stretch` to properly display products on large and very large viewports. * Delete plugins/woocommerce/changelog/add-18026-marketplace-theme-cards Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-18027-themes-to-in-app-search Removing from feature branch before final review * Delete plugins/woocommerce/changelog/add-marketplace-theme-discover-section Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-in-app-multiple-category-filters Removing from feature branch before final review * Delete plugins/woocommerce/changelog/update-theme-no-result-style Removing from feature branch before final review * Add changefile(s) from automation for the following project(s): woocommerce --------- Co-authored-by: And Finally <andfinally@users.noreply.github.com> Co-authored-by: Dan Q <dan@danq.me> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Dan Q <danq@automattic.com>
2023-10-04 16:59:34 +00:00
{ renderTabs( marketplaceContextValue, visibleTabs ) }
</nav>
);
};
export default Tabs;