Skip to content
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

Improve aggregate footer cell display #9124

Merged
merged 18 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { StyledHeaderDropdownButton } from '@/ui/layout/dropdown/components/StyledHeaderDropdownButton';
import styled from '@emotion/styled';
import { AppTooltip, Tag, TooltipDelay } from 'twenty-ui';
import { formatNumber } from '~/utils/format/number';

const StyledButton = styled(StyledHeaderDropdownButton)`
padding: 0;
Expand All @@ -19,10 +18,7 @@ export const RecordBoardColumnHeaderAggregateDropdownButton = ({
return (
<div id={dropdownId}>
<StyledButton>
<Tag
text={value ? formatNumber(Number(value)) : '-'}
color={'transparent'}
/>
<Tag text={value ? value.toString() : '-'} color={'transparent'} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: removing formatNumber() means numbers won't be properly formatted (e.g. 1000000 will show as '1000000' instead of '1M' or '1,000,000'). Consider keeping number formatting for better readability

<AppTooltip
anchorSelect={`#${dropdownId}`}
content={tooltip}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const useAggregateRecordsForRecordBoardColumn = () => {
skip: !isAggregateQueryEnabled,
});

const { value, label } = computeAggregateValueAndLabel({
const { value, labelWithFieldName } = computeAggregateValueAndLabel({
data,
objectMetadataItem,
fieldMetadataId: recordIndexKanbanAggregateOperation?.fieldMetadataId,
Expand All @@ -84,6 +84,6 @@ export const useAggregateRecordsForRecordBoardColumn = () => {

return {
aggregateValue: isAggregateQueryEnabled ? value : recordCount,
aggregateLabel: isDefined(value) ? label : undefined,
aggregateLabel: isDefined(value) ? labelWithFieldName : undefined,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,80 @@ describe('computeAggregateValueAndLabel', () => {

expect(result).toEqual({
value: 2,
label: 'Sum of amount',
label: 'Sum',
labelWithFieldName: 'Sum of amount',
});
});

it('should handle number field as percentage', () => {
const mockObjectMetadataWithPercentageField: ObjectMetadataItem = {
id: '123',
fields: [
{
id: MOCK_FIELD_ID,
name: 'percentage',
type: FieldMetadataType.Number,
settings: {
type: 'percentage',
},
} as FieldMetadataItem,
],
} as ObjectMetadataItem;

const mockData = {
percentage: {
[AGGREGATE_OPERATIONS.avg]: 0.3,
},
};

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadataWithPercentageField,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.avg,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
});

expect(result).toEqual({
value: '30%',
label: 'Average',
labelWithFieldName: 'Average of percentage',
});
});

it('should handle number field with decimals', () => {
const mockObjectMetadataWithDecimalsField: ObjectMetadataItem = {
id: '123',
fields: [
{
id: MOCK_FIELD_ID,
name: 'decimals',
type: FieldMetadataType.Number,
settings: {
decimals: 2,
},
} as FieldMetadataItem,
],
} as ObjectMetadataItem;

const mockData = {
decimals: {
[AGGREGATE_OPERATIONS.sum]: 0.009,
},
};

const result = computeAggregateValueAndLabel({
data: mockData,
objectMetadataItem: mockObjectMetadataWithDecimalsField,
fieldMetadataId: MOCK_FIELD_ID,
aggregateOperation: AGGREGATE_OPERATIONS.sum,
fallbackFieldName: MOCK_KANBAN_FIELD_NAME,
});

expect(result).toEqual({
value: '0.01',
label: 'Sum',
labelWithFieldName: 'Sum of decimals',
});
});

Expand Down Expand Up @@ -87,7 +160,7 @@ describe('computeAggregateValueAndLabel', () => {

expect(result).toEqual({
value: undefined,
label: 'Sum of amount',
label: 'Sum',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getAggregateOperationLabel } from '@/object-record/record-board/record-
import { AGGREGATE_OPERATIONS } from '@/object-record/record-table/constants/AggregateOperations';
import isEmpty from 'lodash.isempty';
import { FieldMetadataType } from '~/generated-metadata/graphql';
import { formatAmount } from '~/utils/format/formatAmount';
import { formatNumber } from '~/utils/format/number';
import { isDefined } from '~/utils/isDefined';

export const computeAggregateValueAndLabel = ({
Expand Down Expand Up @@ -42,18 +44,40 @@ export const computeAggregateValueAndLabel = ({

const aggregateValue = data[field.name]?.[aggregateOperation];

const value =
isDefined(aggregateValue) && field.type === FieldMetadataType.Currency
? Number(aggregateValue) / 1_000_000
: aggregateValue;
let value;

const label =
if (aggregateOperation === AGGREGATE_OPERATIONS.count) {
value = aggregateValue;
} else if (!isDefined(aggregateValue)) {
value = '-';
Comment on lines +51 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: consider using null instead of '-' to maintain consistent typing across the codebase

} else {
value = Number(aggregateValue);

switch (field.type) {
case FieldMetadataType.Currency: {
value = formatAmount(value / 1_000_000);
break;
}

case FieldMetadataType.Number: {
const { decimals, type } = field.settings ?? {};
value =
type === 'percentage'
? `${formatNumber(value * 100, decimals)}%`
: formatNumber(value, decimals);
break;
}
}
}
const label = getAggregateOperationLabel(aggregateOperation);
const labelWithFieldName =
aggregateOperation === AGGREGATE_OPERATIONS.count
? `${getAggregateOperationLabel(AGGREGATE_OPERATIONS.count)}`
: `${getAggregateOperationLabel(aggregateOperation)} of ${field.name}`;

return {
value,
label,
labelWithFieldName,
};
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { recordGroupDefinitionFamilyState } from '@/object-record/record-group/states/recordGroupDefinitionFamilyState';
import { recordGroupIdsComponentState } from '@/object-record/record-group/states/recordGroupIdsComponentState';
import { RecordGroupDefinition } from '@/object-record/record-group/types/RecordGroupDefinition';
import { recordIndexRecordGroupHideComponentState } from '@/object-record/record-index/states/recordIndexRecordGroupHideComponentState';
import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
import { useRecoilComponentCallbackStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentCallbackStateV2';
import { getSnapshotValue } from '@/ui/utilities/state/utils/getSnapshotValue';
import { useSaveCurrentViewGroups } from '@/views/hooks/useSaveCurrentViewGroups';
import { mapRecordGroupDefinitionsToViewGroups } from '@/views/utils/mapRecordGroupDefinitionsToViewGroups';
import { recordGroupDefinitionToViewGroup } from '@/views/utils/recordGroupDefinitionToViewGroup';
import { useRecoilCallback } from 'recoil';
import { isDefined } from '~/utils/isDefined';

type UseRecordGroupVisibilityParams = {
viewBarId: string;
Expand All @@ -13,10 +18,20 @@ type UseRecordGroupVisibilityParams = {
export const useRecordGroupVisibility = ({
viewBarId,
}: UseRecordGroupVisibilityParams) => {
const recordIndexRecordGroupIdsState = useRecoilComponentCallbackStateV2(
recordGroupIdsComponentState,
);

const recordIndexRecordIdsByGroupFamilyState =
useRecoilComponentCallbackStateV2(
recordIndexRecordIdsByGroupComponentFamilyState,
viewBarId,
);

const objectOptionsDropdownRecordGroupHideState =
useRecoilComponentCallbackStateV2(recordIndexRecordGroupHideComponentState);

const { saveViewGroup } = useSaveCurrentViewGroups(viewBarId);
const { saveViewGroup, saveViewGroups } = useSaveCurrentViewGroups(viewBarId);

const handleVisibilityChange = useRecoilCallback(
({ set }) =>
Expand All @@ -35,14 +50,66 @@ export const useRecordGroupVisibility = ({
);

const handleHideEmptyRecordGroupChange = useRecoilCallback(
({ set }) =>
({ snapshot, set }) =>
async () => {
set(
const updatedRecordGroupDefinitions: RecordGroupDefinition[] = [];
const recordGroupIds = getSnapshotValue(
snapshot,
recordIndexRecordGroupIdsState,
);

const currentHideState = getSnapshotValue(
snapshot,
objectOptionsDropdownRecordGroupHideState,
(currentHideState) => !currentHideState,
);
const newHideState = !currentHideState;

set(objectOptionsDropdownRecordGroupHideState, newHideState);

for (const recordGroupId of recordGroupIds) {
const recordGroup = getSnapshotValue(
snapshot,
recordGroupDefinitionFamilyState(recordGroupId),
);

if (!isDefined(recordGroup)) {
throw new Error(
`Record group with id ${recordGroupId} not found in snapshot`,
);
}

const recordGroupRowIds = getSnapshotValue(
snapshot,
recordIndexRecordIdsByGroupFamilyState(recordGroupId),
);

if (recordGroupRowIds.length > 0) {
continue;
}

const updatedRecordGroup = {
...recordGroup,
isVisible: !newHideState,
};

set(
recordGroupDefinitionFamilyState(recordGroupId),
updatedRecordGroup,
);

updatedRecordGroupDefinitions.push(updatedRecordGroup);
}

saveViewGroups(
mapRecordGroupDefinitionsToViewGroups(updatedRecordGroupDefinitions),
);
},
[objectOptionsDropdownRecordGroupHideState],
[
recordIndexRecordGroupIdsState,
objectOptionsDropdownRecordGroupHideState,
saveViewGroups,
recordIndexRecordIdsByGroupFamilyState,
],
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { recordGroupIdsComponentState } from '@/object-record/record-group/state
import { RecordGroupDefinition } from '@/object-record/record-group/types/RecordGroupDefinition';
import { RecordGroupSort } from '@/object-record/record-group/types/RecordGroupSort';
import { recordGroupSortedInsert } from '@/object-record/record-group/utils/recordGroupSortedInsert';
import { recordIndexRecordGroupHideComponentState } from '@/object-record/record-index/states/recordIndexRecordGroupHideComponentState';
import { recordIndexRecordGroupSortComponentState } from '@/object-record/record-index/states/recordIndexRecordGroupSortComponentState';
import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';

import { createComponentSelectorV2 } from '@/ui/utilities/state/component-state/utils/createComponentSelectorV2';
import { ViewComponentInstanceContext } from '@/views/states/contexts/ViewComponentInstanceContext';
Expand All @@ -29,11 +27,6 @@ export const visibleRecordGroupIdsComponentSelector = createComponentSelectorV2<
instanceId,
}),
);
const hideEmptyRecordGroup = get(
recordIndexRecordGroupHideComponentState.atomFamily({
instanceId,
}),
);

const result: RecordGroupDefinition[] = [];

Expand All @@ -56,24 +49,6 @@ export const visibleRecordGroupIdsComponentSelector = createComponentSelectorV2<
const recordGroupDefinition = get(
recordGroupDefinitionFamilyState(recordGroupId),
);
const recordIds = get(
recordIndexRecordIdsByGroupComponentFamilyState.atomFamily({
instanceId,
familyKey: recordGroupId,
}),
);

if (!isDefined(recordGroupDefinition)) {
continue;
}

if (hideEmptyRecordGroup && recordIds.length === 0) {
continue;
}

if (!recordGroupDefinition.isVisible) {
continue;
}

recordGroupSortedInsert(result, recordGroupDefinition, comparator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { ViewComponentInstanceContext } from '@/views/states/contexts/ViewCompon
export const recordIndexRecordGroupHideComponentState =
createComponentStateV2<boolean>({
key: 'recordIndexRecordGroupHideComponentState',
defaultValue: true,
defaultValue: false,
componentInstanceContext: ViewComponentInstanceContext,
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const RecordTable = () => {
<RecordTableEmptyState />
) : (
<>
<StyledTable className="entity-table-cell" ref={tableBodyRef}>
<StyledTable ref={tableBodyRef}>
<RecordTableHeader />
{!hasRecordGroups ? (
<RecordTableNoRecordGroupBody />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const RecordTableBodyRecordGroupDragDropContextProvider = ({
}: {
children: ReactNode;
}) => {
const { objectNameSingular, recordTableId, objectMetadataItem } =
const { objectNameSingular, objectMetadataItem } =
useRecordTableContextOrThrow();

const { updateOneRecord: updateOneRow } = useUpdateOneRecord({
Expand Down
Loading
Loading