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([]); 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(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 (
{!isReceive ? ( <>
addFileInput.current?.click()} > 添加文件
) : ( <> { setIsReceive(false); }} > )} 文件上传 是否上传文件?

文件名: {waitedFileList.length > 0 ? waitedFileList[0].name : ""}
文件大小:{" "} {waitedFileList.length > 0 ? fileSize(waitedFileList[0].size) : ""}
{uploadRatio !== 0 && <>正在上传: {uploadRatio.toFixed(2)}%}
{uploadRatio !== 0 && ( <>已用时间: {(Date.now() - startUploadTime) / 1000}s )}
{uploadRatio !== 0 && <>上传速度: {uploadSpeed}}
{isFinish && <>上传完成}
{uploadRatio === 0 && ( )}
); }