Skip to content

Commit d3fb82d

Browse files
committed
fix(insights): created a new hook to detect the user's theme, even if it's not been set yet, which fixes colors used on the charts. changed tokens word to be keys
1 parent 1158132 commit d3fb82d

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

apps/insights/src/components/PythProApiTokensMenu/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export function PythProApiTokensMenu() {
1818
const [open, setOpen] = useState(false);
1919

2020
/** local variables */
21-
const tooltip = "Configure your API tokens";
22-
const closeTooltip = "Close API tokens config";
21+
const tooltip = "Configure your API keys";
22+
const closeTooltip = "Close API keys config";
2323

2424
return (
2525
<>
@@ -61,7 +61,7 @@ export function PythProApiTokensMenu() {
6161
<div className={classes.fyi}>
6262
In order to provide a quality demo of Pyth Pro real-time
6363
performance relative to other sources, you will need to provide
64-
API tokens to interact with these APIs.
64+
API keys to interact with these APIs.
6565
</div>
6666
<div className={classes.fyi}>
6767
They will be saved securely to your browser for future use here.

apps/insights/src/components/PythProDemoPriceChart/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useAppTheme } from "@pythnetwork/react-hooks/use-app-theme";
12
import { capitalCase } from "change-case";
23
import type {
34
IChartApi,
@@ -6,7 +7,6 @@ import type {
67
UTCTimestamp,
78
} from "lightweight-charts";
89
import { createChart, LineSeries } from "lightweight-charts";
9-
import { useTheme } from "next-themes";
1010
import { useEffect, useLayoutEffect, useRef } from "react";
1111

1212
import type { AppStateContextVal } from "../../context/pyth-pro-demo";
@@ -31,7 +31,7 @@ export function PythProDemoPriceChartImpl({
3131
selectedSource,
3232
}: PythProDemoPriceChartImplProps) {
3333
/** hooks */
34-
const { theme } = useTheme();
34+
const { theme } = useAppTheme();
3535

3636
/** refs */
3737
const containerRef = useRef<HTMLDivElement | null>(null);

packages/react-hooks/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"dependencies": {
2727
"@pythnetwork/shared-lib": "workspace:",
28+
"next-themes": "catalog:",
2829
"nuqs": "catalog:",
2930
"react": "catalog:",
3031
"react-dom": "catalog:",
@@ -49,6 +50,10 @@
4950
"types": "./dist/nuqs.d.ts",
5051
"default": "./dist/nuqs.mjs"
5152
},
53+
"./use-app-theme": {
54+
"types": "./dist/use-app-theme.d.ts",
55+
"default": "./dist/use-app-theme.mjs"
56+
},
5257
"./use-websocket": {
5358
"types": "./dist/use-websocket.d.ts",
5459
"default": "./dist/use-websocket.mjs"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use client";
2+
3+
import { useTheme } from "next-themes";
4+
import { useEffect, useRef, useState } from "react";
5+
6+
function getThemePreferenceMediaQuery() {
7+
return globalThis.window.matchMedia("(prefers-color-scheme: dark)");
8+
}
9+
10+
/**
11+
* wrapped version of the useTheme() hook from "next-themes,"
12+
* this one also detect if a user has selected the "system" theme
13+
* and, if so, returns their current color-scheme theme preference
14+
* (which comes directly from their OS or from their browser's settings)
15+
*/
16+
export function useAppTheme() {
17+
/** refs */
18+
const darkQueryRef = useRef(getThemePreferenceMediaQuery());
19+
20+
/** state */
21+
const [browserThemePreference, setBrowserThemePreference] = useState<
22+
"dark" | "light"
23+
>(darkQueryRef.current.matches ? "dark" : "light");
24+
25+
/** hooks */
26+
const { theme, ...rest } = useTheme();
27+
28+
/** effects */
29+
useEffect(() => {
30+
const { current: mediaQuery } = darkQueryRef;
31+
32+
const handleChange = (e: MediaQueryListEvent) => {
33+
setBrowserThemePreference(e.matches ? "dark" : "light");
34+
};
35+
36+
mediaQuery.addEventListener("change", handleChange);
37+
38+
return () => {
39+
mediaQuery.removeEventListener("change", handleChange);
40+
};
41+
}, []);
42+
43+
return {
44+
...rest,
45+
theme: (!theme || theme === "system"
46+
? browserThemePreference
47+
: theme) as typeof browserThemePreference,
48+
};
49+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)