userscript...

This commit is contained in:
Gabe Yuan
2023-08-06 21:12:01 +08:00
parent 063d7c9ff0
commit fef4d50977
14 changed files with 456 additions and 102 deletions

32
src/hooks/Fetch.js Normal file
View File

@@ -0,0 +1,32 @@
import { useEffect, useState } from "react";
/**
* fetch data hook
* @returns
*/
export const useFetch = (url) => {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
if (!url) {
return;
}
setLoading(true);
fetch(url)
.then((res) => {
if (res.ok) {
if (res.headers.get("Content-Type")?.includes("json")) {
return res.json().then(setData);
}
return res.text().then(setData);
}
setError(`[${res.status}] ${res.statusText}`);
})
.catch(setError)
.finally(() => setLoading(false));
}, [url]);
return [data, loading, error];
};