11/* eslint-disable @typescript-eslint/ban-ts-comment */
2- import React , { useEffect , useMemo } from 'react' ;
2+ import React , { useEffect , useMemo , useRef } from 'react' ;
33import { Workbook } from '@fileverse-dev/fortune-react' ;
44import { Cell } from '@fileverse-dev/fortune-core' ;
55
@@ -61,7 +61,7 @@ export const EditorWorkbook: React.FC<EditorWorkbookProps> = ({
6161 exportDropdownOpen = false ,
6262 commentData,
6363 getCommentCellUI,
64- setExportDropdownOpen = ( ) => { } ,
64+ setExportDropdownOpen = ( ) => { } ,
6565 dsheetId,
6666 storeApiKey,
6767 onDataBlockApiResponse,
@@ -109,6 +109,67 @@ export const EditorWorkbook: React.FC<EditorWorkbookProps> = ({
109109 : [ 'filter' ]
110110 : TOOL_BAR_ITEMS ;
111111
112+ const cryptoPriceRef = useRef < { ETH : number | null , BTC : number | null , SOL : number | null } > ( { BTC : null , ETH : 0 , SOL : 0 } ) ;
113+ const intervalRef = useRef < NodeJS . Timeout | null > ( null ) ;
114+
115+ const fetchPrice = async ( ) => {
116+ // @ts -expect-error later
117+ const cryptoPrices = await fetch ( `${ window . NEXT_PUBLIC_PROXY_BASE_URL } /proxy` , {
118+ headers :
119+ {
120+ 'target-url' : 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,solana&vs_currencies=usd' ,
121+ method : 'GET' ,
122+ 'Content-Type' : 'application/json'
123+ }
124+ } ) ;
125+ const cryptoData = await cryptoPrices . json ( ) ;
126+ const ETH = cryptoData . ethereum . usd ;
127+ const BTC = cryptoData . bitcoin . usd ;
128+ const SOL = cryptoData . solana . usd ;
129+ cryptoPriceRef . current = {
130+ ETH ,
131+ BTC ,
132+ SOL
133+ }
134+ refreshDenomination ( ) ;
135+ }
136+
137+ const refreshDenomination = ( ) => {
138+ const { ETH , BTC , SOL } = cryptoPriceRef . current ;
139+ if ( BTC === null || ETH === null || SOL === null ) return ;
140+ const currentData = sheetEditorRef . current ?. getSheet ( ) ;
141+ const cellData = currentData ?. celldata ;
142+ if ( ! cellData ) return ;
143+
144+ for ( let i = 0 ; i < cellData ?. length ; i ++ ) {
145+ const cell = { ...cellData [ i ] } as any ; cellData [ i ] ;
146+ cell . v = typeof cell . v === 'string' ? cell . v : { ...cellData [ i ] . v } ;
147+
148+ const value = cell . v ?. m ?. toString ( ) ;
149+ const decemialCount = cell . v ?. m ?. includes ( '.' ) ? cell . v ?. m ?. split ( ' ' ) [ 0 ] . split ( '.' ) [ 1 ] . length : 0 ;
150+ if ( value ?. includes ( "BTC" ) ) {
151+ cell . v . m = value . replace ( / \d + ( \. \d + ) ? / , ( cell . v ?. baseValue / BTC ) . toFixed ( decemialCount ) . toString ( ) ) ;
152+ } else if ( value ?. includes ( "ETH" ) ) {
153+ cell . v . m = value . replace ( / \d + ( \. \d + ) ? / , ( cell . v ?. baseValue / ETH ) . toFixed ( decemialCount ) . toString ( ) ) ;
154+ } else if ( value ?. includes ( "SOL" ) ) {
155+ cell . v . m = value . replace ( / \d + ( \. \d + ) ? / , ( cell . v ?. baseValue / SOL ) . toFixed ( decemialCount ) . toString ( ) ) ;
156+ }
157+
158+ sheetEditorRef . current ?. setCellValue ( cell . r , cell . c , cell . v ) ;
159+ }
160+ }
161+
162+ useEffect ( ( ) => {
163+ intervalRef . current = setInterval ( ( ) => {
164+ fetchPrice ( ) ;
165+ } , 20 * 60 * 1000 ) ;
166+
167+ return ( ) => {
168+ if ( intervalRef . current )
169+ clearInterval ( intervalRef . current ) ;
170+ } ;
171+ } , [ ] ) ;
172+
112173 // Memoized workbook component to avoid unnecessary re-renders
113174 return useMemo ( ( ) => {
114175 // Create a unique key to force re-render when needed
@@ -146,23 +207,28 @@ export const EditorWorkbook: React.FC<EditorWorkbookProps> = ({
146207 customToolbarItems = {
147208 ! isReadOnly
148209 ? getCustomToolbarItems ( {
149- setExportDropdownOpen,
150- handleCSVUpload,
151- handleXLSXUpload,
152- handleExportToXLSX,
153- handleExportToCSV,
154- handleExportToJSON,
155- sheetEditorRef,
156- ydocRef,
157- dsheetId,
158- currentDataRef,
159- setForceSheetRender,
160- toggleTemplateSidebar,
161- setShowFetchURLModal,
162- } )
210+ setExportDropdownOpen,
211+ handleCSVUpload,
212+ handleXLSXUpload,
213+ handleExportToXLSX,
214+ handleExportToCSV,
215+ handleExportToJSON,
216+ sheetEditorRef,
217+ ydocRef,
218+ dsheetId,
219+ currentDataRef,
220+ setForceSheetRender,
221+ toggleTemplateSidebar,
222+ setShowFetchURLModal,
223+ } )
163224 : [ ]
164225 }
165226 hooks = { {
227+ afterActivateSheet : ( ) => {
228+ if ( sheetEditorRef . current && sheetEditorRef . current ?. getAllSheets ( ) . length > 0 ) {
229+ refreshDenomination ( ) ;
230+ }
231+ } ,
166232 afterUpdateCell : (
167233 row : number ,
168234 column : number ,
@@ -171,6 +237,7 @@ export const EditorWorkbook: React.FC<EditorWorkbookProps> = ({
171237 ) : void => {
172238 const refObj = { current : sheetEditorRef . current } ;
173239 afterUpdateCell ( {
240+ oldValue : _oldValue ,
174241 row,
175242 column,
176243 newValue,
0 commit comments