-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
179 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { useEffect, useState, useRef } from "react"; | ||
import { VegaLite, VisualizationSpec } from "react-vega"; | ||
import { useResizeObserver } from "@/hooks/useResizeObserver"; | ||
import { getTinyChartData } from "@/utils/apis/getTinyChartData"; | ||
import { TinyVegaChartProps } from "@/types/charts"; | ||
import { determineTrend, createSpec } from "@/utils/charts"; | ||
|
||
const TinyVegaChart: React.FC<TinyVegaChartProps> = ({ tokenAddress }) => { | ||
const [spec, setSpec] = useState<VisualizationSpec>({}); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
useResizeObserver(containerRef, ([entry]) => | ||
updateSpecWidth(entry.contentRect.width) | ||
); | ||
|
||
useEffect(() => { | ||
const fetchDataAndUpdateSpec = async () => { | ||
const values = await getTinyChartData(tokenAddress); | ||
const trend = determineTrend(values); | ||
const newSpec = createSpec(values, trend); | ||
setSpec(newSpec); | ||
}; | ||
|
||
fetchDataAndUpdateSpec(); | ||
}, [tokenAddress]); | ||
|
||
const updateSpecWidth = (width: number) => { | ||
setSpec((currentSpec) => ({ ...currentSpec, width })); | ||
}; | ||
|
||
return ( | ||
<div ref={containerRef} style={{ width: "100%", height: "30px" }}> | ||
<VegaLite spec={spec} actions={false} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TinyVegaChart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useEffect } from "react"; | ||
|
||
export const useResizeObserver = ( | ||
ref: React.RefObject<Element>, | ||
callback: ResizeObserverCallback | ||
) => { | ||
useEffect(() => { | ||
const observer = new ResizeObserver(callback); | ||
|
||
if (ref.current) { | ||
observer.observe(ref.current); | ||
} | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [ref, callback]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface ChartValue { | ||
time: number; | ||
value: number; | ||
} | ||
|
||
export interface TinyVegaChartProps { | ||
tokenAddress: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
interface ChartValue { | ||
time: number; | ||
value: number; | ||
} | ||
|
||
// Mock function to simulate fetching chart data based on a token address | ||
export const getTinyChartData = async ( | ||
tokenAddress: string | ||
): Promise<ChartValue[]> => { | ||
// Define mock data for different tokens | ||
const data: Record<string, ChartValue[]> = { | ||
secret1k0jntykt7e4g3y88ltc60czgjuqdy4c9e8fzek: [ | ||
// Slight upward trend | ||
{ time: 1, value: 50 }, | ||
{ time: 2, value: 52 }, | ||
{ time: 3, value: 51 }, | ||
{ time: 4, value: 53 }, | ||
], | ||
secret15l9cqgz5uezgydrglaak5ahfac69kmx2qpd6xt: [ | ||
// Slight downward trend | ||
{ time: 1, value: 50 }, | ||
{ time: 2, value: 49 }, | ||
{ time: 3, value: 50 }, | ||
{ time: 4, value: 48 }, | ||
], | ||
secret1yxwnyk8htvvq25x2z87yj0r5tqpev452fk6h5h: [ | ||
// Dramatic changes | ||
{ time: 1, value: 50 }, | ||
{ time: 2, value: 60 }, | ||
{ time: 3, value: 40 }, | ||
{ time: 4, value: 65 }, | ||
], | ||
}; | ||
|
||
// Simulate fetching data by returning the mock data for the requested token address | ||
// If the token address is not found, return an empty array | ||
return data[tokenAddress] || []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ChartValue } from "@/types/charts"; | ||
import { VisualizationSpec } from "react-vega"; | ||
|
||
export const createSpec = ( | ||
values: ChartValue[], | ||
trend: "growing" | "ungrowing" | ||
): VisualizationSpec => { | ||
const color = trend === "growing" ? "green" : "red"; | ||
|
||
return { | ||
$schema: "https://vega.github.io/schema/vega-lite/v5.json", | ||
width: 65, | ||
height: 30, | ||
background: "transparent", | ||
data: { values }, | ||
mark: { | ||
type: "line", | ||
strokeWidth: 3, | ||
color, | ||
}, | ||
encoding: { | ||
x: { field: "time", type: "quantitative", axis: null }, | ||
y: { field: "value", type: "quantitative", axis: null }, | ||
}, | ||
config: { | ||
view: { stroke: "none" }, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ChartValue } from "@/types/charts"; | ||
|
||
export const determineTrend = ( | ||
values: ChartValue[] | ||
): "growing" | "ungrowing" => { | ||
return values[values.length - 1].value > values[0].value | ||
? "growing" | ||
: "ungrowing"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "@/utils/charts/determineTrend"; | ||
export * from "@/utils/charts/createSpec"; |