Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup parameters popover #42107

Merged
merged 7 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 0 additions & 116 deletions frontend/src/metabase/dashboard/components/ParametersPopover.jsx

This file was deleted.

100 changes: 100 additions & 0 deletions frontend/src/metabase/dashboard/components/ParametersPopover.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* eslint-disable react/prop-types */
import styled from "@emotion/styled";
import cx from "classnames";
import { t } from "ttag";
import _ from "underscore";

import CS from "metabase/css/core/index.css";
import {
OptionItemDescription,
OptionItemRoot,
OptionItemTitle,
} from "metabase/dashboard/components/ParametersPopover.styled";
import {
getDashboardParameterSections,
getDefaultOptionForParameterSection,
} from "metabase/parameters/utils/dashboard-options";
import { Icon } from "metabase/ui";
import type { ParameterMappingOptions } from "metabase-types/api";

const PopoverBody = styled.div`
max-width: 300px;
`;

interface ParametersPopoverProps {
onClose: () => void;
onAddParameter: (parameter: ParameterMappingOptions) => void;
}

const defaultOptionForParameterSection = getDefaultOptionForParameterSection();
const PARAMETER_SECTIONS = getDashboardParameterSections();

type ParameterSection = typeof PARAMETER_SECTIONS[number];

export const ParametersPopover = ({
onClose,
onAddParameter,
}: ParametersPopoverProps) => {
return (
<ParameterOptionsSectionsPane
sections={PARAMETER_SECTIONS}
onSelectSection={selectedSection => {
const parameterSection = _.findWhere(PARAMETER_SECTIONS, {
id: selectedSection.id,
});

if (parameterSection) {
const defaultOption =
defaultOptionForParameterSection[parameterSection.id];

if (defaultOption) {
onAddParameter(defaultOption);
}
onClose();
}
}}
/>
);
};

const ParameterOptionsSection = ({
section,
onClick,
}: {
section: ParameterSection;
onClick: () => void;
}) => (
<OptionItemRoot onClick={onClick}>
<OptionItemTitle
className={cx(CS.textBold, CS.flex, CS.alignCenter)}
style={{ marginBottom: 4 }}
>
<Icon size="16" name="label" className={CS.mr1} />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescript showed that getParameterIconName expected parameter object and not id, so it never worked as intended and nobody noticed. I just hardcoded the fallback value from getParameterIconName

{section.name}
</OptionItemTitle>
<OptionItemDescription>{section.description}</OptionItemDescription>
</OptionItemRoot>
);

const ParameterOptionsSectionsPane = ({
sections,
onSelectSection,
}: {
sections: ParameterSection[];
onSelectSection: (section: ParameterSection) => void;
}) => (
<PopoverBody className={CS.pb2}>
<h3
className={cx(CS.pb2, CS.pt3, CS.px3)}
>{t`What do you want to filter?`}</h3>
<ul>
{sections.map(section => (
<ParameterOptionsSection
key={section.id}
section={section}
onClick={() => onSelectSection(section)}
/>
))}
</ul>
</PopoverBody>
);