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

Fix: hide Edit for safeTxGas when 0 in 1.3.0+ #2163

Merged
merged 2 commits into from
Jun 26, 2023
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
82 changes: 45 additions & 37 deletions src/components/transactions/TxDetails/SafeTxGasForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { Link, Box, Paper, Button } from '@mui/material'
import { useForm } from 'react-hook-form'
import { SafeTxContext } from '@/components/tx-flow/SafeTxProvider'
import NumberField from '@/components/common/NumberField'
import useSafeInfo from '@/hooks/useSafeInfo'
import { isLegacyVersion } from '@/hooks/coreSDK/safeCoreSDK'

type FormFields = {
safeTxGas: number
}

const SafeTxGasForm = () => {
const { safeTx, safeTxGas = 0, setSafeTxGas } = useContext(SafeTxContext)
const isEditable = safeTx?.signatures.size === 0
const [editing, setEditing] = useState(false)
const Form = ({ onSubmit }: { onSubmit: () => void }) => {
const { safeTxGas = 0, setSafeTxGas } = useContext(SafeTxContext)

const formMethods = useForm<FormFields>({
defaultValues: {
Expand All @@ -20,52 +20,60 @@ const SafeTxGasForm = () => {
mode: 'onChange',
})

const onSubmit = (values: FormFields) => {
const onFormSubmit = (values: FormFields) => {
setSafeTxGas(values.safeTxGas || 0)
setEditing(false)
onSubmit()
}

const onBlur = () => {
setTimeout(() => {
setEditing(false)
formMethods.setValue('safeTxGas', safeTxGas)
onSubmit()
}, 100)
}

return (
<Box display="flex" alignItems="center" gap={1} position="relative">
<>
{safeTxGas}
<Paper sx={{ position: 'absolute', zIndex: 2, p: 1, ml: '-22px' }} elevation={2}>
<form onSubmit={formMethods.handleSubmit(onFormSubmit)} style={{ display: 'flex' }}>
<NumberField
size="small"
autoFocus
type="number"
error={!!formMethods.formState.errors.safeTxGas}
sx={{ width: '7em' }}
{...formMethods.register('safeTxGas', {
valueAsNumber: true,
min: 0,
setValueAs: Math.round,
onBlur,
})}
/>
<Button type="submit" size="small" variant="contained" sx={{ ml: 1 }}>
Save
</Button>
</form>
</Paper>
)
}

{isEditable && (
<Link component="button" onClick={() => setEditing(true)} fontSize="small">
Edit
</Link>
)}
</>
const SafeTxGasForm = () => {
const { safeTx, safeTxGas = 0 } = useContext(SafeTxContext)
const { safe } = useSafeInfo()
const isOldSafe = safe.version && isLegacyVersion(safe.version)
const isEditable = safeTx?.signatures.size === 0 && (safeTxGas > 0 || isOldSafe)
const [editing, setEditing] = useState(false)

{editing && (
<Paper sx={{ position: 'absolute', zIndex: 2, p: 1, ml: '-22px' }} elevation={2}>
<form onSubmit={formMethods.handleSubmit(onSubmit)} style={{ display: 'flex' }}>
<NumberField
size="small"
autoFocus
type="number"
error={!!formMethods.formState.errors.safeTxGas}
sx={{ width: '7em' }}
{...formMethods.register('safeTxGas', {
valueAsNumber: true,
min: 0,
setValueAs: Math.round,
onBlur,
})}
/>
<Button type="submit" size="small" variant="contained" sx={{ ml: 1 }}>
Save
</Button>
</form>
</Paper>
return (
<Box display="flex" alignItems="center" gap={1} position="relative">
{safeTxGas}
Copy link
Member

Choose a reason for hiding this comment

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

I also just realized that we need to adjust the prio of finalSafeTxGas in the provider in case a safe app sends it it should take prio over the recommended value. Right now all Safe App txs send safeTxGas regardless of safe version but we display 0.


{isEditable && (
<Link component="button" onClick={() => setEditing(true)} fontSize="small">
Edit
</Link>
)}

{editing && <Form onSubmit={() => setEditing(false)} />}
Copy link
Member Author

Choose a reason for hiding this comment

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

Moving the form into a separate component fixes the bug that the default value wasn't updated when safeTxGas is recalculated.

</Box>
)
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/transactions/TxDetails/Summary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export const PartialSummary = ({ safeTx }: { safeTx: SafeTransaction }) => {
<TxDataRow title="safeTxGas:">
<SafeTxGasForm />
</TxDataRow>
<TxDataRow title="baseGas:">{txData.baseGas}</TxDataRow>
<TxDataRow title="refundReceiver:">{generateDataRowValue(txData.refundReceiver, 'hash', true)}</TxDataRow>
<TxDataRow title="Raw data:">{generateDataRowValue(txData.data, 'rawData')}</TxDataRow>
</>
)
Expand Down
Loading