diff --git a/nx-dev/feature-ai/src/lib/Toast.tsx b/nx-dev/feature-ai/src/lib/Toast.tsx new file mode 100644 index 00000000000000..5bde4851262890 --- /dev/null +++ b/nx-dev/feature-ai/src/lib/Toast.tsx @@ -0,0 +1,30 @@ +import { useState, useEffect } from 'react'; + +const Toast = ({ message = '', duration = 3000 }) => { + const [visible, setVisible] = useState(true); + + useEffect(() => { + const timer = setTimeout(() => { + setVisible(false); + }, duration); + + return () => clearTimeout(timer); + }, []); + + return visible ? ( +
+ {message} +
+ ) : null; +}; + +export default Toast; diff --git a/nx-dev/feature-ai/src/lib/feature-ai.tsx b/nx-dev/feature-ai/src/lib/feature-ai.tsx index 733681efe036c8..1d002ccf167e49 100644 --- a/nx-dev/feature-ai/src/lib/feature-ai.tsx +++ b/nx-dev/feature-ai/src/lib/feature-ai.tsx @@ -4,19 +4,20 @@ import { sendCustomEvent } from '@nx/nx-dev/feature-analytics'; import { renderMarkdown } from '@nx/nx-dev/ui-markdoc'; import { nxDevDataAccessAi } from '@nx/nx-dev/data-access-ai'; +import Toast from './Toast'; export function FeatureAi(): JSX.Element { const [finalResult, setFinalResult] = useState(null); const [error, setError] = useState(null); const [query, setSearchTerm] = useState(''); const [loading, setLoading] = useState(false); + const [toastMessage, setToastMessage] = useState(null); const warning = ` {% callout type="warning" title="Always double check!" %} This feature is still in Alpha. The results may not be accurate, so please always double check with our documentation. {% /callout %} - `; const handleSubmit = async () => { @@ -38,6 +39,20 @@ export function FeatureAi(): JSX.Element { ); }; + const handleFeedback = (type: 'good' | 'bad') => { + try { + sendCustomEvent('ai_feedback', 'ai', type, undefined, { + query, + result: finalResult, + }); + setToastMessage(null); + setTimeout(() => setToastMessage('Thank you for your feedback!'), 100); + } catch (error) { + setToastMessage(null); + setTimeout(() => setToastMessage('Feedback was not sent.'), 100); + } + }; + return (
- sendCustomEvent('ai_feedback', 'ai', 'good', undefined, { - query, - result: finalResult, - }) - } + onClick={() => handleFeedback('good')} > Answer was helpful{' '} @@ -97,12 +107,7 @@ export function FeatureAi(): JSX.Element {
) : null} + {toastMessage && } {error ?
There was an error: {error['message']}
: null} );