Skip to content

Commit

Permalink
fix: manage tokens performance
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Nov 1, 2024
1 parent 7e16463 commit 9420c72
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/app/components/crypto-asset-item/crypto-asset-item-toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useRef, useState, useTransition } from 'react';

Check failure on line 1 in src/app/components/crypto-asset-item/crypto-asset-item-toggle.tsx

View workflow job for this annotation

GitHub Actions / typecheck

'useState' is declared but its value is never read.
import { useDispatch } from 'react-redux';

import { sanitize } from 'dompurify';
Expand Down Expand Up @@ -28,26 +28,37 @@ export function CryptoAssetItemToggle({
const accountIndex = useCurrentAccountIndex();
const dispatch = useDispatch();

const [isChecked, setIsChecked] = useState(isCheckedByDefault);
const switchRef = useRef<HTMLButtonElement>(null);
const [_isPending, transition] = useTransition();

function handleSelection(enabled: boolean) {
setIsChecked(enabled);
dispatch(
manageTokensSlice.actions.userTogglesTokenVisibility({ id: assetId, enabled, accountIndex })
);
transition(() => {
dispatch(
manageTokensSlice.actions.userTogglesTokenVisibility({
id: assetId,
enabled,
accountIndex,
})
);
});
}

const toggle = (
<VStack h="100%" justifyContent="center">
<Switch.Root onCheckedChange={handleSelection} checked={isChecked} id={assetId}>
<Switch.Root
ref={switchRef}
defaultChecked={isCheckedByDefault}
onCheckedChange={handleSelection}
id={assetId}
>
<Switch.Thumb />
</Switch.Root>
</VStack>
);

return (
<Box my="space.02">
<Pressable onClick={() => handleSelection(!isChecked)} data-testid={sanitize(assetId)}>
<Pressable onClick={() => switchRef.current?.click()} data-testid={sanitize(assetId)}>
<ItemLayout
img={icon}
titleLeft={spamFilter(titleLeft)}
Expand Down

0 comments on commit 9420c72

Please sign in to comment.