added errors and load state
This commit is contained in:
parent
f339d90d00
commit
b27fc16ed2
28
src/App.tsx
28
src/App.tsx
@ -29,31 +29,35 @@ function App(props: Props) {
|
|||||||
const [content,setContent] = useState<string>("");
|
const [content,setContent] = useState<string>("");
|
||||||
const [title,setTitle] = useState<string>("");
|
const [title,setTitle] = useState<string>("");
|
||||||
const [lang,setLang] = useState<string>("text");
|
const [lang,setLang] = useState<string>("text");
|
||||||
|
const [appState,setAppState] = useState<string>("loading");
|
||||||
|
|
||||||
useEffect(()=>{
|
useEffect(()=>{
|
||||||
(async ()=>{
|
(async ()=>{
|
||||||
|
|
||||||
|
if (!props.shareID){
|
||||||
|
// No share defined
|
||||||
|
setAppState("noID");
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch(`/index.php/s/${props.shareID}/download`);
|
const response = await fetch(`/index.php/s/${props.shareID}/download`);
|
||||||
|
|
||||||
if (response.status === 404){
|
if (response.status === 404){
|
||||||
// File not found
|
// File not found
|
||||||
// TODO display error
|
setAppState("notFound");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentLength = parseInt(response.headers.get("content-length")!,10);
|
|
||||||
const filename = parseFilename(response.headers);
|
const filename = parseFilename(response.headers);
|
||||||
setTitle(filename);
|
setTitle(filename);
|
||||||
document.title = filename;
|
document.title = filename;
|
||||||
|
|
||||||
setLang(getLangFromFilename(filename) || "text")
|
setLang(getLangFromFilename(filename) || "text")
|
||||||
|
|
||||||
if (contentLength <= 20000) {
|
|
||||||
const text = await response.text()
|
const text = await response.text()
|
||||||
setContent(text);
|
setContent(text);
|
||||||
|
setAppState("done");
|
||||||
|
|
||||||
}else{
|
|
||||||
// TODO display error
|
|
||||||
}
|
|
||||||
})()
|
})()
|
||||||
},[props.shareID])
|
},[props.shareID])
|
||||||
|
|
||||||
@ -61,6 +65,18 @@ function App(props: Props) {
|
|||||||
window.open(`/index.php/s/${props.shareID}/download`, "_self");
|
window.open(`/index.php/s/${props.shareID}/download`, "_self");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle error/loading cases
|
||||||
|
switch (appState) {
|
||||||
|
case "loading":
|
||||||
|
return <div></div>;
|
||||||
|
case "notFound":
|
||||||
|
return <div className="error">404</div>
|
||||||
|
case "noID":
|
||||||
|
return <div className="error">NO ID PROVIDED</div>
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="fileContainer">
|
<div className="fileContainer">
|
||||||
<div className="headerContainer">
|
<div className="headerContainer">
|
||||||
|
@ -51,6 +51,19 @@
|
|||||||
color: #50545b !important;
|
color: #50545b !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
/*Center element*/
|
||||||
|
margin: 0;
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translate(-50%,-50%);
|
||||||
|
|
||||||
|
font-size: 5rem;
|
||||||
|
font-family: sans-serif;
|
||||||
|
color: #58a6ff;
|
||||||
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
background-color: #0d1117;
|
background-color: #0d1117;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user