Files
bee-dashboard/src/pages/feeds/UpdateFeed.tsx
T
Ferenc Sárai cb5adfe031 feat: sync and update with all changes from solar-punk-ltd fork (#730)
* fix: swap error caused by invalid id and batchcount
* fix: enhance creation messages for admin drive and user drives
* fix: identity and wallet creation
* fix: asset preview types
* fix: fm search unicode text
* fix: feed identity and stamp usage
* fix: ui display changes
* fix: stamp buy and dilute
* fix: vite polyfill warning for stream
* fix: standard mode postage stamp purchase reserves incorrect size and duration
* fix: add syncing message for Bee node and update page state handling
* refactor: stamp depth and amount validation

---------

Co-authored-by: Balint Ujvari <balint.ujvari@solarpunk.buzz>
Co-authored-by: Bálint Ujvári <58116288+bosi95@users.noreply.github.com>
Co-authored-by: rolandlor <33499567+rolandlor@users.noreply.github.com>
2026-04-02 14:53:20 +02:00

168 lines
5.6 KiB
TypeScript

import { Box, Grid, Typography } from '@mui/material'
import { useSnackbar } from 'notistack'
import { ReactElement, useContext, useEffect, useState } from 'react'
import { useNavigate, useParams } from 'react-router'
import Bookmark from 'remixicon-react/BookmarkLineIcon'
import X from 'remixicon-react/CloseLineIcon'
import ExpandableListItemActions from '../../components/ExpandableListItemActions'
import { HistoryHeader } from '../../components/HistoryHeader'
import { SwarmButton } from '../../components/SwarmButton'
import { SelectEvent, SwarmSelect } from '../../components/SwarmSelect'
import TroubleshootConnectionCard from '../../components/TroubleshootConnectionCard'
import { Context as BeeContext } from '../../providers/Bee'
import { Context as IdentityContext, Identity, IdentityType } from '../../providers/Feeds'
import { Context as SettingsContext } from '../../providers/Settings'
import { Context as StampContext } from '../../providers/Stamps'
import { ROUTES } from '../../routes'
import { persistIdentity, updateFeed } from '../../utils/identity'
import { FeedPasswordDialog } from './FeedPasswordDialog'
export default function UpdateFeed(): ReactElement {
const { identities, setIdentities } = useContext(IdentityContext)
const { beeApi } = useContext(SettingsContext)
const { stamps, refresh } = useContext(StampContext)
const { status } = useContext(BeeContext)
const { hash } = useParams()
const [selectedStamp, setSelectedStamp] = useState<string | null>(stamps ? stamps[0]?.batchID.toHex() : null)
const [selectedIdentity, setSelectedIdentity] = useState<Identity | null>(identities[0] ?? null)
const [loading, setLoading] = useState(false)
const { enqueueSnackbar } = useSnackbar()
const [showPasswordPrompt, setShowPasswordPrompt] = useState(false)
const navigate = useNavigate()
useEffect(() => {
refresh()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
function onFeedChange(event: SelectEvent) {
const uuid = event.target.value
setSelectedIdentity(identities.find(x => x.uuid === uuid) || null)
}
function onStampChange(event: SelectEvent) {
const batchId = event.target.value as string
setSelectedStamp(batchId)
}
function onCancel() {
navigate(-1)
}
function onBeginUpdatingFeed() {
if (!selectedIdentity) {
return
}
if (selectedIdentity.type === IdentityType.V3) {
setShowPasswordPrompt(true)
} else {
onFeedUpdate(selectedIdentity)
}
}
async function onFeedUpdate(identity: Identity, password?: string) {
setLoading(true)
if (!beeApi || !selectedStamp) {
enqueueSnackbar(<span>Bee API unavailabe</span>, { variant: 'error' })
setLoading(false)
return
}
if (!hash) {
enqueueSnackbar(<span>Hash is invalid</span>, { variant: 'error' })
setLoading(false)
return
}
try {
await updateFeed(beeApi, identity, hash, selectedStamp, password as string)
persistIdentity(identities, identity)
setIdentities([...identities])
navigate(ROUTES.ACCOUNT_FEEDS_VIEW.replace(':uuid', identity.uuid))
} catch (error: unknown) {
setLoading(false)
const message = (typeof error === 'object' && error !== null && Reflect.get(error, 'message')) || ''
if (message.includes('possibly wrong passphrase')) {
enqueueSnackbar('Wrong password, please try again', { variant: 'error' })
} else {
enqueueSnackbar('Could not update feed at this time, please try again later', { variant: 'error' })
}
}
}
if (!status.all) return <TroubleshootConnectionCard />
return (
<div>
{showPasswordPrompt && selectedIdentity && (
<FeedPasswordDialog
feedName={selectedIdentity.name + ' Website'}
onCancel={() => {
setShowPasswordPrompt(false)
}}
onProceed={(password: string) => {
onFeedUpdate(selectedIdentity, password)
}}
loading={loading}
/>
)}
<HistoryHeader>Update feed</HistoryHeader>
<Box mb={2}>
<Grid container>
{identities && identities.length ? (
<SwarmSelect
value={selectedIdentity?.uuid ?? ''}
options={identities.map(x => ({ value: x.uuid, label: `${x.name} Website` }))}
onChange={onFeedChange}
label="Feed"
/>
) : (
<Typography>You need to create an identiy first to be able to update its feed.</Typography>
)}
</Grid>
</Box>
<Box mb={4}>
<Grid container>
{stamps && stamps.length ? (
<SwarmSelect
value={selectedStamp ?? ''}
options={stamps.map(x => ({
value: x.batchID.toHex(),
label: x.label ? x.batchID.toHex().slice(0, 8) + ` (${x.label})` : x.batchID.toHex().slice(0, 8),
}))}
onChange={onStampChange}
label="Stamp"
/>
) : (
<Typography>You need to buy a stamp first to be able to update a feed.</Typography>
)}
</Grid>
</Box>
<ExpandableListItemActions>
<SwarmButton
onClick={onBeginUpdatingFeed}
iconType={Bookmark}
loading={!showPasswordPrompt && loading}
disabled={loading || !selectedStamp || !selectedIdentity}
>
Update Selected Feed
</SwarmButton>
<SwarmButton onClick={onCancel} iconType={X} disabled={loading} cancel>
Close
</SwarmButton>
</ExpandableListItemActions>
</div>
)
}