ui Fix WOW chart and various fixes.

This commit is contained in:
gerlofvanek 2024-11-28 11:32:37 +01:00
parent 31ead537c9
commit 418e863d26

View file

@ -28,7 +28,7 @@ const config = {
} }
}, },
showVolume: false, showVolume: false,
cacheTTL: 5 * 60 * 1000, // 5 minutes in milliseconds cacheTTL: 5 * 60 * 1000, // 5 minutes
specialCoins: [''], specialCoins: [''],
resolutions: { resolutions: {
year: { days: 365, interval: 'month' }, year: { days: 365, interval: 'month' },
@ -151,11 +151,11 @@ const api = {
throw new AppError(`Invalid data structure received from CoinGecko`); throw new AppError(`Invalid data structure received from CoinGecko`);
} }
const transformedData = Object.entries(data).map(([id, values]) => { const transformedData = {};
Object.entries(data).forEach(([id, values]) => {
const coinConfig = config.coins.find(coin => coin.name === id); const coinConfig = config.coins.find(coin => coin.name === id);
return { const symbol = coinConfig?.symbol.toLowerCase() || id;
id, transformedData[symbol] = {
symbol: coinConfig?.symbol.toLowerCase() || id,
current_price: values.usd, current_price: values.usd,
price_btc: values.btc, price_btc: values.btc,
total_volume: values.usd_24h_vol, total_volume: values.usd_24h_vol,
@ -165,15 +165,11 @@ const api = {
}); });
console.log(`Transformed CoinGecko data:`, transformedData); console.log(`Transformed CoinGecko data:`, transformedData);
cache.set(cacheKey, transformedData); cache.set(cacheKey, transformedData);
return transformedData; return transformedData;
} catch (error) { } catch (error) {
console.error(`Error fetching CoinGecko data:`, error); console.error(`Error fetching CoinGecko data:`, error);
return { return { error: error.message };
error: error.message
};
} }
}, },
@ -237,7 +233,7 @@ const api = {
console.log('Final results object:', JSON.stringify(results, null, 2)); console.log('Final results object:', JSON.stringify(results, null, 2));
return results; return results;
}, }
}; };
// Cache // Cache
@ -299,19 +295,28 @@ const ui = {
const volumeElement = document.querySelector(`#${coin.toLowerCase()}-volume-24h`); const volumeElement = document.querySelector(`#${coin.toLowerCase()}-volume-24h`);
const btcPriceDiv = document.querySelector(`#${coin.toLowerCase()}-btc-price-div`); const btcPriceDiv = document.querySelector(`#${coin.toLowerCase()}-btc-price-div`);
const priceBtcElement = document.querySelector(`#${coin.toLowerCase()}-price-btc`); const priceBtcElement = document.querySelector(`#${coin.toLowerCase()}-price-btc`);
if (priceUsdElement) { if (priceUsdElement) {
priceUsdElement.textContent = isError ? 'N/A' : `$ ${ui.formatPrice(coin, priceUSD)}`; priceUsdElement.textContent = isError ? 'N/A' : `$ ${ui.formatPrice(coin, priceUSD)}`;
} }
if (volumeDiv && volumeElement) { if (volumeDiv && volumeElement) {
volumeElement.textContent = isError ? 'N/A' : `${utils.formatNumber(volume24h, 0)} USD`; volumeElement.textContent = isError ? 'N/A' : `${utils.formatNumber(volume24h, 0)} USD`;
volumeDiv.style.display = volumeToggle.isVisible ? 'flex' : 'none'; volumeDiv.style.display = volumeToggle.isVisible ? 'flex' : 'none';
} }
if (btcPriceDiv && priceBtcElement && coin !== 'BTC') {
if (btcPriceDiv && priceBtcElement) {
if (coin === 'BTC') {
btcPriceDiv.style.display = 'none';
} else {
priceBtcElement.textContent = isError ? 'N/A' : `${priceBTC.toFixed(8)} BTC`; priceBtcElement.textContent = isError ? 'N/A' : `${priceBTC.toFixed(8)} BTC`;
btcPriceDiv.style.display = 'flex'; btcPriceDiv.style.display = 'flex';
} }
}
ui.updatePriceChangeContainer(coin, isError ? null : priceChange1d); ui.updatePriceChangeContainer(coin, isError ? null : priceChange1d);
}; };
try { try {
if (data.error) { if (data.error) {
throw new Error(data.error); throw new Error(data.error);
@ -319,14 +324,16 @@ const ui = {
if (!data || !data.current_price) { if (!data || !data.current_price) {
throw new Error(`Invalid CoinGecko data structure for ${coin}`); throw new Error(`Invalid CoinGecko data structure for ${coin}`);
} }
priceUSD = data.current_price; priceUSD = data.current_price;
priceBTC = data.current_price / app.btcPriceUSD; priceBTC = coin === 'BTC' ? 1 : data.price_btc || (data.current_price / app.btcPriceUSD);
priceChange1d = data.price_change_percentage_24h; priceChange1d = data.price_change_percentage_24h;
volume24h = data.total_volume; volume24h = data.total_volume;
if (isNaN(priceUSD) || isNaN(priceBTC) || isNaN(volume24h)) { if (isNaN(priceUSD) || isNaN(priceBTC) || isNaN(volume24h)) {
throw new Error(`Invalid numeric values in data for ${coin}`); throw new Error(`Invalid numeric values in data for ${coin}`);
} }
updateUI(false); updateUI(false);
} catch (error) { } catch (error) {
console.error(`Error displaying data for ${coin}:`, error.message); console.error(`Error displaying data for ${coin}:`, error.message);
@ -511,6 +518,11 @@ initChart: () => {
gradient.addColorStop(0, 'rgba(77, 132, 240, 0.2)'); gradient.addColorStop(0, 'rgba(77, 132, 240, 0.2)');
gradient.addColorStop(1, 'rgba(77, 132, 240, 0)'); gradient.addColorStop(1, 'rgba(77, 132, 240, 0)');
const formatTime = (date) => {
const hours = date.getHours().toString().padStart(2, '0');
return `${hours}:00`;
};
chartModule.chart = new Chart(ctx, { chartModule.chart = new Chart(ctx, {
type: 'line', type: 'line',
data: { data: {
@ -536,20 +548,37 @@ initChart: () => {
x: { x: {
type: 'time', type: 'time',
time: { time: {
unit: 'day', unit: 'hour',
displayFormats: { displayFormats: {
hour: 'ha', hour: 'HH:00',
day: 'MMM d' day: 'MMM d',
month: 'MMM yyyy'
} }
}, },
ticks: { ticks: {
source: 'data', source: 'data',
maxTicksLimit: 10, maxTicksLimit: 12,
font: { font: {
size: 12, size: 12,
family: "'Inter', sans-serif" family: "'Inter', sans-serif"
}, },
color: 'rgba(156, 163, 175, 1)' color: 'rgba(156, 163, 175, 1)',
callback: function(value) {
const date = new Date(value);
if (config.currentResolution === 'day') {
return formatTime(date);
} else if (config.currentResolution === 'year') {
return date.toLocaleDateString('en-US', {
month: 'short',
year: 'numeric'
});
} else {
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric'
});
}
}
}, },
grid: { grid: {
display: false display: false
@ -588,15 +617,23 @@ initChart: () => {
callbacks: { callbacks: {
title: (tooltipItems) => { title: (tooltipItems) => {
const date = new Date(tooltipItems[0].parsed.x); const date = new Date(tooltipItems[0].parsed.x);
return date.toLocaleString('en-US', { if (config.currentResolution === 'day') {
return `${date.toLocaleDateString('en-US', {
month: 'short', month: 'short',
day: 'numeric', day: 'numeric'
})} ${formatTime(date)}`;
} else if (config.currentResolution === 'year') {
return date.toLocaleDateString('en-US', {
year: 'numeric', year: 'numeric',
hour: 'numeric', month: 'short',
minute: 'numeric', day: 'numeric'
hour12: true,
timeZone: 'UTC'
}); });
} else {
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric'
});
}
}, },
label: (item) => { label: (item) => {
const value = item.parsed.y; const value = item.parsed.y;
@ -616,7 +653,6 @@ initChart: () => {
borderWidth: 1, borderWidth: 1,
radius: 2, radius: 2,
hoverRadius: 4, hoverRadius: 4,
hoverRadius: 4,
hitRadius: 6, hitRadius: 6,
hoverBorderWidth: 2 hoverBorderWidth: 2
}, },
@ -634,8 +670,6 @@ initChart: () => {
}, },
prepareChartData: (coinSymbol, data) => { prepareChartData: (coinSymbol, data) => {
console.log(`Preparing chart data for ${coinSymbol}:`, JSON.stringify(data, null, 2));
if (!data) { if (!data) {
console.error(`No data received for ${coinSymbol}`); console.error(`No data received for ${coinSymbol}`);
return []; return [];
@ -644,7 +678,44 @@ initChart: () => {
try { try {
let preparedData; let preparedData;
if (data.Data && Array.isArray(data.Data)) { if (coinSymbol === 'WOW' && Array.isArray(data)) {
const endTime = new Date(data[data.length - 1][0]);
// Convert to local time
endTime.setMinutes(0, 0, 0);
const endUnix = endTime.getTime();
const startUnix = endUnix - (24 * 3600000);
const hourlyPoints = [];
for (let hourUnix = startUnix; hourUnix <= endUnix; hourUnix += 3600000) {
const targetHour = new Date(hourUnix);
targetHour.setMinutes(0, 0, 0);
const closestPoint = data.reduce((prev, curr) => {
const prevTime = new Date(prev[0]);
const currTime = new Date(curr[0]);
const prevDiff = Math.abs(prevTime - targetHour);
const currDiff = Math.abs(currTime - targetHour);
return currDiff < prevDiff ? curr : prev;
});
hourlyPoints.push({
x: targetHour,
y: closestPoint[1]
});
}
const lastTime = new Date(data[data.length - 1][0]);
if (lastTime.getMinutes() !== 0) {
hourlyPoints.push({
x: lastTime,
y: data[data.length - 1][1]
});
}
preparedData = hourlyPoints;
} else if (data.Data && Array.isArray(data.Data)) {
preparedData = data.Data.map(d => ({ preparedData = data.Data.map(d => ({
x: new Date(d.time * 1000), x: new Date(d.time * 1000),
y: d.close y: d.close
@ -664,7 +735,6 @@ initChart: () => {
return []; return [];
} }
console.log(`Prepared data for ${coinSymbol}:`, preparedData.slice(0, 5));
return preparedData; return preparedData;
} catch (error) { } catch (error) {
console.error(`Error preparing chart data for ${coinSymbol}:`, error); console.error(`Error preparing chart data for ${coinSymbol}:`, error);
@ -837,8 +907,8 @@ const app = {
disabled: 'Auto-refresh: disabled', disabled: 'Auto-refresh: disabled',
justRefreshed: 'Just refreshed', justRefreshed: 'Just refreshed',
}, },
cacheTTL: 15 * 60 * 1000, // 15 minutes in milliseconds cacheTTL: 5 * 60 * 1000, // 5 minutes
minimumRefreshInterval: 60 * 1000, // 1 minute in milliseconds minimumRefreshInterval: 60 * 1000, // 1 minute
init: () => { init: () => {
console.log('Initializing app...'); console.log('Initializing app...');
@ -898,21 +968,22 @@ const app = {
} }
for (const coin of config.coins) { for (const coin of config.coins) {
const coinData = allCoinData.find(data => data.symbol.toUpperCase() === coin.symbol); const coinData = allCoinData[coin.symbol.toLowerCase()];
if (coinData) { if (coinData) {
coinData.displayName = coin.displayName || coin.symbol; coinData.displayName = coin.displayName || coin.symbol;
ui.displayCoinData(coin.symbol, coinData); ui.displayCoinData(coin.symbol, coinData);
const cacheKey = `coinData_${coin.symbol}`; const cacheKey = `coinData_${coin.symbol}`;
cache.set(cacheKey, coinData); cache.set(cacheKey, coinData);
} else { } else {
console.error(`No data found for ${coin.symbol}`); console.warn(`No data found for ${coin.symbol}`);
} }
} }
} catch (error) { } catch (error) {
console.error('Error loading all coin data:', error); console.error('Error loading all coin data:', error);
ui.displayErrorMessage('Failed to load coin data. Please try refreshing the page.'); ui.displayErrorMessage('Failed to load coin data. Please try refreshing the page.');
} } finally {
console.log('All coin data loaded'); console.log('All coin data loaded');
}
}, },
loadCoinData: async (coin) => { loadCoinData: async (coin) => {
@ -1021,7 +1092,6 @@ const app = {
} }
} catch (error) { } catch (error) {
console.error(`Error parsing cached item ${key}:`, error); console.error(`Error parsing cached item ${key}:`, error);
// Remove corrupted cache item
localStorage.removeItem(key); localStorage.removeItem(key);
} }
} }
@ -1057,10 +1127,33 @@ const app = {
app.isRefreshing = true; app.isRefreshing = true;
ui.showLoader(); ui.showLoader();
chartModule.showChartLoader(); chartModule.showChartLoader();
try { try {
cache.clear(); cache.clear();
await app.updateBTCPrice(); await app.updateBTCPrice();
await app.loadAllCoinData();
const allCoinData = await api.fetchCoinGeckoDataXHR();
if (allCoinData.error) {
throw new Error(allCoinData.error);
}
for (const coin of config.coins) {
const symbol = coin.symbol.toLowerCase();
const coinData = allCoinData[symbol];
if (coinData) {
coinData.displayName = coin.displayName || coin.symbol;
ui.displayCoinData(coin.symbol, coinData);
const cacheKey = `coinData_${coin.symbol}`;
cache.set(cacheKey, coinData);
} else {
console.error(`No data found for ${coin.symbol}`);
}
}
if (chartModule.currentCoin) { if (chartModule.currentCoin) {
await chartModule.updateChart(chartModule.currentCoin, true); await chartModule.updateChart(chartModule.currentCoin, true);
} }
@ -1068,7 +1161,9 @@ const app = {
app.lastRefreshedTime = new Date(); app.lastRefreshedTime = new Date();
localStorage.setItem('lastRefreshedTime', app.lastRefreshedTime.getTime().toString()); localStorage.setItem('lastRefreshedTime', app.lastRefreshedTime.getTime().toString());
ui.updateLastRefreshedTime(); ui.updateLastRefreshedTime();
console.log('All data refreshed successfully'); console.log('All data refreshed successfully');
} catch (error) { } catch (error) {
console.error('Error refreshing all data:', error); console.error('Error refreshing all data:', error);
ui.displayErrorMessage('Failed to refresh all data. Please try again.'); ui.displayErrorMessage('Failed to refresh all data. Please try again.');
@ -1176,14 +1271,15 @@ const app = {
updateBTCPrice: async () => { updateBTCPrice: async () => {
console.log('Updating BTC price...'); console.log('Updating BTC price...');
try { try {
const btcData = await api.fetchCoinGeckoDataXHR('bitcoin'); const priceData = await api.fetchCoinGeckoDataXHR();
if (btcData.error) { if (priceData.error) {
console.error('Error fetching BTC price:', btcData.error); console.error('Error fetching BTC price:', priceData.error);
app.btcPriceUSD = 0; app.btcPriceUSD = 0;
} else if (btcData[0] && btcData[0].current_price) { } else if (priceData.btc && priceData.btc.current_price) {
app.btcPriceUSD = btcData[0].current_price;
app.btcPriceUSD = priceData.btc.current_price;
} else { } else {
console.error('Unexpected BTC data structure:', btcData); console.error('Unexpected BTC data structure:', priceData);
app.btcPriceUSD = 0; app.btcPriceUSD = 0;
} }
} catch (error) { } catch (error) {