From fdc551638874f75d2f1d76c66157068e05cea994 Mon Sep 17 00:00:00 2001 From: icechen Date: Wed, 2 Mar 2022 20:07:12 +0800 Subject: [PATCH] update --- src/component/homeButton/index.tsx | 27 ++++++++++++++++++++++++-- src/component/homeSignIn/index.tsx | 31 ++++++++++++++++++++++++++++-- src/index.tsx | 3 ++- src/pages/dashboard/index.tsx | 2 -- src/telegram/telegram.ts | 10 +++++++--- 5 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/component/homeButton/index.tsx b/src/component/homeButton/index.tsx index 0ca1c30..f2ae481 100644 --- a/src/component/homeButton/index.tsx +++ b/src/component/homeButton/index.tsx @@ -1,5 +1,5 @@ import style from "./style.module.css"; -import { useEffect, useRef, useState } from "react"; +import { useEffect, useMemo, useRef, useState } from "react"; import { Close } from "@mui/icons-material"; import { Button, @@ -33,6 +33,22 @@ export function HomeButton(props: Props) { 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 = () => { @@ -67,6 +83,7 @@ export function HomeButton(props: Props) { let handleFileUpload = () => { setUploadRatio(1); + setStartUploadTime(Date.now()); const reader = new FileReader(); reader.onload = function (event) { if (event.target && event.target.result) { @@ -160,7 +177,13 @@ export function HomeButton(props: Props) { 文件大小:{" "} {waitedFileList.length > 0 ? fileSize(waitedFileList[0].size) : ""}
- {uploadRatio !== 0 && <>正在上传: {uploadRatio}%} + {uploadRatio !== 0 && <>正在上传: {uploadRatio.toFixed(2)}%} +
+ {uploadRatio !== 0 && ( + <>已用时间: {(Date.now() - startUploadTime) / 1000}s + )} +
+ {uploadRatio !== 0 && <>上传速度: {uploadSpeed}}
{isFinish && <>上传完成} diff --git a/src/component/homeSignIn/index.tsx b/src/component/homeSignIn/index.tsx index 8d3394f..fad1212 100644 --- a/src/component/homeSignIn/index.tsx +++ b/src/component/homeSignIn/index.tsx @@ -1,13 +1,19 @@ -import { Button, Paper, TextField } from "@mui/material"; +import { Button, Paper, Snackbar, TextField } from "@mui/material"; import style from "./style.module.css"; import CountriesInput, { Country } from "../signin/countriesInput"; import { useMemo, useState } from "react"; -import telegram, { sendSignInCode, signIn } from "../../telegram/telegram"; +import { sendSignInCode, signIn } from "../../telegram/telegram"; +import { getMe } from "../../telegram/user"; +import { login } from "../../store/user"; +import { useDispatch } from "react-redux"; export function HomeSignIn() { + const dispatch = useDispatch(); + let [country, setCountry] = useState({} as Country | null); let [phone, setPhone] = useState(""); let [phoneCode, setPhoneCode] = useState(""); + let [openSuccess, setOpenSuccess] = useState(false); const phoneNumber = useMemo( () => (country ? country.country_code : "") + phone, [country, phone] @@ -29,6 +35,18 @@ export function HomeSignIn() { signIn(phoneNumber, phoneCode, codeHash) .then((user: any) => { console.log("user", user); + setOpenSuccess(true); + getMe() + .then(function (user) { + // 已登录 + dispatch(login(user)); + }) + .catch(function (error) { + if (error.code === 401) { + // Unauthorized + } + console.log(error); + }); }) .catch((error: any) => { console.log("error", error); @@ -124,6 +142,15 @@ export function HomeSignIn() { 未注册账号自动创建为新账号
登录注册均视为已同意 用户协议 和 隐私政策。 + + { + setOpenSuccess(false); + }} + message="登录成功" + /> ); } diff --git a/src/index.tsx b/src/index.tsx index 9adac48..754f3f3 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -12,6 +12,7 @@ import { updateOfStorage } from "./store/fileList"; store.dispatch(updateOfStorage()); +// eslint-disable-next-line no-extend-native Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 @@ -31,7 +32,7 @@ Date.prototype.Format = function (fmt) { if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace( RegExp.$1, - RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length) + RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length) ); return fmt; }; diff --git a/src/pages/dashboard/index.tsx b/src/pages/dashboard/index.tsx index 32d1f30..8f2e24c 100644 --- a/src/pages/dashboard/index.tsx +++ b/src/pages/dashboard/index.tsx @@ -76,8 +76,6 @@ function File() { }; let Download = () => { - // @ts-ignore - console.log("waitDownloadFile", waitDownloadFile?.file_name); downloadFile(waitDownloadFile) .then((res) => { console.log("res", res); diff --git a/src/telegram/telegram.ts b/src/telegram/telegram.ts index 25eba17..364dd4c 100644 --- a/src/telegram/telegram.ts +++ b/src/telegram/telegram.ts @@ -12,7 +12,7 @@ import getLocalStorage from "mtproton/envs/browser/get-local-storage"; class TelegramHelper { private client: any; constructor(appID: number, appHash: string, custom: boolean = false) { - if (custom) { + if (!custom) { this.client = new Client({ api_id: appID, api_hash: appHash, @@ -38,7 +38,7 @@ class TelegramHelper { test: true, }); } - this.client.setDefaultDc(1); + // this.client.setDefaultDc(2); } async call( @@ -49,6 +49,9 @@ class TelegramHelper { ): Promise { try { !hideLog && console.dir(`${method} req\n${obj(params)}`); + options = { + ...options, + }; let resp = await this.client.call(method, params, options); !hideLog && console.dir(`${method} resp\n${obj(resp)}`); return resp; @@ -68,7 +71,7 @@ class TelegramHelper { // } if (error_code === 303) { - const [type, dcIdAsString] = error_message.split("_MIGRATE_"); + const [, dcIdAsString] = error_message.split("_MIGRATE_"); const dcId = Number(dcIdAsString); @@ -110,6 +113,7 @@ async function uploadBigFile( file_total_parts: file_total_parts, bytes: tempBytes, }); + console.log("finished", finished); if (callback) { callback(((i + 1) / file_total_parts) * 100); }