+ // Hack: Allows for text selection if it's a text file (bypass useDrag)
+ const dummyRef = useRef()
+
+ return
}
-const PreviewItem = ({ t, name, cid, size, type, availableGatewayUrl: gatewayUrl, read }) => {
+const PreviewItem = ({ t, name, cid, size, type, availableGatewayUrl: gatewayUrl, read, onDownload }) => {
const [content, setContent] = useState(null)
+ const [hasMoreContent, setHasMoreContent] = useState(false)
+ const [buffer, setBuffer] = useState(null)
- const loadContent = async () => {
- const buf = await read()
- setContent(buf.toString('utf-8'))
- }
+ const loadContent = useCallback(async () => {
+ const readBuffer = buffer || await read()
+ if (!buffer) {
+ setBuffer(readBuffer)
+ }
+
+ const { value, done } = await readBuffer.next()
+ const previousContent = content || ''
+
+ const currentContent = previousContent + fromUint8ArrayToString(value)
+
+ setContent(currentContent)
+
+ const hasMore = !done && new TextEncoder().encode(currentContent).length < size
- const src = `${gatewayUrl}/ipfs/${cid}`
+ setHasMoreContent(hasMore)
+ }, [buffer, content, read, size])
+
+ useEffect(() => {
+ loadContent()
+ }, // eslint-disable-next-line react-hooks/exhaustive-deps
+ [])
+
+ const src = `${gatewayUrl}/ipfs/${cid}?filename=${encodeURIComponent(name)}`
const className = 'mw-100 mt3 bg-snow-muted pa2 br2 border-box'
switch (type) {
@@ -75,19 +100,27 @@ const PreviewItem = ({ t, name, cid, size, type, availableGatewayUrl: gatewayUrl
}
if (!content) {
- loadContent()
return
}
if (isBinary(content)) {
+ loadContent()
return cantPreview
}
- return (
+ return <>
{content}
- )
+ { hasMoreContent &&
+
+
+
}
+ >
}
}
}