From 4e616693613a642b790068d7ba7a09630391cf37 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Mon, 9 Mar 2026 03:16:08 +0100 Subject: [PATCH] Add option to sort prompts in modal --- .../answer-compatibility-question-button.tsx | 24 ++++++++++---- .../answer-compatibility-question-content.tsx | 33 +++++++++++++------ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/web/components/answers/answer-compatibility-question-button.tsx b/web/components/answers/answer-compatibility-question-button.tsx index 9bbf5012..383b7aec 100644 --- a/web/components/answers/answer-compatibility-question-button.tsx +++ b/web/components/answers/answer-compatibility-question-button.tsx @@ -2,9 +2,10 @@ import clsx from 'clsx' import {User} from 'common/user' import Link from 'next/link' import router from 'next/router' -import {useState} from 'react' +import {useMemo, useState} from 'react' import toast from 'react-hot-toast' import {Button} from 'web/components/buttons/button' +import {compareBySort, CompatibilitySort} from 'web/components/compatibility/sort-widget' import {Col} from 'web/components/layout/col' import {Modal, MODAL_CLASS, SCROLLABLE_MODAL_CLASS} from 'web/components/layout/modal' import {QuestionWithCountType} from 'web/hooks/use-questions' @@ -164,6 +165,15 @@ function AnswerCompatibilityQuestionModal(props: { const {open, setOpen, user, otherQuestions, refreshCompatibilityAll, onClose, fromSignup} = props const [questionIndex, setQuestionIndex] = useState(0) const [showOnboarding, setShowOnboarding] = useState(fromSignup ?? false) + const [sort, setSort] = useState('random') + + const sortedQuestions = useMemo(() => { + const isCore = otherQuestions.some((q) => q.importance_score === 0) + if (isCore) return otherQuestions + return [...otherQuestions].sort((a, b) => { + return compareBySort(a, b, sort) + }) as QuestionWithCountType[] + }, [otherQuestions, sort]) const handleStartQuestions = () => { if (otherQuestions.length === 0) { @@ -198,22 +208,24 @@ function AnswerCompatibilityQuestionModal(props: { /> ) : ( { setOpen(false) }} - isLastQuestion={questionIndex === otherQuestions.length - 1} + isLastQuestion={questionIndex === sortedQuestions.length - 1} onNext={() => { - if (questionIndex === otherQuestions.length - 1) { + if (questionIndex === sortedQuestions.length - 1) { setOpen(false) } else { setQuestionIndex(questionIndex + 1) } }} + sort={sort} + setSort={setSort} /> )} diff --git a/web/components/answers/answer-compatibility-question-content.tsx b/web/components/answers/answer-compatibility-question-content.tsx index aa089984..448d52dc 100644 --- a/web/components/answers/answer-compatibility-question-content.tsx +++ b/web/components/answers/answer-compatibility-question-content.tsx @@ -111,15 +111,26 @@ export function AnswerCompatibilityQuestionContent(props: { onNext?: () => void isLastQuestion: boolean noSkip?: boolean + sort?: CompatibilitySort + setSort?: (sort: CompatibilitySort) => void }) { - const {compatibilityQuestion, user, onSubmit, isLastQuestion, onNext, noSkip, index, total} = - props + const { + compatibilityQuestion, + user, + onSubmit, + isLastQuestion, + onNext, + noSkip, + index, + total, + sort, + setSort, + } = props const t = useT() const [answer, setAnswer] = useState( (props.answer as CompatibilityAnswerSubmitType) ?? getEmptyAnswer(user.id, compatibilityQuestion.id), ) - const [sort, setSort] = useState('random') const [loading, setLoading] = useState(false) const [skipLoading, setSkipLoading] = useState(false) @@ -177,13 +188,15 @@ export function AnswerCompatibilityQuestionContent(props: { )} - + {sort && setSort && ( + + )}
{compatibilityQuestion.question}