From 3fa119746c06434b799d4338d4f31cda3fffb309 Mon Sep 17 00:00:00 2001 From: Philipp Walter Date: Wed, 9 Oct 2024 14:02:16 +0200 Subject: [PATCH] fix(transfer): #2262 fix auto-deselection of channels (#2305) --- src/screens/Transfer/SavingsAdvanced.tsx | 36 +++++++++++++----------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/screens/Transfer/SavingsAdvanced.tsx b/src/screens/Transfer/SavingsAdvanced.tsx index d4c709502..e7980c67e 100644 --- a/src/screens/Transfer/SavingsAdvanced.tsx +++ b/src/screens/Transfer/SavingsAdvanced.tsx @@ -27,15 +27,12 @@ const Channel = ({ }: { channel: TChannel; isEnabled: boolean; - onPress: (channel: TChannel) => void; + onPress: () => void; }): ReactElement => { const channelName = useLightningChannelName(channel); return ( - onPress(channel)}> + {channelName} @@ -50,13 +47,20 @@ const SavingsAdvanced = ({ const switchUnit = useSwitchUnit(); const { t } = useTranslation('lightning'); const channels = useAppSelector(openChannelsSelector); - const [selected, setSelected] = useState(channels); + const channelIds = channels.map((channel) => channel.channel_id); + const [selected, setSelected] = useState(channelIds); - const onToggle = (channel: TChannel): void => { + const selectedChannels = channels.filter((channel) => { + return selected.includes(channel.channel_id); + }); + + const onToggle = (channelId: string): void => { setSelected((prev) => { - return prev.includes(channel) - ? prev.filter((c) => c !== channel) - : [...prev, channel]; + if (prev.includes(channelId)) { + return prev.filter((id) => id !== channelId); + } else { + return [...prev, channelId]; + } }); }; @@ -64,13 +68,13 @@ const SavingsAdvanced = ({ if (selected.length === channels.length) { navigation.navigate('SavingsConfirm'); } else { - navigation.navigate('SavingsConfirm', { channels: selected }); + navigation.navigate('SavingsConfirm', { channels: selectedChannels }); } }; - const amount = channels - .filter((channel) => selected.includes(channel)) - .reduce((acc, channel) => acc + channel.balance_sat, 0); + const amount = selectedChannels.reduce((acc, channel) => { + return acc + channel.balance_sat; + }, 0); return ( @@ -97,8 +101,8 @@ const SavingsAdvanced = ({ onToggle(channel.channel_id)} /> ))}