-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
♻️ (typescript) convert Balance.jsx to .tsx #3874
base: master
Are you sure you want to change the base?
Changes from 6 commits
92ff91d
2675e91
48d632c
ebeb5da
8229290
b26d36c
5d13180
a6938e6
73316b4
13700ea
7a5f5f1
ac135af
b8cf7f7
5ab74fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||
import React, { useRef } from 'react'; | ||||
import React, { useRef, type ComponentProps } from 'react'; | ||||
import { useTranslation } from 'react-i18next'; | ||||
|
||||
import { useHover } from 'usehooks-ts'; | ||||
|
@@ -7,6 +7,7 @@ import { isPreviewId } from 'loot-core/shared/transactions'; | |||
import { useCachedSchedules } from 'loot-core/src/client/data-hooks/schedules'; | ||||
import { q } from 'loot-core/src/shared/query'; | ||||
import { getScheduledAmount } from 'loot-core/src/shared/schedules'; | ||||
import { type AccountEntity } from 'loot-core/types/models'; | ||||
|
||||
import { useSelectedItems } from '../../hooks/useSelected'; | ||||
import { SvgArrowButtonRight1 } from '../../icons/v2'; | ||||
|
@@ -16,10 +17,23 @@ import { Text } from '../common/Text'; | |||
import { View } from '../common/View'; | ||||
import { PrivacyFilter } from '../PrivacyFilter'; | ||||
import { CellValue, CellValueText } from '../spreadsheet/CellValue'; | ||||
import { type SheetFields, type SheetNames } from '../spreadsheet/index'; | ||||
import { useFormat } from '../spreadsheet/useFormat'; | ||||
import { useSheetValue } from '../spreadsheet/useSheetValue'; | ||||
|
||||
function DetailedBalance({ name, balance, isExactBalance = true }) { | ||||
import { type ReconcilingMessage } from './Reconcile'; | ||||
|
||||
type DetailedBalanceProps = { | ||||
name: string; | ||||
balance: number | null; | ||||
isExactBalance: boolean; | ||||
}; | ||||
|
||||
function DetailedBalance({ | ||||
name, | ||||
balance, | ||||
isExactBalance = true, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
}: DetailedBalanceProps) { | ||||
const format = useFormat(); | ||||
return ( | ||||
<Text | ||||
|
@@ -42,32 +56,40 @@ function DetailedBalance({ name, balance, isExactBalance = true }) { | |||
); | ||||
} | ||||
|
||||
function SelectedBalance({ selectedItems, account }) { | ||||
function SelectedBalance({ | ||||
selectedItems, | ||||
account, | ||||
}: { | ||||
selectedItems: Set<string>; | ||||
account: AccountEntity; | ||||
}) { | ||||
const { t } = useTranslation(); | ||||
|
||||
type SelectedBalanceName = `selected-balance-${string}`; | ||||
cindywu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
const name = `selected-balance-${[...selectedItems].join('-')}`; | ||||
cindywu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
const rows = useSheetValue({ | ||||
name, | ||||
const rows = useSheetValue<'balance', SelectedBalanceName>({ | ||||
name: name as SelectedBalanceName, | ||||
query: q('transactions') | ||||
.filter({ | ||||
id: { $oneof: [...selectedItems] }, | ||||
parent_id: { $oneof: [...selectedItems] }, | ||||
}) | ||||
.select('id'), | ||||
}); | ||||
const ids = new Set((rows || []).map(r => r.id)); | ||||
const ids = new Set(Array.isArray(rows) ? rows.map(r => r.id) : []); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this always returns an array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
complains because useSheetValue doesn't always return an array. It can return a number. |
||||
|
||||
const finalIds = [...selectedItems].filter(id => !ids.has(id)); | ||||
let balance = useSheetValue({ | ||||
name: name + '-sum', | ||||
type SelectedBalanceSumName = `selected-balance-${string}-sum`; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please declare outside the component |
||||
let balance = useSheetValue<'balance', SelectedBalanceName>({ | ||||
name: (name + '-sum') as SelectedBalanceSumName, | ||||
query: q('transactions') | ||||
.filter({ id: { $oneof: finalIds } }) | ||||
.options({ splits: 'all' }) | ||||
.calculate({ $sum: '$amount' }), | ||||
}); | ||||
|
||||
let scheduleBalance = null; | ||||
let scheduleBalance = 0; | ||||
cindywu marked this conversation as resolved.
Show resolved
Hide resolved
cindywu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
const { isLoading, schedules = [] } = useCachedSchedules(); | ||||
|
||||
|
@@ -114,7 +136,7 @@ function SelectedBalance({ selectedItems, account }) { | |||
); | ||||
} | ||||
|
||||
function FilteredBalance({ filteredAmount }) { | ||||
function FilteredBalance({ filteredAmount }: { filteredAmount: number }) { | ||||
cindywu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
const { t } = useTranslation(); | ||||
|
||||
return ( | ||||
|
@@ -126,34 +148,58 @@ function FilteredBalance({ filteredAmount }) { | |||
); | ||||
} | ||||
|
||||
function MoreBalances({ balanceQuery }) { | ||||
function MoreBalances({ | ||||
balanceQuery, | ||||
}: { | ||||
balanceQuery: ComponentProps<typeof ReconcilingMessage>['balanceQuery']; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please extract to a Props type and this should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What makes you say that this should be a I am seeing this in a different file, so I updated it to that for now.
|
||||
}) { | ||||
const { t } = useTranslation(); | ||||
|
||||
const cleared = useSheetValue({ | ||||
name: balanceQuery.name + '-cleared', | ||||
type SelectedBalanceClearedName = `balance-query-${string}-cleared`; | ||||
cindywu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
const cleared = useSheetValue<'balance', SelectedBalanceClearedName>({ | ||||
name: (balanceQuery.name + '-cleared') as SelectedBalanceClearedName, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works. There must be a better way to write this, but I can't figure out how rn. So, I just "made it work"...
cindywu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
query: balanceQuery.query.filter({ cleared: true }), | ||||
}); | ||||
const uncleared = useSheetValue({ | ||||
name: balanceQuery.name + '-uncleared', | ||||
|
||||
type SelectedBalanceUnclearedName = `balance-query-${string}-uncleared`; | ||||
cindywu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
const uncleared = useSheetValue<'balance', SelectedBalanceUnclearedName>({ | ||||
name: (balanceQuery.name + '-uncleared') as SelectedBalanceUnclearedName, | ||||
query: balanceQuery.query.filter({ cleared: false }), | ||||
}); | ||||
|
||||
return ( | ||||
<View style={{ flexDirection: 'row' }}> | ||||
<DetailedBalance name={t('Cleared total:')} balance={cleared} /> | ||||
<DetailedBalance name={t('Uncleared total:')} balance={uncleared} /> | ||||
<DetailedBalance | ||||
name={t('Cleared total:')} | ||||
balance={cleared} | ||||
isExactBalance | ||||
/> | ||||
<DetailedBalance | ||||
name={t('Uncleared total:')} | ||||
balance={uncleared} | ||||
isExactBalance | ||||
/> | ||||
</View> | ||||
); | ||||
} | ||||
|
||||
type BalancesProps = { | ||||
balanceQuery: ComponentProps<typeof ReconcilingMessage>['balanceQuery']; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What makes you say that? Do you have a suggested edit? I am seeing this in a different file.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one way I could think of to set it as a
The root issue seems to be in
Possibly we could refactor this removing
|
||||
showExtraBalances: boolean; | ||||
onToggleExtraBalances: () => void; | ||||
account: AccountEntity; | ||||
isFiltered: boolean; | ||||
filteredAmount: number; | ||||
}; | ||||
|
||||
export function Balances({ | ||||
balanceQuery, | ||||
showExtraBalances, | ||||
onToggleExtraBalances, | ||||
account, | ||||
isFiltered, | ||||
filteredAmount, | ||||
}) { | ||||
}: BalancesProps) { | ||||
const selectedItems = useSelectedItems(); | ||||
const buttonRef = useRef(null); | ||||
const isButtonHovered = useHover(buttonRef); | ||||
|
@@ -177,7 +223,14 @@ export function Balances({ | |||
paddingBottom: 1, | ||||
}} | ||||
> | ||||
<CellValue binding={{ ...balanceQuery, value: 0 }} type="financial"> | ||||
<CellValue | ||||
binding={{ | ||||
name: balanceQuery.name as SheetFields<SheetNames>, | ||||
query: balanceQuery.query, | ||||
value: 0, | ||||
}} | ||||
type="financial" | ||||
> | ||||
{props => ( | ||||
<CellValueText | ||||
{...props} | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
category: Maintenance | ||
authors: [cindywu] | ||
--- | ||
|
||
Convert Balance.jsx to tsx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be non-nullable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!