|
| 1 | +import { MAX_RETRIES } from "components/system/Desktop/Wallpapers/constants"; |
1 | 2 | import { |
2 | 3 | type WallpaperHandler, |
3 | 4 | type ApodResponse, |
4 | 5 | type ArtInstituteOfChicagoResponse, |
5 | 6 | } from "components/system/Desktop/Wallpapers/types"; |
6 | 7 | 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"; |
8 | 13 | import { |
9 | 14 | jsonFetch, |
10 | 15 | viewWidth, |
@@ -88,25 +93,48 @@ export const wallpaperHandler: Record<string, WallpaperHandler> = { |
88 | 93 | }, |
89 | 94 | }, |
90 | 95 | }; |
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 | + } |
104 | 132 |
|
105 | 133 | return { |
106 | | - fallbackBackground: imageUrl(false), |
| 134 | + fallbackBackground: "", |
107 | 135 | newWallpaperFit: "fit", |
108 | 136 | updateTimeout: MILLISECONDS_IN_HOUR, |
109 | | - wallpaperUrl: imageUrl(true), |
| 137 | + wallpaperUrl, |
110 | 138 | }; |
111 | 139 | }, |
112 | 140 | LOREM_PICSUM: () => ({ |
|
0 commit comments