feat!: release v2 (#6545)
Co-authored-by: Artur <145776561+arturict@users.noreply.github.com> Co-authored-by: arturict <arturict@users.noreply.github.com> Co-authored-by: Thomas <thomas@ajnart.fr> Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com> Co-authored-by: Danura30082 <danura30082+github@gmail.com> Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com> Co-authored-by: ajnart <thomas@ajnart.fr> Co-authored-by: ajnart <thomas@ajnart.dev> Co-authored-by: chenjun <chenjun@foxmail.com> Co-authored-by: Wikibear <wikibear@users.noreply.github.com> Co-authored-by: Bergasha <bergasha@gmail.com> Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com> Co-authored-by: Lucas Araujo <lucas.lra@gmail.com> Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com> Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social> Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com> Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com> Co-authored-by: thrawnx <tight.bulb9384@fastmail.com> Co-authored-by: Chris <58918656+nordada@users.noreply.github.com> Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com> Co-authored-by: Yanis <naamaneyanis@gmail.com> Co-authored-by: Wikibear <me@sej.de> Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
This commit is contained in:
3286 files changed
+284386
-139176
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { getStockLayout, getStockSummary } from "./component";
|
||||
|
||||
describe("stock summary", () => {
|
||||
it("returns no summary for an empty series", () => {
|
||||
expect(getStockSummary([], 10)).toBeNull();
|
||||
});
|
||||
|
||||
it("does not divide by a zero previous close", () => {
|
||||
expect(getStockSummary([0, 5], 0)).toMatchObject({
|
||||
currentPrice: 5,
|
||||
previousClose: 0,
|
||||
change: 5,
|
||||
changePercentage: null,
|
||||
minimum: 0,
|
||||
maximum: 5,
|
||||
graphValues: [50, 55],
|
||||
});
|
||||
});
|
||||
|
||||
it("derives every advanced metric from the owned price series", () => {
|
||||
expect(getStockSummary([90, 110, 105], 100)).toMatchObject({
|
||||
currentPrice: 105,
|
||||
previousClose: 100,
|
||||
change: 5,
|
||||
changePercentage: 5,
|
||||
minimum: 90,
|
||||
maximum: 110,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("stock layout", () => {
|
||||
it("keeps compact widgets focused on symbol and price", () => {
|
||||
expect(getStockLayout(320, 100, true)).toMatchObject({
|
||||
showName: false,
|
||||
showChange: false,
|
||||
showRange: false,
|
||||
priceOrder: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it("reveals secondary data only when both axes have room", () => {
|
||||
expect(getStockLayout(320, 160, true)).toMatchObject({
|
||||
showName: false,
|
||||
showChange: true,
|
||||
showRange: true,
|
||||
});
|
||||
expect(getStockLayout(320, 220, true)).toMatchObject({
|
||||
showName: true,
|
||||
showChange: true,
|
||||
showRange: true,
|
||||
graphHeight: "68%",
|
||||
});
|
||||
});
|
||||
|
||||
it("exposes the complete summary when the widget is roomy", () => {
|
||||
expect(getStockLayout(640, 360, true)).toEqual({
|
||||
showName: true,
|
||||
showChange: true,
|
||||
showRange: true,
|
||||
graphHeight: "75%",
|
||||
priceOrder: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("hides name, change, and range regardless of size when showDetails is off", () => {
|
||||
expect(getStockLayout(640, 360, false)).toMatchObject({
|
||||
showName: false,
|
||||
showChange: false,
|
||||
showRange: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,15 @@
|
||||
"use client";
|
||||
|
||||
import { Sparkline } from "@mantine/charts";
|
||||
import { Flex, Stack, Text, Title, useMantineTheme } from "@mantine/core";
|
||||
import { IconTrendingDown, IconTrendingUp } from "@tabler/icons-react";
|
||||
import { Badge, Box, Flex, Group, Paper, SimpleGrid, Stack, Text, Title, useMantineTheme } from "@mantine/core";
|
||||
import { IconMinus, IconTrendingDown, IconTrendingUp } from "@tabler/icons-react";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
|
||||
|
||||
import { WidgetEmptyState } from "../common/empty-state";
|
||||
import { getUsableWidgetQueryData, isInitialWidgetQueryPending } from "../common/query-state";
|
||||
import { WidgetQueryLoadingState } from "../common/query-state-indicator";
|
||||
import type { WidgetComponentProps } from "../definition";
|
||||
|
||||
function round(value: number) {
|
||||
@@ -19,88 +21,194 @@ function calculateChange(currentPrice: number, previousClose: number) {
|
||||
}
|
||||
|
||||
function calculateChangePercentage(currentPrice: number, previousClose: number) {
|
||||
if (previousClose === 0) return null;
|
||||
return 100 * ((currentPrice - previousClose) / previousClose);
|
||||
}
|
||||
|
||||
export default function StockPriceWidget({ options, width, height }: WidgetComponentProps<"stockPrice">) {
|
||||
const t = useScopedI18n("widget.stockPrice");
|
||||
const theme = useMantineTheme();
|
||||
const { data: result } = clientApi.widget.stockPrice.getPriceHistory.useQuery(options);
|
||||
export interface StockSummary {
|
||||
currentPrice: number;
|
||||
previousClose: number;
|
||||
change: number;
|
||||
changePercentage: number | null;
|
||||
minimum: number;
|
||||
maximum: number;
|
||||
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, showDetails: boolean): StockLayout {
|
||||
return {
|
||||
showName: showDetails && width >= 300 && height >= 190,
|
||||
showChange: showDetails && width >= 260 && height >= 130,
|
||||
showRange: showDetails && width >= 220 && height >= 130,
|
||||
graphHeight: height >= 320 ? "75%" : 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;
|
||||
|
||||
const minimum = Math.min(...priceHistory);
|
||||
return {
|
||||
currentPrice,
|
||||
previousClose,
|
||||
change: round(calculateChange(currentPrice, previousClose)),
|
||||
changePercentage: calculateChangePercentage(currentPrice, previousClose),
|
||||
minimum,
|
||||
maximum: Math.max(...priceHistory),
|
||||
graphValues: priceHistory.map((value) => value - minimum + 50),
|
||||
};
|
||||
}
|
||||
|
||||
export default function StockPriceWidget({
|
||||
options,
|
||||
width,
|
||||
height,
|
||||
displayMode = "compact",
|
||||
}: WidgetComponentProps<"stockPrice">) {
|
||||
const t = useI18n("widget.stockPrice");
|
||||
const tCommon = useI18n("common");
|
||||
const locale = useCurrentIntlLocale();
|
||||
const numberFormatter = new Intl.NumberFormat(locale);
|
||||
const theme = useMantineTheme();
|
||||
const stockQuery = clientApi.widget.stockPrice.getPriceHistory.useQuery(options);
|
||||
const result = getUsableWidgetQueryData(stockQuery);
|
||||
|
||||
if (isInitialWidgetQueryPending(stockQuery)) return <WidgetQueryLoadingState />;
|
||||
if (!result) return <WidgetEmptyState />;
|
||||
const { data } = result;
|
||||
|
||||
const stockValuesChange = round(calculateChange(data.priceHistory.at(-1) ?? 0, data.previousClose));
|
||||
const stockValuesChangePercentage = round(
|
||||
calculateChangePercentage(data.priceHistory.at(-1) ?? 0, data.previousClose),
|
||||
);
|
||||
|
||||
const stockValuesMin = Math.min(...data.priceHistory);
|
||||
const stockGraphValues = data.priceHistory.map((value) => value - stockValuesMin + 50);
|
||||
const summary = getStockSummary(data.priceHistory, data.previousClose);
|
||||
if (!summary) return <WidgetEmptyState />;
|
||||
const stockValuesChange = summary.change;
|
||||
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, options.showDetails);
|
||||
const formatValue = (value: number) => numberFormatter.format(round(value));
|
||||
const formatSignedValue = (value: number) => `${value > 0 ? "+" : ""}${formatValue(value)}`;
|
||||
|
||||
const content =
|
||||
displayMode === "advanced" ? (
|
||||
<Stack h="100%" w="100%" gap="md" p="md" style={{ overflow: "hidden" }}>
|
||||
<Group justify="space-between" align="flex-start" gap="sm" wrap="wrap">
|
||||
<Stack gap={2}>
|
||||
<Title order={2}>{data.symbol}</Title>
|
||||
<Text c="dimmed">{data.shortName}</Text>
|
||||
</Stack>
|
||||
<Group gap="xs" wrap="wrap" justify="flex-end">
|
||||
<Badge variant="light">
|
||||
{t("option.timeRange.label")}: {t(`option.timeRange.option.${options.timeRange}.label`)}
|
||||
</Badge>
|
||||
<Badge variant="light">
|
||||
{t("option.timeInterval.label")}: {t(`option.timeInterval.option.${options.timeInterval}.label`)}
|
||||
</Badge>
|
||||
</Group>
|
||||
</Group>
|
||||
|
||||
<SimpleGrid cols={width >= 840 ? 6 : width >= 480 ? 3 : 2} spacing="xs">
|
||||
<StockMetric label={t("advanced.currentPrice")} value={formatValue(summary.currentPrice)} />
|
||||
<StockMetric label={t("advanced.previousClose")} value={formatValue(summary.previousClose)} />
|
||||
<StockMetric label={tCommon("action.change")} value={formatSignedValue(summary.change)} color={trendColor} />
|
||||
<StockMetric
|
||||
label={t("advanced.changePercentage")}
|
||||
value={summary.changePercentage === null ? "—" : `${formatSignedValue(summary.changePercentage)}%`}
|
||||
color={trendColor}
|
||||
/>
|
||||
<StockMetric label={t("advanced.minimum")} value={formatValue(summary.minimum)} />
|
||||
<StockMetric label={t("advanced.maximum")} value={formatValue(summary.maximum)} />
|
||||
</SimpleGrid>
|
||||
|
||||
<Box style={{ flex: 1, minHeight: 160 }}>
|
||||
<Sparkline
|
||||
w="100%"
|
||||
h="100%"
|
||||
data={data.priceHistory}
|
||||
curveType="linear"
|
||||
color={trendColor}
|
||||
fillOpacity={0.35}
|
||||
strokeWidth={2.5}
|
||||
/>
|
||||
</Box>
|
||||
</Stack>
|
||||
) : (
|
||||
<Flex h="100%" w="100%">
|
||||
<Sparkline
|
||||
pos="absolute"
|
||||
bottom={10}
|
||||
w="100%"
|
||||
h={layout.graphHeight}
|
||||
data={stockGraphValues}
|
||||
curveType="linear"
|
||||
color={trendColor}
|
||||
fillOpacity={0.6}
|
||||
strokeWidth={2.5}
|
||||
/>
|
||||
|
||||
<Stack pos="absolute" top={10} left={10}>
|
||||
<Text size="xl" fw={700} lh="0.715">
|
||||
{stockValuesChange > 0 ? (
|
||||
<IconTrendingUp size="1.5rem" color={theme.colors.green[7]} />
|
||||
) : stockValuesChange < 0 ? (
|
||||
<IconTrendingDown size="1.5rem" color={theme.colors.red[7]} />
|
||||
) : (
|
||||
<IconMinus size="1.5rem" color={theme.colors.gray[6]} />
|
||||
)}
|
||||
{data.symbol}
|
||||
</Text>
|
||||
{layout.showName && (
|
||||
<Text size="md" lh="1">
|
||||
{data.shortName}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
|
||||
<Title pos="absolute" bottom={10} right={10} order={layout.priceOrder} fw={700}>
|
||||
{formatValue(summary.currentPrice)}
|
||||
</Title>
|
||||
|
||||
{layout.showChange && (
|
||||
<Text pos="absolute" top={10} right={10} size="xl" fw={700}>
|
||||
{numberFormatter.format(stockValuesChange)}
|
||||
{stockValuesChangePercentage === null
|
||||
? null
|
||||
: ` (${stockValuesChange > 0 ? "+" : ""}${numberFormatter.format(stockValuesChangePercentage)}%)`}
|
||||
</Text>
|
||||
)}
|
||||
|
||||
{layout.showRange && (
|
||||
<Text pos="absolute" bottom={10} left={10} fw={700}>
|
||||
{t(`option.timeRange.option.${options.timeRange}.label`)}
|
||||
</Text>
|
||||
)}
|
||||
</Flex>
|
||||
);
|
||||
|
||||
return (
|
||||
<Flex h="100%" w="100%">
|
||||
<Sparkline
|
||||
pos="absolute"
|
||||
bottom={10}
|
||||
w="100%"
|
||||
h={height > 280 ? "75%" : "50%"}
|
||||
data={stockGraphValues}
|
||||
curveType="linear"
|
||||
color={trendColor}
|
||||
fillOpacity={0.6}
|
||||
strokeWidth={2.5}
|
||||
/>
|
||||
|
||||
<Stack pos="absolute" top={10} left={10}>
|
||||
<Text size="xl" fw={700} lh="0.715">
|
||||
{stockValuesChange > 0 ? (
|
||||
<IconTrendingUp size="1.5rem" color={theme.colors.green[7]} />
|
||||
) : (
|
||||
<IconTrendingDown size="1.5rem" color={theme.colors.red[7]} />
|
||||
)}
|
||||
{data.symbol}
|
||||
</Text>
|
||||
{width > 280 && height > 280 && (
|
||||
<Text size="md" lh="1">
|
||||
{data.shortName}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
|
||||
<Title pos="absolute" bottom={10} right={10} order={width > 280 ? 1 : 2} fw={700}>
|
||||
{new Intl.NumberFormat().format(round(data.priceHistory.at(-1) ?? 0))}
|
||||
</Title>
|
||||
|
||||
{width > 280 && (
|
||||
<Text pos="absolute" top={10} right={10} size="xl" fw={700}>
|
||||
{new Intl.NumberFormat().format(stockValuesChange)} ({stockValuesChange > 0 ? "+" : ""}
|
||||
{new Intl.NumberFormat().format(stockValuesChangePercentage)}%)
|
||||
</Text>
|
||||
)}
|
||||
|
||||
{width > 280 && (
|
||||
<Text pos="absolute" bottom={10} left={10} fw={700}>
|
||||
{t(`option.timeRange.option.${options.timeRange}.label`)}
|
||||
</Text>
|
||||
)}
|
||||
|
||||
<Stack pos="absolute" top={10} left={10}>
|
||||
<Text size="xl" fw={700} lh="0.715">
|
||||
{stockValuesChange > 0 ? (
|
||||
<IconTrendingUp size="1.5rem" color={theme.colors.green[7]} />
|
||||
) : (
|
||||
<IconTrendingDown size="1.5rem" color={theme.colors.red[7]} />
|
||||
)}
|
||||
{data.symbol}
|
||||
</Text>
|
||||
{width > 280 && height > 280 && (
|
||||
<Text size="md" lh="1">
|
||||
{data.shortName}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
</Flex>
|
||||
<Box h="100%" w="100%" pos="relative">
|
||||
{content}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
function StockMetric({ label, value, color }: { label: string; value: string; color?: string }) {
|
||||
return (
|
||||
<Paper p="sm" radius="md">
|
||||
<Text size="xs" c="dimmed" truncate>
|
||||
{label}
|
||||
</Text>
|
||||
<Text size="lg" fw={700} c={color} truncate title={value}>
|
||||
{value}
|
||||
</Text>
|
||||
</Paper>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IconBuildingBank } from "@tabler/icons-react";
|
||||
|
||||
import { createWidgetDefinition } from "../definition";
|
||||
import { createWidgetDefinition, widgetQueryInputMatches } from "../definition";
|
||||
import { optionsBuilder } from "../options";
|
||||
|
||||
export const stockPriceTimeFrames = {
|
||||
@@ -12,7 +12,15 @@ const timeRangeOptions = stockPriceTimeFrames.range;
|
||||
const timeIntervalOptions = stockPriceTimeFrames.interval;
|
||||
|
||||
export const { definition, componentLoader } = createWidgetDefinition("stockPrice", {
|
||||
supportsAdvancedFocus: true,
|
||||
icon: IconBuildingBank,
|
||||
queryKey: [["widget", "stockPrice", "getPriceHistory"]],
|
||||
queryMatcher: ({ input }, scope) =>
|
||||
widgetQueryInputMatches(input, {
|
||||
stock: scope.options.stock,
|
||||
timeRange: scope.options.timeRange,
|
||||
timeInterval: scope.options.timeInterval,
|
||||
}),
|
||||
refetchInterval: null,
|
||||
createOptions() {
|
||||
return optionsBuilder.from((factory) => ({
|
||||
@@ -33,6 +41,10 @@ export const { definition, componentLoader } = createWidgetDefinition("stockPric
|
||||
label: (t) => t(`widget.stockPrice.option.timeInterval.option.${value}.label`),
|
||||
})),
|
||||
}),
|
||||
showDetails: factory.switch({
|
||||
defaultValue: true,
|
||||
withDescription: true,
|
||||
}),
|
||||
}));
|
||||
},
|
||||
}).withDynamicImport(() => import("./component"));
|
||||
Reference in new issue
Block a user