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

Clean up dashboard card types #38444

Merged
merged 11 commits into from
Feb 7, 2024
Merged

Clean up dashboard card types #38444

merged 11 commits into from
Feb 7, 2024

Conversation

kulyk
Copy link
Member

@kulyk kulyk commented Feb 5, 2024

A few changes around the DashboardCard type which should make it easier to write code handling different kinds of dashboard cards.

There're two main changes:

  1. DashboardCard is now split into QuestionDashboardCard and VirtualDashboardCard type. The old code was basically combining the two, making some type checks awkward.
  2. DashboardCard is now ActionDashboardCard | QuestionDashboardCard | VirtualDashboardCard, which should force us to narrow down the dashcards types while working with them (there are type guards)

The rest is fixing existing code to pass the type check

To verify: CI should be green

@kulyk kulyk added no-backport Do not backport this PR to any branch .Team/DashViz Dashboard and Viz team labels Feb 5, 2024
@kulyk kulyk self-assigned this Feb 5, 2024
@metabase-bot metabase-bot bot added the visual Run Percy visual testing label Feb 5, 2024
Copy link
Contributor

github-actions bot commented Feb 5, 2024

Codenotify: Notifying subscribers in CODENOTIFY files for diff ff09242...1ed9a9a.

Notify File(s)
@alxnddr frontend/src/metabase/visualizations/components/ObjectDetail/types.ts
frontend/src/metabase/visualizations/index.ts
frontend/src/metabase/visualizations/visualizations/Heading/Heading.tsx
frontend/src/metabase/visualizations/visualizations/Heading/Heading.unit.spec.tsx
frontend/src/metabase/visualizations/visualizations/LinkViz/LinkViz.tsx
frontend/src/metabase/visualizations/visualizations/LinkViz/LinkViz.unit.spec.tsx

Copy link

replay-io bot commented Feb 5, 2024

Status Complete ↗︎
Commit 1ed9a9a
Results
⚠️ 4 Flaky
2268 Passed

@kulyk kulyk changed the title 🚧 WIP — Clean up dashboard card types Clean up dashboard card types Feb 5, 2024
Copy link
Contributor

@kamilmielnik kamilmielnik left a comment

Choose a reason for hiding this comment

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

Amazing work 👏

Comment on lines 84 to 85
): dashcard is QuestionDashboardCard {
return "card_id" in dashcard && "card" in dashcard;
Copy link
Contributor

Choose a reason for hiding this comment

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

This type guard will return true also for ActionDashboardCard and VirtualDashboardCard - I doin't think we want that.

Suggested change
): dashcard is QuestionDashboardCard {
return "card_id" in dashcard && "card" in dashcard;
): dashcard is QuestionDashboardCard {
return
"card_id" in dashcard &&
"card" in dashcard &&
!isActionDashCard(dashcard) &&
!isVirtualDashCard(dashcard);

Or do we need a less specific type-guard, i.e. isDashboardCard(dashcard): dashcard is DashboardCard?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call, fixed in 0f8540c

@@ -58,7 +64,6 @@ export type BaseDashboardCard = {
visualization_settings?: {
[key: string]: unknown;
virtual_card?: VirtualCard;
link?: LinkCardSettings;
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment is actually for the line above this one

Should we remove virtual_card from BaseDashboardCard["visualization_settings"]?

Copy link
Member Author

@kulyk kulyk Feb 7, 2024

Choose a reason for hiding this comment

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

I'm not yet sure how valuable it would be; it's actually quite convenient for some generic code, like here
I'd keep it for now, and we can always follow up on that later

Copy link
Contributor

Choose a reason for hiding this comment

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

some generic code, like here

Actually this is a good place to use isVirtualDashCard type-guard.

@@ -292,7 +292,7 @@ class DashboardGrid extends Component<DashboardGridProps, DashboardGridState> {

getLayoutForDashCard = (dashcard: BaseDashboardCard) => {
const visualization = getVisualizationRaw([
{ card: (dashcard as DashboardCard).card } as SingleSeries,
{ card: (dashcard as QuestionDashboardCard).card } as SingleSeries,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we avoid the first type cast by using a type-guard?

Alternatively we should be able to use DashboardCard instead of BaseDashboardCard throughout this file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 61c17a7

@@ -114,7 +114,7 @@ export function getDashboardUiParameters(
metadata: Metadata,
): UiParameter[] {
const { parameters, dashcards } = dashboard;
const mappings = getMappings(dashcards as DashboardCard[]);
const mappings = getMappings(dashcards as QuestionDashboardCard[]);
Copy link
Contributor

Choose a reason for hiding this comment

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

We can filter out dashcards using a type-guard to avoid the cast.
Same goes for hasMatchingParameters below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in bcd3dc2

return cards.filter(dashcard => {
return (
return dashCards.filter(
dashcard =>
Copy link
Contributor

Choose a reason for hiding this comment

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

We can replace the cast with this:

Suggested change
dashcard =>
(dashcard): dashcard is QuestionDashboardCard =>

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 1ed9a9a

dashcard.visualization_settings,
),
}),
[dashcard],
);

const cards = useMemo(() => {
if (Array.isArray(dashcard.series)) {
if ("series" in dashcard && Array.isArray(dashcard.series)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use a type guard here:

Suggested change
if ("series" in dashcard && Array.isArray(dashcard.series)) {
if (isQuestionDashCard(dashcard)) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 5e7d3eb

@kamilmielnik kamilmielnik requested a review from a team February 7, 2024 07:51
@kulyk
Copy link
Member Author

kulyk commented Feb 7, 2024

Thanks for the review @kamilmielnik 🙏
I've applied all of your suggestions, please take another look

Copy link
Contributor

@kamilmielnik kamilmielnik left a comment

Choose a reason for hiding this comment

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

🥇

@kamilmielnik kamilmielnik requested a review from a team February 7, 2024 10:27
@kulyk kulyk merged commit 12db70d into master Feb 7, 2024
109 checks passed
@kulyk kulyk deleted the virtual-dashcard-types branch February 7, 2024 10:43
Copy link
Contributor

github-actions bot commented Feb 7, 2024

@kulyk Did you forget to add a milestone to the issue for this PR? When and where should I add a milestone?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-backport Do not backport this PR to any branch .Team/DashViz Dashboard and Viz team visual Run Percy visual testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants