feat: refine widget interactions

This commit is contained in:
Thomas Camlong authored and ajnart committed 2026-08-14 11:07:08 +02:00
1 parent a8a7545f40
commit 842e86012d
107 files changed
+3144 -1233

No files matched your search

+36 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { getStockSummary } from "./component";
import { getStockLayout, getStockSummary } from "./component";
describe("stock summary", () => {
it("returns no summary for an empty series", () => {
@@ -16,3 +16,38 @@ describe("stock summary", () => {
});
});
});
describe("stock layout", () => {
it("keeps compact widgets focused on symbol and price", () => {
expect(getStockLayout(320, 100, false)).toMatchObject({
showName: false,
showChange: false,
showRange: false,
priceOrder: 2,
});
});
it("reveals secondary data only when both axes have room", () => {
expect(getStockLayout(320, 160, false)).toMatchObject({
showName: false,
showChange: true,
showRange: true,
});
expect(getStockLayout(320, 220, false)).toMatchObject({
showName: true,
showChange: true,
showRange: true,
graphHeight: "68%",
});
});
it("always exposes the complete advanced summary", () => {
expect(getStockLayout(120, 80, true)).toEqual({
showName: true,
showChange: true,
showRange: true,
graphHeight: "75%",
priceOrder: 1,
});
});
});
+34 -5
View File
@@ -30,6 +30,34 @@ export interface StockSummary {
graphValues: number[];
}
export interface StockLayout {
showName: boolean;
showChange: boolean;
showRange: boolean;
graphHeight: "48%" | "68%" | "75%";
priceOrder: 1 | 2;
}
export function getStockLayout(width: number, height: number, isAdvanced: boolean): StockLayout {
if (isAdvanced) {
return {
showName: true,
showChange: true,
showRange: true,
graphHeight: "75%",
priceOrder: 1,
};
}
return {
showName: width >= 300 && height >= 190,
showChange: width >= 260 && height >= 130,
showRange: width >= 220 && height >= 130,
graphHeight: height >= 220 ? "68%" : "48%",
priceOrder: width >= 280 && height >= 120 ? 1 : 2,
};
}
export function getStockSummary(priceHistory: number[], previousClose: number): StockSummary | null {
const currentPrice = priceHistory.at(-1);
if (currentPrice === undefined) return null;
@@ -57,6 +85,7 @@ export default function StockPriceWidget({ options, width, height, displayMode }
const stockValuesChangePercentage = summary.changePercentage === null ? null : round(summary.changePercentage);
const stockGraphValues = summary.graphValues;
const trendColor = stockValuesChange > 0 ? "green.7" : stockValuesChange < 0 ? "red.7" : "gray.6";
const layout = getStockLayout(width, height, displayMode === "advanced");
return (
<Flex h="100%" w="100%">
@@ -64,7 +93,7 @@ export default function StockPriceWidget({ options, width, height, displayMode }
pos="absolute"
bottom={10}
w="100%"
h={displayMode === "advanced" || height > 280 ? "75%" : "50%"}
h={layout.graphHeight}
data={stockGraphValues}
curveType="linear"
color={trendColor}
@@ -81,18 +110,18 @@ export default function StockPriceWidget({ options, width, height, displayMode }
)}
{data.symbol}
</Text>
{(displayMode === "advanced" || (width > 280 && height > 280)) && (
{layout.showName && (
<Text size="md" lh="1">
{data.shortName}
</Text>
)}
</Stack>
<Title pos="absolute" bottom={10} right={10} order={width > 280 ? 1 : 2} fw={700}>
<Title pos="absolute" bottom={10} right={10} order={layout.priceOrder} fw={700}>
{new Intl.NumberFormat().format(round(summary.currentPrice))}
</Title>
{(displayMode === "advanced" || width > 280) && (
{layout.showChange && (
<Text pos="absolute" top={10} right={10} size="xl" fw={700}>
{new Intl.NumberFormat().format(stockValuesChange)}
{stockValuesChangePercentage === null
@@ -101,7 +130,7 @@ export default function StockPriceWidget({ options, width, height, displayMode }
</Text>
)}
{(displayMode === "advanced" || width > 280) && (
{layout.showRange && (
<Text pos="absolute" bottom={10} left={10} fw={700}>
{t(`option.timeRange.option.${options.timeRange}.label`)}
</Text>