Skip to content

A local-first personal finance app

Mobile payees - swipe to delete (#5824)

Matiss Janis Aboltins5ac29473f255
diff --git a/packages/desktop-client/src/components/mobile/ActionableGridListItem.tsx b/packages/desktop-client/src/components/mobile/ActionableGridListItem.tsx
new file mode 100644
index 0000000000..e240a37d5e
--- /dev/null
+++ b/packages/desktop-client/src/components/mobile/ActionableGridListItem.tsx
@@ -0,0 +1,140 @@
+import React, { type ReactNode, useRef, useState } from 'react';
+import { GridListItem, type GridListItemProps } from 'react-aria-components';
+import { useSpring, animated, config } from 'react-spring';
+
+import { styles } from '@actual-app/components/styles';
+import { theme } from '@actual-app/components/theme';
+import { useDrag } from '@use-gesture/react';
+
+import { type WithRequired } from 'loot-core/types/util';
+
+type ActionableGridListItemProps<T> = {
+ actions?: ReactNode;
+ actionsBackgroundColor?: string;
+ actionsWidth?: number;
+ children?: ReactNode;
+} & Omit<WithRequired<GridListItemProps<T>, 'value'>, 'children'>;
+
+export function ActionableGridListItem<T extends object>({
+ value,
+ textValue,
+ actions,
+ actionsBackgroundColor = theme.errorBackground,
+ actionsWidth = 100,
+ children,
+ onAction,
+ ...props
+}: ActionableGridListItemProps<T>) {
+ const dragStartedRef = useRef(false);
+ const [isRevealed, setIsRevealed] = useState(false);
+
+ const hasActions = !!actions;
+
+ // Spring animation for the swipe
+ const [{ x }, api] = useSpring(() => ({
+ x: 0,
+ config: config.stiff,
+ }));
+
+ // Handle drag gestures
+ const bind = useDrag(
+ ({ active, movement: [mx], velocity: [vx] }) => {
+ const startPos = isRevealed ? -actionsWidth : 0;
+ const currentX = startPos + mx;
+
+ if (active) {
+ dragStartedRef.current = true;
+ api.start({
+ x: Math.max(-actionsWidth, Math.min(0, currentX)),
+ onRest: () => {
+ dragStartedRef.current = false;
+ },
+ });
+ return;
+ }
+
+ // Snap to revealed (-actionsWidth) or closed (0) based on position and velocity
+ const shouldReveal =
+ currentX < -actionsWidth / 2 ||
+ (vx < -0.5 && currentX < -actionsWidth / 5);
+
+ api.start({
+ x: shouldReveal ? -actionsWidth : 0,
+ onRest: () => {
+ dragStartedRef.current = false;
+ setIsRevealed(shouldReveal);
+ },
+ });
+ },
+ {
+ axis: 'x',
+ from: () => [isRevealed ? -actionsWidth : 0, 0],
+ enabled: hasActions,
+ },
+ );
+
+ // Prevent onAction from firing when dragging or if a drag was started
+ const handleAction = () => {
+ // Only allow action if no drag was started
+ if (!dragStartedRef.current) {
+ onAction?.();
+ }
+ };
+
+ return (
+ <GridListItem
+ {...props}
+ value={value}
+ textValue={textValue}
+ style={{
+ ...styles.mobileListItem,
+ padding: 0,
+ backgroundColor: hasActions
+ ? actionsBackgroundColor
+ : (styles.mobileListItem.backgroundColor ?? 'transparent'),
+ overflow: 'hidden',
+ }}
+ >
+ <animated.div
+ {...(hasActions ? bind() : {})}
+ style={{
+ ...(hasActions
+ ? { transform: x.to(v => `translate3d(${v}px,0,0)`) }
+ : {}),
+ display: 'flex',
+ touchAction: hasActions ? 'pan-y' : 'auto',
+ cursor: hasActions ? 'grab' : 'pointer',
+ }}
+ >
+ {/* Main content */}
+ <div
+ style={{
+ display: 'flex',
+ alignItems: 'center',
+ flex: 1,
+ backgroundColor: theme.tableBackground,
+ minWidth: '100%',
+ padding: 16,
+ }}
+ onClick={handleAction}
+ >
+ {children}
+ </div>
+
+ {/* Actions that appear when swiped */}
+ {hasActions && (
+ <div
+ style={{
+ display: 'flex',
+ justifyContent: 'center',
+ backgroundColor: actionsBackgroundColor,
+ minWidth: actionsWidth,
+ }}
+ >
+ {actions}
+ </div>
+ )}
+ </animated.div>
+ </GridListItem>
+ );
+}
diff --git a/packages/desktop-client/src/components/mobile/payees/MobilePayeesPage.tsx b/packages/desktop-client/src/components/mobile/payees/MobilePayeesPage.tsx
index f8ec9abb1a..a2b441f209 100644
--- a/packages/desktop-client/src/components/mobile/payees/MobilePayeesPage.tsx
+++ b/packages/desktop-client/src/components/mobile/payees/MobilePayeesPage.tsx
@@ -16,12 +16,16 @@ import { MobilePageHeader, Page } from '@desktop-client/components/Page';
import { useNavigate } from '@desktop-client/hooks/useNavigate';
import { usePayeeRuleCounts } from '@desktop-client/hooks/usePayeeRuleCounts';
import { usePayees } from '@desktop-client/hooks/usePayees';
-import { useSelector } from '@desktop-client/redux';
+import { useUndo } from '@desktop-client/hooks/useUndo';
+import { addNotification } from '@desktop-client/notifications/notificationsSlice';
+import { useDispatch, useSelector } from '@desktop-client/redux';
export function MobilePayeesPage() {
const { t } = useTranslation();
+ const dispatch = useDispatch();
const navigate = useNavigate();
const payees = usePayees();
+ const { showUndoNotification } = useUndo();
const [filter, setFilter] = useState('');
const { ruleCounts, isLoading: isRuleCountsLoading } = usePayeeRuleCounts();
const isLoading = useSelector(
@@ -76,6 +80,30 @@ export function MobilePayeesPage() {
[navigate, ruleCounts],
);
+ const handlePayeeDelete = useCallback(
+ async (payee: PayeeEntity) => {
+ try {
+ await send('payees-batch-change', { deleted: [{ id: payee.id }] });
+ showUndoNotification({
+ message: t('Payee “{{name}}” deleted successfully', {
+ name: payee.name,
+ }),
+ });
+ } catch (error) {
+ console.error('Failed to delete payee:', error);
+ dispatch(
+ addNotification({
+ notification: {
+ type: 'error',
+ message: t('Failed to delete payee. Please try again.'),
+ },
+ }),
+ );
+ }
+ },
+ [dispatch, showUndoNotification, t],
+ );
+
return (
<Page header={<MobilePageHeader title={t('Payees')} />} padding={0}>
<View
@@ -108,6 +136,7 @@ export function MobilePayeesPage() {
isRuleCountsLoading={isRuleCountsLoading}
isLoading={isLoading}
onPayeePress={handlePayeePress}
+ onPayeeDelete={handlePayeeDelete}
/>
</Page>
);
diff --git a/packages/desktop-client/src/components/mobile/payees/PayeesList.tsx b/packages/desktop-client/src/components/mobile/payees/PayeesList.tsx
index fa456cce0f..9c59b5a83d 100644
--- a/packages/desktop-client/src/components/mobile/payees/PayeesList.tsx
+++ b/packages/desktop-client/src/components/mobile/payees/PayeesList.tsx
@@ -18,6 +18,7 @@ type PayeesListProps = {
isRuleCountsLoading?: boolean;
isLoading?: boolean;
onPayeePress: (payee: PayeeEntity) => void;
+ onPayeeDelete: (payee: PayeeEntity) => void;
};
export function PayeesList({
@@ -26,6 +27,7 @@ export function PayeesList({
isRuleCountsLoading = false,
isLoading = false,
onPayeePress,
+ onPayeeDelete,
}: PayeesListProps) {
const { t } = useTranslation();
@@ -86,6 +88,7 @@ export function PayeesList({
ruleCount={ruleCounts.get(payee.id) ?? 0}
isRuleCountLoading={isRuleCountsLoading}
onAction={() => onPayeePress(payee)}
+ onDelete={() => onPayeeDelete(payee)}
/>
)}
</GridList>
diff --git a/packages/desktop-client/src/components/mobile/payees/PayeesListItem.tsx b/packages/desktop-client/src/components/mobile/payees/PayeesListItem.tsx
index 7bc0c386f3..2c3ceb6fe2 100644
--- a/packages/desktop-client/src/components/mobile/payees/PayeesListItem.tsx
+++ b/packages/desktop-client/src/components/mobile/payees/PayeesListItem.tsx
@@ -1,26 +1,29 @@
import React, { memo } from 'react';
-import { GridListItem, type GridListItemProps } from 'react-aria-components';
-import { useTranslation } from 'react-i18next';
+import { type GridListItemProps } from 'react-aria-components';
+import { Trans, useTranslation } from 'react-i18next';
+import { Button } from '@actual-app/components/button';
import { SvgBookmark } from '@actual-app/components/icons/v1';
import { SpaceBetween } from '@actual-app/components/space-between';
-import { styles } from '@actual-app/components/styles';
import { theme } from '@actual-app/components/theme';
import { type PayeeEntity } from 'loot-core/types/models';
+import { type WithRequired } from 'loot-core/types/util';
+import { ActionableGridListItem } from '@desktop-client/components/mobile/ActionableGridListItem';
import { PayeeRuleCountLabel } from '@desktop-client/components/payees/PayeeRuleCountLabel';
type PayeesListItemProps = {
- value: PayeeEntity;
ruleCount: number;
isRuleCountLoading?: boolean;
-} & Omit<GridListItemProps<PayeeEntity>, 'value'>;
+ onDelete: () => void;
+} & WithRequired<GridListItemProps<PayeeEntity>, 'value'>;
export const PayeesListItem = memo(function PayeeListItem({
value: payee,
ruleCount,
isRuleCountLoading,
+ onDelete,
...props
}: PayeesListItemProps) {
const { t } = useTranslation();
@@ -30,14 +33,27 @@ export const PayeesListItem = memo(function PayeeListItem({
: payee.name;
return (
- <GridListItem
+ <ActionableGridListItem
id={payee.id}
value={payee}
textValue={label}
- style={styles.mobileListItem}
+ actions={
+ !payee.transfer_acct && (
+ <Button
+ variant="bare"
+ onPress={onDelete}
+ style={{
+ color: theme.errorText,
+ width: '100%',
+ }}
+ >
+ <Trans>Delete</Trans>
+ </Button>
+ )
+ }
{...props}
>
- <SpaceBetween gap={5}>
+ <SpaceBetween gap={5} style={{ flex: 1 }}>
{payee.favorite && (
<SvgBookmark
aria-hidden
@@ -94,6 +110,6 @@ export const PayeesListItem = memo(function PayeeListItem({
</span>
</SpaceBetween>
</SpaceBetween>
- </GridListItem>
+ </ActionableGridListItem>
);
});
diff --git a/upcoming-release-notes/5824.md b/upcoming-release-notes/5824.md
new file mode 100644
index 0000000000..87fd2a775c
--- /dev/null
+++ b/upcoming-release-notes/5824.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [MatissJanis]
+---
+
+Mobile payees - swipe to delete