Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
780801c4
Commit
780801c4
authored
5 months ago
by
Oisin Coveney
Browse files
Options
Downloads
Patches
Plain Diff
Memoize useChartSettingsSections variables (#49470)
parent
84c1b89e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/hooks.ts
+101
-0
101 additions, 0 deletions
...tions/components/ChartSettings/BaseChartSettings/hooks.ts
with
101 additions
and
0 deletions
frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/hooks.ts
0 → 100644
+
101
−
0
View file @
780801c4
import
{
useMemo
,
useState
}
from
"
react
"
;
import
{
t
}
from
"
ttag
"
;
import
_
from
"
underscore
"
;
import
type
{
Widget
}
from
"
../types
"
;
import
type
{
BaseChartSettingsProps
}
from
"
./types
"
;
// section names are localized
const
DEFAULT_TAB_PRIORITY
=
[
t
`Data`
];
export
const
useChartSettingsSections
=
({
initial
,
widgets
,
}:
Pick
<
BaseChartSettingsProps
,
"
initial
"
|
"
widgets
"
>
)
=>
{
const
[
currentSection
,
setCurrentSection
]
=
useState
<
string
|
null
>
(
initial
?.
section
??
null
,
);
const
sections
:
Record
<
string
,
Widget
[]
>
=
useMemo
(()
=>
{
const
sectionObj
:
Record
<
string
,
Widget
[]
>
=
{};
for
(
const
widget
of
widgets
)
{
if
(
widget
.
widget
&&
!
widget
.
hidden
)
{
sectionObj
[
widget
.
section
]
=
sectionObj
[
widget
.
section
]
||
[];
sectionObj
[
widget
.
section
].
push
(
widget
);
}
}
// Move settings from the "undefined" section in the first tab
if
(
sectionObj
[
"
undefined
"
]
&&
Object
.
values
(
sectionObj
).
length
>
1
)
{
const
extra
=
sectionObj
[
"
undefined
"
];
delete
sectionObj
[
"
undefined
"
];
Object
.
values
(
sectionObj
)[
0
].
unshift
(...
extra
);
}
return
sectionObj
;
},
[
widgets
]);
// This sorts the section radio buttons.
const
sectionNames
=
useMemo
(()
=>
{
const
names
=
Object
.
keys
(
sections
);
const
sectionSortOrder
=
[
"
data
"
,
"
display
"
,
"
axes
"
,
// include all section names so any forgotten sections are sorted to the end
...
names
.
map
(
x
=>
x
.
toLowerCase
()),
];
names
.
sort
((
a
,
b
)
=>
{
const
[
aIdx
,
bIdx
]
=
[
a
,
b
].
map
(
x
=>
sectionSortOrder
.
indexOf
(
x
.
toLowerCase
()),
);
return
aIdx
-
bIdx
;
});
return
names
;
},
[
sections
]);
const
chartSettingCurrentSection
=
useMemo
(
()
=>
currentSection
&&
sections
[
currentSection
]
?
currentSection
:
_
.
find
(
DEFAULT_TAB_PRIORITY
,
name
=>
name
in
sections
)
||
sectionNames
[
0
],
[
currentSection
,
sectionNames
,
sections
],
);
const
visibleWidgets
=
useMemo
(
()
=>
sections
[
chartSettingCurrentSection
]
||
[],
[
chartSettingCurrentSection
,
sections
],
);
const
currentSectionHasColumnSettings
=
useMemo
(
()
=>
visibleWidgets
.
some
((
widget
:
Widget
)
=>
widget
.
id
===
"
column_settings
"
),
[
visibleWidgets
],
);
const
showSectionPicker
=
useMemo
(
()
=>
// don't show section tabs for a single section
sectionNames
.
length
>
1
&&
// hide the section picker if the only widget is column_settings
!
(
visibleWidgets
.
length
===
1
&&
visibleWidgets
[
0
].
id
===
"
column_settings
"
&&
// and this section doesn't have that as a direct child
!
currentSectionHasColumnSettings
),
[
currentSectionHasColumnSettings
,
sectionNames
.
length
,
visibleWidgets
],
);
return
{
sectionNames
,
setCurrentSection
,
currentSectionHasColumnSettings
,
chartSettingCurrentSection
,
showSectionPicker
,
visibleWidgets
,
};
};
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment