File size: 3,046 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
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { Button, CardFooter } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { backup, comment, Icon } from '@wordpress/icons';
import { useI18n } from '@wordpress/react-i18n';
import { ReactElement, ReactNode } from 'react';
import { Route, Routes, useNavigate } from 'react-router-dom';
import { useHelpCenterContext } from '../contexts/HelpCenterContext';
import { useGetHistoryChats, useStillNeedHelpURL } from '../hooks';

import './help-center-footer.scss';

const HelpCenterFooterButton = ( {
	children,
	eventName,
	buttonTextEventProp,
	redirectTo,
	icon,
}: {
	children: ReactNode;
	eventName: string;
	buttonTextEventProp: string;
	redirectTo: string;
	icon: ReactElement;
} ) => {
	const { url, isLoading } = useStillNeedHelpURL();
	const { sectionName } = useHelpCenterContext();
	const redirectToWpcom = url === 'https://wordpress.com/help/contact';
	const navigate = useNavigate();
	const [ isCreatingChat, setIsCreatingChat ] = useState( false );
	const handleContactButtonClicked = ( {
		eventName,
		buttonTextEventProp,
	}: {
		eventName: string;
		buttonTextEventProp: string;
	} ) => {
		recordTracksEvent( eventName, {
			force_site_id: true,
			location: 'help-center',
			section: sectionName,
			button_type: buttonTextEventProp,
		} );
	};

	const redirectionURL = () => {
		if ( buttonTextEventProp === 'Still need help?' ) {
			if ( isLoading ) {
				return '';
			}
			return redirectToWpcom ? { pathname: url } : url;
		}
		return redirectTo;
	};

	const handleClick = async () => {
		setIsCreatingChat( true );
		handleContactButtonClicked( {
			eventName: eventName,
			buttonTextEventProp: buttonTextEventProp,
		} );

		setIsCreatingChat( false );
		const url = redirectionURL();
		navigate( url );
	};

	return (
		<Button
			onClick={ handleClick }
			disabled={ isCreatingChat }
			className="button help-center-contact-page__button"
		>
			<Icon icon={ icon } />
			{ children }
		</Button>
	);
};

export const HelpCenterContactButton = () => {
	const { recentConversations } = useGetHistoryChats();
	const { __ } = useI18n();

	return (
		<>
			<HelpCenterFooterButton
				icon={ comment }
				eventName="calypso_inlinehelp_morehelp_click"
				buttonTextEventProp="Still need help?"
				redirectTo="/odie"
			>
				{ __( 'Still need help?', __i18n_text_domain__ ) }
			</HelpCenterFooterButton>

			{ recentConversations.length > 1 && (
				<HelpCenterFooterButton
					icon={ backup }
					eventName="calypso_inlinehelp_history_click"
					buttonTextEventProp="History"
					redirectTo="/chat-history"
				>
					{ __( 'History', __i18n_text_domain__ ) }
				</HelpCenterFooterButton>
			) }
		</>
	);
};

const HelpCenterFooter: React.FC = () => {
	return (
		<CardFooter className="help-center__container-footer">
			<Routes>
				<Route path="/" element={ <HelpCenterContactButton /> } />
				<Route path="*" element={ null } />
			</Routes>
		</CardFooter>
	);
};

export default HelpCenterFooter;