Skip to content

Commit bc70d4d

Browse files
authored
feat(staking): support multiple main accounts per wallet (#3235)
1 parent 606d67b commit bc70d4d

File tree

1 file changed

+39
-24
lines changed
  • apps/staking/src/components/WalletButton

1 file changed

+39
-24
lines changed

apps/staking/src/components/WalletButton/index.tsx

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -164,43 +164,52 @@ const StakeAccountSelector = ({ children, api }: StakeAccountSelectorProps) => {
164164
refreshInterval: REFRESH_INTERVAL,
165165
});
166166
const accounts = useMemo(() => {
167-
if (data.type === DataStateType.Loaded) {
168-
const main = api.allAccounts.find((account) =>
169-
data.data.integrityStakingPublishers.some((publisher) =>
170-
publisher.stakeAccount?.equals(account),
171-
),
172-
);
173-
const other = api.allAccounts
174-
.filter((account) => account !== main)
175-
.map((account) => ({
176-
account,
177-
id: account.toBase58(),
178-
}));
179-
return { main, other };
180-
} else {
181-
return;
182-
}
167+
return data.type === DataStateType.Loaded
168+
? Object.groupBy(
169+
api.allAccounts.map((account) => ({
170+
id: account.toBase58(),
171+
account,
172+
publisher: data.data.integrityStakingPublishers.find((publisher) =>
173+
publisher.stakeAccount?.equals(account),
174+
)?.publicKey,
175+
})),
176+
({ publisher }) => (publisher === undefined ? "other" : "main"),
177+
)
178+
: undefined;
183179
}, [data, api]);
184180

185181
return accounts === undefined ||
186182
// eslint-disable-next-line unicorn/no-null
187-
(accounts.main === undefined && accounts.other.length === 1) ? null : (
183+
(accounts.main === undefined && accounts.other?.length === 1) ? null : (
188184
<>
189185
<Section>
190186
<SubmenuTrigger>
191187
{children}
192-
<Menu items={accounts.other} className="-mr-20 xs:mr-0">
193-
{accounts.main === undefined ? (
188+
<Menu items={accounts.other ?? []} className="-mr-20 xs:mr-0">
189+
{accounts.main?.[0] === undefined ? (
194190
({ account }) => <AccountMenuItem account={account} api={api} />
195191
) : (
196192
<>
197193
<Section>
198194
<Header className="mx-4 text-sm font-semibold">
199-
Main Account
195+
Main Accounts
200196
</Header>
201-
<AccountMenuItem account={accounts.main} api={api} />
197+
<Collection items={accounts.main}>
198+
{({ account, publisher }) => (
199+
<AccountMenuItem
200+
account={account}
201+
publisher={
202+
accounts.main !== undefined &&
203+
accounts.main.length > 1
204+
? publisher
205+
: undefined
206+
}
207+
api={api}
208+
/>
209+
)}
210+
</Collection>
202211
</Section>
203-
{accounts.other.length > 0 && (
212+
{accounts.other !== undefined && accounts.other.length > 0 && (
204213
<>
205214
<Separator />
206215
<Section>
@@ -228,9 +237,10 @@ const StakeAccountSelector = ({ children, api }: StakeAccountSelectorProps) => {
228237
type AccountMenuItemProps = {
229238
api: States[ApiStateType.Loaded];
230239
account: PublicKey;
240+
publisher?: PublicKey | undefined;
231241
};
232242

233-
const AccountMenuItem = ({ account, api }: AccountMenuItemProps) => (
243+
const AccountMenuItem = ({ account, api, publisher }: AccountMenuItemProps) => (
234244
<MenuItem
235245
onAction={() => {
236246
api.selectAccount(account);
@@ -242,10 +252,15 @@ const AccountMenuItem = ({ account, api }: AccountMenuItemProps) => (
242252
>
243253
<CheckIcon
244254
className={clsx("size-4 text-pythpurple-600", {
245-
invisible: account !== api.account,
255+
invisible: !account.equals(api.account),
246256
})}
247257
/>
248258
<TruncatedKey>{account}</TruncatedKey>
259+
{publisher !== undefined && (
260+
<>
261+
(Publisher: <TruncatedKey>{publisher}</TruncatedKey>)
262+
</>
263+
)}
249264
</MenuItem>
250265
);
251266

0 commit comments

Comments
 (0)