-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Conversation
Codenotify: Notifying subscribers in CODENOTIFY files for diff ff09242...1ed9a9a.
|
|
dea3ba5
to
bc0b30a
Compare
bc0b30a
to
08abf25
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work 👏
): dashcard is QuestionDashboardCard { | ||
return "card_id" in dashcard && "card" in dashcard; |
There was a problem hiding this comment.
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.
): 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
?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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"]
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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[]); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 => |
There was a problem hiding this comment.
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:
dashcard => | |
(dashcard): dashcard is QuestionDashboardCard => |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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:
if ("series" in dashcard && Array.isArray(dashcard.series)) { | |
if (isQuestionDashCard(dashcard)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 5e7d3eb
Thanks for the review @kamilmielnik 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥇
@kulyk Did you forget to add a milestone to the issue for this PR? When and where should I add a milestone? |
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:
DashboardCard
is now split intoQuestionDashboardCard
andVirtualDashboardCard
type. The old code was basically combining the two, making some type checks awkward.DashboardCard
is nowActionDashboardCard | 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