Skip to content

Commit 68d3ab5

Browse files
euu par contre la y'a plus de variations du marcher.... genre les truc o
1 parent 68e0e90 commit 68d3ab5

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

src/context/market-data-context.tsx

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const MarketDataContext = createContext<MarketDataContextType | undefined>(undef
2626
export const MarketDataProvider = ({ children }: { children: ReactNode }) => {
2727
const [loading, setLoading] = useState(true);
2828
const [assets, setAssets] = useState<AssetsMap>({});
29+
const [initialAssets, setInitialAssets] = useState<AssetsMap>({}); // To store original prices for % change
2930
const [historicalData, setHistoricalData] = useState<HistoricalDataMap>({});
3031

3132
const initializeMarket = useCallback(async () => {
@@ -36,22 +37,27 @@ export const MarketDataProvider = ({ children }: { children: ReactNode }) => {
3637
acc[asset.ticker] = asset;
3738
return acc;
3839
}, {} as AssetsMap);
40+
41+
const initialAssetsMap = JSON.parse(JSON.stringify(assetsMap));
42+
3943
setAssets(assetsMap);
44+
setInitialAssets(initialAssetsMap);
4045

4146
// Generate a flat historical baseline for all assets for charting
4247
const now = Date.now();
4348
const initialHist: HistoricalDataMap = {};
4449
for (const ticker in assetsMap) {
4550
const asset = assetsMap[ticker];
4651
const oneDayAgo = subDays(now, 1);
47-
let currentPrice = asset.price;
4852
const data: HistoricalDataPoint[] = [];
49-
// Generate 24 hours of data points (one per hour)
50-
for (let i = 0; i < 24 * 2; i++) { // 48 points for smoother chart
51-
const timestamp = oneDayAgo + i * 30 * 60 * 1000; // 30 min intervals
52-
data.push({ date: new Date(timestamp).toISOString(), price: currentPrice });
53+
// Generate 48 points for smoother chart
54+
for (let i = 0; i < 48; i++) {
55+
const timestamp = oneDayAgo.getTime() + i * 30 * 60 * 1000; // 30 min intervals
56+
// Add some randomness to the historical data to make it look real
57+
const priceFluctuation = asset.price * (1 + (Math.random() - 0.5) * 0.1);
58+
data.push({ date: new Date(timestamp).toISOString(), price: priceFluctuation });
5359
}
54-
data.push({ date: new Date(now).toISOString(), price: currentPrice });
60+
data.push({ date: new Date(now).toISOString(), price: asset.price });
5561
initialHist[ticker] = data;
5662
}
5763

@@ -62,6 +68,53 @@ export const MarketDataProvider = ({ children }: { children: ReactNode }) => {
6268
useEffect(() => {
6369
initializeMarket();
6470
}, [initializeMarket]);
71+
72+
// EFFECT FOR MARKET SIMULATION
73+
useEffect(() => {
74+
if (loading || Object.keys(initialAssets).length === 0) return;
75+
76+
const simulationInterval = setInterval(() => {
77+
setAssets(currentAssets => {
78+
const newAssets = { ...currentAssets };
79+
const updatedTickers: { [ticker: string]: number } = {};
80+
81+
for (const ticker in newAssets) {
82+
const asset = { ...newAssets[ticker] };
83+
const initialPrice = initialAssets[ticker]?.price;
84+
if (!initialPrice) continue;
85+
86+
// Simulate price change
87+
const volatility = asset.type === 'Crypto' ? 0.0015 : 0.0005;
88+
const changeFactor = 1 + (Math.random() - 0.5) * 2 * volatility;
89+
asset.price *= changeFactor;
90+
updatedTickers[ticker] = asset.price;
91+
92+
// Update 24h change
93+
const change = asset.price - initialPrice;
94+
const changePercent = (change / initialPrice) * 100;
95+
asset.change24h = `${changePercent >= 0 ? '+' : ''}${changePercent.toFixed(2)}%`;
96+
97+
newAssets[ticker] = asset;
98+
}
99+
100+
// Now update historical data based on the new prices
101+
setHistoricalData(currentHistData => {
102+
const newHistData = { ...currentHistData };
103+
for (const ticker in updatedTickers) {
104+
const newPrice = updatedTickers[ticker];
105+
const newPoint = { date: new Date().toISOString(), price: newPrice };
106+
const updatedHistory = [...(newHistData[ticker] || []), newPoint].slice(-100);
107+
newHistData[ticker] = updatedHistory;
108+
}
109+
return newHistData;
110+
});
111+
112+
return newAssets;
113+
});
114+
}, 3000); // Update every 3 seconds
115+
116+
return () => clearInterval(simulationInterval);
117+
}, [loading, initialAssets]);
65118

66119
const getAssetByTicker = useCallback((ticker: string): AssetFromDb | undefined => {
67120
return assets[ticker];

0 commit comments

Comments
 (0)