Skip to content

Commit 0d6ae11

Browse files
committed
fix: strict null checking and other edge cases
1 parent a34453c commit 0d6ae11

File tree

10 files changed

+21
-23
lines changed

10 files changed

+21
-23
lines changed

src/components/DynamicCurrentStatus.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ const DynamicCurrentStatus: FC = memo(() => {
7575

7676
const lastStatus = statuses[statuses.length - 1];
7777
const lastNowPlayingData = isNowPlayingData(lastStatus) ? lastStatus : null;
78-
const hasNewNowPlayingData =
79-
!!updatedNowPlayingData &&
80-
updatedNowPlayingData.uri !== lastNowPlayingData?.uri;
8178

82-
if (hasNewNowPlayingData) {
79+
if (
80+
!!updatedNowPlayingData &&
81+
updatedNowPlayingData.uri !== lastNowPlayingData?.uri
82+
) {
8383
console.debug('New now playing data found...', updatedNowPlayingData);
8484
setStatuses((prev) => [...prev, updatedNowPlayingData]);
8585
}

src/components/DynamicTime.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ const timeToColor = (hour: number, time: string): TextGradientInfo => {
6262
case '11PM':
6363
case '12AM':
6464
return [`${110 + hour * 2}deg`, '#271F3F', '#062B79'];
65+
66+
default:
67+
return [`90deg`, '#000', '#000'];
6568
}
6669
};
6770

src/components/core/Link.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const Link: FC<LinkProps> = ({ bare = false, children, ...rest }) => {
3232
onMouseLeave={() => setIsHoveringLink(false)}
3333
{...rest}
3434
>
35-
{children}
35+
{children as any}
3636
</A>
3737
);
3838
};

src/pages/api/update-status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
66
res.setHeader('Content-Type', 'application/json');
77

88
const shouldClearStatus = !!req.query['clear'];
9-
console.log({ shouldClearStatus });
109
const status = shouldClearStatus ? null : req.query['status'];
1110

1211
if (status !== undefined) {
1312
try {
13+
console.log(`Updating status to ${status}`);
1414
const client = new StorageClient();
1515
const statusSetOk = await client.set(StorageKey.STATUS, status);
1616
client.disconnect();

src/pages/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const IndexPage = (props: TPageProps) => (
7676
);
7777

7878
export async function getStaticProps() {
79-
console.debug('Retreving now playing data and timezone...');
79+
console.log('Retrieving now playing data and timezone...');
8080
const client = new StorageClient();
8181
const { token } = await client.getSpotifyCredentials();
8282
const currentOffset = await client.getTimezoneOffset();

src/services/_server_/now-playing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const SERVER_SIDE_COLOR_OPTIONS = {
77
imageClass: CanvasImage,
88
};
99

10-
const getNowPlayingDataServerSide = async (accessToken: string) =>
10+
const getNowPlayingDataServerSide = async (accessToken: string | null) =>
1111
getNowPlaying(accessToken, SERVER_SIDE_COLOR_OPTIONS);
1212

1313
export { getNowPlayingDataServerSide };

src/services/_server_/storage.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,10 @@ class StorageClient {
3737
);
3838

3939
if (accessTokenExpiry < Date.now()) {
40-
console.debug(
41-
`Access token expired, requesting new and setting token expiry to ${new Date(
42-
Date.now() + 3600 * 1000
43-
).toLocaleString()}`
44-
);
45-
4640
const { access_token } = await requestNewToken();
4741
const newExpiry = Date.now() + 3600 * 1000; // spotify tokens expire in an hour
4842

43+
console.debug(`Token expired, new with expiry ${newExpiry.toString()}`);
4944
await this.client.set(StorageKey.ACCESS_TOKEN, access_token);
5045
await this.client.set(StorageKey.ACCESS_TOKEN_EXPIRY, newExpiry);
5146

src/services/now-playing.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const requestNewToken = async () => {
2626
refresh_token: process.env.REFRESH_TOKEN,
2727
})
2828
.map(
29-
([key, val]) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`
29+
([key, val]) =>
30+
`${encodeURIComponent(key)}=${encodeURIComponent(val as string)}`
3031
)
3132
.join('&');
3233

@@ -51,7 +52,7 @@ const requestNewToken = async () => {
5152
const extractNowPlayingData = (data: any) => {
5253
const isPlaylist = data?.context?.type === 'playlist';
5354

54-
switch (data.currently_playing_type) {
55+
switch (data.currently_playing_type as 'track' | 'episode') {
5556
case 'track': {
5657
const {
5758
item: {
@@ -88,7 +89,6 @@ const extractNowPlayingData = (data: any) => {
8889
type: 'episode' as const,
8990
uri,
9091
name,
91-
artistName: null,
9292
podcastName,
9393
link: isPlaylist ? data.context?.external_urls.spotify : spotify,
9494
images,
@@ -119,12 +119,12 @@ const getNowPlaying = async (
119119
if (!res.ok)
120120
throw new Error(`Request error ${res.status}: ${JSON.stringify(data)}`);
121121

122-
const { images, ...nowPlayingData } = extractNowPlayingData(data);
123-
const coverArtSrc = images.find((i: any) => i.width === 64)?.url;
122+
const np = extractNowPlayingData(data);
123+
const coverArtSrc = np.images.find((i: any) => i.width === 64)?.url;
124124
const coverArtColor = await getBestTextColor(coverArtSrc, colorOptions);
125125

126126
return {
127-
...nowPlayingData,
127+
...np,
128128
coverArtSrc,
129129
coverArtColor,
130130
};

src/services/site/context.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const shuffledTaglines = getShuffledArray(TAGLINES);
4242

4343
const SiteContextProvider: FC<TPageProps> = ({
4444
currentOffset,
45+
spotifyToken,
4546
children,
4647
...rest
4748
}) => {
@@ -57,6 +58,7 @@ const SiteContextProvider: FC<TPageProps> = ({
5758
currentDate: getDateFromOffset(currentOffset),
5859
activity,
5960
talkingPoint,
61+
spotifyToken: spotifyToken ?? '',
6062
...rest,
6163

6264
isEasterEggActive,

src/services/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const getShuffledArray = <T = unknown>(arr: T[]) => {
1818
const getDateFromOffset = (offsetMins: string): Date => {
1919
const offsetMinsNum = Number(offsetMins);
2020

21-
console.log(offsetMins);
2221
const now = new Date();
2322
const curUTC = new Date(
2423
now.getUTCFullYear(),
@@ -30,7 +29,6 @@ const getDateFromOffset = (offsetMins: string): Date => {
3029
now.getUTCMilliseconds()
3130
);
3231
curUTC.setMinutes(curUTC.getMinutes() + offsetMinsNum);
33-
console.log(curUTC.toString());
3432
return curUTC;
3533
};
3634

@@ -70,7 +68,7 @@ const useVisibilityChange = (
7068

7169
if (hidden && CHANGE_EVENT_NAME) {
7270
const onVisibilityChange = () =>
73-
visibilityChangeHandler(document[hidden]);
71+
visibilityChangeHandler((document as any)[hidden]);
7472

7573
document.addEventListener(CHANGE_EVENT_NAME, onVisibilityChange, false);
7674
return () =>

0 commit comments

Comments
 (0)