Skip to content

Commit

Permalink
fix(transfer): #2262 fix auto-deselection of channels
Browse files Browse the repository at this point in the history
  • Loading branch information
pwltr committed Oct 9, 2024
1 parent b3f9f30 commit 6c45948
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/screens/Transfer/SavingsAdvanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ const Channel = ({
}: {
channel: TChannel;
isEnabled: boolean;
onPress: (channel: TChannel) => void;
onPress: () => void;
}): ReactElement => {
const channelName = useLightningChannelName(channel);

return (
<SwitchRow
style={styles.channel}
isEnabled={isEnabled}
onPress={() => onPress(channel)}>
<SwitchRow style={styles.channel} isEnabled={isEnabled} onPress={onPress}>
<Caption13Up style={styles.channelLabel} color="secondary">
{channelName}
</Caption13Up>
Expand All @@ -49,27 +46,33 @@ const SavingsAdvanced = ({
}: TransferScreenProps<'SavingsAdvanced'>): ReactElement => {
const switchUnit = useSwitchUnit();
const { t } = useTranslation('lightning');
const channels = useAppSelector(openChannelsSelector);
const [selected, setSelected] = useState<TChannel[]>(channels);
const openChannels = useAppSelector(openChannelsSelector);
const channelIds = openChannels.map((channel) => channel.channel_id);
const [selected, setSelected] = useState<string[]>(channelIds);

const onToggle = (channel: TChannel): void => {
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];
}
});
};

const onContinue = (): void => {
if (selected.length === channels.length) {
if (selected.length === openChannels.length) {
navigation.navigate('SavingsConfirm');
} else {
navigation.navigate('SavingsConfirm', { channels: selected });
const channels = openChannels.filter((channel) => {
return selected.includes(channel.channel_id);
});
navigation.navigate('SavingsConfirm', { channels });
}
};

const amount = channels
.filter((channel) => selected.includes(channel))
const amount = openChannels
.filter((channel) => selected.includes(channel.channel_id))
.reduce((acc, channel) => acc + channel.balance_sat, 0);

return (
Expand All @@ -93,12 +96,12 @@ const SavingsAdvanced = ({
</BodyM>

<ScrollView contentContainerStyle={styles.channels}>
{Object.values(channels).map((channel) => (
{Object.values(openChannels).map((channel) => (
<Channel
key={channel.channel_id}
channel={channel}
isEnabled={selected.includes(channel)}
onPress={onToggle}
isEnabled={selected.includes(channel.channel_id)}
onPress={(): void => onToggle(channel.channel_id)}
/>
))}
</ScrollView>
Expand Down

0 comments on commit 6c45948

Please sign in to comment.