Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(insights): build header & tabs for price feed details page #2189

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apps/api-reference/jsx.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* This file only exists because in react 19, the JSX namespace was moved under
* the React export. However, some libraries (e.g. react-markdown) still have
* some things typed as `JSX.<Something>`. Until those libraries update to
* import the namespace correctly, we'll need this declaration file in place to
* expose JSX via the old global location.
*/

import type { JSX as Jsx } from "react/jsx-runtime";

declare global {
namespace JSX {
type ElementClass = Jsx.ElementClass;
type Element = Jsx.Element;
type IntrinsicElements = Jsx.IntrinsicElements;
}
}
2 changes: 1 addition & 1 deletion apps/api-reference/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
3 changes: 0 additions & 3 deletions apps/api-reference/src/markdown-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export const MARKDOWN_COMPONENTS = {
</Code>
);
} else {
// @ts-expect-error react-markdown doesn't officially support react 19
// yet; there's no issues here in practice but the types don't currently
// unify
return <pre {...props} />;
}
},
Expand Down
2 changes: 1 addition & 1 deletion apps/insights/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
2 changes: 1 addition & 1 deletion apps/insights/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"clsx": "catalog:",
"cryptocurrency-icons": "catalog:",
"dnum": "catalog:",
"framer-motion": "catalog:",
"motion": "catalog:",
"next": "catalog:",
"next-themes": "catalog:",
"nuqs": "catalog:",
Expand Down
13 changes: 13 additions & 0 deletions apps/insights/src/app/price-feeds/[slug]/layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Metadata } from "next";

import { client } from "../../../services/pyth";
export { PriceFeedLayout as default } from "../../../components/PriceFeed/layout";

export const metadata: Metadata = {
title: "Price Feeds",
};

export const generateStaticParams = async () => {
const data = await client.getData();
return data.symbols.map((symbol) => ({ slug: encodeURIComponent(symbol) }));
};
1 change: 1 addition & 0 deletions apps/insights/src/app/price-feeds/[slug]/page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Chart as default } from "../../../components/PriceFeed/chart";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PriceComponents as default } from "../../../../components/PriceFeed/price-components";
1 change: 1 addition & 0 deletions apps/insights/src/app/price-feeds/layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PriceFeedsLayout as default } from "../../components/PriceFeeds/layout";
6 changes: 6 additions & 0 deletions apps/insights/src/app/price-feeds/page.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import type { Metadata } from "next";

export { PriceFeeds as default } from "../../components/PriceFeeds";

export const metadata: Metadata = {
title: "Price Feeds",
};
6 changes: 6 additions & 0 deletions apps/insights/src/app/publishers/page.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import type { Metadata } from "next";

export { Publishers as default } from "../../components/Publishers";

export const metadata: Metadata = {
title: "Publishers",
};
21 changes: 0 additions & 21 deletions apps/insights/src/components/AsyncValue/index.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@use "@pythnetwork/component-library/theme";

.changePercent {
font-size: theme.font-size("sm");
transition: color 100ms linear;
display: flex;
flex-flow: row nowrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import { CaretUp } from "@phosphor-icons/react/dist/ssr/CaretUp";
import { Skeleton } from "@pythnetwork/component-library/Skeleton";
import clsx from "clsx";
import { type ComponentProps, createContext, use } from "react";
import { useNumberFormatter } from "react-aria";
import { z } from "zod";

import styles from "./change-percent.module.scss";
import styles from "./index.module.scss";
import { StateType, useData } from "../../use-data";
import { useLivePrice } from "../LivePrices";

Expand All @@ -18,20 +19,17 @@ const REFRESH_YESTERDAYS_PRICES_INTERVAL = ONE_HOUR_IN_MS;
const CHANGE_PERCENT_SKELETON_WIDTH = 15;

type Props = Omit<ComponentProps<typeof YesterdaysPricesContext>, "value"> & {
symbolsToFeedKeys: Record<string, string>;
feeds: (Feed & { symbol: string })[];
};

const YesterdaysPricesContext = createContext<
undefined | ReturnType<typeof useData<Map<string, number>>>
>(undefined);

export const YesterdaysPricesProvider = ({
symbolsToFeedKeys,
...props
}: Props) => {
export const YesterdaysPricesProvider = ({ feeds, ...props }: Props) => {
const state = useData(
["yesterdaysPrices", Object.values(symbolsToFeedKeys)],
() => getYesterdaysPrices(symbolsToFeedKeys),
["yesterdaysPrices", feeds.map((feed) => feed.symbol)],
() => getYesterdaysPrices(feeds),
{
refreshInterval: REFRESH_YESTERDAYS_PRICES_INTERVAL,
},
Expand All @@ -41,17 +39,21 @@ export const YesterdaysPricesProvider = ({
};

const getYesterdaysPrices = async (
symbolsToFeedKeys: Record<string, string>,
feeds: (Feed & { symbol: string })[],
): Promise<Map<string, number>> => {
const url = new URL("/yesterdays-prices", window.location.origin);
for (const symbol of Object.keys(symbolsToFeedKeys)) {
url.searchParams.append("symbols", symbol);
for (const feed of feeds) {
url.searchParams.append("symbols", feed.symbol);
}
const response = await fetch(url);
const data: unknown = await response.json();
return new Map(
Object.entries(yesterdaysPricesSchema.parse(data)).map(
([symbol, value]) => [symbolsToFeedKeys[symbol] ?? "", value],
([symbol, value]) => [
feeds.find((feed) => feed.symbol === symbol)?.product.price_account ??
"",
value,
],
),
);
};
Expand All @@ -69,10 +71,17 @@ const useYesterdaysPrices = () => {
};

type ChangePercentProps = {
feedKey: string;
className?: string | undefined;
feed: Feed;
};

export const ChangePercent = ({ feedKey }: ChangePercentProps) => {
type Feed = {
product: {
price_account: string;
};
};

export const ChangePercent = ({ feed, className }: ChangePercentProps) => {
const yesterdaysPriceState = useYesterdaysPrices();

switch (yesterdaysPriceState.type) {
Expand All @@ -85,60 +94,68 @@ export const ChangePercent = ({ feedKey }: ChangePercentProps) => {
case StateType.NotLoaded: {
return (
<Skeleton
className={styles.changePercent}
className={clsx(styles.changePercent, className)}
width={CHANGE_PERCENT_SKELETON_WIDTH}
/>
);
}

case StateType.Loaded: {
const yesterdaysPrice = yesterdaysPriceState.data.get(feedKey);
const yesterdaysPrice = yesterdaysPriceState.data.get(
feed.product.price_account,
);
// eslint-disable-next-line unicorn/no-null
return yesterdaysPrice === undefined ? null : (
<ChangePercentLoaded priorPrice={yesterdaysPrice} feedKey={feedKey} />
<ChangePercentLoaded
className={clsx(styles.changePercent, className)}
priorPrice={yesterdaysPrice}
feed={feed}
/>
);
}
}
};

type ChangePercentLoadedProps = {
className?: string | undefined;
priorPrice: number;
feedKey: string;
feed: Feed;
};

const ChangePercentLoaded = ({
className,
priorPrice,
feedKey,
feed,
}: ChangePercentLoadedProps) => {
const currentPrice = useLivePrice(feedKey);
const currentPrice = useLivePrice(feed);

return currentPrice === undefined ? (
<Skeleton
className={styles.changePercent}
width={CHANGE_PERCENT_SKELETON_WIDTH}
/>
<Skeleton className={className} width={CHANGE_PERCENT_SKELETON_WIDTH} />
) : (
<PriceDifference
currentPrice={currentPrice.price}
className={className}
currentPrice={currentPrice.aggregate.price}
priorPrice={priorPrice}
/>
);
};

type PriceDifferenceProps = {
className?: string | undefined;
currentPrice: number;
priorPrice: number;
};

const PriceDifference = ({
className,
currentPrice,
priorPrice,
}: PriceDifferenceProps) => {
const numberFormatter = useNumberFormatter({ maximumFractionDigits: 2 });
const direction = getDirection(currentPrice, priorPrice);

return (
<span data-direction={direction} className={styles.changePercent}>
<span data-direction={direction} className={className}>
<CaretUp weight="fill" className={styles.caret} />
{numberFormatter.format(
(100 * Math.abs(currentPrice - priorPrice)) / currentPrice,
Expand Down
38 changes: 6 additions & 32 deletions apps/insights/src/components/CopyButton/index.module.scss
Original file line number Diff line number Diff line change
@@ -1,55 +1,29 @@
@use "@pythnetwork/component-library/theme";

.copyButton {
margin: -#{theme.spacing(0.5)} -#{theme.spacing(1)};
display: inline-block;
white-space: nowrap;
border-radius: theme.border-radius("md");
padding: theme.spacing(0.5) theme.spacing(1);
background: none;
cursor: pointer;
transition-property: background-color, color, border-color, outline-color;
transition-duration: 100ms;
transition-timing-function: linear;
border: 1px solid transparent;
outline-offset: 0;
outline: theme.spacing(1) solid transparent;

.iconContainer {
position: relative;
top: 0.125em;
margin-left: theme.spacing(1);
display: inline-block;

.copyIcon {
opacity: 0.5;
transition: opacity 100ms linear;
width: 1em;
height: 1em;
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}

.checkIcon {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
color: theme.color("states", "success", "normal");
opacity: 0;
transition: opacity 100ms linear;
}
}

&[data-hovered] {
background-color: theme.color("button", "outline", "background", "hover");
}

&[data-pressed] {
background-color: theme.color("button", "outline", "background", "active");
}

&[data-focus-visible] {
border-color: theme.color("focus");
outline-color: theme.color("focus-dim");
}

&[data-is-copied] .iconContainer {
.copyIcon {
opacity: 0;
Expand Down
Loading
Loading