2022-03-02 20:07:12 +08:00

215 lines
5.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import style from "./style.module.css";
import { useEffect, useMemo, useRef, useState } from "react";
import { Close } from "@mui/icons-material";
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
IconButton,
} from "@mui/material";
import fileSize from "filesize";
import SendIcon from "@mui/icons-material/Send";
import { inputFile, sendFile, uploadBigFile } from "../../telegram/telegram";
import { useDispatch } from "react-redux";
import { addFileOfStorage } from "../../store/fileList";
enum Status {
normal,
addFile,
receive,
}
interface Props {
isSignIn: boolean;
onNotSignIn: () => void;
}
export function HomeButton(props: Props) {
let [status, setStatus] = useState(Status.normal);
let [boxStyle, setBoxStyle] = useState([style.box]);
let [isReceive, setIsReceive] = useState(false);
let [waitedFileList, setWaitedFileList] = useState<any[]>([]);
let [uploadRatio, setUploadRatio] = useState(0);
let [isFinish, setIsFinish] = useState(false);
let [startUploadTime, setStartUploadTime] = useState(0);
let uploadSpeed = useMemo(() => {
if (waitedFileList.length === 0) {
return "0/s";
}
let size = waitedFileList[0].size;
if (uploadRatio === 0) {
return "0/s";
}
return (
fileSize((size * uploadRatio * 10) / (Date.now() - startUploadTime)) +
"/s"
);
}, [startUploadTime, uploadRatio, waitedFileList]);
const dispatch = useDispatch();
let onNormal = () => {
setStatus(Status.normal);
};
let onAddFile = () => {
setStatus(Status.addFile);
};
let onReceive = () => {
setStatus(Status.receive);
};
useEffect(() => {
switch (status) {
case Status.normal:
setBoxStyle([style.box]);
break;
case Status.addFile:
setBoxStyle([style.box, style.boxActivePlus]);
break;
case Status.receive:
setBoxStyle([style.box, style.boxActiveReceive]);
break;
}
}, [status]);
let addFileInput = useRef<HTMLInputElement>(null);
let OnChangeFileInput = (e: any) => {
setWaitedFileList(e.target.files);
};
let handleFileUpload = () => {
setUploadRatio(1);
setStartUploadTime(Date.now());
const reader = new FileReader();
reader.onload = function (event) {
if (event.target && event.target.result) {
let bytes = event.target.result;
uploadBigFile(bytes as ArrayBuffer, setUploadRatio)
.then((file) => {
console.log("uploadFile ret:", file);
sendFile(
// @ts-ignore
inputFile(file.file_id, file.total_part, waitedFileList[0].name),
waitedFileList[0].type
).then((result) => {
console.log("sendFile ret:", result);
result.updates[1].message.media.document.file_name =
waitedFileList[0].name;
dispatch(
addFileOfStorage(result.updates[1].message.media.document)
);
setIsFinish(true);
});
})
.catch((error) => {
console.log("uploadFile error:", error);
alert(error);
});
}
};
reader.readAsArrayBuffer(waitedFileList[0]);
};
return (
<div className={boxStyle.join(" ")}>
{!isReceive ? (
<>
<div className={style.plus}></div>
<div
className={style.addFile}
onMouseEnter={onAddFile}
onMouseLeave={onNormal}
onClick={() => addFileInput.current?.click()}
>
<input
ref={addFileInput}
type="file"
style={{ display: "none" }}
onChange={OnChangeFileInput}
/>
</div>
<button
className={style.receive}
onMouseEnter={onReceive}
onMouseLeave={onNormal}
onClick={() => {
if (props.isSignIn) {
setIsReceive(true);
} else {
props.onNotSignIn();
}
}}
>
</button>
</>
) : (
<>
<input
placeholder={"输入6位传输口令获取文件"}
style={{}}
className={isReceive ? style.receiveInput : style.receiveInputNone}
/>
<IconButton
className={style.receiveInputClose}
aria-label="delete"
onClick={() => {
setIsReceive(false);
}}
>
<Close />
</IconButton>
</>
)}
<Dialog open={waitedFileList.length !== 0}>
<DialogTitle></DialogTitle>
<DialogContent sx={{ minWidth: 300 }}>
?
<br />
<br />
: {waitedFileList.length > 0 ? waitedFileList[0].name : ""}
<br />
:{" "}
{waitedFileList.length > 0 ? fileSize(waitedFileList[0].size) : ""}
<br />
{uploadRatio !== 0 && <>: {uploadRatio.toFixed(2)}%</>}
<br />
{uploadRatio !== 0 && (
<>: {(Date.now() - startUploadTime) / 1000}s</>
)}
<br />
{uploadRatio !== 0 && <>: {uploadSpeed}</>}
<br />
{isFinish && <></>}
</DialogContent>
<DialogActions>
<Button
variant="outlined"
onClick={() => {
setWaitedFileList([]);
setUploadRatio(0);
setIsFinish(false);
}}
>
</Button>
{uploadRatio === 0 && (
<Button
variant="contained"
onClick={handleFileUpload}
endIcon={<SendIcon />}
>
</Button>
)}
</DialogActions>
</Dialog>
</div>
);
}