2018-06-28 13:52:45 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-06-29 15:20:08 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-02-19 13:57:17 +00:00
|
|
|
import { lazy, useState } from '@wordpress/element';
|
2020-08-05 22:02:24 +00:00
|
|
|
import { Button } from '@wordpress/components';
|
2021-02-19 13:57:17 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2020-08-05 22:02:24 +00:00
|
|
|
import { uniqueId, find } from 'lodash';
|
2020-04-29 18:01:27 +00:00
|
|
|
import CrossIcon from 'gridicons/dist/cross-small';
|
2020-08-05 22:02:24 +00:00
|
|
|
import classnames from 'classnames';
|
2021-02-17 22:54:02 +00:00
|
|
|
import { Icon, help as helpIcon, inbox as inboxIcon } from '@wordpress/icons';
|
2020-12-02 13:30:39 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/wc-admin-settings';
|
2021-02-16 20:01:11 +00:00
|
|
|
import { H, Section } from '@woocommerce/components';
|
2021-02-19 13:57:17 +00:00
|
|
|
import { OPTIONS_STORE_NAME, useUser } from '@woocommerce/data';
|
2020-08-05 22:02:24 +00:00
|
|
|
import { getHistory, getNewPath } from '@woocommerce/navigation';
|
2020-12-18 13:17:07 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2020-07-14 12:20:51 +00:00
|
|
|
|
2018-06-28 13:52:45 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-06-29 15:20:08 +00:00
|
|
|
import './style.scss';
|
2018-07-09 15:46:31 +00:00
|
|
|
import ActivityPanelToggleBubble from './toggle-bubble';
|
2020-12-02 13:30:39 +00:00
|
|
|
import { getUnreadNotes } from './unread-indicators';
|
2020-08-24 21:51:41 +00:00
|
|
|
import { isWCAdmin } from '../../dashboard/utils';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { Tabs } from './tabs';
|
|
|
|
import { SetupProgress } from './setup-progress';
|
2020-11-16 13:48:50 +00:00
|
|
|
import { DisplayOptions } from './display-options';
|
2020-12-18 13:17:07 +00:00
|
|
|
import { HighlightTooltip } from './highlight-tooltip';
|
2021-02-16 20:01:11 +00:00
|
|
|
import { Panel } from './panel';
|
2020-07-14 12:20:51 +00:00
|
|
|
|
|
|
|
const HelpPanel = lazy( () =>
|
|
|
|
import( /* webpackChunkName: "activity-panels-help" */ './panels/help' )
|
|
|
|
);
|
|
|
|
|
2020-04-29 18:01:27 +00:00
|
|
|
const InboxPanel = lazy( () =>
|
2020-10-06 13:13:32 +00:00
|
|
|
import(
|
|
|
|
/* webpackChunkName: "activity-panels-inbox" */ '../../inbox-panel'
|
|
|
|
)
|
2020-04-29 18:01:27 +00:00
|
|
|
);
|
2020-11-25 18:51:15 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
export const ActivityPanel = ( { isEmbedded, query, userPreferencesData } ) => {
|
|
|
|
const [ currentTab, setCurrentTab ] = useState( '' );
|
|
|
|
const [ isPanelOpen, setIsPanelOpen ] = useState( false );
|
|
|
|
const [ isPanelSwitching, setIsPanelSwitching ] = useState( false );
|
|
|
|
const [ mobileOpen, setMobileOpen ] = useState( false );
|
|
|
|
|
|
|
|
const {
|
|
|
|
hasUnreadNotes,
|
|
|
|
requestingTaskListOptions,
|
|
|
|
setupTaskListComplete,
|
|
|
|
setupTaskListHidden,
|
|
|
|
trackedCompletedTasks,
|
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
const { getOption, isResolving } = select( OPTIONS_STORE_NAME );
|
2020-08-05 22:02:24 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
return {
|
|
|
|
hasUnreadNotes: getUnreadNotes( select ),
|
|
|
|
requestingTaskListOptions:
|
|
|
|
isResolving( 'getOption', [
|
|
|
|
'woocommerce_task_list_complete',
|
|
|
|
] ) ||
|
|
|
|
isResolving( 'getOption', [ 'woocommerce_task_list_hidden' ] ),
|
|
|
|
setupTaskListComplete:
|
|
|
|
getOption( 'woocommerce_task_list_complete' ) === 'yes',
|
|
|
|
setupTaskListHidden:
|
|
|
|
getOption( 'woocommerce_task_list_hidden' ) === 'yes',
|
|
|
|
trackedCompletedTasks:
|
|
|
|
getOption( 'woocommerce_task_list_tracked_completed_tasks' ) ||
|
|
|
|
[],
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
|
|
|
const { currentUserCan } = useUser();
|
|
|
|
|
|
|
|
const togglePanel = ( { name: tabName }, isTabOpen ) => {
|
|
|
|
const panelSwitching =
|
|
|
|
tabName !== currentTab &&
|
|
|
|
currentTab !== '' &&
|
|
|
|
isTabOpen &&
|
|
|
|
isPanelOpen;
|
|
|
|
|
|
|
|
setCurrentTab( tabName );
|
|
|
|
setMobileOpen( isTabOpen );
|
|
|
|
setIsPanelOpen( isTabOpen );
|
|
|
|
setIsPanelSwitching( panelSwitching );
|
|
|
|
};
|
|
|
|
|
|
|
|
const closePanel = () => {
|
|
|
|
setIsPanelOpen( false );
|
|
|
|
};
|
|
|
|
|
|
|
|
const clearPanel = () => {
|
2021-02-25 12:56:00 +00:00
|
|
|
if ( ! isPanelOpen ) {
|
|
|
|
setIsPanelSwitching( false );
|
|
|
|
setCurrentTab( '' );
|
|
|
|
}
|
2021-02-19 13:57:17 +00:00
|
|
|
};
|
2019-07-25 01:33:41 +00:00
|
|
|
|
2018-06-29 15:20:08 +00:00
|
|
|
// On smaller screen, the panel buttons are hidden behind a toggle.
|
2021-02-19 13:57:17 +00:00
|
|
|
const toggleMobile = () => {
|
|
|
|
const tabs = getTabs();
|
|
|
|
setCurrentTab( mobileOpen ? '' : tabs[ 0 ].name );
|
|
|
|
setMobileOpen( ! mobileOpen );
|
|
|
|
setIsPanelOpen( ! mobileOpen );
|
|
|
|
};
|
|
|
|
|
|
|
|
const isHomescreen = () => {
|
|
|
|
return query.page === 'wc-admin' && ! query.path;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isPerformingSetupTask = () => {
|
|
|
|
return (
|
2020-07-14 12:20:51 +00:00
|
|
|
query.task &&
|
|
|
|
! query.path &&
|
|
|
|
( requestingTaskListOptions === true ||
|
2021-01-27 18:40:02 +00:00
|
|
|
( setupTaskListHidden === false &&
|
2021-02-19 13:57:17 +00:00
|
|
|
setupTaskListComplete === false ) )
|
|
|
|
);
|
|
|
|
};
|
2020-10-22 22:13:14 +00:00
|
|
|
|
|
|
|
// @todo Pull in dynamic unread status/count
|
2021-02-19 13:57:17 +00:00
|
|
|
const getTabs = () => {
|
|
|
|
const inbox = {
|
|
|
|
name: 'inbox',
|
|
|
|
title: __( 'Inbox', 'woocommerce-admin' ),
|
|
|
|
icon: <Icon icon={ inboxIcon } />,
|
|
|
|
unread: hasUnreadNotes,
|
|
|
|
visible:
|
|
|
|
( isEmbedded || ! isHomescreen() ) && ! isPerformingSetupTask(),
|
|
|
|
};
|
2021-02-16 20:01:11 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
const setup = {
|
|
|
|
name: 'setup',
|
|
|
|
title: __( 'Store Setup', 'woocommerce-admin' ),
|
|
|
|
icon: <SetupProgress />,
|
|
|
|
onClick: () => {
|
|
|
|
const currentLocation = window.location.href;
|
|
|
|
const homescreenLocation = getAdminLink(
|
|
|
|
'admin.php?page=wc-admin'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Don't navigate if we're already on the homescreen, this will cause an infinite loop
|
|
|
|
if ( currentLocation !== homescreenLocation ) {
|
|
|
|
// Ensure that if the user is trying to get to the task list they can see it even if
|
|
|
|
// it was dismissed.
|
|
|
|
if ( setupTaskListHidden === 'no' ) {
|
|
|
|
redirectToHomeScreen();
|
|
|
|
} else {
|
|
|
|
updateOptions( {
|
|
|
|
woocommerce_task_list_hidden: 'no',
|
|
|
|
} ).then( redirectToHomeScreen );
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 20:01:11 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
visible:
|
|
|
|
currentUserCan( 'manage_woocommerce' ) &&
|
|
|
|
! setupTaskListComplete &&
|
|
|
|
! setupTaskListHidden &&
|
|
|
|
! isPerformingSetupTask() &&
|
|
|
|
( ! isHomescreen() || isEmbedded ),
|
|
|
|
};
|
2020-10-15 19:49:46 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
const help = {
|
|
|
|
name: 'help',
|
|
|
|
title: __( 'Help', 'woocommerce-admin' ),
|
|
|
|
icon: <Icon icon={ helpIcon } />,
|
|
|
|
visible:
|
|
|
|
( isHomescreen() && ! isEmbedded ) || isPerformingSetupTask(),
|
|
|
|
};
|
2020-09-24 21:23:27 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
const displayOptions = {
|
|
|
|
component: DisplayOptions,
|
|
|
|
visible:
|
|
|
|
! isEmbedded && isHomescreen() && ! isPerformingSetupTask(),
|
|
|
|
};
|
2020-11-16 13:48:50 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
return [ inbox, setup, displayOptions, help ].filter(
|
|
|
|
( tab ) => tab.visible
|
|
|
|
);
|
|
|
|
};
|
2018-06-28 13:52:45 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
const getPanelContent = ( tab ) => {
|
2020-08-05 22:02:24 +00:00
|
|
|
const { task } = query;
|
|
|
|
|
2018-07-06 12:40:05 +00:00
|
|
|
switch ( tab ) {
|
2018-10-12 19:20:48 +00:00
|
|
|
case 'inbox':
|
|
|
|
return <InboxPanel />;
|
2020-07-14 12:20:51 +00:00
|
|
|
case 'help':
|
|
|
|
return <HelpPanel taskName={ task } />;
|
2018-06-28 13:52:45 +00:00
|
|
|
default:
|
2018-07-09 15:46:31 +00:00
|
|
|
return null;
|
2018-06-28 13:52:45 +00:00
|
|
|
}
|
2021-02-19 13:57:17 +00:00
|
|
|
};
|
2018-07-06 12:40:05 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
const redirectToHomeScreen = () => {
|
2020-08-05 22:02:24 +00:00
|
|
|
if ( isWCAdmin( window.location.href ) ) {
|
|
|
|
getHistory().push( getNewPath( {}, '/', {} ) );
|
|
|
|
} else {
|
|
|
|
window.location.href = getAdminLink( 'admin.php?page=wc-admin' );
|
2018-07-13 20:28:01 +00:00
|
|
|
}
|
2021-02-19 13:57:17 +00:00
|
|
|
};
|
2018-06-28 13:52:45 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
const closedHelpPanelHighlight = () => {
|
2020-12-18 13:17:07 +00:00
|
|
|
recordEvent( 'help_tooltip_click' );
|
|
|
|
if (
|
|
|
|
userPreferencesData &&
|
|
|
|
userPreferencesData.updateUserPreferences
|
|
|
|
) {
|
|
|
|
userPreferencesData.updateUserPreferences( {
|
|
|
|
help_panel_highlight_shown: 'yes',
|
|
|
|
} );
|
|
|
|
}
|
2021-02-19 13:57:17 +00:00
|
|
|
};
|
2020-12-18 13:17:07 +00:00
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
const shouldShowHelpTooltip = () => {
|
2020-12-18 13:17:07 +00:00
|
|
|
const { task } = query;
|
|
|
|
const startedTasks =
|
|
|
|
userPreferencesData &&
|
|
|
|
userPreferencesData.task_list_tracked_started_tasks;
|
|
|
|
const highlightShown =
|
|
|
|
userPreferencesData &&
|
|
|
|
userPreferencesData.help_panel_highlight_shown;
|
|
|
|
if (
|
|
|
|
task &&
|
|
|
|
highlightShown !== 'yes' &&
|
|
|
|
( startedTasks || {} )[ task ] > 1 &&
|
|
|
|
! trackedCompletedTasks.includes( task )
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-02-19 13:57:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const tabs = getTabs();
|
|
|
|
const headerId = uniqueId( 'activity-panel-header_' );
|
|
|
|
const panelClasses = classnames( 'woocommerce-layout__activity-panel', {
|
|
|
|
'is-mobile-open': mobileOpen,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const showHelpHighlightTooltip = shouldShowHelpTooltip();
|
|
|
|
const hasUnread = tabs.some( ( tab ) => tab.unread );
|
|
|
|
const viewLabel = hasUnread
|
|
|
|
? __(
|
|
|
|
'View Activity Panel, you have unread activity',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
: __( 'View Activity Panel', 'woocommerce-admin' );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<H id={ headerId } className="screen-reader-text">
|
|
|
|
{ __( 'Store Activity', 'woocommerce-admin' ) }
|
|
|
|
</H>
|
|
|
|
<Section
|
|
|
|
component="aside"
|
|
|
|
id="woocommerce-activity-panel"
|
|
|
|
aria-labelledby={ headerId }
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
onClick={ () => {
|
|
|
|
toggleMobile();
|
|
|
|
} }
|
|
|
|
label={
|
|
|
|
mobileOpen
|
|
|
|
? __( 'Close Activity Panel', 'woocommerce-admin' )
|
|
|
|
: viewLabel
|
|
|
|
}
|
|
|
|
aria-expanded={ mobileOpen }
|
|
|
|
className="woocommerce-layout__activity-panel-mobile-toggle"
|
2020-02-14 02:23:21 +00:00
|
|
|
>
|
2021-02-19 13:57:17 +00:00
|
|
|
{ mobileOpen ? (
|
|
|
|
<CrossIcon />
|
|
|
|
) : (
|
|
|
|
<ActivityPanelToggleBubble hasUnread={ hasUnread } />
|
|
|
|
) }
|
|
|
|
</Button>
|
|
|
|
<div className={ panelClasses }>
|
|
|
|
<Tabs
|
|
|
|
tabs={ tabs }
|
|
|
|
tabOpen={ isPanelOpen }
|
|
|
|
selectedTab={ currentTab }
|
|
|
|
onTabClick={ ( tab, tabOpen ) => {
|
|
|
|
if ( tab.onClick ) {
|
|
|
|
tab.onClick();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
togglePanel( tab, tabOpen );
|
2020-08-05 22:02:24 +00:00
|
|
|
} }
|
2020-12-18 13:17:07 +00:00
|
|
|
/>
|
2021-02-19 13:57:17 +00:00
|
|
|
<Panel
|
|
|
|
currentTab
|
|
|
|
isPanelOpen={ isPanelOpen }
|
|
|
|
isPanelSwitching={ isPanelSwitching }
|
|
|
|
tab={ find( getTabs(), { name: currentTab } ) }
|
|
|
|
content={ getPanelContent( currentTab ) }
|
|
|
|
closePanel={ () => closePanel() }
|
|
|
|
clearPanel={ () => clearPanel() }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Section>
|
|
|
|
{ showHelpHighlightTooltip ? (
|
|
|
|
<HighlightTooltip
|
|
|
|
delay={ 1000 }
|
|
|
|
useAnchor={ true }
|
|
|
|
title={ __( "We're here for help", 'woocommerce-admin' ) }
|
|
|
|
content={ __(
|
|
|
|
'If you have any questions, feel free to explore the WooCommerce docs listed here.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
closeButtonText={ __( 'Got it', 'woocommerce-admin' ) }
|
|
|
|
id="activity-panel-tab-help"
|
|
|
|
onClose={ () => closedHelpPanelHighlight() }
|
|
|
|
onShow={ () => recordEvent( 'help_tooltip_view' ) }
|
|
|
|
/>
|
|
|
|
) : null }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2018-06-28 13:52:45 +00:00
|
|
|
|
2020-07-14 12:20:51 +00:00
|
|
|
ActivityPanel.defaultProps = {
|
|
|
|
getHistory,
|
|
|
|
};
|
|
|
|
|
2021-02-19 13:57:17 +00:00
|
|
|
export default ActivityPanel;
|