Skip to content

Commit b0330db

Browse files
feat(explorer): new version integration (#1683)
Co-authored-by: Uriel Mihura <[email protected]>
1 parent 76954ee commit b0330db

File tree

25 files changed

+737
-429
lines changed

25 files changed

+737
-429
lines changed

explorer/assets/css/tooltip.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
.chart-tooltip-dot {
99
height: 10px;
1010
width: 10px;
11-
top: -5px;
11+
bottom: -5px;
1212
border-radius: 100%;
13-
background-color: hsl(var(--foreground));
14-
opacity: 0.2;
13+
background-color: hsl(var(--accent));
1514
position: absolute;
1615
transition: all 0.2s;
1716
}
1817

1918
.chart-tooltip-dot:hover {
20-
opacity: 0.5;
2119
transform: scale(1.2);
2220
cursor: pointer;
2321
}
@@ -26,7 +24,7 @@
2624
min-height: 50px;
2725
min-width: 250px;
2826
padding: 20px;
29-
margin-top: 8px;
27+
margin-bottom: 8px;
3028
background-color: hsl(var(--card));
3129
border-radius: 8px;
3230
border-width: 1px;
@@ -49,6 +47,7 @@
4947
.chart-tooltip-item {
5048
display: flex;
5149
justify-content: space-between;
50+
gap: 5px;
5251
width: 100%;
5352
}
5453

@@ -59,3 +58,4 @@
5958
.chart-tooltip-item-value {
6059
color: hsl(var(--foreground));
6160
}
61+

explorer/assets/vendor/charts/batch_size.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
import { yTickCallbackShowMinAndMaxValues } from "./helpers";
12
import { alignedTooltip } from "./tooltip";
23

34
export const batchSizeCustomOptions = (options, data) => {
45
// show only min and max values
5-
options.scales.y.ticks.callback = (_value, index, values) => {
6-
const dataY = data.datasets[0].data.map((point) => parseFloat(point.y));
7-
if (index === 0) return `${Math.min(...dataY)} proofs`;
8-
if (index === values.length - 1) {
9-
return `${Math.max(...dataY)} proofs`;
10-
}
11-
return "";
12-
};
6+
options.scales.y.ticks.callback = yTickCallbackShowMinAndMaxValues(
7+
data,
8+
(val) => `${val} proofs`
9+
);
1310

1411
// show age min, mean and max age in x axis
1512
options.scales.x.ticks.callback = (_value, index, values) => {
@@ -23,9 +20,10 @@ export const batchSizeCustomOptions = (options, data) => {
2320

2421
options.plugins.tooltip.external = (context) =>
2522
alignedTooltip(context, {
23+
name: "batch-size",
2624
title: "Batch size",
2725
items: [
28-
{ title: "Cost", id: "cost" },
26+
{ title: "Fee per proof", id: "cost" },
2927
{ title: "Age", id: "age" },
3028
{ title: "Merkle root", id: "merkle_root" },
3129
{ title: "Block number", id: "block_number" },
@@ -40,16 +38,15 @@ export const batchSizeCustomOptions = (options, data) => {
4038
onTooltipUpdate: (tooltipModel) => {
4139
const dataset = tooltipModel.dataPoints[0].dataset;
4240
const idx = tooltipModel.dataPoints[0].dataIndex;
43-
44-
const cost = `${dataset.data[idx].y} USD`;
41+
const amount_of_proofs = dataset.data[idx].y;
4542
const age = dataset.age[idx];
4643
const merkleRootHash = dataset.merkle_root[idx];
4744
const merkle_root = `${merkleRootHash.slice(
4845
0,
4946
6
5047
)}...${merkleRootHash.slice(merkleRootHash.length - 4)}`;
5148
const block_number = dataset.data[idx].x;
52-
const amount_of_proofs = dataset.amount_of_proofs[idx];
49+
const cost = `${dataset.fee_per_proof[idx]} USD`;
5350

5451
return {
5552
cost,

explorer/assets/vendor/charts/cost_per_proof.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
import { yTickCallbackShowMinAndMaxValues } from "./helpers";
12
import { alignedTooltip } from "./tooltip";
23

34
export const costPerProofCustomOptions = (options, data) => {
4-
// show only min and max values
5-
options.scales.y.ticks.callback = (_value, index, values) => {
6-
const dataY = data.datasets[0].data.map((point) => parseFloat(point.y));
7-
if (index === 0) return `${Math.min(...dataY)} USD`;
8-
if (index === values.length - 1) {
9-
return `${Math.max(...dataY)} USD`;
10-
}
11-
return "";
12-
};
5+
// show only 0, min and max values
6+
options.scales.y.ticks.callback = yTickCallbackShowMinAndMaxValues(
7+
data,
8+
(val) => `${val} USD`
9+
);
1310

1411
// show age min, mean and max age in x axis
1512
options.scales.x.ticks.callback = (_value, index, values) => {
@@ -23,9 +20,10 @@ export const costPerProofCustomOptions = (options, data) => {
2320

2421
options.plugins.tooltip.external = (context) =>
2522
alignedTooltip(context, {
23+
name: "cost-per-proof",
2624
title: "Cost per proof",
2725
items: [
28-
{ title: "Cost", id: "cost" },
26+
{ title: "Fee per proof", id: "cost" },
2927
{ title: "Age", id: "age" },
3028
{ title: "Merkle root", id: "merkle_root" },
3129
{ title: "Block number", id: "block_number" },
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const findClosestIndex = (target, values) => {
2+
let closestIndex = 0;
3+
let smallestDiff = Math.abs(values[0] - target);
4+
for (let i = 1; i < values.length; i++) {
5+
const diff = Math.abs(values[i] - target);
6+
if (diff < smallestDiff) {
7+
closestIndex = i;
8+
smallestDiff = diff;
9+
}
10+
}
11+
return closestIndex;
12+
};
13+
14+
/**
15+
* A callback function to customize y-axis tick labels by showing only the zero, minimum and maximum data values.
16+
*
17+
* @param {Object} data - The chart data object containing datasets and their values.
18+
* @param {Function} renderText - A function to format and render text for the tick labels.
19+
* @returns {Function} - A callback function for Chart.js tick customization.
20+
*
21+
* The returned function compares the current tick index with the indices of the values closest
22+
* to the minimum and maximum data points, and displays these values formatted using the
23+
* `renderText` function.
24+
*
25+
* @example
26+
* options.scales.y.ticks.callback = yTickCallbackShowMinAndMaxValues(data, (val) => `${val} USD`);
27+
*/
28+
export const yTickCallbackShowMinAndMaxValues =
29+
(data, renderText) => (_value, index, values) => {
30+
if (index === 0) return renderText(0);
31+
32+
const dataY = data.datasets[0].data.map((point) => parseFloat(point.y));
33+
const sortedData = dataY.sort((a, b) => b - a);
34+
const min = sortedData[0];
35+
const max = sortedData[sortedData.length - 1];
36+
const valsData = values.map((item) => item.value);
37+
const idxClosestToMin = findClosestIndex(min, valsData);
38+
const idxClosestToMax = findClosestIndex(max, valsData);
39+
40+
if (index == idxClosestToMin) return renderText(min);
41+
if (index == idxClosestToMax) return renderText(max);
42+
return "";
43+
};

explorer/assets/vendor/charts/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const applyOptionsByChartId = (id, options, data) => {
2121
export default {
2222
mounted() {
2323
this.initChart();
24-
window.addEventListener("theme-changed", this.reinitChart.bind(this));
24+
window.addEventListener("resize", this.resizeChart.bind(this));
2525
},
2626

2727
updated() {
@@ -31,9 +31,8 @@ export default {
3131
destroyed() {
3232
if (this.chart) {
3333
this.chart.destroy();
34+
window.removeEventListener("resize", this.resizeChart.bind(this));
3435
}
35-
36-
window.removeEventListener("theme-changed", this.reinitChart.bind(this));
3736
},
3837

3938
initChart() {
@@ -51,6 +50,8 @@ export default {
5150
data,
5251
options,
5352
});
53+
54+
this.resizeChart();
5455
},
5556

5657
reinitChart() {
@@ -59,4 +60,10 @@ export default {
5960
}
6061
this.initChart();
6162
},
63+
64+
resizeChart() {
65+
if (this.chart) {
66+
this.chart.resize();
67+
}
68+
},
6269
};

explorer/assets/vendor/charts/tooltip.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const tooltipComponent = ({ title, isTooltipClickable, items }) => `
2626
*
2727
* @param {Object} context - The chart.js context, typically passed as `this` within chart hooks.
2828
* @param {Object} params - An object containing configuration for the tooltip.
29+
* @param {Object} params.name - A string that serves as an identifier for the tooltip.
2930
* @param {string} params.title - The title text to display in the tooltip.
3031
* @param {Array} params.items - An array of items (with ids) to be displayed inside the tooltip.
3132
* @param {Array} params.onTooltipClick - A callback that receives `tooltipModel` and gets triggered when the tooltip is clicked.
@@ -36,6 +37,7 @@ const tooltipComponent = ({ title, isTooltipClickable, items }) => `
3637
*
3738
* @example
3839
* alignedTooltip(context, {
40+
* name: "my-chart",
3941
* title: "Tooltip Title",
4042
* items: [{ title: "Cost", id: "cost_id" }, { title: "Timestamp", id: "timestamp_id" }],
4143
* onTooltipClick: (tooltipModel) => {
@@ -58,15 +60,15 @@ const tooltipComponent = ({ title, isTooltipClickable, items }) => `
5860
*/
5961
export const alignedTooltip = (
6062
context,
61-
{ title, items, onTooltipClick, onTooltipUpdate }
63+
{ name, title, items, onTooltipClick, onTooltipUpdate }
6264
) => {
6365
const tooltipModel = context.tooltip;
64-
let tooltipEl = document.getElementById("chartjs-tooltip");
66+
let tooltipEl = document.getElementById(`chartjs-tooltip-${name}`);
6567
if (!tooltipEl) {
6668
tooltipEl = document.createElement("div");
6769
tooltipEl.style = "transition: opacity 0.3s;";
6870
tooltipEl.style = "transition: left 0.1s;";
69-
tooltipEl.id = "chartjs-tooltip";
71+
tooltipEl.id = `chartjs-tooltip-${name}`;
7072
tooltipEl.innerHTML = tooltipComponent({
7173
title,
7274
isTooltipClickable: !!onTooltipClick,
@@ -81,6 +83,10 @@ export const alignedTooltip = (
8183
tooltipEl.style.opacity = 0;
8284
tooltipEl.style.zIndex = -1;
8385
};
86+
// this is needed to maintain responsiveness
87+
window.addEventListener("resize", () => {
88+
tooltipEl.remove();
89+
});
8490
if (onTooltipClick)
8591
tooltipEl.querySelector(".chart-tooltip-dot").onclick = () =>
8692
onTooltipClick(tooltipModel);
@@ -91,12 +97,6 @@ export const alignedTooltip = (
9197
tooltipEl.style.opacity = 0;
9298
return;
9399
}
94-
tooltipEl.classList.remove("above", "below", "no-transform");
95-
if (tooltipModel.yAlign) {
96-
tooltipEl.classList.add(tooltipModel.yAlign);
97-
} else {
98-
tooltipEl.classList.add("no-transform");
99-
}
100100

101101
const values = onTooltipUpdate(tooltipModel);
102102
items.forEach((item) => {
@@ -115,5 +115,9 @@ export const alignedTooltip = (
115115
tooltipModel.caretX +
116116
"px";
117117
tooltipEl.style.top =
118-
position.top + window.scrollY + tooltipModel.caretY + "px";
118+
position.top -
119+
tooltipEl.offsetHeight +
120+
window.scrollY +
121+
tooltipModel.caretY +
122+
"px";
119123
};

explorer/assets/vendor/dark_mode.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ const setupThemeToggle = () => {
4141
.getElementById("theme-toggle")
4242
.addEventListener("click", function () {
4343
toggleVisibility(!isDark());
44-
// chart.js listens for this event to re-render the chart and update its colors
45-
window.dispatchEvent(new Event("theme-changed"));
4644
});
4745
};
4846

explorer/assets/vendor/tooltip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Tooltip {
4141
setupFloatingUI() {
4242
this.cleanup = autoUpdate(this.$parent, this.$tooltip, () => {
4343
computePosition(this.$parent, this.$tooltip, {
44-
placement: "top",
44+
placement: "bottom",
4545
middleware: [offset(5), flip(), shift({ padding: 5 })]
4646
}).then(({ x, y }) => {
4747
Object.assign(this.$tooltip.style, {

explorer/lib/explorer/models/batches.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ defmodule Batches do
139139
result -> result
140140
end
141141
end
142+
143+
def get_avg_fee_per_proof() do
144+
query =
145+
from(b in Batches,
146+
where: b.is_verified == true,
147+
select: avg(b.fee_per_proof)
148+
)
149+
150+
case Explorer.Repo.one(query) do
151+
nil -> 0
152+
result -> result
153+
end
154+
end
142155

143156
def get_amount_of_verified_proofs() do
144157
query = from(b in Batches,

explorer/lib/explorer_web/components/assets_cta.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule AssetsCTAComponent do
2525
View all active operators
2626
</.tooltip>
2727
</.link>
28-
<.link navigate={~p"/restakes"} class="flex-1 flex flex-col justify-start gap-0.5 group">
28+
<.link navigate={~p"/restaked"} class="flex-1 flex flex-col justify-start gap-0.5 group">
2929
<div class="text-muted-foreground font-semibold flex gap-2 items-center">
3030
<h2>
3131
Total Restaked

0 commit comments

Comments
 (0)