import { useMemo, useState } from 'react'; import type { AllowedFormat } from '../ContentGenerator/ContentGenerator'; import { QuestionAnswerChat, type QuestionAnswerChatMessage, } from '../ContentGenerator/QuestionAnswerChat'; import { Modal } from '../Modal'; type UpdatePreferencesProps = { onClose: () => void; term: string; format: AllowedFormat; questionAndAnswers?: QuestionAnswerChatMessage[]; onUpdatePreferences: ( questionAndAnswers: QuestionAnswerChatMessage[], ) => void; isUpdating: boolean; }; export function UpdatePreferences(props: UpdatePreferencesProps) { const { onClose, questionAndAnswers: defaultQuestionAndAnswers, term, format, onUpdatePreferences, isUpdating, } = props; const [questionAnswerChatMessages, setQuestionAnswerChatMessages] = useState< QuestionAnswerChatMessage[] >(defaultQuestionAndAnswers || []); const defaultQuestions = defaultQuestionAndAnswers ?.filter((message) => message.role === 'assistant') .map((message) => ({ question: message.question, possibleAnswers: message.possibleAnswers, })); const hasChangedQuestionAndAnswers = useMemo(() => { return ( JSON.stringify(questionAnswerChatMessages) !== JSON.stringify(defaultQuestionAndAnswers) ); }, [questionAnswerChatMessages, defaultQuestionAndAnswers]); console.log(questionAnswerChatMessages); console.log(defaultQuestionAndAnswers); const userAnswers = questionAnswerChatMessages.filter( (message) => message.role === 'user', ); const hasAnsweredAllQuestions = userAnswers.length === defaultQuestions?.length; return (

Update Preferences

Update your preferences for better content

{ onUpdatePreferences(questionAnswerChatMessages); }} className="-mx-2 h-[400px] border-none p-0" type="update" /> {hasChangedQuestionAndAnswers && hasAnsweredAllQuestions && ( )}
); }