Bastien Dewerse
12/14/2023, 9:54 AMremoveLayoutSection
and addLayoutSection
through useDispatchDashboardCommand
inside a CustomDashboardWidgetComponent
While I'm able to remove/add dashboard sections, it takes ages to load (actually it take longer for addLayoutSection
to dispatch than it takes to initially load the complete dashboard with all sections).
But once a section has been loaded once with addLayoutSection
, consecutive loads are almost instant (yet they are stashed after the initial complete dashboard loading so even the first switch should be instant).
Now I tried various things with the dashboard stash, by passing identifiers to removeLayoutSection
but the results are inconsistent, I've managed to magically make it works once (as it: the very first switch toward an unseen yet section was instant), but I can't reproduce it (even though I can see the items in the dashboard stash thanks to the event listeners).
The type description of StashedDashboardItemsId
states:
> Identifier of a stashed dashboard items. When removing one or more item, the caller may decide to 'stash' these items under some identifier. This stashed items can then be used in subsequent command that places items on the layout by providing the stash identifier.
But as far as I'm aware, none of the commands to add things to the dashboard layout accept a stashIdentifier as param (be it addLayoutSection
or addSectionItem
).Bastien Dewerse
12/14/2023, 9:54 AMregister(
_ctx: DashboardContext,
customize: IDashboardCustomizer,
_eventing: IDashboardEventHandling,
) {
_eventing.subscribeToStateChanges((s) => {
const stash = selectStash(s);
console.log(stash);
});
customize
.customWidgets()
.addCustomWidget("sectionManager", SectionManagerWidget);
customize.layout().customizeFluidLayout((layout, customizer) => {
customizer.addSection(
0,
newDashboardSection(
// "Section manager",
undefined,
newDashboardItem(
newCustomWidget("sectionManager", "sectionManager", {
sections: layout.sections,
}),
{
xl: {
gridWidth: 12,
gridHeight: 2,
},
},
),
),
);
});
}
And here is the CustomWidget:
const SectionManagerWidget: CustomDashboardWidgetComponent = (
props: React.PropsWithChildren<
IDashboardWidgetProps & {
widget?: ExtendedDashboardWidget & {
sections?: IDashboardLayoutSection<ExtendedDashboardWidget>[];
};
}
>,
) => {
const allSections = props.widget?.sections;
const [currentIndex, setCurrentIndex] = useState<number>(0);
const addSection = useDispatchDashboardCommand(addLayoutSection);
const removeSection = useDispatchDashboardCommand(removeLayoutSection);
const toggleSection = (
section: IDashboardLayoutSection<ExtendedDashboardWidget>,
) => {
removeSection(1, `section-${currentIndex}`);
addSection(1, section.header, section.items);
setCurrentIndex(allSections?.indexOf(section) ?? 0);
};
useEffect(() => {
if (allSections && allSections.length > 1) {
allSections.forEach((section, i) => {
if (i > 0) {
removeSection(2, `section-${i}`);
}
});
}
}, []);
if (!allSections || allSections.length <= 1) {
return null;
}
return (
<Box display="flex" alignItems="center" justifyContent="space-evenly">
{allSections.map((section, i) => (
<Button key={`section-${i}`} onClick={() => toggleSection(section)}>{`${
section.header?.title || `Section ${i + 1}`
}`}</Button>
))}
</Box>
);
};
Bastien Dewerse
12/14/2023, 10:30 AMBastien Dewerse
12/14/2023, 10:31 AMRadek Novacek
12/14/2023, 12:43 PMBastien Dewerse
12/14/2023, 1:01 PMremoveLayoutSection
and undoLayoutChanges
.
So I don't have the old code anymore although it should be easily reproducible with the above sample.
For anyone interested in the solution: instead of removing unwantend sections then trying to add previously removed ones, I use undoLayoutChanges
to cancel all removals up to the initial dashboard state, and then remove the "new" unwanted ones.Radek Novacek
12/14/2023, 2:16 PM