Fix race condition with using getComputedStyle primary color for dynamic activity grid darkening

Instead just use the color from the current theme directly. Tested works on initial load and theme changes.
Fixes https://github.com/gabehf/Koito/issues/75
This commit is contained in:
Michael Landry 2025-09-26 16:52:31 -04:00 committed by Michael Landry
parent b980026f1f
commit 8d3c51eb3d

View file

@ -1,28 +1,10 @@
import { useQuery } from "@tanstack/react-query" import { useQuery } from "@tanstack/react-query"
import { getActivity, type getActivityArgs, type ListenActivityItem } from "api/api" import { getActivity, type getActivityArgs, type ListenActivityItem } from "api/api"
import Popup from "./Popup" import Popup from "./Popup"
import { useEffect, useState } from "react" import { useState } from "react"
import { useTheme } from "~/hooks/useTheme" import { useTheme } from "~/hooks/useTheme"
import ActivityOptsSelector from "./ActivityOptsSelector" import ActivityOptsSelector from "./ActivityOptsSelector"
import { themes } from "~/styles/themes.css"
function getPrimaryColor(): string {
const value = getComputedStyle(document.documentElement)
.getPropertyValue('--color-primary')
.trim();
const rgbMatch = value.match(/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/);
if (rgbMatch) {
const [, r, g, b] = rgbMatch.map(Number);
return (
'#' +
[r, g, b]
.map((n) => n.toString(16).padStart(2, '0'))
.join('')
);
}
return value;
}
interface Props { interface Props {
step?: string step?: string
@ -47,7 +29,6 @@ export default function ActivityGrid({
configurable = false, configurable = false,
}: Props) { }: Props) {
const [color, setColor] = useState(getPrimaryColor())
const [stepState, setStep] = useState(step) const [stepState, setStep] = useState(step)
const [rangeState, setRange] = useState(range) const [rangeState, setRange] = useState(range)
@ -69,14 +50,8 @@ export default function ActivityGrid({
const { theme } = useTheme(); const { theme } = useTheme();
useEffect(() => { const currentTheme = themes.find(t => t.name === theme);
const raf = requestAnimationFrame(() => { const color = currentTheme?.primary || '#f5a97f';
const color = getPrimaryColor()
setColor(color);
});
return () => cancelAnimationFrame(raf);
}, [theme]);
if (isPending) { if (isPending) {
return ( return (