Skip to content
Snippets Groups Projects
Unverified Commit eb6b3356 authored by github-automation-metabase's avatar github-automation-metabase Committed by GitHub
Browse files

refactor(sdk): use ignore flag instead of mounted ref (#50069) (#50149)


* Revert "fix(sdk): put a bandage on the flashing error on static question in strict mode (#49659)"

This reverts commit c57b3a38.

* refactor(sdk): use ignore flag instead of mounted ref

Co-authored-by: default avatarNicolò Pretto <info@npretto.com>
parent df161bb2
No related branches found
No related tags found
No related merge requests found
import { useEffect, useRef, useState } from "react";
import { useEffect, useState } from "react";
import { loadStaticQuestion } from "embedding-sdk/lib/load-static-question";
import type { GenericErrorResponse } from "metabase/lib/errors";
......@@ -17,17 +17,6 @@ export function useLoadStaticQuestion(
questionId: number | null,
parameterValues?: Record<string, string | number>,
) {
// This is a hack around strict mode calling the useEffect twice ->
// the first request is being cancelled and we render a error state for a few renders
// This needs to start at true, it needs to bypass the double use effect of strict mode
// See https://github.com/metabase/metabase/issues/49620 for more details on the issue
const isMounted = useRef(true);
useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);
const [questionState, setQuestionState] = useState<QuestionState>({
loading: false,
card: null,
......@@ -45,6 +34,7 @@ export function useLoadStaticQuestion(
useEffect(() => {
const cancelDeferred = defer();
let ignore = false; // flag to ignore the result if the component unmounts: https://react.dev/learn/you-might-not-need-an-effect#fetching-data
async function loadCardData() {
setQuestionState(state => ({ ...state, loading: true }));
......@@ -60,24 +50,24 @@ export function useLoadStaticQuestion(
cancelDeferred,
});
setQuestionState({
card,
result,
loading: false,
error: null,
});
} catch (error) {
if (!isMounted.current) {
return;
}
if (typeof error === "object") {
if (!ignore) {
setQuestionState({
result: null,
card: null,
card,
result,
loading: false,
error,
error: null,
});
}
} catch (error) {
if (typeof error === "object") {
if (!ignore) {
setQuestionState({
result: null,
card: null,
loading: false,
error,
});
}
} else {
console.error("error loading static question", error);
}
......@@ -89,6 +79,7 @@ export function useLoadStaticQuestion(
return () => {
// cancel pending requests upon unmount
cancelDeferred.resolve();
ignore = true;
};
}, [questionId, parameterValues]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment