Skip to content

Commit 2eca5f5

Browse files
committed
Always find an art image
1 parent ae32334 commit 2eca5f5

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

components/system/Desktop/Wallpapers/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,5 @@ export const BASE_VIDEO_SELECTOR = ":scope > video";
115115
export const STABLE_DIFFUSION_DELAY_IN_MIN = 10;
116116

117117
export const PRELOAD_ID = "preloadWallpaper";
118+
119+
export const MAX_RETRIES = 5;

components/system/Desktop/Wallpapers/handlers.ts

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { MAX_RETRIES } from "components/system/Desktop/Wallpapers/constants";
12
import {
23
type WallpaperHandler,
34
type ApodResponse,
45
type ArtInstituteOfChicagoResponse,
56
} from "components/system/Desktop/Wallpapers/types";
67
import { type WallpaperFit } from "contexts/session/types";
7-
import { MILLISECONDS_IN_DAY, MILLISECONDS_IN_HOUR } from "utils/constants";
8+
import {
9+
HIGH_PRIORITY_REQUEST,
10+
MILLISECONDS_IN_DAY,
11+
MILLISECONDS_IN_HOUR,
12+
} from "utils/constants";
813
import {
914
jsonFetch,
1015
viewWidth,
@@ -88,25 +93,48 @@ export const wallpaperHandler: Record<string, WallpaperHandler> = {
8893
},
8994
},
9095
};
91-
const response = (await jsonFetch(API_URL.ART_INSTITUTE_OF_CHICAGO, {
92-
body: JSON.stringify(requestPayload),
93-
headers: {
94-
"Content-Type": "application/json",
95-
},
96-
method: "POST",
97-
})) as ArtInstituteOfChicagoResponse;
98-
const imageUrl = (isMaxSize: boolean): string =>
99-
response?.data?.[0]?.image_id
100-
? `https://www.artic.edu/iiif/2/${response.data[0].image_id}/full/${
101-
isMaxSize ? "1686" : "843"
102-
},/0/default.jpg`
103-
: "";
96+
const fetchArtwork = (): Promise<ArtInstituteOfChicagoResponse> =>
97+
jsonFetch<ArtInstituteOfChicagoResponse>(
98+
API_URL.ART_INSTITUTE_OF_CHICAGO,
99+
{
100+
body: JSON.stringify(requestPayload),
101+
headers: {
102+
"Content-Type": "application/json",
103+
},
104+
method: "POST",
105+
}
106+
);
107+
let wallpaperUrl = "";
108+
109+
for (let a = 0; a < MAX_RETRIES; a++) {
110+
try {
111+
// eslint-disable-next-line no-await-in-loop
112+
const { data: [{ image_id } = {}] = [] } = await fetchArtwork();
113+
114+
if (image_id) {
115+
const url = `https://www.artic.edu/iiif/2/${image_id}/full/1686,/0/default.jpg`;
116+
117+
// eslint-disable-next-line no-await-in-loop
118+
const { ok } = await fetch(url, {
119+
...HIGH_PRIORITY_REQUEST,
120+
method: "HEAD",
121+
});
122+
123+
if (ok) {
124+
wallpaperUrl = url;
125+
break;
126+
}
127+
}
128+
} catch {
129+
// Ignore failure to get wallpaper
130+
}
131+
}
104132

105133
return {
106-
fallbackBackground: imageUrl(false),
134+
fallbackBackground: "",
107135
newWallpaperFit: "fit",
108136
updateTimeout: MILLISECONDS_IN_HOUR,
109-
wallpaperUrl: imageUrl(true),
137+
wallpaperUrl,
110138
};
111139
},
112140
LOREM_PICSUM: () => ({

utils/functions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,12 +1159,12 @@ export const getGifJs = async (): Promise<GIFWithWorkers> => {
11591159
}) as GIFWithWorkers;
11601160
};
11611161

1162-
export const jsonFetch = async (
1162+
export const jsonFetch = async <T extends Record<string, unknown>>(
11631163
url: string,
11641164
options?: RequestInit
1165-
): Promise<Record<string, unknown>> => {
1165+
): Promise<T> => {
11661166
const response = await fetch(url, { ...HIGH_PRIORITY_REQUEST, ...options });
1167-
const json = (await response.json()) as Record<string, unknown>;
1167+
const json = (await response.json()) as T;
11681168

11691169
return json || {};
11701170
};

0 commit comments

Comments
 (0)