Skip to content

Commit 2e28d84

Browse files
authored
crypto denomination (#133)
* crypto deno * minor changes * minor changes * 20 min refresh * minor changes * minor changes * minor changes * minor changes * minor chnages * minor changes * minor changes * minor changes
1 parent dfb43e2 commit 2e28d84

11 files changed

Lines changed: 336 additions & 290 deletions

File tree

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
"typescript": "^5.2.2",
3030
"vite": "^5.3.4"
3131
}
32-
}
32+
}

demo/src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ function App() {
1818
const sheetEditorRef = useRef<WorkbookInstance>(null);
1919

2020
// Use a stable dsheetId
21-
const dsheetId = 'demo-dsheet-2';
21+
const dsheetId = 'demo-dsheet-3';
22+
// @ts-expect-error later
23+
window.NEXT_PUBLIC_PROXY_BASE_URL = 'https://staging-api-proxy-ca4268d7d581.herokuapp.com';
2224

2325
// Handle data changes in the sheet - kept empty as we don't need to log anything
2426
const handleSheetChange = useCallback(() => { }, []);

demo/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineConfig, loadEnv } from 'vite'
22
import react from '@vitejs/plugin-react'
3+
34
export default defineConfig(({ mode }) => {
45
const env = loadEnv(mode, process.cwd())
56
return {
@@ -10,4 +11,4 @@ export default defineConfig(({ mode }) => {
1011
}
1112
}
1213
}
13-
})
14+
})

package-lock.json

Lines changed: 211 additions & 240 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@fileverse-dev/dsheet",
33
"private": false,
44
"description": "DSheet",
5-
"version": "1.0.53",
5+
"version": "1.0.61",
66
"main": "dist/index.es.js",
77
"module": "dist/index.es.js",
88
"exports": {
@@ -34,10 +34,10 @@
3434
},
3535
"dependencies": {
3636
"@fileverse-dev/dsheets-templates": "^0.0.17",
37-
"@fileverse-dev/formula-parser": "^0.2.30",
38-
"@fileverse-dev/formulajs": "^4.4.11-mod-78",
39-
"@fileverse-dev/fortune-core": "^1.0.39",
40-
"@fileverse-dev/fortune-react": "^1.0.39",
37+
"@fileverse-dev/formula-parser": "^0.2.37",
38+
"@fileverse-dev/formulajs": "^4.4.11-mod-83",
39+
"@fileverse-dev/fortune-core": "^1.0.50",
40+
"@fileverse-dev/fortune-react": "^1.0.50",
4141
"@fileverse/ui": "^4.1.7-patch-18",
4242
"classnames": "^2.5.1",
4343
"exceljs": "^4.4.0",
@@ -80,4 +80,4 @@
8080
"typescript": "^5.2.2",
8181
"vite": "^5.0.0"
8282
}
83-
}
83+
}

package/components/editor-workbook.tsx

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
2-
import React, { useEffect, useMemo } from 'react';
2+
import React, { useEffect, useMemo, useRef } from 'react';
33
import { Workbook } from '@fileverse-dev/fortune-react';
44
import { 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,

package/constants/shared-constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const TOOL_BAR_ITEMS = [
3333
'vertical-align',
3434
'|',
3535
'currency-format',
36+
'currency',
3637
'percentage-format',
3738
'number-decrease',
3839
'number-increase',

package/styles/index.css

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@tailwind utilities;
77

88
@layer base {
9+
910
ul,
1011
ol {
1112
list-style: revert;
@@ -46,6 +47,7 @@ body {
4647
}
4748

4849
.fortune-read-only {
50+
4951
.luckysheet-rich-text-editor,
5052
.luckysheet-sheets-item-function,
5153
.fortune-sheettab-button,
@@ -68,8 +70,7 @@ body {
6870
}
6971

7072
/* Hide edit and remove buttons in link toolbar but keep "Open link" */
71-
.fortune-link-modify-modal.link-toolbar
72-
.fortune-toolbar-button:not(:first-child) {
73+
.fortune-link-modify-modal.link-toolbar .fortune-toolbar-button:not(:first-child) {
7374
display: none !important;
7475
}
7576
}
@@ -164,8 +165,4 @@ body {
164165
right: 0;
165166
bottom: 0;
166167
z-index: 1000;
167-
}
168-
169-
.luckysheet-scrollbar-x {
170-
bottom: 12px !important;
171-
}
168+
}

package/utils/after-update-cell.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ const FLVURL_FUNCTIONS = ['FLVURL', 'flvurl'];
3333
interface AfterUpdateCellParams {
3434
row: number;
3535
column: number;
36+
oldValue: Cell;
3637
newValue: Cell;
3738
sheetEditorRef: React.RefObject<WorkbookInstance | null>;
3839
onboardingComplete: boolean | undefined;
3940
setFetchingURLData: (fetching: boolean) => void;
4041
onboardingHandler: OnboardingHandlerType | undefined;
4142
dataBlockApiKeyHandler: DataBlockApiKeyHandlerType | undefined;
4243
setInputFetchURLDataBlock:
43-
| React.Dispatch<React.SetStateAction<string>>
44-
| undefined;
44+
| React.Dispatch<React.SetStateAction<string>>
45+
| undefined;
4546
storeApiKey?: (apiKeyName: string) => void;
4647
onDataBlockApiResponse?: (dataBlockName: string) => void;
4748
setDataBlockCalcFunction?: React.Dispatch<
@@ -432,6 +433,28 @@ export const afterUpdateCell = async (
432433
return;
433434
}
434435

436+
if (!newValue?.m && !newValue?.v) {
437+
sheetEditorRef.current?.setCellValue(params.row, params.column, {
438+
...newValue,
439+
baseValue: undefined,
440+
ct: { fa: '@', t: 's' },
441+
});
442+
}
443+
444+
// @ts-expect-error later
445+
if (newValue?.baseValue && params.oldValue?.baseValue && params.oldValue.m && params.oldValue?.v !== newValue?.v && newValue?.m && newValue?.v) {
446+
const decemialCount = params.oldValue.m?.toString().includes('.') ? params.oldValue.m?.toString().split(' ')[0].split('.')[1].length : 0;
447+
const separatedValue = parseFloat(params.oldValue.m.toString().split(" ")[0] as string);
448+
const coin = params.oldValue?.m?.toString().split(" ")[1]
449+
const price = parseFloat(params.oldValue.v as string) / separatedValue;
450+
const newCell = {
451+
...newValue,
452+
baseValue: parseFloat(newValue.v as string),
453+
m: `${(parseFloat(newValue?.v as string) / price).toFixed(decemialCount)} ${coin}`,
454+
}
455+
sheetEditorRef.current?.setCellValue(params.row, params.column, newCell);
456+
}
457+
435458
// Apply text block formatting if needed
436459
if (shouldApplyTextBlockFormatting(newValue)) {
437460
applyTextBlockFormatting(

package/utils/custom-toolbar-item.tsx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,6 @@ export const getCustomToolbarItems = ({
8787
/>
8888
),
8989
},
90-
{
91-
/*template-button is used in use xocument style */
92-
key: 'ethereum',
93-
tooltip: 'Crypto denominations: Coming soon',
94-
icon: (
95-
<IconButton
96-
className="cursor-not-allowed !min-w-[30px] w-[30px] h-[30px] !px-0 rounded-lg bg-[#E8EBEC] hover:bg-[#E8EBEC]"
97-
icon="Ethereum"
98-
size="md"
99-
variant="ghost"
100-
color="blue"
101-
/>
102-
),
103-
onClick: () => { },
104-
},
10590
{
10691
/*template-button is used in use xocument style */
10792
key: 'fetch-url',

0 commit comments

Comments
 (0)