I think maybe improved performance of year thing?

I changed it to work with the useCallback system on the relevant parts, which apparently should reduce unnecessary redraws?
replace/0ee117191a1fdb7a781a6f4cd1ee7f752d634283
Cobular 5 years ago
parent 0e38f49493
commit 89ce253c82

@ -1,16 +1,17 @@
import "./AboutMe.scss"; import "./AboutMe.scss";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import { Tooltip } from "antd"; import { Tooltip } from "antd";
import { useState, useEffect } from "react"; import { useCallback, useEffect, useState } from "react";
import axios from "axios"; import axios from "axios";
interface NowPlayingData { interface NowPlayingData {
introduction: string, introduction: string;
lastfm: { lastfm: {
artist: string, artist: string;
track: string, track: string;
playing: boolean playing: boolean;
} url: string | undefined;
};
} }
function LastFmLi() { function LastFmLi() {
@ -19,17 +20,16 @@ function LastFmLi() {
lastfm: { lastfm: {
artist: "nobody", artist: "nobody",
playing: false, playing: false,
track: "nothing" track: "nothing",
} url: undefined,
},
}); });
async function updateNowPlaying() { async function updateNowPlaying() {
try { try {
const songRequestData = await axios.get( const songRequestData = await axios.get("https://api.jakecover.me");
"https://api.jakecover.me"
);
setNowPlayingData(songRequestData.data) setNowPlayingData(songRequestData.data);
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
@ -45,10 +45,22 @@ function LastFmLi() {
}; };
}, []); }, []);
if (nowPlayingData.lastfm.playing) { if (nowPlayingData.lastfm.playing) {
return <li>listening to {nowPlayingData.lastfm.track} by {nowPlayingData.lastfm.artist}</li>; return (
<li>
<a
href={nowPlayingData.lastfm.url}
target={"_blank"}
rel="noreferrer"
style={{ color: "inherit", textDecoration: "inherit" }}
>
listening to {nowPlayingData.lastfm.track} by{" "}
{nowPlayingData.lastfm.artist}
</a>
</li>
);
} }
return <li>not listening to anything right now.</li>; return <li>not listening to anything right now.</li>;
}; }
function Age() { function Age() {
// Accounts for leap years and stuff // Accounts for leap years and stuff
@ -77,9 +89,9 @@ function Age() {
// Check to see if we've gone too deep // Check to see if we've gone too deep
if (count > 25) return [center_n, center_d]; if (count > 25) return [center_n, center_d];
// Check to see if we're close enough to the target // Check to see if we're close enough to the target
if (Math.abs(target - (center_n / center_d)) > 0.001) { if (Math.abs(target - center_n / center_d) > 0.001) {
// Look right down the tree // Look right down the tree
if (target > (center_n / center_d)) if (target > center_n / center_d)
return searchSternBorcotTree( return searchSternBorcotTree(
target, target,
center_n, center_n,
@ -116,28 +128,36 @@ function Age() {
return `${ageYearsComponent} and ${target_n}/${target_d}`; return `${ageYearsComponent} and ${target_n}/${target_d}`;
} }
const [age, setAge] = useState(CalcAge()); const millisecondsYear = milliseconds_year();
const time = new Date().getTime();
// eslint-disable-next-line
const memoizedCallbackCalcAge = useCallback(CalcAge, [
millisecondsYear,
time,
]);
const [age, setAge] = useState(memoizedCallbackCalcAge());
function UpdateAge() { function UpdateAge() {
setAge(CalcAge()); setAge(memoizedCallbackCalcAge());
} }
const memoizedCallbackUpdateAge = useCallback(UpdateAge, [
memoizedCallbackCalcAge,
]);
useEffect(() => { useEffect(() => {
UpdateAge(); memoizedCallbackUpdateAge();
const interval = setInterval(UpdateAge, 5000); const interval = setInterval(memoizedCallbackUpdateAge, 5000);
return () => { return () => {
clearInterval(interval); clearInterval(interval);
}; };
}, []); }, [memoizedCallbackUpdateAge]);
return ( return <li>{age} years old</li>;
<li> }
{age} years old
</li>
);
};
export function AboutMe() { export function AboutMe() {
function EmailHandler() { function EmailHandler() {
@ -223,19 +243,18 @@ export function AboutMe() {
<h2>I'm:</h2> <h2>I'm:</h2>
<ul> <ul>
<Age /> <Age />
<LastFmLi />
<Tooltip <Tooltip
title={ title={
"Hey! This part is still a work in progress, check back in a bit to see if it's working!" "Hey! This part is still a work in progress, check back in a bit to see if it's working!"
} }
> >
<LastFmLi />
<li> <li>
doing (general stuff - in class, projects, sleeping, games) doing (general stuff - in class, projects, sleeping, games)
</li> </li>
</Tooltip> </Tooltip>
<li> <li>rather colorblind</li>
rather colorblind
</li>
</ul> </ul>
</div> </div>
</div> </div>

Loading…
Cancel
Save