File size: 8,373 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 | /* eslint-disable wpcalypso/jsx-classname-namespace */
import { recordTracksEvent } from '@automattic/calypso-analytics';
import { LoadingPlaceholder } from '@automattic/components';
import { HelpCenterSelect } from '@automattic/data-stores';
import styled from '@emotion/styled';
import { Button, ExternalLink } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { createElement, createInterpolateElement } from '@wordpress/element';
import { sprintf } from '@wordpress/i18n';
import { useI18n } from '@wordpress/react-i18n';
import React, { useEffect, useMemo, useState } from 'react';
import stripTags from 'striptags';
import { useJetpackSearchAIQuery } from '../data/use-jetpack-search-ai';
import { useTyper } from '../hooks';
import { HELP_CENTER_STORE } from '../stores';
import HelpCenterSearchResults from './help-center-search-results';
import type { JetpackSearchAIResult } from '../data/use-jetpack-search-ai';
import type { SearchResult } from '../types';
import './help-center-gpt.scss';
const GPTResponsePlaceholder = styled( LoadingPlaceholder )< { width?: string } >`
:not( :last-child ) {
margin-block-end: 0.5rem;
}
width: ${ ( { width = '100%' } ) => width };
`;
const GPTResponseDisclaimer = styled.div`
color: var( --studio-gray-50 );
font-size: 11px;
text-align: right;
@media ( min-width: 600px ) {
padding: 0 0 15px;
}
`;
interface LoadingPlaceholderProps {
loadingMessage: string;
}
const LoadingPlaceholders: React.FC< LoadingPlaceholderProps > = ( { loadingMessage } ) => (
<>
<p className="help-center-gpt-response__loading">{ loadingMessage }</p>
<GPTResponsePlaceholder width="80%" />
<GPTResponsePlaceholder width="80%" />
<GPTResponsePlaceholder width="55%" />
</>
);
interface Props {
onResponseReceived: ( response: JetpackSearchAIResult ) => void;
redirectToArticle: (
event: React.MouseEvent< HTMLAnchorElement, MouseEvent >,
result: SearchResult
) => void;
}
const handleContentClick = ( event: React.MouseEvent ) => {
// Check if the clicked element is a link
if ( event.target instanceof HTMLAnchorElement ) {
const url = event.target.getAttribute( 'href' );
if ( url ) {
event.preventDefault();
window.open( url, '_blank' );
}
}
};
export function HelpCenterGPT( { onResponseReceived, redirectToArticle }: Props ) {
const { __ } = useI18n();
const [ feedbackGiven, setFeedbackGiven ] = useState< boolean >( false );
// Report loading state up.
const { message } = useSelect( ( select ) => {
const store = select( HELP_CENTER_STORE ) as HelpCenterSelect;
const subject = store.getSubject() || '';
const message = store.getMessage();
// if the first 50 chars of subject and message match, only show the message (they're probably identical)
if ( subject && message && subject.slice( 0, 50 ) !== message.slice( 0, 50 ) ) {
return {
message: `${ subject }\n\n${ message }`,
};
}
return {
message,
};
}, [] );
const query = message ?? '';
const { data, isError: isGPTError } = useJetpackSearchAIQuery( {
siteId: '9619154',
query: query,
stopAt: 'response',
enabled: true,
} );
const allowedTags = [ 'a', 'p', 'ol', 'ul', 'li', 'br', 'b', 'strong', 'i', 'em' ];
useEffect( () => {
if ( data?.response ) {
recordTracksEvent( 'calypso_helpcenter_show_gpt_response', {
location: 'help-center',
answer_source: data?.source,
} );
onResponseReceived( data );
}
}, [ data ] );
const loadingMessages: string[] = [
__( 'Finding relevant support documentation…', __i18n_text_domain__ ),
__( 'Gathering all the data…', __i18n_text_domain__ ),
__( 'Thanks for your patience, this might take a bit…', __i18n_text_domain__ ),
__( 'Processing the information found…', __i18n_text_domain__ ),
__( "I'm still writing, thank you for your patience!", __i18n_text_domain__ ),
__( 'Any minute now…', __i18n_text_domain__ ),
__( 'Really, any minute now…', __i18n_text_domain__ ),
];
const loadingMessage = useTyper( loadingMessages, ! data?.response, {
delayBetweenCharacters: 80,
delayBetweenPhrases: 1400,
} );
const doThumbsUp = ( source: string ) => {
return () => {
setFeedbackGiven( true );
recordTracksEvent( 'calypso_helpcenter_gpt_response_thumbs_up', {
location: 'help-center',
answer_source: source,
} );
};
};
const doThumbsDown = ( source: string ) => {
return () => {
setFeedbackGiven( true );
recordTracksEvent( 'calypso_helpcenter_gpt_response_thumbs_down', {
location: 'help-center',
answer_source: source,
} );
};
};
const aiResponseHeader = useMemo( () => {
return __( 'AI Generated Response:', __i18n_text_domain__ );
}, [ __ ] );
return (
<div className="help-center-gpt__container">
<HelpCenterSearchResults
onSelect={ redirectToArticle }
searchQuery={ message || '' }
openAdminInNewTab
placeholderLines={ 4 }
location="help-center-contact-form"
/>
<h1 id="help-center--contextual_help" className="help-center__section-title">
{ aiResponseHeader }
</h1>
{ isGPTError && (
<div className="help-center-gpt-error">
{ __(
"Sorry, we couldn't load the AI-generated response. Please click the button below to send your message to our support team.",
__i18n_text_domain__
) }
</div>
) }
{ ! isGPTError && (
<>
<div className="help-center-gpt-response">
{ ! data?.response && (
<>
<p>
{ __(
'Our system is currently generating a possible solution for you, which typically takes about 30 seconds.',
__i18n_text_domain__
) }
</p>
<p>
{ createInterpolateElement(
sprintf(
/* translators: the cancelButtonLabel is the already translated "Cancel" button label */
__(
'We know your time is valuable. If you prefer not to wait, you can click the <em>%(cancelButtonLabel)s</em> button below to go back and submit your message right away.'
),
{ cancelButtonLabel: __( 'Cancel', __i18n_text_domain__ ) }
),
{
em: createElement( 'em' ),
}
) }
</p>
</>
) }
{ ! data?.response && LoadingPlaceholders( { loadingMessage } ) }
{ data?.response && (
<>
{ /* eslint-disable-next-line jsx-a11y/no-static-element-interactions,jsx-a11y/click-events-have-key-events */ }
<div
className="help-center-gpt-response__content"
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={ {
__html: stripTags( data.response, allowedTags ),
} }
onClick={ handleContentClick }
/>
<div className="help-center-gpt-response__actions">
{ feedbackGiven ? (
<div className="help-center-gpt-response__feedback">
{ __(
'Thank you for your feedback! We will use it to improve our AI.',
__i18n_text_domain__
) }
</div>
) : (
<>
<Button onClick={ doThumbsUp( data?.source ) }>👍</Button>
<Button onClick={ doThumbsDown( data?.source ) }>👎</Button>
</>
) }
</div>
<GPTResponseDisclaimer>
{ __(
"Generated by WordPress.com's Support AI. AI-generated responses may contain inaccurate information.",
__i18n_text_domain__
) }
<ExternalLink href="https://automattic.com/ai-guidelines">
{ ' ' }
{ __( 'Learn more.', __i18n_text_domain__ ) }
</ExternalLink>
</GPTResponseDisclaimer>
<div className="help-center-gpt-response__continue">
<p>
{ createInterpolateElement(
__(
'<strong>Need more help?</strong> Click the button below to send your message. For reference, here is what you wrote:',
__i18n_text_domain__
),
{
strong: createElement( 'strong' ),
}
) }
</p>
<div className="help-center-gpt-response__continue_quote">
{ query.split( '\n\n' ).map( ( line, index ) => (
<p key={ index }>{ line }</p>
) ) }
</div>
</div>
</>
) }
</div>
</>
) }
</div>
);
}
|