Skip to content

A local-first personal finance app

[Maintenance] Remove usage of a raw variable in AccountAutocomplete component (#5685)

Joel Jeremy Marquezde2966a06cdd
diff --git a/packages/desktop-client/src/components/autocomplete/AccountAutocomplete.tsx b/packages/desktop-client/src/components/autocomplete/AccountAutocomplete.tsx
index 29173fe208..b9ea2fd995 100644
--- a/packages/desktop-client/src/components/autocomplete/AccountAutocomplete.tsx
+++ b/packages/desktop-client/src/components/autocomplete/AccountAutocomplete.tsx
@@ -5,6 +5,7 @@ import React, {
type ComponentPropsWithoutRef,
type ReactElement,
type CSSProperties,
+ useMemo,
} from 'react';
import { useTranslation } from 'react-i18next';
@@ -48,7 +49,43 @@ function AccountList({
renderAccountItem = defaultRenderAccountItem,
}: AccountListProps) {
const { t } = useTranslation();
- let lastItem = null;
+ // Split by onbudget, offbudget, closed and add their indeces relative
+ // to the full list. This is needed to highlight the correct item.
+ const { onBudgetAccounts, offBudgetAccounts, closedAccounts } =
+ useMemo(() => {
+ return items.reduce(
+ (acc, item, index) => {
+ if (item.closed) {
+ acc.closedAccounts.push({
+ ...item,
+ highlightedIndex: index,
+ });
+ } else if (item.offbudget) {
+ acc.offBudgetAccounts.push({
+ ...item,
+ highlightedIndex: index,
+ });
+ } else {
+ acc.onBudgetAccounts.push({
+ ...item,
+ highlightedIndex: index,
+ });
+ }
+ return acc;
+ },
+ {
+ onBudgetAccounts: [] as (AccountAutocompleteItem & {
+ highlightedIndex: number;
+ })[],
+ offBudgetAccounts: [] as (AccountAutocompleteItem & {
+ highlightedIndex: number;
+ })[],
+ closedAccounts: [] as (AccountAutocompleteItem & {
+ highlightedIndex: number;
+ })[],
+ },
+ );
+ }, [items]);
return (
<View>
@@ -59,38 +96,53 @@ function AccountList({
...(!embedded && { maxHeight: 175 }),
}}
>
- {items.map((item, idx) => {
- const showGroup = lastItem
- ? (item.offbudget !== lastItem.offbudget && !item.closed) ||
- (item.closed !== lastItem.closed && !item.offbudget)
- : true;
-
- const group = `${
- item.closed
- ? t('Closed Accounts')
- : item.offbudget
- ? t('Off budget')
- : t('On budget')
- }`;
-
- lastItem = item;
-
- return [
- showGroup ? (
- <Fragment key={group}>
- {renderAccountItemGroupHeader({ title: group })}
- </Fragment>
- ) : null,
- <Fragment key={item.id}>
- {renderAccountItem({
- ...(getItemProps ? getItemProps({ item }) : null),
- item,
- highlighted: highlightedIndex === idx,
- embedded,
- })}
- </Fragment>,
- ];
- })}
+ {onBudgetAccounts.length > 0 && (
+ <Fragment key="On budget">
+ {renderAccountItemGroupHeader({ title: t('On budget') })}
+ </Fragment>
+ )}
+ {onBudgetAccounts.map(item => (
+ <Fragment key={item.id}>
+ {renderAccountItem({
+ ...(getItemProps ? getItemProps({ item }) : {}),
+ item,
+ highlighted: highlightedIndex === item.highlightedIndex,
+ embedded,
+ })}
+ </Fragment>
+ ))}
+
+ {offBudgetAccounts.length > 0 && (
+ <Fragment key="Off budget">
+ {renderAccountItemGroupHeader({ title: t('Off budget') })}
+ </Fragment>
+ )}
+ {offBudgetAccounts.map(item => (
+ <Fragment key={item.id}>
+ {renderAccountItem({
+ ...(getItemProps ? getItemProps({ item }) : {}),
+ item,
+ highlighted: highlightedIndex === item.highlightedIndex,
+ embedded,
+ })}
+ </Fragment>
+ ))}
+
+ {closedAccounts.length > 0 && (
+ <Fragment key="Closed accounts">
+ {renderAccountItemGroupHeader({ title: t('Closed accounts') })}
+ </Fragment>
+ )}
+ {closedAccounts.map(item => (
+ <Fragment key={item.id}>
+ {renderAccountItem({
+ ...(getItemProps ? getItemProps({ item }) : {}),
+ item,
+ highlighted: highlightedIndex === item.highlightedIndex,
+ embedded,
+ })}
+ </Fragment>
+ ))}
</View>
</View>
);
diff --git a/upcoming-release-notes/5685.md b/upcoming-release-notes/5685.md
new file mode 100644
index 0000000000..19cc8e71f0
--- /dev/null
+++ b/upcoming-release-notes/5685.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Remove usage of a raw variable in AccountAutocomplete component