Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically perform swap estimation on user input #1920

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Trace } from './trace';
import { Metadata, ValueView } from '@penumbra-zone/protobuf/penumbra/core/asset/v1/asset_pb';
import { ValueViewComponent } from '@penumbra-zone/ui-deprecated/components/ui/value';
import { ArrowDown, ArrowUp } from 'lucide-react';
import { ArrowRight } from 'lucide-react';

export const Traces = ({
traces,
Expand All @@ -29,17 +29,14 @@

<div className='mt-4 flex overflow-auto [scrollbar-width:thin]'>
<div className='mx-2 w-min grow'>
<div className='-mx-2 -mb-8 flex justify-between'>
<div className='flex flex-col items-start gap-2'>
<ValueViewComponent view={input} size='sm' />
<ArrowDown size={17} className='relative z-10' />
</div>
<div className='flex flex-col items-end gap-2'>
<ValueViewComponent view={output} size='sm' />
<ArrowUp size={17} className='relative z-10' />
<div className='relative flex items-center justify-between'>
<ValueViewComponent view={input} size='sm' />
<div className='absolute left-1/2 -translate-x-1/2 flex items-center'>

Check warning on line 34 in apps/minifront/src/components/swap/swap-form/simulate-swap-result/traces/index.tsx

View workflow job for this annotation

GitHub Actions / Lint

Invalid Tailwind CSS classnames order
<ArrowRight size={20} strokeWidth={2.5} className='text-white' />
Copy link
Contributor Author

@TalDerei TalDerei Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: thoughts about replacing the up and down arrows with a single right arrow? the up and down arrows had alignment issues as the routes expanded


before

Screenshot 2024-11-21 at 7 49 07 PM

after

Screenshot 2024-11-21 at 7 49 33 PM

</div>
<ValueViewComponent view={output} size='sm' />
</div>
<div className='inline-flex w-max min-w-full flex-col pb-10'>
<div className='inline-flex w-max min-w-full flex-col pb-10 mt-2'>

Check warning on line 39 in apps/minifront/src/components/swap/swap-form/simulate-swap-result/traces/index.tsx

View workflow job for this annotation

GitHub Actions / Lint

Invalid Tailwind CSS classnames order
{traces.map((trace, index) => (
<div
key={index}
Expand Down
19 changes: 18 additions & 1 deletion apps/minifront/src/components/swap/swap-form/simulate-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@ import { SimulateSwapResult } from './simulate-swap-result';
import { AllSlices } from '../../../state';
import { useStoreShallow } from '../../../utils/use-store-shallow';
import { EstimateButton } from './estimate-button';
import { useEffect } from 'react';

const simulateSwapSelector = (state: AllSlices) => ({
simulateSwap: state.swap.instantSwap.simulateSwap,
resetSimulateSwap: state.swap.instantSwap.reset,
disabled:
state.swap.txInProgress || !state.swap.amount || state.swap.instantSwap.simulateSwapLoading,
result: state.swap.instantSwap.simulateSwapResult,
amount: state.swap.amount,
});

// Automatically trigger swap simulation on user input
export const SimulateSwap = ({ layoutId }: { layoutId: string }) => {
const { simulateSwap, disabled, result } = useStoreShallow(simulateSwapSelector);
const { simulateSwap, resetSimulateSwap, disabled, result, amount } =
useStoreShallow(simulateSwapSelector);

useEffect(() => {
Copy link
Collaborator

@grod220 grod220 Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not critical to fix, but in the future, we should always see if it's possible to use react-query first (versus useEffect()) for all async tasks. The complexities of loading/error/cache invalidation is abstracted away.

Copy link
Contributor Author

@TalDerei TalDerei Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally, forgot about using react-query here – that's an easy refactor

if (!disabled && amount && parseFloat(amount) > 0) {
// Prevents re-triggering the swap simulation if it's already computed
if (!result) {
void simulateSwap();
Copy link
Contributor Author

@TalDerei TalDerei Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: we can optionally wrap this in a framer motion div for smoother rendering

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be future work

}
} else if (result) {
// Reset simulation
resetSimulateSwap();
}
}, [simulateSwap, resetSimulateSwap, disabled, result, amount]);

return (
<Box
Expand Down
Loading