-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from Troid-Tech/fix-missing-desks
Fix missing desks
- Loading branch information
Showing
5 changed files
with
75 additions
and
26 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default async function fileExists(url: string): Promise<boolean> { | ||
return new Promise((resolve) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open('HEAD', url); | ||
xhr.onload = function checkLoad() { | ||
if (xhr.readyState === 4) { | ||
if (xhr.status === 200) { | ||
resolve(true); | ||
} else { | ||
resolve(false); | ||
} | ||
} | ||
}; | ||
xhr.onerror = function checkError() { | ||
resolve(false); | ||
}; | ||
xhr.send(null); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import fileExists from "./fileExists"; | ||
|
||
/** | ||
* This function takes a list of urls and returns the first one that exists. | ||
* It checks all the URLs in parallel. | ||
* @param urls the list of URLs to check | ||
* @returns either the first URL that exists or null if none were found | ||
*/ | ||
export default async function filesExist(urls: string[]): Promise<string | null> { | ||
const promises = urls.map(async (url) => { | ||
if (await fileExists(url)) { | ||
return url; | ||
} | ||
return null; | ||
}); | ||
|
||
// Run all in parallel | ||
const results = await Promise.all(promises); | ||
|
||
// Find the first URL that exists (not null) or return null if none exist | ||
for (const result of results) { | ||
if (result !== null) { | ||
return result; | ||
} | ||
} | ||
|
||
return null; // None of the URLs exist | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import filesExist from "./filesExist"; | ||
import transparentPng from '../constants/transparentPng' | ||
|
||
/** | ||
* This function takes a list of urls and returns the first one that exists. | ||
* If none is found, return a transparent png. | ||
* The function will always return a value that is appriopriate for an img src. | ||
* @param urls The list of urls to try | ||
* @returns The image source of the first url that exists, or a transparent png if none exist | ||
*/ | ||
export default async function findImgSrc(urls: string[]): Promise<string> { | ||
return filesExist(urls).then((url) => { | ||
if (url !== null) { | ||
return url; | ||
} | ||
// If none of the images exist, return a transparent png | ||
return transparentPng; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters