@fileverse-dev/fortune-react 1.0.49 → 1.0.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { Settings, Context, CellWithRowAndCol, Sheet as SheetType, Op, CellMatrix, api } from "@fileverse-dev/fortune-core";
2
2
  import React from "react";
3
3
  import "./index.css";
4
+ import { getCryptoPrice } from "../../utils/cryptoApi";
4
5
  import { generateAPIs } from "./api";
5
6
  export type WorkbookInstance = ReturnType<typeof generateAPIs>;
6
7
  type AdditionalProps = {
@@ -9,7 +10,7 @@ type AdditionalProps = {
9
10
  };
10
11
  declare const Workbook: React.ForwardRefExoticComponent<Settings & AdditionalProps & React.RefAttributes<{
11
12
  applyOp: (ops: Op[]) => void;
12
- getCryptoPrice: typeof import("../../utils/cryptoApi").getCryptoPrice;
13
+ getCryptoPrice: typeof getCryptoPrice;
13
14
  getCellValue: (row: number, column: number, options?: api.CommonOptions & {
14
15
  type?: "rt" | "m" | "v" | "mc" | "f" | "ct" | "qp" | "spl" | "bg" | "lo" | "ps" | "hl" | keyof import("@fileverse-dev/fortune-core").CellStyle | undefined;
15
16
  }) => any;
@@ -19,6 +19,7 @@ import { defaultContext, defaultSettings, initSheetIndex, handleGlobalKeyDown, g
19
19
  import React, { useMemo, useState, useCallback, useEffect, useRef, useImperativeHandle } from "react";
20
20
  import "./index.css";
21
21
  import produce, { applyPatches, enablePatches, produceWithPatches } from "immer";
22
+ import { getCryptoPrice } from "../../utils/cryptoApi";
22
23
  import _ from "lodash";
23
24
  import Sheet from "../Sheet";
24
25
  import WorkbookContext from "../../context";
@@ -322,6 +323,9 @@ var Workbook = /*#__PURE__*/React.forwardRef(function (_a, ref) {
322
323
  var _a, _b;
323
324
  (_b = (_a = mergedSettings.hooks) === null || _a === void 0 ? void 0 : _a.afterActivateSheet) === null || _b === void 0 ? void 0 : _b.call(_a, context.currentSheetId);
324
325
  }, [context.currentSheetId]);
326
+ useEffect(function () {
327
+ getCryptoPrice("bitcoin", "usd");
328
+ }, []);
325
329
  useEffect(function () {
326
330
  var _a, _b;
327
331
  setContext(function (ctx) {
@@ -113,42 +113,69 @@ var __generator = this && this.__generator || function (thisArg, body) {
113
113
  };
114
114
  }
115
115
  };
116
- var COINGECKO_API = "https://api.coingecko.com/api/v3/simple/price";
116
+ var COINGECKO_API = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,solana&vs_currencies=usd,eur,gbp,jpy,cny,inr,cad,aud,nzd,sek,pln";
117
117
  var CACHE_DURATION = 60 * 1000;
118
+ var CRYPTO_LIST = ["bitcoin", "ethereum", "solana"];
119
+ var FIAT_LIST = ["usd", "eur", "gbp", "jpy", "cny", "inr", "cad", "aud", "nzd", "sek", "pln"];
118
120
  var priceCache = {};
119
121
  export function getCryptoPrice(crypto, fiat) {
120
122
  var _a;
121
123
  return __awaiter(this, void 0, void 0, function () {
122
- var key, now, url, resp, data, price;
124
+ var key, now, data, price;
123
125
  return __generator(this, function (_b) {
124
126
  switch (_b.label) {
125
127
  case 0:
126
128
  key = "".concat(crypto, "-").concat(fiat).toLowerCase();
127
129
  now = Date.now();
128
- console.log("Fetching crypto price for ".concat(key), priceCache);
129
130
  if (priceCache[key] && now - priceCache[key].timestamp < CACHE_DURATION) {
130
- console.log("Using cached price for ".concat(key));
131
+ return [2, priceCache[key].price];
132
+ } else if (priceCache[key]) {
133
+ fetchCall(COINGECKO_API).then(function (data) {
134
+ updatePriceCache(data, now);
135
+ });
131
136
  return [2, priceCache[key].price];
132
137
  }
133
- url = "".concat(COINGECKO_API, "?ids=").concat(crypto, "&vs_currencies=").concat(fiat);
134
- return [4, fetch(url)];
138
+ return [4, fetchCall(COINGECKO_API)];
135
139
  case 1:
136
- resp = _b.sent();
137
- if (!resp.ok) throw new Error("Failed to fetch crypto price");
138
- return [4, resp.json()];
139
- case 2:
140
140
  data = _b.sent();
141
141
  price = (_a = data[crypto]) === null || _a === void 0 ? void 0 : _a[fiat];
142
142
  if (typeof price !== "number") throw new Error("Invalid price data");
143
- priceCache[key] = {
144
- price: price,
145
- timestamp: now
146
- };
143
+ updatePriceCache(data, now);
147
144
  return [2, price];
148
145
  }
149
146
  });
150
147
  });
151
148
  }
149
+ function updatePriceCache(data, timestamp) {
150
+ CRYPTO_LIST.forEach(function (crypto) {
151
+ FIAT_LIST.forEach(function (fiat) {
152
+ var _a;
153
+ var price = (_a = data[crypto]) === null || _a === void 0 ? void 0 : _a[fiat];
154
+ if (typeof price === "number") {
155
+ var key = "".concat(crypto, "-").concat(fiat).toLowerCase();
156
+ priceCache[key] = {
157
+ price: price,
158
+ timestamp: timestamp
159
+ };
160
+ }
161
+ });
162
+ });
163
+ }
164
+ var fetchCall = function fetchCall(url) {
165
+ return __awaiter(void 0, void 0, void 0, function () {
166
+ return __generator(this, function (_a) {
167
+ return [2, new Promise(function (resolve, reject) {
168
+ fetch(url).then(function (response) {
169
+ return response.json();
170
+ }).then(function (data) {
171
+ return resolve(data);
172
+ }).catch(function (error) {
173
+ return reject(error);
174
+ });
175
+ })];
176
+ });
177
+ });
178
+ };
152
179
  export function clearCryptoPriceCache() {
153
180
  Object.keys(priceCache).forEach(function (key) {
154
181
  return delete priceCache[key];
@@ -1,6 +1,7 @@
1
1
  import { Settings, Context, CellWithRowAndCol, Sheet as SheetType, Op, CellMatrix, api } from "@fileverse-dev/fortune-core";
2
2
  import React from "react";
3
3
  import "./index.css";
4
+ import { getCryptoPrice } from "../../utils/cryptoApi";
4
5
  import { generateAPIs } from "./api";
5
6
  export type WorkbookInstance = ReturnType<typeof generateAPIs>;
6
7
  type AdditionalProps = {
@@ -9,7 +10,7 @@ type AdditionalProps = {
9
10
  };
10
11
  declare const Workbook: React.ForwardRefExoticComponent<Settings & AdditionalProps & React.RefAttributes<{
11
12
  applyOp: (ops: Op[]) => void;
12
- getCryptoPrice: typeof import("../../utils/cryptoApi").getCryptoPrice;
13
+ getCryptoPrice: typeof getCryptoPrice;
13
14
  getCellValue: (row: number, column: number, options?: api.CommonOptions & {
14
15
  type?: "rt" | "m" | "v" | "mc" | "f" | "ct" | "qp" | "spl" | "bg" | "lo" | "ps" | "hl" | keyof import("@fileverse-dev/fortune-core").CellStyle | undefined;
15
16
  }) => any;
@@ -9,6 +9,7 @@ var _fortuneCore = require("@fileverse-dev/fortune-core");
9
9
  var _react = _interopRequireWildcard(require("react"));
10
10
  require("./index.css");
11
11
  var _immer = _interopRequireWildcard(require("immer"));
12
+ var _cryptoApi = require("../../utils/cryptoApi");
12
13
  var _lodash = _interopRequireDefault(require("lodash"));
13
14
  var _Sheet = _interopRequireDefault(require("../Sheet"));
14
15
  var _context = _interopRequireDefault(require("../../context"));
@@ -331,6 +332,9 @@ var Workbook = /*#__PURE__*/_react.default.forwardRef(function (_a, ref) {
331
332
  var _a, _b;
332
333
  (_b = (_a = mergedSettings.hooks) === null || _a === void 0 ? void 0 : _a.afterActivateSheet) === null || _b === void 0 ? void 0 : _b.call(_a, context.currentSheetId);
333
334
  }, [context.currentSheetId]);
335
+ (0, _react.useEffect)(function () {
336
+ (0, _cryptoApi.getCryptoPrice)("bitcoin", "usd");
337
+ }, []);
334
338
  (0, _react.useEffect)(function () {
335
339
  var _a, _b;
336
340
  setContext(function (ctx) {
@@ -120,42 +120,69 @@ var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
120
120
  };
121
121
  }
122
122
  };
123
- var COINGECKO_API = "https://api.coingecko.com/api/v3/simple/price";
123
+ var COINGECKO_API = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,solana&vs_currencies=usd,eur,gbp,jpy,cny,inr,cad,aud,nzd,sek,pln";
124
124
  var CACHE_DURATION = 60 * 1000;
125
+ var CRYPTO_LIST = ["bitcoin", "ethereum", "solana"];
126
+ var FIAT_LIST = ["usd", "eur", "gbp", "jpy", "cny", "inr", "cad", "aud", "nzd", "sek", "pln"];
125
127
  var priceCache = {};
126
128
  function getCryptoPrice(crypto, fiat) {
127
129
  var _a;
128
130
  return __awaiter(this, void 0, void 0, function () {
129
- var key, now, url, resp, data, price;
131
+ var key, now, data, price;
130
132
  return __generator(this, function (_b) {
131
133
  switch (_b.label) {
132
134
  case 0:
133
135
  key = "".concat(crypto, "-").concat(fiat).toLowerCase();
134
136
  now = Date.now();
135
- console.log("Fetching crypto price for ".concat(key), priceCache);
136
137
  if (priceCache[key] && now - priceCache[key].timestamp < CACHE_DURATION) {
137
- console.log("Using cached price for ".concat(key));
138
+ return [2, priceCache[key].price];
139
+ } else if (priceCache[key]) {
140
+ fetchCall(COINGECKO_API).then(function (data) {
141
+ updatePriceCache(data, now);
142
+ });
138
143
  return [2, priceCache[key].price];
139
144
  }
140
- url = "".concat(COINGECKO_API, "?ids=").concat(crypto, "&vs_currencies=").concat(fiat);
141
- return [4, fetch(url)];
145
+ return [4, fetchCall(COINGECKO_API)];
142
146
  case 1:
143
- resp = _b.sent();
144
- if (!resp.ok) throw new Error("Failed to fetch crypto price");
145
- return [4, resp.json()];
146
- case 2:
147
147
  data = _b.sent();
148
148
  price = (_a = data[crypto]) === null || _a === void 0 ? void 0 : _a[fiat];
149
149
  if (typeof price !== "number") throw new Error("Invalid price data");
150
- priceCache[key] = {
151
- price: price,
152
- timestamp: now
153
- };
150
+ updatePriceCache(data, now);
154
151
  return [2, price];
155
152
  }
156
153
  });
157
154
  });
158
155
  }
156
+ function updatePriceCache(data, timestamp) {
157
+ CRYPTO_LIST.forEach(function (crypto) {
158
+ FIAT_LIST.forEach(function (fiat) {
159
+ var _a;
160
+ var price = (_a = data[crypto]) === null || _a === void 0 ? void 0 : _a[fiat];
161
+ if (typeof price === "number") {
162
+ var key = "".concat(crypto, "-").concat(fiat).toLowerCase();
163
+ priceCache[key] = {
164
+ price: price,
165
+ timestamp: timestamp
166
+ };
167
+ }
168
+ });
169
+ });
170
+ }
171
+ var fetchCall = function fetchCall(url) {
172
+ return __awaiter(void 0, void 0, void 0, function () {
173
+ return __generator(this, function (_a) {
174
+ return [2, new Promise(function (resolve, reject) {
175
+ fetch(url).then(function (response) {
176
+ return response.json();
177
+ }).then(function (data) {
178
+ return resolve(data);
179
+ }).catch(function (error) {
180
+ return reject(error);
181
+ });
182
+ })];
183
+ });
184
+ });
185
+ };
159
186
  function clearCryptoPriceCache() {
160
187
  Object.keys(priceCache).forEach(function (key) {
161
188
  return delete priceCache[key];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fileverse-dev/fortune-react",
3
- "version": "1.0.49",
3
+ "version": "1.0.51",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "module": "es/index.js",
@@ -16,7 +16,7 @@
16
16
  "tsc": "tsc"
17
17
  },
18
18
  "dependencies": {
19
- "@fileverse-dev/fortune-core": "1.0.49",
19
+ "@fileverse-dev/fortune-core": "1.0.51",
20
20
  "@fileverse/ui": "^4.1.7-patch-16",
21
21
  "@tippyjs/react": "^4.2.6",
22
22
  "@types/regenerator-runtime": "^0.13.6",