aboutsummaryrefslogtreecommitdiff
path: root/webAO/utils
diff options
context:
space:
mode:
authorstonedDiscord <Tukz@gmx.de>2023-11-30 02:35:02 +0100
committerGitHub <noreply@github.com>2023-11-30 02:35:02 +0100
commit4435a4014c0a68fc156a29f83e01cbfab4912794 (patch)
tree3c4f6927ae2c515dca1585430aca6cd7f1bf8874 /webAO/utils
parent92aa1322f8b086484a74f40619cf60b4b702720c (diff)
parent90ed4b74cac7e8e410175cc3445d7140671e221f (diff)
Merge pull request #205 from Troid-Tech/fix-missing-desks
Fix missing desks
Diffstat (limited to 'webAO/utils')
-rw-r--r--webAO/utils/fileExists.js18
-rw-r--r--webAO/utils/fileExists.ts19
-rw-r--r--webAO/utils/filesExist.ts28
-rw-r--r--webAO/utils/findImgSrc.ts19
4 files changed, 66 insertions, 18 deletions
diff --git a/webAO/utils/fileExists.js b/webAO/utils/fileExists.js
deleted file mode 100644
index 5b60976..0000000
--- a/webAO/utils/fileExists.js
+++ /dev/null
@@ -1,18 +0,0 @@
-const fileExists = async (url) => 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);
-});
-export default fileExists;
diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts
new file mode 100644
index 0000000..abb2928
--- /dev/null
+++ b/webAO/utils/fileExists.ts
@@ -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);
+ });
+}
diff --git a/webAO/utils/filesExist.ts b/webAO/utils/filesExist.ts
new file mode 100644
index 0000000..2f39427
--- /dev/null
+++ b/webAO/utils/filesExist.ts
@@ -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
+}
diff --git a/webAO/utils/findImgSrc.ts b/webAO/utils/findImgSrc.ts
new file mode 100644
index 0000000..b4db849
--- /dev/null
+++ b/webAO/utils/findImgSrc.ts
@@ -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;
+ });
+}