File tree Expand file tree Collapse file tree
src/frontend/apps/drive/src/features/ui/preview Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import React , { useEffect } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22
33interface PreviewCsvProps {
44 title ?: string ;
55 src ?: string ;
66 }
77
88 export const PreviewCsv = ( { src, title } : PreviewCsvProps ) => {
9+
10+ const [ csvContent , setCsvContent ] = useState < string | null > ( null ) ;
11+
12+ // Get grist-static csv script
913 useEffect ( ( ) => {
1014 if ( src ) {
15+ // Load the grist csv viewer script dynamically
1116 const script = document . createElement ( 'script' ) ;
1217 script . src = 'https://grist-static.com/csv-viewer.js' ;
18+ script . async = true ;
1319 document . body . appendChild ( script ) ;
20+
21+ // fetch CSV content
22+ fetch ( src , { credentials : 'include' } )
23+ . then ( response => {
24+ if ( ! response . ok ) {
25+ throw new Error ( `Failed to fetch CSV content: ${ response . statusText } ` ) ;
26+ }
27+ return response . text ( ) ;
28+ } )
29+ . then ( text => {
30+ console . log ( "Fetched CSV content:" , text ) ;
31+ setCsvContent ( text ) ;
32+ } )
33+ . catch ( err => {
34+ console . error ( err ) ; // Optional: fallback or error handling
35+ } ) ;
1436 }
1537 } , [ src ] ) ;
38+
1639 return (
1740 < div className = 'csv-preview' >
1841 < div className = 'csv-preview__app' >
19- < csv-viewer className = 'csv-preview' initial-data = "/csvtest.csv" single-page = "true" loader = "true" >
20- </ csv-viewer >
42+ { ! ! csvContent &&
43+ < csv-viewer className = 'csv-preview' initial-content = { csvContent } single-page = "true" loader = "true" > </ csv-viewer >
44+ }
2145 </ div >
2246 </ div >
2347 ) ;
Original file line number Diff line number Diff line change @@ -14,8 +14,8 @@ interface PreviewGristProps {
1414 // Assuming bootstrapGrist is globally available
1515 if ( window ) {
1616 window . bootstrapGrist ?.( {
17- initialFile : "/gristtest.grist" , // Todo: use src instead (gots to figure out that CORS issue with NGINX...)
18- name : title || "Grist document ",
17+ // Todo: need to make a PR in grist-static to fetch with credentials
18+ initialFile : "/gristtest.grist ",
1919 elementId : 'grist-app' ,
2020 singlePage : false ,
2121 } ) ;
You can’t perform that action at this time.
0 commit comments