File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed
web/src/components/FileViewer Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ const StyledDocViewer = styled(DocViewer)`
2828 * @returns renders the file
2929 */
3030const FileViewer : React . FC < { url : string } > = ( { url } ) => {
31- const docs = [ { uri : url } ] ;
31+ const docs = [ { uri : url , fileName : fileNameIfIpfsUrl ( url ) } ] ;
3232 return (
3333 < Wrapper className = "file-viewer-wrapper" >
3434 < StyledDocViewer
@@ -50,4 +50,25 @@ const FileViewer: React.FC<{ url: string }> = ({ url }) => {
5050 ) ;
5151} ;
5252
53+ const fileNameIfIpfsUrl = ( url : string ) => {
54+ if ( ! url || typeof url !== "string" ) {
55+ return "document" ;
56+ }
57+
58+ const ipfsPattern = / (?: i p f s : \/ \/ | h t t p s ? : \/ \/ (?: [ A - Z a - z 0 - 9 . - ] + ) \/ i p f s \/ ) ( [ A - Z a - z 0 - 9 ] + [ A - Z a - z 0 - 9 \- _ ] * ) \/ ? ( .* ) / ;
59+
60+ const match = url . match ( ipfsPattern ) ;
61+
62+ if ( match ) {
63+ const ipfsHash = match [ 1 ] ;
64+ const path = match [ 2 ] || "" ;
65+
66+ const sanitizedPath = path . replace ( / \/ / g, "_" ) ;
67+
68+ return `ipfs-${ ipfsHash } ${ sanitizedPath ? `_${ sanitizedPath } ` : "" } ` ;
69+ } else {
70+ return "document" ;
71+ }
72+ } ;
73+
5374export default FileViewer ;
You can’t perform that action at this time.
0 commit comments