import clsx from "clsx"; import React, { useState } from "react"; import { useLocation } from "@docusaurus/router"; import { AnimatePresence, motion } from "framer-motion"; type Props = { className?: string; }; // users can submit rating(numbers displaying as a emoji) feedback and besides that they can also submit optional text feedback // if they submit rating feedback, we show the text feedback input // after they submit text feedback, we show a thank you message export const DocSurveyWidget = ({ className }: Props) => { const refWidget = React.useRef(null); const location = useLocation(); // users can change their rating feedback, so we need to keep track of the survey response const [survey, setSurvey] = useState(null); // if the user submits rating feedback, we show the text feedback input const [isSurveyTextVisible, setIsSurveyTextVisible] = useState(false); // if the user submits text feedback, we show a thank you message const [isFinished, setIsFinished] = useState(false); // the user can change their rating feedback, so we need to keep track of the selected option const [selectedOption, setSelectedOption] = useState( null, ); const handleSurveyOptionClick = async (option: SurveyOption) => { setSelectedOption(option); setIsSurveyTextVisible(true); // setTimeout is needed because the text view has a transition // we need to scroll to the bottom after the transition is finished so that the text input is visible setTimeout(() => { refWidget.current?.scrollIntoView({ behavior: "smooth", }); }, 150); if (survey) { const data = await updateSurvey({ surveyId: survey.id, body: { response: option }, }); if (!data) return; setSurvey(data); } else { const data = await createSurvey({ body: { response: option, entityId: location.pathname, }, }); if (!data) return; setSurvey(data); } }; const handleSurveyTextSubmit = async (text: string) => { if (text.trim() === "") { return; } const data = await updateSurvey({ surveyId: survey.id, body: { response: selectedOption, responseText: text }, }); if (!data) return; setSurvey(data); // when the user submits text feedback, we show a thank you message setIsFinished(true); // reset the survey after N seconds so that the user can submit another survey setTimeout(() => { setSelectedOption(null); setSurvey(null); setIsFinished(false); setIsSurveyTextVisible(false); }, 3000); }; return (
{isFinished ? ( ) : ( <> {isSurveyTextVisible && ( )} )}
); }; const SurveyOptions = (props: { className?: string; options: { value: SurveyOption; img: string; }[]; selectedOption: SurveyOption | null; onOptionClick: (option: SurveyOption) => void; }) => { const hasSelectedOption = !!props.selectedOption; return (
Was this helpful?
{props.options.map(({ value, img }) => { const isSelected = props.selectedOption === value; return ( ); })}
); }; const SurveyText = (props: { className?: string; onSubmit: (text: string) => void; }) => { const [text, setText] = useState(""); return (
{ e.preventDefault(); props.onSubmit(text); }} >