Skip to content

Commit 79bb315

Browse files
LevilkTheRealLevente Kiss
andauthored
feat: wait for upload sync (#649)
* feat: add basic progress bar (#605) * feat: add syncing to share page (#605) * refactor: use the documentation text comp (#605) * refactor: move sync logic to new comp (#605) * refactor: optimize sync check (#605) * chore: linting (#605) --------- Co-authored-by: Levente Kiss <levente.kiss@solarpunk.bzz>
1 parent 5871223 commit 79bb315

4 files changed

Lines changed: 121 additions & 1 deletion

File tree

src/components/ProgressBar.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { ReactElement } from 'react'
2+
import LinearProgress, { LinearProgressProps } from '@material-ui/core/LinearProgress'
3+
import Typography from '@material-ui/core/Typography'
4+
import Box from '@material-ui/core/Box'
5+
6+
interface Props {
7+
linearProgressProps?: LinearProgressProps
8+
value: number
9+
}
10+
11+
export function LinearProgressWithLabel(props: Props): ReactElement {
12+
return (
13+
<Box display="flex" alignItems="center">
14+
<Box width="100%" mr={1}>
15+
<LinearProgress variant="determinate" {...props} />
16+
</Box>
17+
<Box minWidth={35}>
18+
<Typography variant="body2" color="textSecondary">{`${Math.round(props.value)}%`}</Typography>
19+
</Box>
20+
</Box>
21+
)
22+
}

src/pages/files/AssetSyncing.tsx

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Context as SettingsContext } from '../../providers/Settings'
2+
import { Box } from '@material-ui/core'
3+
import { ReactElement, useContext, useEffect, useRef, useState } from 'react'
4+
import { DocumentationText } from '../../components/DocumentationText'
5+
import { LinearProgressWithLabel } from '../../components/ProgressBar'
6+
7+
interface Props {
8+
reference: string
9+
}
10+
11+
export function AssetSyncing({ reference }: Props): ReactElement {
12+
const { beeApi } = useContext(SettingsContext)
13+
14+
const syncTimer = useRef<NodeJS.Timer>()
15+
const [isRetrieveChecking, setIsRetrieveChecking] = useState<boolean>(false)
16+
const [syncProgress, setSyncProgress] = useState<number>(0)
17+
18+
const syncCheck = async () => {
19+
if (!beeApi) {
20+
return
21+
}
22+
23+
const tags = await beeApi.getAllTags()
24+
const tag = tags.find(t => t.address === reference)
25+
26+
if (tag) {
27+
const progress = ((tag.seen + tag.synced) / tag.split) * 100
28+
setSyncProgress(progress)
29+
}
30+
}
31+
32+
useEffect(() => {
33+
syncTimer.current = setInterval(syncCheck, 2000)
34+
35+
return () => {
36+
if (syncTimer.current) {
37+
clearInterval(syncTimer.current)
38+
}
39+
}
40+
// eslint-disable-next-line react-hooks/exhaustive-deps
41+
}, [reference])
42+
43+
useEffect(() => {
44+
if (syncProgress === 100 && syncTimer.current) {
45+
clearInterval(syncTimer.current)
46+
}
47+
}, [syncProgress])
48+
49+
useEffect(() => {
50+
/*
51+
There are instances when it seems that the content isn't synchronized, despite being already available.
52+
To ensure it's not due to invalid synchronization data,
53+
verify availability from at least 70% using one of the stewardship endpoints.
54+
55+
TODO: is 70 a good number?
56+
*/
57+
if (beeApi && !isRetrieveChecking && syncProgress > 10 && syncProgress < 100) {
58+
// It's a long running task make sure only one run occurs at a time.
59+
setIsRetrieveChecking(true)
60+
61+
beeApi.isReferenceRetrievable(reference).then(isRetriavable => {
62+
if (isRetriavable) {
63+
setSyncProgress(100)
64+
}
65+
66+
setIsRetrieveChecking(false)
67+
})
68+
}
69+
}, [syncProgress, isRetrieveChecking, beeApi, reference])
70+
71+
return (
72+
<>
73+
<Box mb={2}>
74+
<DocumentationText>
75+
Files are not immediately accessible on the Swarm network. Please wait until your upload is synced to the
76+
network.{' '}
77+
<a href="https://docs.ethswarm.org/docs/develop/access-the-swarm/syncing">Learn more about syncing</a>.
78+
</DocumentationText>
79+
</Box>
80+
<Box mb={4}>
81+
<LinearProgressWithLabel value={syncProgress}></LinearProgressWithLabel>
82+
</Box>
83+
</>
84+
)
85+
}

src/pages/files/Share.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ManifestJs } from '../../utils/manifest'
1616
import { AssetPreview } from './AssetPreview'
1717
import { AssetSummary } from './AssetSummary'
1818
import { DownloadActionBar } from './DownloadActionBar'
19+
import { AssetSyncing } from './AssetSyncing'
1920

2021
export function Share(): ReactElement {
2122
const { apiUrl, beeApi } = useContext(SettingsContext)
@@ -152,6 +153,9 @@ export function Share(): ReactElement {
152153
<Box mb={4}>
153154
<AssetSummary isWebsite={metadata?.isWebsite} reference={reference} />
154155
</Box>
156+
<Box mb={4}>
157+
<AssetSyncing reference={reference} />
158+
</Box>
155159
<DownloadActionBar
156160
onOpen={onOpen}
157161
onCancel={onClose}

src/pages/files/Upload.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,16 @@ export function Upload(): ReactElement {
204204
</Box>
205205
</>
206206
)}
207-
{step === 2 && stamp && <StampPreview stamp={stamp} />}
207+
{step === 2 && stamp && (
208+
<>
209+
<StampPreview stamp={stamp} />
210+
<Box mb={4}>
211+
<DocumentationText>
212+
Please do not close the application until your file is uploaded to your local node!
213+
</DocumentationText>
214+
</Box>
215+
</>
216+
)}
208217
<UploadActionBar
209218
step={step}
210219
onCancel={reset}

0 commit comments

Comments
 (0)