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: allow json and text fiels to be in preview file component #1646

Merged
merged 7 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 26 additions & 10 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"simplify-number": "^1.0.0",
"tachyons": "^4.12.0",
"topojson": "^3.0.2",
"uint8arrays": "^1.1.0",
"video-extensions": "^1.1.0",
"window-or-global": "^1.0.1",
"window.ipfs-fallback": "^1.2.1"
Expand Down
1 change: 1 addition & 0 deletions public/locales/en/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"paragraph3": "Pro tip: drag and drop a file from any other page of the Web UI to add them to the root of your MFS."
}
},
"loadMore": "Load more",
"previousFolder": "Go back to previous folder",
"fileLabel": "Select {type} {name} with size: {size}",
"hashUnavailable": "hash unavailable",
Expand Down
2 changes: 1 addition & 1 deletion src/files/FilesPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class FilesPage extends React.Component {

if (files.type === 'file') {
return (
<FilePreview {...files} />
<FilePreview {...files} onDownload={() => this.onDownload([files])} />
)
}

Expand Down
55 changes: 44 additions & 11 deletions src/files/file-preview/FilePreview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import { connect } from 'redux-bundler-react'
Expand All @@ -9,6 +9,8 @@ import ComponentLoader from '../../loader/ComponentLoader.js'
import './FilePreview.css'
import CID from 'cids'
import { useDrag } from 'react-dnd'
import fromUint8ArrayToString from 'uint8arrays/to-string'
import Button from '../../components/button/Button'

const Preview = (props) => {
const { name, size, cid, path } = props
Expand All @@ -18,20 +20,43 @@ const Preview = (props) => {

const type = typeFromExt(name)

return <div className={ classNames(type !== 'pdf' && 'dib') } ref={drag}>
// Hack: Allows for text selection if it's a text file (bypass useDrag)
const dummyRef = useRef()

return <div className={ classNames(type !== 'pdf' && type !== 'text' && type !== 'json' && 'dib') } ref={type === 'text' ? dummyRef : drag}>
<PreviewItem {...props} type={type} />
</div>
}

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)}`
rafaelramalho19 marked this conversation as resolved.
Show resolved Hide resolved
const className = 'mw-100 mt3 bg-snow-muted pa2 br2 border-box'

switch (type) {
Expand Down Expand Up @@ -75,19 +100,27 @@ const PreviewItem = ({ t, name, cid, size, type, availableGatewayUrl: gatewayUrl
}

if (!content) {
loadContent()
return <ComponentLoader pastDelay />
}

if (isBinary(content)) {
loadContent()
return cantPreview
}

return (
return <>
<pre className={`${className} overflow-auto monospace`}>
{content}
</pre>
)
{ hasMoreContent && <div className="w-100 flex items-center justify-center">
<Button onClick={ loadContent }>
{ t('loadMore')}
</Button>
<Button className="mh2" onClick={ onDownload }>
{ t('app:actions.download')}
</Button>
</div>}
</>
}
}
}
Expand Down