File tree 1 file changed +22
-1
lines changed
web/src/components/FileViewer
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)`
28
28
* @returns renders the file
29
29
*/
30
30
const FileViewer : React . FC < { url : string } > = ( { url } ) => {
31
- const docs = [ { uri : url } ] ;
31
+ const docs = [ { uri : url , fileName : fileNameIfIpfsUrl ( url ) } ] ;
32
32
return (
33
33
< Wrapper className = "file-viewer-wrapper" >
34
34
< StyledDocViewer
@@ -50,4 +50,25 @@ const FileViewer: React.FC<{ url: string }> = ({ url }) => {
50
50
) ;
51
51
} ;
52
52
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
+
53
74
export default FileViewer ;
You can’t perform that action at this time.
0 commit comments