fix: prevent empty selector error in SurveyLoadingAnimation

- Add type guard for isWelcomeCardEnabled to ensure it's a boolean
- Add validation before querySelectorAll to prevent empty selector errors
- Add guard in checkMediaLoaded callback for defensive programming

Fixes FORMBRICKS-K3
This commit is contained in:
Cursor Agent
2026-02-14 15:05:21 +00:00
parent 08ac490512
commit 0cdb11a445

View File

@@ -20,10 +20,20 @@ export const SurveyLoadingAnimation = ({
const [isMediaLoaded, setIsMediaLoaded] = useState(false); // Tracks if all media are fully loaded
const [isSurveyPackageLoaded, setIsSurveyPackageLoaded] = useState(false); // Tracks if the survey package has been loaded into the DOM
const isReadyToTransition = isMediaLoaded && minTimePassed && isBackgroundLoaded;
const cardId = isWelcomeCardEnabled ? `questionCard--1` : `questionCard-0`;
// Ensure cardId is always a valid string by explicitly checking for boolean type
const cardId =
typeof isWelcomeCardEnabled === "boolean" && isWelcomeCardEnabled
? `questionCard--1`
: `questionCard-0`;
// Function to check if all media elements (images and iframes) within the survey card are loaded
const checkMediaLoaded = useCallback(() => {
// Guard against empty cardId
if (!cardId) {
setIsMediaLoaded(true);
return;
}
const cardElement = document.getElementById(cardId);
const images = cardElement ? Array.from(cardElement.getElementsByTagName("img")) : [];
@@ -37,6 +47,12 @@ export const SurveyLoadingAnimation = ({
useEffect(() => {
if (!isSurveyPackageLoaded) return;
// Guard against empty cardId to prevent "'' is not a valid selector" error
if (!cardId) {
setIsMediaLoaded(true);
return;
}
checkMediaLoaded();
const mediaElements = document.querySelectorAll(`#${cardId} img, #${cardId} iframe`);