-
Phoomparin Mano authored
* revamp the create question component * docs updates * add stories and update styles * prevent switching to visualization when it is not ready * ensure CreateQuestion works in flex parents * add basic e2e tests for CreateQuestion * ensure that switching between editor and visualization works * use the save button for the disabled state * update the question title when the question is saved * implement updating questions in place * rename replaceQuestion * make CreateQuestion props all optional * add background color to modal header * hide the save button if question is dirty * hide save button instead of disabling per design feedback * update e2e tests * revert e2e helpers * add title update assertions * whitespace changes
Phoomparin Mano authored* revamp the create question component * docs updates * add stories and update styles * prevent switching to visualization when it is not ready * ensure CreateQuestion works in flex parents * add basic e2e tests for CreateQuestion * ensure that switching between editor and visualization works * use the save button for the disabled state * update the question title when the question is saved * implement updating questions in place * rename replaceQuestion * make CreateQuestion props all optional * add background color to modal header * hide the save button if question is dirty * hide save button instead of disabling per design feedback * update e2e tests * revert e2e helpers * add title update assertions * whitespace changes
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
CreateQuestion.tsx 3.06 KiB
import { useState } from "react";
import { FlexibleSizeComponent } from "embedding-sdk";
import { useInteractiveQuestionContext } from "embedding-sdk/components/private/InteractiveQuestion/context";
import { SaveQuestionModal } from "metabase/containers/SaveQuestionModal";
import { Button, Flex } from "metabase/ui";
import {
InteractiveQuestion,
type InteractiveQuestionProps,
} from "../InteractiveQuestion";
type CreateQuestionProps = Partial<
Omit<InteractiveQuestionProps, "questionId" | "children">
>;
export const CreateQuestion = ({
onSave,
isSaveEnabled = true,
...props
}: CreateQuestionProps = {}) => {
const [isSaveModalOpen, setSaveModalOpen] = useState(false);
return (
<InteractiveQuestion
{...props}
isSaveEnabled={isSaveEnabled}
onSave={(question, context) => {
if (question) {
setSaveModalOpen(false);
onSave?.(question, context);
}
}}
>
<CreateQuestionDefaultView
isSaveModalOpen={isSaveModalOpen}
setSaveModalOpen={setSaveModalOpen}
/>
</InteractiveQuestion>
);
};
export const CreateQuestionDefaultView = ({
isSaveModalOpen,
setSaveModalOpen,
}: {
isSaveModalOpen: boolean;
setSaveModalOpen: (isOpen: boolean) => void;
}) => {
const [isVisualizationView, setIsVisualizationView] = useState(false);
const {
isSaveEnabled,
question,
originalQuestion,
onSave,
onCreate,
queryResults,
saveToCollectionId,
} = useInteractiveQuestionContext();
// We show "question not found" when the query results is not available in QueryVisualization.
// Don't allow switching to visualization view when it is not yet ready.
const isVisualizationReady = question && queryResults;
return (
<FlexibleSizeComponent>
<Flex w="100%" justify="space-between" pb="lg">
<Flex>
<InteractiveQuestion.Title />
</Flex>
<Flex gap="sm">
{isVisualizationReady && (
<Button
onClick={() => setIsVisualizationView(!isVisualizationView)}
>
Show {isVisualizationView ? "editor" : "visualization"}
</Button>
)}
<InteractiveQuestion.SaveButton
onClick={() => setSaveModalOpen(true)}
/>
</Flex>
</Flex>
{isVisualizationView && (
<Flex h="500px">
<InteractiveQuestion.QuestionVisualization />
</Flex>
)}
{!isVisualizationView && (
<InteractiveQuestion.Editor
onApply={() => setIsVisualizationView(true)}
/>
)}
{/* Refer to the SaveQuestionProvider for context on why we have to do it like this */}
{isSaveEnabled && isSaveModalOpen && question && (
<SaveQuestionModal
question={question}
originalQuestion={originalQuestion ?? null}
opened
closeOnSuccess
onClose={() => setSaveModalOpen(false)}
onCreate={onCreate}
onSave={onSave}
saveToCollectionId={saveToCollectionId}
/>
)}
</FlexibleSizeComponent>
);
};