feat(don): fix flicking by calculating size only once
Build and Push Image / build-frontend (push) Successful in 18s
Build and Push Image / build-backend (push) Successful in 16s
Build and Push Image / push (push) Successful in 15s

This commit is contained in:
2026-06-10 14:29:24 +07:00 Unverified
parent a31c8fbb54
commit a667f21376
2 changed files with 13 additions and 4 deletions
+11 -2
View File
@@ -9,6 +9,7 @@ type Data = Awaited<ReturnType<typeof getTopDonate>>;
export default function TopDonateWidget() {
const [top, setTop] = useState<Data>();
const textRef = useRef<HTMLDivElement | null>(null);
const stableSize = useRef<Record<string, number>>({});
useEffect(() => {
async function update() {
@@ -27,13 +28,21 @@ export default function TopDonateWidget() {
if (!textRef.current) return;
var fontSize = 48,
count = 0;
const el = textRef.current;
const el = textRef.current,
key = `${top?.name}${top?.amount}`;
if (stableSize.current[key]) {
el.style.fontSize = `${stableSize.current[key]}px`;
return;
}
const interval = setInterval(() => {
console.log(el.scrollWidth, el.clientWidth);
fontSize = (fontSize * el.clientWidth) / el.scrollWidth;
el.style.fontSize = `${fontSize}px`;
count++;
if (count > 20) clearInterval(interval);
if (count > 20) {
stableSize.current[key] = fontSize;
clearInterval(interval);
}
}, 100);
return () => clearInterval(interval);
}, [textRef, top]);