Skip to content

Commit

Permalink
fix: wrong delegation tracks (#2150)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokolova-an authored Aug 20, 2024
1 parent c6cab9a commit 08276df
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/renderer/widgets/DelegateModal/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { nonNullable } from '@shared/lib/utils';
import { treasurySpendsDescription } from './constants';

export const getTreasuryTrackDescription = (asset: Asset | null, description: string, t: TFunction) => {
if (nonNullable(asset) && treasurySpendsDescription[description][asset.symbol]) {
if (nonNullable(asset) && treasurySpendsDescription[description]?.[asset.symbol]) {
return t(description, { value: treasurySpendsDescription[description][asset.symbol], asset: asset.symbol });
}

Expand Down
36 changes: 28 additions & 8 deletions src/renderer/widgets/DelegateModal/model/select-tracks-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { type Account, type Chain } from '@/shared/core';
import { addUniqueItems, removeItemsFromCollection, toAddress } from '@/shared/lib/utils';
import { votingService } from '@/entities/governance';
import { accountUtils, walletModel } from '@/entities/wallet';
import { delegationAggregate, votingAggregate } from '@/features/governance';
import { delegationAggregate, tracksAggregate, votingAggregate } from '@/features/governance';
import { adminTracks, fellowshipTracks, governanceTracks, treasuryTracks } from '../lib/constants';

const formInitiated = createEvent<Chain>();
const formSubmitted = createEvent<{ tracks: number[]; accounts: Account[] }>();
Expand All @@ -17,6 +18,10 @@ const $chain = restore(formInitiated, null);
const $tracks = createStore<number[]>([]).reset(formInitiated);
const $accounts = createStore<Account[]>([]);

const $availableTracks = combine(tracksAggregate.$tracks, (tracks) => {
return Object.keys(tracks);
});

const $votedTracks = combine(
{
votes: votingAggregate.$activeWalletVotes,
Expand Down Expand Up @@ -77,24 +82,39 @@ sample({

sample({
clock: tracksSelected,
source: $tracks,
fn: (tracks, newTracks) => {
if (newTracks.every((t) => tracks.includes(t))) {
return removeItemsFromCollection(tracks, newTracks);
source: { tracks: $tracks, votedTracks: $votedTracks },
fn: ({ tracks, votedTracks }, newTracks) => {
const resultArray = newTracks.filter((num) => !votedTracks.includes(num.toString()));

if (resultArray.every((t) => tracks.includes(t))) {
return removeItemsFromCollection(tracks, resultArray);
}

return addUniqueItems(tracks, newTracks);
return addUniqueItems(tracks, resultArray);
},
target: $tracks,
});

const $tracksGroup = combine($availableTracks, (availableTracks) => {
const availableTrackIds = new Set(availableTracks);

return {
adminTracks: adminTracks.filter((track) => availableTrackIds.has(track.id)),
governanceTracks: governanceTracks.filter((track) => availableTrackIds.has(track.id)),
treasuryTracks: treasuryTracks.filter((track) => availableTrackIds.has(track.id)),
fellowshipTracks: fellowshipTracks.filter((track) => availableTrackIds.has(track.id)),
};
});

export const selectTracksModel = {
$tracks,
$availableTracks,
$votedTracks,
$tracksGroup,

$accounts,
$availableAccounts,

$chain,
$votedTracks,

events: {
formInitiated,
Expand Down
9 changes: 6 additions & 3 deletions src/renderer/widgets/DelegateModal/ui/SelectTracksForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { BaseModal, Button, Checkbox, FootnoteText, Icon, MultiSelect, SmallTitl
import { OperationTitle } from '@/entities/chain';
import { AccountAddress, accountUtils } from '@/entities/wallet';
import { votingAssetModel } from '@/features/governance';
import { adminTracks, allTracks, fellowshipTracks, governanceTracks, treasuryTracks } from '../lib/constants';
import { getTreasuryTrackDescription } from '../lib/helpers';
import { delegateModel } from '../model/delegate-model';
import { selectTracksModel } from '../model/select-tracks-model';
Expand All @@ -22,9 +21,13 @@ export const SelectTrackForm = ({ isOpen, onClose }: Props) => {
const chain = useUnit(selectTracksModel.$chain);
const tracks = useUnit(selectTracksModel.$tracks);
const accounts = useUnit(selectTracksModel.$accounts);
const availableTracks = useUnit(selectTracksModel.$availableTracks);
const votedTracks = useUnit(selectTracksModel.$votedTracks);
const tracksGroup = useUnit(selectTracksModel.$tracksGroup);
const asset = useUnit(votingAssetModel.$votingAsset);

const { adminTracks, governanceTracks, treasuryTracks, fellowshipTracks } = tracksGroup;

return (
<BaseModal
closeButton
Expand All @@ -44,9 +47,9 @@ export const SelectTrackForm = ({ isOpen, onClose }: Props) => {
<div className="flex flex-1 flex-col gap-6 px-5">
<div className="flex gap-3">
<Button
pallet={allTracks.every((track) => tracks.includes(Number(track.id))) ? 'primary' : 'secondary'}
pallet={availableTracks.every((trackId) => tracks.includes(Number(trackId))) ? 'primary' : 'secondary'}
variant="chip"
onClick={() => selectTracksModel.events.tracksSelected(allTracks.map((track) => Number(track.id)))}
onClick={() => selectTracksModel.events.tracksSelected(availableTracks.map((trackId) => Number(trackId)))}
>
{t('governance.addDelegation.group.selectAll')}
</Button>
Expand Down

0 comments on commit 08276df

Please sign in to comment.