Skip to content

Commit

Permalink
Custom reports header fix (#2085)
Browse files Browse the repository at this point in the history
* tablechanges

* notes

* trigger rerun

* fixes

* lint fixes
  • Loading branch information
carkom authored Dec 26, 2023
1 parent b39d106 commit edbcaba
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 59 deletions.
22 changes: 17 additions & 5 deletions packages/desktop-client/src/components/reports/ChooseGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,19 @@ function ChooseGraph({
const listScrollRef = useRef<HTMLDivElement>(null);
const totalScrollRef = useRef<HTMLDivElement>(null);

const handleScrollTotals = scroll => {
headerScrollRef.current.scrollLeft = scroll.target.scrollLeft;
listScrollRef.current.scrollLeft = scroll.target.scrollLeft;
const handleScroll = scroll => {
if (scroll.target.id === 'header') {
totalScrollRef.current.scrollLeft = scroll.target.scrollLeft;
listScrollRef.current.scrollLeft = scroll.target.scrollLeft;
}
if (scroll.target.id === 'total') {
headerScrollRef.current.scrollLeft = scroll.target.scrollLeft;
listScrollRef.current.scrollLeft = scroll.target.scrollLeft;
}
if (scroll.target.id === 'list') {
headerScrollRef.current.scrollLeft = scroll.target.scrollLeft;
totalScrollRef.current.scrollLeft = scroll.target.scrollLeft;
}
};

if (graphType === 'AreaGraph') {
Expand Down Expand Up @@ -96,14 +106,16 @@ function ChooseGraph({
<View>
<ReportTableHeader
headerScrollRef={headerScrollRef}
interval={mode === 'time' && months}
handleScroll={handleScroll}
interval={mode === 'time' && data.monthData}
scrollWidth={scrollWidth}
groupBy={groupBy}
balanceType={balanceType}
/>
<ReportTable
saveScrollWidth={saveScrollWidth}
listScrollRef={listScrollRef}
handleScroll={handleScroll}
>
<ReportTableList
data={data}
Expand All @@ -116,7 +128,7 @@ function ChooseGraph({
</ReportTable>
<ReportTableTotals
totalScrollRef={totalScrollRef}
handleScrollTotals={handleScrollTotals}
handleScroll={handleScroll}
scrollWidth={scrollWidth}
data={data}
mode={mode}
Expand Down
12 changes: 10 additions & 2 deletions packages/desktop-client/src/components/reports/ReportTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { useLayoutEffect, useRef, type ReactNode } from 'react';
import React, {
type UIEventHandler,
useLayoutEffect,
useRef,
type ReactNode,
} from 'react';
import { type RefProp } from 'react-spring';

import { type CSSProperties } from '../../style';
Expand All @@ -9,13 +14,15 @@ type ReportTableProps = {
listScrollRef?: RefProp<HTMLDivElement>;
style?: CSSProperties;
children?: ReactNode;
handleScroll?: UIEventHandler<HTMLDivElement>;
};

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

Expand All @@ -28,6 +35,8 @@ export default function ReportTable({
return (
<View
innerRef={listScrollRef}
onScroll={handleScroll}
id="list"
style={{
overflowY: 'auto',
scrollbarWidth: 'none',
Expand All @@ -38,7 +47,6 @@ export default function ReportTable({
...style,
}}
tabIndex={1}
data-testid="table"
>
<View>
<div ref={contentRef}>{children}</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
import React, { type Ref } from 'react';

import * as d from 'date-fns';
import React, { type UIEventHandler } from 'react';
import { type RefProp } from 'react-spring';

import { styles, theme } from '../../style';
import View from '../common/View';
import { Row, Cell } from '../table';

import { type Month } from './entities';
import { type MonthData } from './entities';

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

export default function ReportTableHeader({
function ReportTableHeader({
scrollWidth,
groupBy,
interval,
balanceType,
headerScrollRef,
handleScroll,
}: ReportTableHeaderProps) {
return (
<View
innerRef={headerScrollRef}
<Row
collapsed={true}
style={{
overflowX: 'auto',
scrollbarWidth: 'none',
'::-webkit-scrollbar': { display: 'none' },
justifyContent: 'center',
borderTopWidth: 1,
borderBottomWidth: 1,
borderColor: theme.tableBorder,
color: theme.tableHeaderText,
backgroundColor: theme.tableHeaderBackground,
fontWeight: 600,
}}
>
<Row
collapsed={true}
<View
innerRef={headerScrollRef}
onScroll={handleScroll}
id="header"
style={{
color: theme.tableHeaderText,
backgroundColor: theme.tableHeaderBackground,
fontWeight: 600,
overflowX: 'auto',
scrollbarWidth: 'none',
'::-webkit-scrollbar': { display: 'none' },
flexDirection: 'row',
flex: 1,
}}
>
<Cell
style={{
minWidth: 125,
width: 120,
flexShrink: 0,
...styles.tnum,
}}
value={groupBy}
width="flex"
/>
{interval
? interval.map((header, index) => {
Expand All @@ -60,8 +65,7 @@ export default function ReportTableHeader({
...styles.tnum,
}}
key={index}
// eslint-disable-next-line rulesdir/typography
value={d.format(d.parseISO(`${header}-01`), "MMM ''yy")}
value={header.date}
width="flex"
/>
);
Expand Down Expand Up @@ -102,8 +106,9 @@ export default function ReportTableHeader({
value="Average"
width="flex"
/>
{scrollWidth > 0 && <Cell width={scrollWidth} />}
</Row>
</View>
</View>
</Row>
);
}

export default ReportTableHeader;
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ const TableRow = memo(
>
<Cell
value={item[groupByItem]}
width="flex"
title={item[groupByItem].length > 12 && item[groupByItem]}
style={{
minWidth: 125,
width: 120,
flexShrink: 0,
...styles.tnum,
}}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { type UIEventHandler } from 'react';
import React, { type UIEventHandler, useLayoutEffect, useState } from 'react';
import { type RefProp } from 'react-spring';

import {
Expand All @@ -20,45 +20,63 @@ type ReportTableTotalsProps = {
mode: string;
monthsCount: number;
totalScrollRef: RefProp<HTMLDivElement>;
handleScrollTotals: UIEventHandler<HTMLDivElement>;
handleScroll: UIEventHandler<HTMLDivElement>;
};

export default function ReportTableTotals({
function ReportTableTotals({
data,
scrollWidth,
balanceTypeOp,
mode,
monthsCount,
totalScrollRef,
handleScrollTotals,
handleScroll,
}: ReportTableTotalsProps) {
const [scrollWidthTotals, setScrollWidthTotals] = useState(0);

useLayoutEffect(() => {
if (totalScrollRef.current) {
const [parent, child] = [
totalScrollRef.current.offsetParent
? totalScrollRef.current.parentElement.scrollHeight
: 0,
totalScrollRef.current ? totalScrollRef.current.scrollHeight : 0,
];
setScrollWidthTotals(parent > 0 && child > 0 && parent - child);
}
});

const average = amountToInteger(data[balanceTypeOp]) / monthsCount;
return (
<View
innerRef={totalScrollRef}
onScroll={handleScrollTotals}
<Row
collapsed={true}
height={32 + scrollWidthTotals}
style={{
overflowX: 'auto',
borderTopWidth: 1,
borderColor: theme.tableBorder,
justifyContent: 'center',
color: theme.tableHeaderText,
backgroundColor: theme.tableHeaderBackground,
fontWeight: 600,
}}
>
<Row
collapsed={true}
<View
innerRef={totalScrollRef}
onScroll={handleScroll}
id="total"
style={{
color: theme.tableHeaderText,
backgroundColor: theme.tableHeaderBackground,
fontWeight: 600,
overflowX: 'auto',
flexDirection: 'row',
flex: 1,
}}
>
<Cell
style={{
minWidth: 125,
width: 120,
flexShrink: 0,
...styles.tnum,
}}
value="Totals"
width="flex"
/>
{mode === 'time'
? data.monthData.map(item => {
Expand Down Expand Up @@ -133,9 +151,9 @@ export default function ReportTableTotals({
width="flex"
privacyFilter
/>

{scrollWidth > 0 && <Cell width={scrollWidth} />}
</Row>
</View>
</View>
</Row>
);
}

export default ReportTableTotals;
9 changes: 3 additions & 6 deletions packages/desktop-client/src/components/reports/entities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,16 @@ export type MonthData = {
totalTotals: number;
};

/* this will be used in a future PR
export type GroupedEntity = {
type GroupedEntity = {
id: string;
name: string;
date?: string;
monthData: Array<MonthData>;
categories?: Array<ItemEntity>;
monthData: MonthData[];
categories?: ItemEntity[];
totalAssets: number;
totalDebts: number;
totalTotals: number;
};
*/

export type Month = {
month: string;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2085.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [carkom]
---

Realign and fix header/totals row for table graph in custom reports

0 comments on commit edbcaba

Please sign in to comment.