Skip to content

Commit

Permalink
refactor: clean up postage stamp screens (#642)
Browse files Browse the repository at this point in the history
* refactor: clean up postage stamp screens

* fix: add immutable flag
  • Loading branch information
Cafe137 committed Dec 4, 2023
1 parent 4f9abc6 commit 080d9f2
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 82 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"url": "https://github.com/ethersphere/bee-dashboard.git"
},
"dependencies": {
"@ethersphere/bee-js": "^6.6.0",
"@ethersphere/bee-js": "^6.7.0",
"@ethersphere/swarm-cid": "^0.1.0",
"@material-ui/core": "4.12.3",
"@material-ui/icons": "4.11.2",
Expand Down
76 changes: 48 additions & 28 deletions src/pages/stamps/PostageStampAdvancedCreation.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { PostageBatchOptions } from '@ethersphere/bee-js'
import { Box, Grid, Typography } from '@material-ui/core'
import { createStyles, makeStyles, Theme } from '@material-ui/core/styles'
import { Box, Grid, Typography, createStyles, makeStyles } from '@material-ui/core'
import BigNumber from 'bignumber.js'
import { useSnackbar } from 'notistack'
import { ReactElement, useContext, useState } from 'react'
import { Link } from 'react-router-dom'
import Check from 'remixicon-react/CheckLineIcon'
import { SwarmButton } from '../../components/SwarmButton'
import { SwarmSelect } from '../../components/SwarmSelect'
import { SwarmTextInput } from '../../components/SwarmTextInput'
import { Context as BeeContext } from '../../providers/Bee'
import { Context as SettingsContext } from '../../providers/Settings'
import { Context as StampsContext } from '../../providers/Stamps'
import { ROUTES } from '../../routes'
import {
calculateStampPrice,
convertAmountToSeconds,
Expand All @@ -19,14 +20,12 @@ import {
waitUntilStampExists,
} from '../../utils'
import { getHumanReadableFileSize } from '../../utils/file'
import { Link } from 'react-router-dom'
import { ROUTES } from '../../routes'

interface Props {
onFinished: () => void
}

const useStyles = makeStyles((theme: Theme) =>
const useStyles = makeStyles(() =>
createStyles({
link: {
color: '#dd7700',
Expand Down Expand Up @@ -174,25 +173,22 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen

return (
<>
<Box mb={1}>
<Typography variant="h2">Batch name</Typography>
</Box>
<Box mb={2}>
<SwarmTextInput name="label" label="Label" optional onChange={event => setLabelInput(event.target.value)} />
</Box>
<Box mb={2}>
<SwarmSelect
label="Immutable"
defaultValue="No"
onChange={event => setImmutable(event.target.value === 'Yes')}
options={[
{ value: 'Yes', label: 'Yes' },
{ value: 'No', label: 'No' },
]}
/>
<Box mb={4}>
<Typography>
To upload data to Swarm network, you will need to purchase a postage stamp. If you&apos;re not familiar with
this, please read{' '}
<a
href="https://medium.com/ethereum-swarm/how-to-upload-data-to-the-swarm-network-c0766c3ae381"
target="_blank"
rel="noreferrer"
>
this guide
</a>
.
</Typography>
</Box>
<Box mb={2}>
<SwarmTextInput name="depth" label="Batch depth" onChange={event => validateDepthInput(event.target.value)} />
<SwarmTextInput name="depth" label="Depth" onChange={event => validateDepthInput(event.target.value)} />
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
<Grid container justifyContent="space-between">
<Typography>Corresponding file size</Typography>
Expand All @@ -209,14 +205,38 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen
<Typography>{!amountError && amountInput ? getTtl(Number.parseInt(amountInput, 10)) : '-'}</Typography>
</Grid>
</Box>
<Box display="flex" justifyContent={'right'} mt={0.5}>
<Typography style={{ fontSize: '10px', color: 'rgba(0, 0, 0, 0.26)' }}>
Current price of 24000 per block
</Typography>
</Box>
{amountError && <Typography>{amountError}</Typography>}
</Box>

<Box mb={2}>
<SwarmTextInput name="label" label="Label" optional onChange={event => setLabelInput(event.target.value)} />
</Box>
<Box mb={2}>
<SwarmSelect
label="Immutable"
defaultValue="No"
onChange={event => setImmutable(event.target.value === 'Yes')}
options={[
{ value: 'Yes', label: 'Yes' },
{ value: 'No', label: 'No' },
]}
/>
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
<Grid container justifyContent="space-between">
{immutable && (
<Typography>
Once an immutable stamp is maxed out, it disallows further content uploads, thereby safeguarding your
previously uploaded content from unintentional overwriting.
</Typography>
)}
{!immutable && (
<Typography>
When a mutable stamp reaches full capacity, it still permits new content uploads. However, this comes
with the caveat of overwriting previously uploaded content associated with the same stamp.
</Typography>
)}
</Grid>
</Box>
</Box>
<Box mb={4} sx={{ bgcolor: '#fcf2e8' }} p={2}>
<Grid container justifyContent="space-between">
<Typography>Indicative Price</Typography>
Expand Down
40 changes: 19 additions & 21 deletions src/pages/stamps/PostageStampStandardCreation.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { PostageBatchOptions } from '@ethersphere/bee-js'
import { Utils } from '@ethersphere/bee-js'
import { PostageBatchOptions, Utils } from '@ethersphere/bee-js'
import { Box, Button, Grid, Slider, Typography } from '@material-ui/core'
import { createStyles, makeStyles, Theme } from '@material-ui/core/styles'
import { Theme, createStyles, makeStyles } from '@material-ui/core/styles'
import { useSnackbar } from 'notistack'
import { ReactElement, useContext, useState } from 'react'
import { Link } from 'react-router-dom'
import Check from 'remixicon-react/CheckLineIcon'
import { SwarmButton } from '../../components/SwarmButton'
import { SwarmTextInput } from '../../components/SwarmTextInput'
import { Context as SettingsContext } from '../../providers/Settings'
import { Context as StampsContext } from '../../providers/Stamps'
import { Link } from 'react-router-dom'
import { ROUTES } from '../../routes'
import { calculateStampPrice, convertAmountToSeconds, secondsToTimeString, waitUntilStampExists } from '../../utils'

Expand Down Expand Up @@ -43,30 +42,31 @@ const marks = [
]

export function PostageStampStandardCreation({ onFinished }: Props): ReactElement {
const getDepthForCapacity = Utils.getDepthForCapacity
const getAmountForTtl = Utils.getAmountForTtl
const classes = useStyles()
const { refresh } = useContext(StampsContext)
const { beeDebugApi } = useContext(SettingsContext)

const [depthInput, setDepthInput] = useState<number>(getDepthForCapacity(4))
const [amountInput, setAmountInput] = useState<number>(Number.parseInt(getAmountForTtl(30)))
const [depthInput, setDepthInput] = useState<number>(Utils.getDepthForCapacity(4))
const [amountInput, setAmountInput] = useState<string>(Utils.getAmountForTtl(30))
const [labelInput, setLabelInput] = useState('')
const [submitting, setSubmitting] = useState(false)
const [buttonValue, setButtonValue] = useState(4)

function sliderValueChange(event: any, newValue: any) {
const amountValue = Number.parseInt(getAmountForTtl(newValue))
function sliderValueChange(_: unknown, newValue: number | number[]) {
if (typeof newValue !== 'number') {
return
}
const amountValue = Utils.getAmountForTtl(newValue)
setAmountInput(amountValue)
}

const { enqueueSnackbar } = useSnackbar()

function getTtl(amount: number): string {
function getTtl(amount: string): string {
const pricePerBlock = 24000

return `${secondsToTimeString(
convertAmountToSeconds(amount, pricePerBlock),
convertAmountToSeconds(parseInt(amount, 10), pricePerBlock),
)} (with price of ${pricePerBlock.toFixed(0)} per block)`
}

Expand All @@ -93,7 +93,7 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
const options: PostageBatchOptions = {
waitForUsable: false,
label: labelInput || undefined,
immutableFlag: false,
immutableFlag: true,
}

const batchId = await beeDebugApi.createPostageBatch(amount.toString(), depth, options)
Expand All @@ -107,11 +107,9 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
setSubmitting(false)
}

function handleBatchSize(event: any) {
let value = event.target.innerText
value = Number(value.substring(0, value.length - 3))
setButtonValue(value)
const capacity = getDepthForCapacity(value)
function handleBatchSize(gigabytes: number) {
setButtonValue(gigabytes)
const capacity = Utils.getDepthForCapacity(gigabytes)
setDepthInput(capacity)
}

Expand Down Expand Up @@ -146,7 +144,7 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
<Button
variant="contained"
fullWidth
onClick={handleBatchSize}
onClick={() => handleBatchSize(4)}
className={buttonValue === 4 ? classes.buttonSelected : ''}
>
4 GB
Expand All @@ -156,7 +154,7 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
<Button
variant="contained"
fullWidth
onClick={handleBatchSize}
onClick={() => handleBatchSize(32)}
className={buttonValue === 32 ? classes.buttonSelected : ''}
>
32 GB
Expand All @@ -166,7 +164,7 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
<Button
variant="contained"
fullWidth
onClick={handleBatchSize}
onClick={() => handleBatchSize(256)}
className={buttonValue === 256 ? classes.buttonSelected : ''}
>
256 GB
Expand Down
25 changes: 0 additions & 25 deletions src/utils/stamps.ts

This file was deleted.

0 comments on commit 080d9f2

Please sign in to comment.