import { useCallback, useEffect, useRef, useState } from 'react'; import { FeedbackContainer, VoteButton, VoteButtonReplace, VoteButtonsContainer, FeedbackText, Divider, ButtonStyles } from './styles'; import { trackFeedbackSubmission } from '../../utils/track'; import { Icon, Button } from '@cloudscape-design/components'; enum FeedbackState { START = 'START', UP = 'UP', DOWN = 'DOWN', HIDDEN = 'HIDDEN' } type Feedback = { vote: boolean; page_path: string; id?: string; comment?: string; }; export default function Feedback({ footerRef }) { const [state, setState] = useState(FeedbackState.START); const containerRef = useRef(null); useEffect(() => { const footer = footerRef.current; if (typeof window !== 'undefined') { window.addEventListener('touchmove', hideFeedback); window.addEventListener('scroll', hideFeedback); let prevScrollPos = window.scrollY; // eslint-disable-next-line no-inner-declarations function hideFeedback() { // Scroll variables and calculations const currPos = window.scrollY, footerBuffer = 50, footerVisible = document.body.scrollHeight - footer.offsetHeight + footerBuffer <= currPos + window.innerHeight, scrollUp = prevScrollPos >= currPos, scrollDown = prevScrollPos < currPos, container = containerRef.current; if ( container && ((scrollUp && footerVisible) || (scrollDown && currentState != FeedbackState.DOWN) || (scrollDown && footerVisible)) && !container.classList.contains('close') ) { container.classList.remove('slideIn'); container.classList.add('slideOut'); container.ariaHidden = 'true'; } else if ( container && scrollUp && !container.classList.contains('close') ) { container.classList.remove('slideOut'); container.classList.add('slideIn'); container.ariaHidden = 'false'; } prevScrollPos = currPos; } } }, []); // Feedback Component Customizations const c = { feedbackQuestion: 'Was this page helpful?', yesVoteResponse: 'Thanks for your feedback!', noVoteResponse: 'Thanks for your feedback!', noVoteCTA: 'Can you provide more details?', noVoteCTAButton: 'File an issue on GitHub', ctaIcon: 'external', iconPosition: 'right', buttonLink: 'https://github.com/aws-amplify/docs/issues/new/choose' }; let currentState = state; const onYesVote = useCallback(() => { currentState = FeedbackState.UP; setState(currentState); trackFeedbackSubmission(true); }, []); const onNoVote = useCallback(() => { currentState = FeedbackState.DOWN; setState(currentState); trackFeedbackSubmission(false); }, []); const close = useCallback(() => { if (containerRef.current) { containerRef.current.classList.add('close'), containerRef.current.classList.remove('slideIn'), (containerRef.current.ariaHidden = 'true'); } }, []); return ( {(() => { switch (state) { case 'START': return (
{c.feedbackQuestion}
); case 'UP': return (
{c.yesVoteResponse}
); case 'DOWN': return (
{c.noVoteResponse}

{c.noVoteCTA}

); default: return
; } })()}
); }