/**
* 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, external } from '@wordpress/icons';
import { getAdminLink, getSetting } from '@woocommerce/settings';
import { H, Section } from '@woocommerce/components';
import {
ONBOARDING_STORE_NAME,
OPTIONS_STORE_NAME,
useUser,
useUserPreferences,
} from '@woocommerce/data';
import { getHistory, getNewPath } from '@woocommerce/navigation';
import { recordEvent } from '@woocommerce/tracks';
import { useSlot } from '@woocommerce/experimental';
/**
* Internal dependencies
*/
import './style.scss';
import { IconFlag } from './icon-flag';
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 } ) => {
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 { updateUserPreferences, ...userData } = useUserPreferences();
const getPreviewSiteBtnTrackData = ( select, getOption ) => {
let trackData = {};
if ( query.page === 'wc-admin' && query.task === 'appearance' ) {
const { getTaskLists } = select( ONBOARDING_STORE_NAME );
const taskLists = getTaskLists();
const tasks = taskLists.reduce(
( acc, taskList ) => [ ...acc, ...taskList.tasks ],
[]
);
const task = tasks.find( ( t ) => t.id === 'appearance' );
const demoNotice = getOption( 'woocommerce_demo_store_notice' );
trackData = {
set_notice: demoNotice ? 'Y' : 'N',
create_homepage:
task?.additionalData?.hasHomepage === true ? 'Y' : 'N',
upload_logo: task?.additionalData?.themeMods?.custom_logo
? 'Y'
: 'N',
};
}
return trackData;
};
function getThingsToDoNextCount( extendedTaskList ) {
if (
! extendedTaskList ||
! extendedTaskList.tasks.length ||
extendedTaskList.isHidden
) {
return 0;
}
return extendedTaskList.tasks.filter(
( task ) => task.canView && ! task.isComplete && ! task.isDismissed
).length;
}
function isAbbreviatedPanelVisible(
select,
setupTaskListHidden,
thingsToDoNextCount
) {
const orderStatuses = getOrderStatuses( select );
const isOrdersCardVisible =
setupTaskListHidden && isPanelOpen
? getUnreadOrders( select, orderStatuses ) > 0
: false;
const isReviewsCardVisible =
setupTaskListHidden && isPanelOpen
? getUnapprovedReviews( select )
: false;
const isLowStockCardVisible =
setupTaskListHidden && isPanelOpen
? getLowStockProducts( select )
: false;
return (
thingsToDoNextCount > 0 ||
isOrdersCardVisible ||
isReviewsCardVisible ||
isLowStockCardVisible ||
hasExtendedNotifications
);
}
const {
hasUnreadNotes,
hasAbbreviatedNotifications,
isCompletedTask,
thingsToDoNextCount,
requestingTaskListOptions,
setupTaskListComplete,
setupTaskListHidden,
previewSiteBtnTrackData,
} = useSelect( ( select ) => {
const { getOption } = select( OPTIONS_STORE_NAME );
const { getTask, getTaskList, hasFinishedResolution } = select(
ONBOARDING_STORE_NAME
);
const isSetupTaskListHidden = getTaskList( 'setup' )?.isHidden;
const extendedTaskList = getTaskList( 'extended' );
const thingsToDoCount = getThingsToDoNextCount( extendedTaskList );
return {
hasUnreadNotes: isNotesPanelVisible( select ),
hasAbbreviatedNotifications: isAbbreviatedPanelVisible(
select,
isSetupTaskListHidden,
thingsToDoCount
),
thingsToDoNextCount: thingsToDoCount,
requestingTaskListOptions: ! hasFinishedResolution(
'getTaskLists'
),
setupTaskListComplete: getTaskList( 'setup' )?.isComplete,
setupTaskListHidden: isSetupTaskListHidden,
isCompletedTask: Boolean(
query.task && getTask( query.task )?.isComplete
),
previewSiteBtnTrackData: getPreviewSiteBtnTrackData(
select,
getOption
),
};
} );
const { unhideTaskList } = useDispatch( ONBOARDING_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 ) )
);
};
const redirectToHomeScreen = () => {
if ( isWCAdmin( window.location.href ) ) {
getHistory().push( getNewPath( {}, '/', {} ) );
} else {
window.location.href = getAdminLink( 'admin.php?page=wc-admin' );
}
};
// @todo Pull in dynamic unread status/count
const getTabs = () => {
const activity = {
name: 'activity',
title: __( 'Activity', 'woocommerce' ),
icon: