diff --git a/apps/web/app/s/[surveyId]/components/LinkSurvey.tsx b/apps/web/app/s/[surveyId]/components/LinkSurvey.tsx index f5d4bd1a13..f36db9e4a5 100644 --- a/apps/web/app/s/[surveyId]/components/LinkSurvey.tsx +++ b/apps/web/app/s/[surveyId]/components/LinkSurvey.tsx @@ -50,11 +50,28 @@ export default function LinkSurvey({ const isPreview = searchParams?.get("preview") === "true"; const sourceParam = searchParams?.get("source"); const suId = searchParams?.get("suId"); + const startAt = searchParams?.get("startAt"); + + const isStartAtValid = useMemo(() => { + if (!startAt) return false; + if (survey?.welcomeCard.enabled && startAt === "start") return true; + + const isValid = survey?.questions.some((question) => question.id === startAt); + + // To remove startAt query param from URL if it is not valid: + if (!isValid && typeof window !== "undefined") { + const url = new URL(window.location.href); + url.searchParams.delete("startAt"); + window.history.replaceState({}, "", url.toString()); + } + + return isValid; + }, [survey, startAt]); // pass in the responseId if the survey is a single use survey, ensures survey state is updated with the responseId const [surveyState, setSurveyState] = useState(new SurveyState(survey.id, singleUseId, responseId, userId)); const [activeQuestionId, setActiveQuestionId] = useState( - survey.welcomeCard.enabled ? "start" : survey?.questions[0]?.id + startAt && isStartAtValid ? startAt : survey.welcomeCard.enabled ? "start" : survey?.questions[0]?.id ); const prefillResponseData: TResponseData | undefined = prefillAnswer