b0e256221a
We checked for widget types by doing `configuration?.__typename === 'LineChartConfiguration'` which made the code difficult to read. In this PR, I introduce type guards for each widget type. Note: the configuration type is `WidgetConfiguration | FieldsConfiguration` for now but should be changed to `WidgetConfiguration` when @Devessier adds FieldsConfiguration to the backend type `WidgetConfiguration`.
27 lines
893 B
TypeScript
27 lines
893 B
TypeScript
import { isPieChartConfiguration } from '@/command-menu/pages/page-layout/utils/isPieChartConfiguration';
|
|
import { type PageLayoutWidget } from '@/page-layout/types/PageLayoutWidget';
|
|
import { assertIsDefinedOrThrow } from 'twenty-shared/utils';
|
|
import { type PieChartConfiguration } from '~/generated/graphql';
|
|
|
|
type AssertPieChartWidgetOrThrow = (
|
|
widget: PageLayoutWidget,
|
|
) => asserts widget is PageLayoutWidget & {
|
|
objectMetadataId: string;
|
|
configuration: PieChartConfiguration;
|
|
};
|
|
|
|
export const assertPieChartWidgetOrThrow: AssertPieChartWidgetOrThrow = (
|
|
widget: PageLayoutWidget,
|
|
) => {
|
|
assertIsDefinedOrThrow(
|
|
widget.objectMetadataId,
|
|
new Error('Widget objectMetadataId is required'),
|
|
);
|
|
|
|
if (!isPieChartConfiguration(widget.configuration)) {
|
|
throw new Error(
|
|
`Expected PieChartConfiguration but got ${widget.configuration?.__typename}`,
|
|
);
|
|
}
|
|
};
|