Skip to content

Commit 3326c6a

Browse files
committed
trying to DRY current prices without react context
1 parent 56de8e5 commit 3326c6a

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

docs/snippets/CurrentPricesTable.jsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { usePrices, weiToTokens, formatPrice, LitActionPriceComponent, NodePriceMeasurement } from './PriceProvider';
1+
import { weiToTokens, formatPrice, LitActionPriceComponent, NodePriceMeasurement } from './PriceProvider';
22

3-
export const CurrentPricesTable = () => {
3+
export const CurrentPricesTable = ({ priceData }) => {
44
// Product IDs
55
const ProductId = {
66
PkpSign: 0,
@@ -44,6 +44,14 @@ export const CurrentPricesTable = () => {
4444
[NodePriceMeasurement.perCount]: '/count',
4545
};
4646

47+
if (!priceData) {
48+
return (
49+
<div style={{ padding: '20px', textAlign: 'center' }}>
50+
<p>Price data not available. Please wrap this component with PriceProvider.</p>
51+
</div>
52+
);
53+
}
54+
4755
const {
4856
loading,
4957
error,
@@ -55,7 +63,7 @@ export const CurrentPricesTable = () => {
5563
usagePercent,
5664
pkpMintCost,
5765
ethers,
58-
} = usePrices();
66+
} = priceData;
5967

6068
if (loading) {
6169
return (

docs/snippets/ExampleLitActionCosts.jsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import { usePrices, weiToTokens, formatPrice, LitActionPriceComponent, NodePriceMeasurement } from './PriceProvider';
1+
import { weiToTokens, formatPrice, LitActionPriceComponent, NodePriceMeasurement } from './PriceProvider';
2+
3+
export const ExampleLitActionCosts = ({ priceData }) => {
4+
if (!priceData) {
5+
return (
6+
<div style={{ padding: '20px', textAlign: 'center' }}>
7+
<p>Price data not available. Please wrap this component with PriceProvider.</p>
8+
</div>
9+
);
10+
}
211

3-
export const ExampleLitActionCosts = () => {
412
const {
513
loading,
614
error,
715
litActionConfigs,
816
litKeyPriceUSD,
917
ethers,
10-
} = usePrices();
18+
} = priceData;
1119

1220
// Helper to get price for a specific component
1321
const getComponentPrice = (componentType, measurementType) => {

docs/snippets/PriceProvider.jsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { createContext, useContext, useEffect, useState } from 'react';
2-
3-
// Create context
4-
const PriceContext = createContext(null);
1+
import { useEffect, useState, Children, cloneElement } from 'react';
52

63
// Constants - defined inside component for Mintlify compatibility
74
const NAGA_PROD_PRICE_FEED_ADDRESS = '0x88F5535Fa6dA5C225a3C06489fE4e3405b87608C';
@@ -283,7 +280,7 @@ export const PriceProvider = ({ children }) => {
283280
fetchPrices();
284281
}, [ethersLoaded]);
285282

286-
const value = {
283+
const priceData = {
287284
loading,
288285
error,
289286
basePrices,
@@ -296,14 +293,17 @@ export const PriceProvider = ({ children }) => {
296293
ethers: window.ethers,
297294
};
298295

299-
return <PriceContext.Provider value={value}>{children}</PriceContext.Provider>;
300-
};
301-
302-
export const usePrices = () => {
303-
const context = useContext(PriceContext);
304-
if (!context) {
305-
throw new Error('usePrices must be used within a PriceProvider');
296+
// Clone children and pass price data as props
297+
// Handle both single child and multiple children
298+
if (!children) {
299+
return null;
306300
}
307-
return context;
301+
302+
return Children.map(children, (child) => {
303+
if (child && typeof child === 'object' && 'type' in child && typeof child.type !== 'string') {
304+
return cloneElement(child, { priceData });
305+
}
306+
return child;
307+
});
308308
};
309309

0 commit comments

Comments
 (0)