Create typeguards for widget configurations (#16627)
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`.
This commit is contained in:
+2
-8
@@ -1,3 +1,4 @@
|
||||
import { isPieChartConfiguration } from '@/command-menu/pages/page-layout/utils/isPieChartConfiguration';
|
||||
import { useDateTimeFormat } from '@/localization/hooks/useDateTimeFormat';
|
||||
import { useApolloCoreClient } from '@/object-metadata/hooks/useApolloCoreClient';
|
||||
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
|
||||
@@ -14,7 +15,6 @@ import { isDefined } from 'twenty-shared/utils';
|
||||
import {
|
||||
type BarChartConfiguration,
|
||||
type LineChartConfiguration,
|
||||
type PieChartConfiguration,
|
||||
} from '~/generated/graphql';
|
||||
|
||||
export const useGraphWidgetGroupByQuery = ({
|
||||
@@ -57,13 +57,7 @@ export const useGraphWidgetGroupByQuery = ({
|
||||
throw new Error('Aggregate operation not found');
|
||||
}
|
||||
|
||||
const isPieChart = (
|
||||
config: GroupByChartConfiguration,
|
||||
): config is PieChartConfiguration => {
|
||||
return config.__typename === 'PieChartConfiguration';
|
||||
};
|
||||
|
||||
const groupByQueryVariables = isPieChart(configuration)
|
||||
const groupByQueryVariables = isPieChartConfiguration(configuration)
|
||||
? generateGroupByQueryVariablesFromPieChartConfiguration({
|
||||
objectMetadataItem,
|
||||
objectMetadataItems,
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
import { isAggregateChartConfiguration } from '@/command-menu/pages/page-layout/utils/isAggregateChartConfiguration';
|
||||
import { type PageLayoutWidget } from '@/page-layout/types/PageLayoutWidget';
|
||||
import { assertIsDefinedOrThrow } from 'twenty-shared/utils';
|
||||
import { type AggregateChartConfiguration } from '~/generated/graphql';
|
||||
@@ -16,7 +17,7 @@ export const assertAggregateChartWidgetOrThrow: AssertAggregateChartWidgetOrThro
|
||||
new Error('Widget objectMetadataId is required'),
|
||||
);
|
||||
|
||||
if (widget.configuration?.__typename !== 'AggregateChartConfiguration') {
|
||||
if (!isAggregateChartConfiguration(widget.configuration)) {
|
||||
throw new Error(
|
||||
`Expected AggregateChartConfiguration but got ${widget.configuration?.__typename}`,
|
||||
);
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
import { isBarChartConfiguration } from '@/command-menu/pages/page-layout/utils/isBarChartConfiguration';
|
||||
import { type PageLayoutWidget } from '@/page-layout/types/PageLayoutWidget';
|
||||
import { assertIsDefinedOrThrow } from 'twenty-shared/utils';
|
||||
import { type BarChartConfiguration } from '~/generated/graphql';
|
||||
@@ -17,7 +18,7 @@ export const assertBarChartWidgetOrThrow: AssertBarChartWidgetOrThrow = (
|
||||
new Error('Widget objectMetadataId is required'),
|
||||
);
|
||||
|
||||
if (widget.configuration?.__typename !== 'BarChartConfiguration') {
|
||||
if (!isBarChartConfiguration(widget.configuration)) {
|
||||
throw new Error(
|
||||
`Expected BarChartConfiguration but got ${widget.configuration?.__typename}`,
|
||||
);
|
||||
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
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';
|
||||
@@ -17,7 +18,7 @@ export const assertPieChartWidgetOrThrow: AssertPieChartWidgetOrThrow = (
|
||||
new Error('Widget objectMetadataId is required'),
|
||||
);
|
||||
|
||||
if (widget.configuration?.__typename !== 'PieChartConfiguration') {
|
||||
if (!isPieChartConfiguration(widget.configuration)) {
|
||||
throw new Error(
|
||||
`Expected PieChartConfiguration but got ${widget.configuration?.__typename}`,
|
||||
);
|
||||
|
||||
+4
-5
@@ -1,3 +1,5 @@
|
||||
import { isBarOrLineChartConfiguration } from '@/command-menu/pages/page-layout/utils/isBarOrLineChartConfiguration';
|
||||
import { isPieChartConfiguration } from '@/command-menu/pages/page-layout/utils/isPieChartConfiguration';
|
||||
import {
|
||||
type BarChartConfiguration,
|
||||
type GraphOrderBy,
|
||||
@@ -19,10 +21,7 @@ export const normalizeChartConfigurationFields = (
|
||||
| LineChartConfiguration
|
||||
| PieChartConfiguration,
|
||||
): NormalizedChartConfigurationFields => {
|
||||
if (
|
||||
configuration.__typename === 'BarChartConfiguration' ||
|
||||
configuration.__typename === 'LineChartConfiguration'
|
||||
) {
|
||||
if (isBarOrLineChartConfiguration(configuration)) {
|
||||
return {
|
||||
groupByFieldMetadataId: configuration.primaryAxisGroupByFieldMetadataId,
|
||||
groupBySubFieldName: configuration.primaryAxisGroupBySubFieldName,
|
||||
@@ -31,7 +30,7 @@ export const normalizeChartConfigurationFields = (
|
||||
};
|
||||
}
|
||||
|
||||
if (configuration.__typename === 'PieChartConfiguration') {
|
||||
if (isPieChartConfiguration(configuration)) {
|
||||
return {
|
||||
groupByFieldMetadataId: configuration.groupByFieldMetadataId,
|
||||
groupBySubFieldName: configuration.groupBySubFieldName,
|
||||
|
||||
Reference in New Issue
Block a user