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

Custom Reports: fix table rendering #2192

Merged
merged 13 commits into from
Jan 16, 2024
3 changes: 3 additions & 0 deletions packages/desktop-client/src/components/common/Block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { type HTMLProps, type Ref } from 'react';

import { css } from 'glamor';

import { type CSSProperties } from '../../style';

type BlockProps = HTMLProps<HTMLDivElement> & {
innerRef?: Ref<HTMLDivElement>;
style?: CSSProperties;
};

export function Block(props: BlockProps) {
Expand Down
23 changes: 12 additions & 11 deletions packages/desktop-client/src/components/reports/ChooseGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { LineGraph } from './graphs/LineGraph';
import { StackedBarGraph } from './graphs/StackedBarGraph';
import { ReportTable } from './graphs/tableGraph/ReportTable';
import { ReportTableHeader } from './graphs/tableGraph/ReportTableHeader';
import { ReportTableList } from './graphs/tableGraph/ReportTableList';
import { ReportTableTotals } from './graphs/tableGraph/ReportTableTotals';
import { ReportOptions } from './ReportOptions';

Expand Down Expand Up @@ -42,6 +41,12 @@ export function ChooseGraph({
viewLabels,
}: ChooseGraphProps) {
const balanceTypeOp = ReportOptions.balanceTypeMap.get(balanceType);
const groupByData =
groupBy === 'Category'
? 'groupedData'
: ['Month', 'Year'].includes(groupBy)
? 'monthData'
: 'data';

const saveScrollWidth = value => {
setScrollWidth(!value ? 0 : value);
Expand Down Expand Up @@ -128,16 +133,12 @@ export function ChooseGraph({
saveScrollWidth={saveScrollWidth}
listScrollRef={listScrollRef}
handleScroll={handleScroll}
>
<ReportTableList
data={data}
empty={showEmpty}
monthsCount={months.length}
balanceTypeOp={balanceTypeOp}
mode={mode}
groupBy={groupBy}
/>
</ReportTable>
balanceTypeOp={balanceTypeOp}
groupBy={groupBy}
data={data[groupByData]}
mode={mode}
monthsCount={months.length}
/>
<ReportTableTotals
totalScrollRef={totalScrollRef}
handleScroll={handleScroll}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
} from 'loot-core/src/types/models';

const balanceTypeOptions = [
{ description: 'Payment', format: 'totalDebts' },
{ description: 'Deposit', format: 'totalAssets' },
{ description: 'Net', format: 'totalTotals' },
{ description: 'Payment', format: 'totalDebts' as const },
{ description: 'Deposit', format: 'totalAssets' as const },
{ description: 'Net', format: 'totalTotals' as const },
];

const groupByOptions = [
Expand Down
8 changes: 4 additions & 4 deletions packages/desktop-client/src/components/reports/entities.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type DataEntity = {
data: Array<ItemEntity>;
monthData: Array<MonthData>;
groupedData: Array<GroupedEntity>;
data: GroupedEntity[];
monthData: GroupedEntity[];
groupedData: GroupedEntity[];
legend: LegendEntity[];
startDate: string;
endDate: string;
Expand Down Expand Up @@ -31,7 +31,7 @@ export type MonthData = {
totalTotals: number;
};

type GroupedEntity = {
export type GroupedEntity = {
id: string;
name: string;
date?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
// @ts-strict-ignore
import React, {
type UIEventHandler,
useCallback,
useLayoutEffect,
useRef,
type ReactNode,
type UIEventHandler,
} from 'react';
import { type RefProp } from 'react-spring';

import { type CSSProperties } from '../../../../style';
import { Block } from '../../../common/Block';
import { View } from '../../../common/View';
import { type GroupedEntity } from '../../entities';

import { ReportTableList } from './ReportTableList';
import { ReportTableRow } from './ReportTableRow';

type ReportTableProps = {
saveScrollWidth?: (value: number) => void;
listScrollRef?: RefProp<HTMLDivElement>;
saveScrollWidth: (value: number) => void;
listScrollRef: RefProp<HTMLDivElement>;
handleScroll: UIEventHandler<HTMLDivElement>;
style?: CSSProperties;
children?: ReactNode;
handleScroll?: UIEventHandler<HTMLDivElement>;
groupBy: string;
balanceTypeOp: 'totalDebts' | 'totalTotals' | 'totalAssets';
data: GroupedEntity[];
mode: string;
monthsCount: number;
};

export function ReportTable({
saveScrollWidth,
listScrollRef,
style,
children,
handleScroll,
style,
groupBy,
balanceTypeOp,
data,
mode,
monthsCount,
}: ReportTableProps) {
const contentRef = useRef<HTMLDivElement>(null);

Expand All @@ -33,25 +46,56 @@ export function ReportTable({
}
});

const renderItem = useCallback(
({ item, groupByItem, mode, style, key, monthsCount }) => {
return (
<ReportTableRow
key={key}
item={item}
balanceTypeOp={balanceTypeOp}
groupByItem={groupByItem}
mode={mode}
style={style}
monthsCount={monthsCount}
/>
);
},
[],
);

return (
<View
innerRef={listScrollRef}
onScroll={handleScroll}
id="list"
style={{
overflowY: 'auto',
scrollbarWidth: 'none',
'::-webkit-scrollbar': { display: 'none' },
flex: 1,
flexDirection: 'row',
outline: 'none',
'& .animated .animated-row': { transition: '.25s transform' },
...style,
}}
tabIndex={1}
>
<View>
<div ref={contentRef}>{children}</div>
</View>
<Block
innerRef={listScrollRef}
onScroll={handleScroll}
id="list"
style={{
overflowY: 'auto',
scrollbarWidth: 'none',
'::-webkit-scrollbar': { display: 'none' },
flex: 1,
outline: 'none',
'& .animated .animated-row': { transition: '.25s transform' },
...style,
}}
>
<ReportTableList
data={data}
monthsCount={monthsCount}
mode={mode}
groupBy={groupBy}
renderItem={renderItem}
/>
</Block>
</View>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { type RefProp } from 'react-spring';
import { styles, theme } from '../../../../style';
import { View } from '../../../common/View';
import { Row, Cell } from '../../../table';
import { type MonthData } from '../../entities';
import { type GroupedEntity } from '../../entities';

type ReportTableHeaderProps = {
scrollWidth?: number;
groupBy: string;
interval?: MonthData[];
interval?: GroupedEntity[];
balanceType: string;
headerScrollRef: RefProp<HTMLDivElement>;
handleScroll?: UIEventHandler<HTMLDivElement>;
handleScroll: UIEventHandler<HTMLDivElement>;
};

export function ReportTableHeader({
Expand Down
Loading