From 17ff49941133a44bb64abf19d94006325b22a010 Mon Sep 17 00:00:00 2001 From: Mohamed Muhsin Kamil Date: Thu, 7 Sep 2023 20:18:26 +0200 Subject: [PATCH] refactor: add types to cached accounts & payees --- .../src/components/common/Menu.tsx | 4 ++-- .../src/components/schedules/SchedulesTable.tsx | 16 +++++++--------- .../loot-core/src/client/data-hooks/accounts.tsx | 6 +++--- .../loot-core/src/client/data-hooks/payees.tsx | 13 ++++++++++--- .../loot-core/src/types/models/schedule.d.ts | 2 +- tsconfig.json | 1 + 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/packages/desktop-client/src/components/common/Menu.tsx b/packages/desktop-client/src/components/common/Menu.tsx index 84ced272fbf..6012cf8e33f 100644 --- a/packages/desktop-client/src/components/common/Menu.tsx +++ b/packages/desktop-client/src/components/common/Menu.tsx @@ -23,7 +23,7 @@ function Keybinding({ keyName }: KeybindingProps) { ); } -type MenuItem = { +export type MenuItem = { type?: string | symbol; name: string; disabled?: boolean; @@ -37,7 +37,7 @@ type MenuProps = { header?: ReactNode; footer?: ReactNode; items: Array; - onMenuSelect; + onMenuSelect: (itemName: MenuItem['name']) => void; }; export default function Menu({ diff --git a/packages/desktop-client/src/components/schedules/SchedulesTable.tsx b/packages/desktop-client/src/components/schedules/SchedulesTable.tsx index 79f9f673cee..4cce1a00bc7 100644 --- a/packages/desktop-client/src/components/schedules/SchedulesTable.tsx +++ b/packages/desktop-client/src/components/schedules/SchedulesTable.tsx @@ -7,7 +7,7 @@ import { type ScheduleStatusType, type ScheduleStatuses, } from 'loot-core/src/client/data-hooks/schedules'; -import * as monthUtils from 'loot-core/src/shared/months'; +import { format as monthUtilFormat } from 'loot-core/src/shared/months'; import { getScheduledAmount } from 'loot-core/src/shared/schedules'; import { integerToCurrency } from 'loot-core/src/shared/util'; import { type ScheduleEntity } from 'loot-core/src/types/models'; @@ -16,7 +16,7 @@ import DotsHorizontalTriple from '../../icons/v1/DotsHorizontalTriple'; import Check from '../../icons/v2/Check'; import { colors } from '../../style'; import Button from '../common/Button'; -import Menu from '../common/Menu'; +import Menu, { type MenuItem } from '../common/Menu'; import Text from '../common/Text'; import View from '../common/View'; import PrivacyFilter from '../PrivacyFilter'; @@ -41,7 +41,7 @@ type SchedulesTableProps = { type CompletedScheduleItem = { id: 'show-completed' }; type SchedulesTableItem = ScheduleEntity | CompletedScheduleItem; -export let ROW_HEIGHT = 43; +export const ROW_HEIGHT = 43; function OverflowMenu({ schedule, @@ -52,7 +52,7 @@ function OverflowMenu({ status: ScheduleStatusType; onAction: SchedulesTableProps['onAction']; }) { - let [open, setOpen] = useState(false); + const [open, setOpen] = useState(false); return ( @@ -77,7 +77,7 @@ function OverflowMenu({ onClose={() => setOpen(false)} > { + onMenuSelect={(name: MenuItem['name']) => { onAction(name, schedule.id); setOpen(false); }} @@ -194,7 +194,7 @@ export function SchedulesTable({ (amount > 0 ? '+' : '') + integerToCurrency(Math.abs(amount || 0)); const dateStr = schedule.next_date - ? monthUtils.format(schedule.next_date, dateFormat) + ? monthUtilFormat(schedule.next_date, dateFormat) : null; return ( @@ -252,9 +252,7 @@ export function SchedulesTable({ - {item.next_date - ? monthUtils.format(item.next_date, dateFormat) - : null} + {item.next_date ? monthUtilFormat(item.next_date, dateFormat) : null} diff --git a/packages/loot-core/src/client/data-hooks/accounts.tsx b/packages/loot-core/src/client/data-hooks/accounts.tsx index fd3d4fd002b..9ed2b18497a 100644 --- a/packages/loot-core/src/client/data-hooks/accounts.tsx +++ b/packages/loot-core/src/client/data-hooks/accounts.tsx @@ -12,7 +12,7 @@ function useAccounts() { let AccountsContext = createContext(null); export function AccountsProvider({ children }) { - let data = useAccounts(); + const data: AccountEntity[] = useAccounts(); return ; } @@ -25,9 +25,9 @@ export function useCachedAccounts(): AccountEntity[]; export function useCachedAccounts({ idKey, }: { - idKey: string; + idKey: boolean; }): Record; -export function useCachedAccounts({ idKey }: { idKey?: string } = {}) { +export function useCachedAccounts({ idKey }: { idKey?: boolean } = {}) { const data: AccountEntity[] = useContext(AccountsContext); return idKey && data ? getAccountsById(data) : data; } diff --git a/packages/loot-core/src/client/data-hooks/payees.tsx b/packages/loot-core/src/client/data-hooks/payees.tsx index 2adba3bb71f..bdf73189634 100644 --- a/packages/loot-core/src/client/data-hooks/payees.tsx +++ b/packages/loot-core/src/client/data-hooks/payees.tsx @@ -1,5 +1,6 @@ import React, { createContext, useContext } from 'react'; +import { type PayeeEntity } from '../../types/models'; import q from '../query-helpers'; import { useLiveQuery } from '../query-hooks'; import { getPayeesById } from '../reducers/queries'; @@ -11,7 +12,7 @@ function usePayees() { let PayeesContext = createContext(null); export function PayeesProvider({ children }) { - let data = usePayees(); + const data: PayeeEntity[] = usePayees(); return ; } @@ -20,7 +21,13 @@ export function CachedPayees({ children, idKey }) { return children(data); } -export function useCachedPayees({ idKey }: { idKey? } = {}) { - let data = useContext(PayeesContext); +export function useCachedPayees(): PayeeEntity[]; +export function useCachedPayees({ + idKey, +}: { + idKey: boolean; +}): Record; +export function useCachedPayees({ idKey }: { idKey?: boolean } = {}) { + const data: PayeeEntity[] = useContext(PayeesContext); return idKey && data ? getPayeesById(data) : data; } diff --git a/packages/loot-core/src/types/models/schedule.d.ts b/packages/loot-core/src/types/models/schedule.d.ts index 03dd826213a..57bad1bd52c 100644 --- a/packages/loot-core/src/types/models/schedule.d.ts +++ b/packages/loot-core/src/types/models/schedule.d.ts @@ -13,7 +13,7 @@ export interface ScheduleEntity { // These are special fields that are actually pulled from the // underlying rule - _payee: PayeeEntity; + _payee: PayeeEntity['id']; _account: AccountEntity['id']; _amount: unknown; _amountOp: string; diff --git a/tsconfig.json b/tsconfig.json index 8936baed895..0b575736fe9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,6 +16,7 @@ // TODO: enable once every file is ts // "strict": true, "noFallthroughCasesInSwitch": true, + "noImplicitAny": true, "skipLibCheck": true, "jsx": "preserve", // Check JS files too