File size: 9,476 Bytes
1e92f2d |
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
/* eslint-disable no-restricted-imports */
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { useGetHistoryChats } from '@automattic/help-center/src/hooks/use-get-history-chats';
import { EllipsisMenu } from '@automattic/odie-client';
import { CardHeader, Button, Flex, ToggleControl } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo, useCallback, useEffect, useState } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { _n } from '@wordpress/i18n';
import {
closeSmall,
chevronUp,
lineSolid,
scheduled,
page,
Icon,
comment,
commentContent,
} from '@wordpress/icons';
import { useI18n } from '@wordpress/react-i18n';
import clsx from 'clsx';
import { useLocation, useSearchParams, useNavigate } from 'react-router-dom';
import { usePostByUrl } from '../hooks';
import { useResetSupportInteraction } from '../hooks/use-reset-support-interaction';
import { DragIcon } from '../icons';
import { HELP_CENTER_STORE } from '../stores';
import { BackButton } from './back-button';
import type { Header } from '../types';
import type { HelpCenterSelect } from '@automattic/data-stores';
import './help-center-header.scss';
export function ArticleTitle() {
const { __ } = useI18n();
const [ searchParams ] = useSearchParams();
const postUrl = searchParams.get( 'link' ) || '';
const { data: post } = usePostByUrl( postUrl );
return (
<>
<Icon icon={ page } />
<span className="help-center-header__article-title">
{ ( post && decodeEntities( post?.title ) ) ?? __( 'Help Center', __i18n_text_domain__ ) }
</span>
</>
);
}
const SupportModeTitle = () => {
const { __ } = useI18n();
const { search } = useLocation();
const params = new URLSearchParams( search );
const mode = params.get( 'mode' );
switch ( mode ) {
case 'CHAT':
return (
<>
<Icon icon={ commentContent } />
{ __( 'Contact WordPress.com Support', __i18n_text_domain__ ) }
</>
);
case 'EMAIL': {
return <>{ __( 'Send us an email', __i18n_text_domain__ ) }</>;
}
case 'FORUM': {
return <>{ __( 'Ask in our community forums', __i18n_text_domain__ ) }</>;
}
default: {
return <>{ __( 'Help Center', __i18n_text_domain__ ) }</>;
}
}
};
const ChatEllipsisMenu = () => {
const { __ } = useI18n();
const resetSupportInteraction = useResetSupportInteraction();
const navigate = useNavigate();
const { recentConversations } = useGetHistoryChats();
const { areSoundNotificationsEnabled } = useSelect( ( select ) => {
const helpCenterSelect: HelpCenterSelect = select( HELP_CENTER_STORE );
return {
areSoundNotificationsEnabled: helpCenterSelect.getAreSoundNotificationsEnabled(),
};
}, [] );
const { setAreSoundNotificationsEnabled } = useDispatch( HELP_CENTER_STORE );
const clearChat = async () => {
await resetSupportInteraction();
recordTracksEvent( 'calypso_inlinehelp_clear_conversation' );
};
const handleViewChats = () => {
recordTracksEvent( 'calypso_inlinehelp_view_open_chats_menu', {
total_number_of_conversations: recentConversations.length,
} );
navigate( '/chat-history' );
};
const toggleSoundNotifications = ( event: React.MouseEvent< HTMLButtonElement > ) => {
event.stopPropagation();
setAreSoundNotificationsEnabled( ! areSoundNotificationsEnabled );
};
return (
<EllipsisMenu
popoverClassName="help-center help-center__container-header-menu conversation-menu__wrapper"
position="bottom"
trackEventProps={ { source: 'help_center' } }
ariaLabel={ __( 'Chat options', __i18n_text_domain__ ) }
>
<button tabIndex={ 0 } onClick={ clearChat }>
<Icon icon={ comment } />
<div>{ __( 'New chat', __i18n_text_domain__ ) }</div>
</button>
<button
tabIndex={ 0 }
onClick={ handleViewChats }
disabled={ recentConversations.length < 2 }
>
<Icon icon={ scheduled } />
<div>
{ _n(
'View recent chat',
'View recent chats',
recentConversations.length,
__i18n_text_domain__
) }
</div>
</button>
<button
tabIndex={ 0 }
onClick={ toggleSoundNotifications }
aria-pressed={ !! areSoundNotificationsEnabled }
aria-label={ __( 'Notification sound', __i18n_text_domain__ ) }
>
<ToggleControl
className="conversation-menu__notification-toggle"
label={ __( 'Notification sound', __i18n_text_domain__ ) }
checked={ areSoundNotificationsEnabled }
onChange={ ( newValue ) => {
setAreSoundNotificationsEnabled( newValue );
} }
__nextHasNoMarginBottom
/>
</button>
</EllipsisMenu>
);
};
const useHeaderText = () => {
const { __ } = useI18n();
const { pathname } = useLocation();
const [ isConversationWithZendesk, setIsConversationWithZendesk ] = useState< boolean >( false );
const { currentSupportInteraction } = useSelect( ( select ) => {
const store = select( HELP_CENTER_STORE ) as HelpCenterSelect;
return {
currentSupportInteraction: store.getCurrentSupportInteraction(),
};
}, [] );
useEffect( () => {
if ( currentSupportInteraction ) {
const zendeskEvent = currentSupportInteraction?.events.find(
( event ) => event.event_source === 'zendesk'
);
setIsConversationWithZendesk( !! zendeskEvent );
}
}, [ currentSupportInteraction ] );
return useMemo( () => {
switch ( pathname ) {
case '/':
return __( 'Help Center', __i18n_text_domain__ );
case '/inline-chat':
return __( 'Live Chat', __i18n_text_domain__ );
case '/contact-form':
return <SupportModeTitle />;
case '/post':
case '/post/':
return <ArticleTitle />;
case '/success':
return __( 'Message Submitted', __i18n_text_domain__ );
case '/odie':
return isConversationWithZendesk
? __( 'Support Team', __i18n_text_domain__ )
: __( 'Support Assistant', __i18n_text_domain__ );
case '/chat-history':
return __( 'History', __i18n_text_domain__ );
default:
return __( 'Help Center', __i18n_text_domain__ );
}
}, [ __, isConversationWithZendesk, pathname ] );
};
const HeaderText = () => {
const headerText = useHeaderText();
return (
<span id="header-text" role="presentation" className="help-center-header__text">
{ headerText }
</span>
);
};
const Content = ( { onMinimize }: { onMinimize?: () => void } ) => {
const { __ } = useI18n();
const { pathname } = useLocation();
const { helpCenterOptions } = useSelect( ( select ) => {
const store = select( HELP_CENTER_STORE ) as HelpCenterSelect;
return {
helpCenterOptions: store.getHelpCenterOptions(),
};
}, [] );
const isChat = pathname.startsWith( '/odie' );
const isHelpCenterHome = pathname === '/';
// Show the back button if it's not the help center home page and:
// - it's a chat and the hideBackButton option is not set
// - it's not a chat
const shouldShowBackButton =
! isHelpCenterHome && ( ( isChat && ! helpCenterOptions?.hideBackButton ) || ! isChat );
return (
<>
{ shouldShowBackButton ? <BackButton /> : <DragIcon /> }
<HeaderText />
{ isChat && <ChatEllipsisMenu /> }
<Button
className="help-center-header__minimize"
label={ __( 'Minimize Help Center', __i18n_text_domain__ ) }
icon={ lineSolid }
tooltipPosition="top left"
onClick={ () => onMinimize?.() }
onTouchStart={ () => onMinimize?.() }
/>
</>
);
};
const ContentMinimized = ( {
unreadCount = 0,
handleClick,
onMaximize,
}: {
unreadCount: number;
handleClick?: ( event: React.SyntheticEvent ) => void;
onMaximize?: () => void;
} ) => {
const { __ } = useI18n();
const headerText = useHeaderText();
const formattedUnreadCount = unreadCount > 9 ? '9+' : unreadCount;
return (
<>
<p
id="header-text"
className="help-center-header__text"
onClick={ handleClick }
onKeyUp={ handleClick }
role="presentation"
>
{ headerText }
{ unreadCount > 0 && (
<span className="help-center-header__unread-count">{ formattedUnreadCount }</span>
) }
</p>
<Button
className="help-center-header__maximize"
label={ __( 'Maximize Help Center', __i18n_text_domain__ ) }
icon={ chevronUp }
tooltipPosition="top left"
onClick={ onMaximize }
onTouchStart={ onMaximize }
/>
</>
);
};
const HelpCenterHeader = ( { isMinimized = false, onMinimize, onMaximize, onDismiss }: Header ) => {
const { __ } = useI18n();
const location = useLocation();
const unreadCount = useSelect(
( select ) => ( select( HELP_CENTER_STORE ) as HelpCenterSelect ).getUnreadCount(),
[]
);
const handleClick = useCallback( () => {
if ( isMinimized ) {
onMaximize?.();
}
}, [ onMaximize, isMinimized ] );
const classNames = clsx(
'help-center__container-header',
location?.pathname?.replace( /^\//, '' ),
{
'has-unread': unreadCount > 0 && isMinimized,
}
);
return (
<CardHeader className={ classNames }>
<Flex onClick={ handleClick }>
{ isMinimized ? (
<ContentMinimized
unreadCount={ unreadCount }
handleClick={ handleClick }
onMaximize={ onMaximize }
/>
) : (
<Content onMinimize={ onMinimize } />
) }
<Button
className="help-center-header__close"
label={ __( 'Close Help Center', __i18n_text_domain__ ) }
tooltipPosition="top left"
icon={ closeSmall }
onClick={ onDismiss }
onTouchStart={ onDismiss }
/>
</Flex>
</CardHeader>
);
};
export default HelpCenterHeader;
|