/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { lazy, useState } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { uniqueId, find } from 'lodash';
import {
Icon,
help as helpIcon,
inbox as inboxIcon,
external,
} from '@wordpress/icons';
import { getAdminLink } from '@woocommerce/wc-admin-settings';
import { H, Section } from '@woocommerce/components';
import {
ONBOARDING_STORE_NAME,
OPTIONS_STORE_NAME,
useUser,
} from '@woocommerce/data';
import { getHistory, getNewPath } from '@woocommerce/navigation';
import { recordEvent } from '@woocommerce/tracks';
import { applyFilters } from '@wordpress/hooks';
import { useSlot } from '@woocommerce/experimental';
/**
* Internal dependencies
*/
import './style.scss';
import { isNotesPanelVisible } from './unread-indicators';
import { isWCAdmin } from '../../dashboard/utils';
import { Tabs } from './tabs';
import { SetupProgress } from './setup-progress';
import { DisplayOptions } from './display-options';
import { HighlightTooltip } from './highlight-tooltip';
import { Panel } from './panel';
import {
getLowStockCount as getLowStockProducts,
getOrderStatuses,
getUnreadOrders,
} from '../../homescreen/activity-panel/orders/utils';
import { getUnapprovedReviews } from '../../homescreen/activity-panel/reviews/utils';
import { ABBREVIATED_NOTIFICATION_SLOT_NAME } from './panels/inbox/abbreviated-notifications-panel';
const HelpPanel = lazy( () =>
import( /* webpackChunkName: "activity-panels-help" */ './panels/help' )
);
const InboxPanel = lazy( () =>
import(
/* webpackChunkName: "activity-panels-inbox" */ './panels/inbox/inbox-panel'
)
);
export const ActivityPanel = ( { isEmbedded, query, userPreferencesData } ) => {
const [ currentTab, setCurrentTab ] = useState( '' );
const [ isPanelClosing, setIsPanelClosing ] = useState( false );
const [ isPanelOpen, setIsPanelOpen ] = useState( false );
const [ isPanelSwitching, setIsPanelSwitching ] = useState( false );
const { fills } = useSlot( ABBREVIATED_NOTIFICATION_SLOT_NAME );
const hasExtendedNotifications = Boolean( fills?.length );
const getPreviewSiteBtnTrackData = ( select, getOption ) => {
let trackData = {};
if ( query.page === 'wc-admin' && query.task === 'appearance' ) {
const { getTasksStatus } = select( ONBOARDING_STORE_NAME );
const tasksStatus = getTasksStatus();
const demoNotice = getOption( 'woocommerce_demo_store_notice' );
trackData = {
set_notice: demoNotice ? 'Y' : 'N',
create_homepage: tasksStatus.hasHomepage === true ? 'Y' : 'N',
upload_logo: tasksStatus.themeMods?.custom_logo ? 'Y' : 'N',
};
}
return trackData;
};
function getThingsToDoNextCount(
tasks,
dismissedTasks,
isExtendedTaskListHidden
) {
if ( ! tasks || isExtendedTaskListHidden ) {
return 0;
}
return tasks.filter(
( task ) =>
task.visible &&
! task.completed &&
! dismissedTasks.includes( task.key )
).length;
}
function isAbbreviatedPanelVisible(
select,
setupTaskListHidden,
thingsToDoNextCount
) {
const orderStatuses = getOrderStatuses( select );
const isOrdersCardVisible = setupTaskListHidden
? getUnreadOrders( select, orderStatuses ) > 0
: false;
const isReviewsCardVisible = setupTaskListHidden
? getUnapprovedReviews( select )
: false;
const isLowStockCardVisible = setupTaskListHidden
? getLowStockProducts( select )
: false;
return (
thingsToDoNextCount > 0 ||
isOrdersCardVisible ||
isReviewsCardVisible ||
isLowStockCardVisible ||
hasExtendedNotifications
);
}
const {
hasUnreadNotes,
hasAbbreviatedNotifications,
thingsToDoNextCount,
requestingTaskListOptions,
setupTaskListComplete,
setupTaskListHidden,
trackedCompletedTasks,
previewSiteBtnTrackData,
} = useSelect( ( select ) => {
const { getOption, isResolving } = select( OPTIONS_STORE_NAME );
const isSetupTaskListHidden =
getOption( 'woocommerce_task_list_hidden' ) === 'yes';
const isExtendedTaskListHidden =
getOption( 'woocommerce_extended_task_list_hidden' ) === 'yes';
const extendedTaskList = applyFilters(
'woocommerce_admin_onboarding_task_list',
[],
query
);
const dismissedTasks =
getOption( 'woocommerce_task_list_dismissed_tasks' ) || [];
const thingsToDoCount = getThingsToDoNextCount(
extendedTaskList,
dismissedTasks,
isExtendedTaskListHidden
);
return {
hasUnreadNotes: isNotesPanelVisible( select ),
hasAbbreviatedNotifications: isAbbreviatedPanelVisible(
select,
isSetupTaskListHidden,
thingsToDoCount
),
thingsToDoNextCount: thingsToDoCount,
requestingTaskListOptions:
isResolving( 'getOption', [
'woocommerce_task_list_complete',
] ) ||
isResolving( 'getOption', [ 'woocommerce_task_list_hidden' ] ),
setupTaskListComplete:
getOption( 'woocommerce_task_list_complete' ) === 'yes',
setupTaskListHidden: isSetupTaskListHidden,
trackedCompletedTasks:
getOption( 'woocommerce_task_list_tracked_completed_tasks' ) ||
[],
previewSiteBtnTrackData: getPreviewSiteBtnTrackData(
select,
getOption
),
};
} );
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
const { currentUserCan } = useUser();
const togglePanel = ( { name: tabName }, isTabOpen ) => {
const panelSwitching =
tabName !== currentTab &&
currentTab !== '' &&
isTabOpen &&
isPanelOpen;
if ( isPanelClosing ) {
return;
}
setCurrentTab( tabName );
setIsPanelOpen( isTabOpen );
setIsPanelSwitching( panelSwitching );
};
const closePanel = () => {
setIsPanelClosing( true );
setIsPanelOpen( false );
};
const clearPanel = () => {
if ( ! isPanelOpen ) {
setIsPanelClosing( false );
setIsPanelSwitching( false );
setCurrentTab( '' );
}
};
const isHomescreen = () => {
return query.page === 'wc-admin' && ! query.path;
};
const isPerformingSetupTask = () => {
return (
query.task &&
! query.path &&
( requestingTaskListOptions === true ||
( setupTaskListHidden === false &&
setupTaskListComplete === false ) )
);
};
// @todo Pull in dynamic unread status/count
const getTabs = () => {
const inbox = {
name: 'inbox',
title: __( 'Inbox', 'woocommerce-admin' ),
icon: