• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

teableio / teable / 8389034572

22 Mar 2024 10:38AM CUT coverage: 26.087% (-2.1%) from 28.208%
8389034572

Pull #487

github

web-flow
Merge 3045b1f94 into a06c6afb1
Pull Request #487: refactor: move zod schema to openapi

2100 of 3363 branches covered (62.44%)

282 of 757 new or added lines in 74 files covered. (37.25%)

224 existing lines in 8 files now uncovered.

25574 of 98035 relevant lines covered (26.09%)

5.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/packages/sdk/src/components/grid-enhancements/hooks/use-grid-column-statistics.ts
1
import { statisticFunc2NameMap } from '@teable/core';
×
NEW
2
import type { IAggregationVo } from '@teable/openapi';
×
3
import { isEmpty, keyBy } from 'lodash';
×
4
import { useEffect, useMemo, useRef, useState } from 'react';
×
5
import type { IColumnStatistics, IGridColumn } from '../..';
×
6
import { useAggregation } from '../../../hooks/use-aggregation';
×
7
import { useFields } from '../../../hooks/use-fields';
×
8
import { useViewId } from '../../../hooks/use-view-id';
×
9
import { statisticsValue2DisplayValue } from '../../../utils';
×
10

×
11
export function useGridColumnStatistics(columns: (IGridColumn & { id: string })[]) {
×
12
  const viewId = useViewId();
×
13
  const fields = useFields({ withHidden: true });
×
14
  const remoteStatistics = useAggregation();
×
15
  const [columnStatistics, setColumnStatistics] = useState<IColumnStatistics>({});
×
16
  const columnsRef = useRef(columns);
×
17
  const fieldsRef = useRef(keyBy(fields, 'id'));
×
18
  columnsRef.current = columns;
×
19

×
20
  fieldsRef.current = useMemo(() => keyBy(fields, 'id'), [fields]);
×
21

×
22
  const getColumnStatistics = (source: IAggregationVo | null) => {
×
23
    if (source == null) return {};
×
24
    const { aggregations } = source;
×
25
    if (isEmpty(aggregations)) return {};
×
26
    const aggregationMap = keyBy(aggregations, 'fieldId');
×
27

×
28
    return columnsRef.current?.reduce((acc, column) => {
×
29
      const { id: columnId } = column;
×
30

×
31
      const columnAggregations = aggregationMap[columnId];
×
32

×
33
      const { total } = columnAggregations ?? {};
×
34

×
35
      if (columnAggregations === null || total === null) {
×
36
        acc[columnId] = null;
×
37
        return acc;
×
38
      }
×
39

×
40
      const field = fieldsRef.current[columnId];
×
41

×
42
      if (total != null && field != null) {
×
43
        const { aggFunc, value } = total;
×
44

×
45
        const displayValue = statisticsValue2DisplayValue(aggFunc, value, field);
×
46
        acc[columnId] = {
×
47
          total: `${statisticFunc2NameMap[aggFunc]} ${displayValue}`,
×
48
        };
×
49
      }
×
50
      return acc;
×
51
    }, {} as IColumnStatistics);
×
52
  };
×
53

×
54
  useEffect(() => {
×
55
    if (remoteStatistics == null || viewId == null) return;
×
56

×
57
    const partialColumnStatistics = getColumnStatistics(remoteStatistics);
×
58
    if (partialColumnStatistics == null) return;
×
59

×
60
    setColumnStatistics(partialColumnStatistics);
×
61
  }, [remoteStatistics, viewId]);
×
62

×
63
  return { columnStatistics };
×
64
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc