Skip to content

Commit

Permalink
Code rabbit suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Oct 17, 2024
1 parent b793865 commit f0a0e2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function TransactionListWithPreviews({
});

const { data: previewTransactions, isLoading: isPreviewTransactionsLoading } =
usePreviewTransactions({ options: { isDisabled: isSearching } });
usePreviewTransactions();

const dateFormat = useDateFormat() || 'MM/dd/yyyy';
const dispatch = useDispatch();
Expand Down Expand Up @@ -294,12 +294,7 @@ function TransactionListWithPreviews({
150,
);

const onSearch = useCallback(
(text: string) => {
updateSearchQuery(text);
},
[updateSearchQuery],
);
const onSearch = useCallback(updateSearchQuery, [updateSearchQuery]);

const onOpenTransaction = useCallback(
(transaction: TransactionEntity) => {
Expand Down Expand Up @@ -331,10 +326,14 @@ function TransactionListWithPreviews({
[accountId, account],
);

const transcationsToDisplay = !isSearching
? previewTransactions.concat(transactions)
: transactions;

return (
<TransactionListWithBalances
isLoading={isLoading || isPreviewTransactionsLoading}
transactions={previewTransactions.concat(transactions)}
transactions={transcationsToDisplay}
balance={balanceQueries.balance}
balanceCleared={balanceQueries.cleared}
balanceUncleared={balanceQueries.uncleared}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ export function CategoryTransactions({ category, month }) {
150,
);

const onSearch = useCallback(
text => {
updateSearchQuery(text);
},
[updateSearchQuery],
);
const onSearch = useCallback(updateSearchQuery, [updateSearchQuery]);

const onOpenTransaction = useCallback(
transaction => {
Expand Down
38 changes: 17 additions & 21 deletions packages/loot-core/src/client/data-hooks/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useEffect, useRef, useState, useMemo } from 'react';

import { send } from '../../platform/client/fetch';
import { type Query } from '../../shared/query';
Expand Down Expand Up @@ -84,21 +84,13 @@ export function useTransactions({
};
}

type UsePreviewTransactionsProps = {
options?: {
isDisabled?: boolean;
};
};

type UsePreviewTransactionsResult = {
data: ReadonlyArray<TransactionEntity>;
isLoading: boolean;
error?: Error;
};

export function usePreviewTransactions({
options: { isDisabled } = { isDisabled: false },
}: UsePreviewTransactionsProps = {}): UsePreviewTransactionsResult {
export function usePreviewTransactions(): UsePreviewTransactionsResult {
const [previewTransactions, setPreviewTransactions] = useState<
TransactionEntity[]
>([]);
Expand All @@ -111,32 +103,36 @@ export function usePreviewTransactions({
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | undefined>(undefined);

useEffect(() => {
const scheduleTransactions = useMemo(() => {
if (isSchedulesLoading) {
return;
return [];
}

let isUnmounted = false;

setIsLoading(schedules.length > 0);

// Kick off an async rules application
const schedulesForPreview = schedules.filter(s =>
isForPreview(s, statuses),
);

const baseTransactions = schedulesForPreview.map(schedule => ({
return schedulesForPreview.map(schedule => ({
id: 'preview/' + schedule.id,
payee: schedule._payee,
account: schedule._account,
amount: schedule._amount,
date: schedule.next_date,
schedule: schedule.id,
}));
}, [isSchedulesLoading, schedules, statuses]);

if (baseTransactions?.length) {
useEffect(() => {
let isUnmounted = false;

setIsLoading(scheduleTransactions.length > 0);

if (scheduleTransactions.length) {
Promise.all(
baseTransactions.map(transaction => send('rules-run', { transaction })),
scheduleTransactions.map(transaction =>
send('rules-run', { transaction }),
),
)
.then(newTrans => {
if (!isUnmounted) {
Expand Down Expand Up @@ -168,10 +164,10 @@ export function usePreviewTransactions({
return () => {
isUnmounted = true;
};
}, [isSchedulesLoading, schedules, statuses]);
}, [scheduleTransactions, schedules, statuses]);

return {
data: isDisabled ? [] : previewTransactions,
data: previewTransactions,
isLoading: isLoading || isSchedulesLoading,
error: error || scheduleQueryError,
};
Expand Down

0 comments on commit f0a0e2a

Please sign in to comment.