Skip to content

Commit ec258c3

Browse files
committed
Refactor + backup ipsum wp
1 parent 89acca3 commit ec258c3

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

components/system/Desktop/Wallpapers/handlers.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ const API_URL = {
2424
ART_INSTITUTE_OF_CHICAGO: "https://api.artic.edu/api/v1/artworks/search",
2525
};
2626

27+
const isResourceOk = async (url: string): Promise<boolean> => {
28+
try {
29+
const { ok } = await fetch(url, {
30+
...HIGH_PRIORITY_REQUEST,
31+
method: "HEAD",
32+
});
33+
34+
return ok;
35+
} catch {
36+
return false;
37+
}
38+
};
39+
2740
export const wallpaperHandler: Record<string, WallpaperHandler> = {
2841
APOD: async ({ isAlt }) => {
2942
const response = await jsonFetch(
@@ -111,12 +124,8 @@ export const wallpaperHandler: Record<string, WallpaperHandler> = {
111124

112125
if (image_id) {
113126
const url = `https://www.artic.edu/iiif/2/${image_id}/full/1686,/0/default.jpg`;
114-
const { ok } = await fetch(url, {
115-
...HIGH_PRIORITY_REQUEST,
116-
method: "HEAD",
117-
});
118127

119-
if (ok) return url;
128+
if (await isResourceOk(url)) return url;
120129
}
121130
} catch {
122131
// Ignore failure to get wallpaper
@@ -139,10 +148,16 @@ export const wallpaperHandler: Record<string, WallpaperHandler> = {
139148
wallpaperUrl: await maybeFetchArtwork(),
140149
};
141150
},
142-
LOREM_PICSUM: () => ({
143-
fallbackBackground: "",
144-
newWallpaperFit: "fill",
145-
updateTimeout: MILLISECONDS_IN_HOUR,
146-
wallpaperUrl: `https://picsum.photos/seed/${Date.now()}/${viewWidth()}/${viewHeight()}`,
147-
}),
151+
LOREM_PICSUM: () => {
152+
// eslint-disable-next-line unicorn/consistent-function-scoping
153+
const createLoremPicsumUrl = (): string =>
154+
`https://picsum.photos/seed/${Math.floor(Math.random() * Date.now())}/${viewWidth()}/${viewHeight()}`;
155+
156+
return {
157+
fallbackBackground: createLoremPicsumUrl(),
158+
newWallpaperFit: "fill",
159+
updateTimeout: MILLISECONDS_IN_HOUR,
160+
wallpaperUrl: createLoremPicsumUrl(),
161+
};
162+
},
148163
};

0 commit comments

Comments
 (0)