import { ArrowLeftOutlined, BugOutlined, CloudDownloadOutlined, CloudUploadOutlined, CrownOutlined, DeleteOutlined, DownloadOutlined, ExpandAltOutlined, ExperimentOutlined, FrownOutlined, InfoOutlined, ImportOutlined, LoginOutlined, LogoutOutlined, MobileOutlined, MonitorOutlined, ReloadOutlined, ExportOutlined, SkinOutlined, SyncOutlined, WarningOutlined } from '@ant-design/icons' import { Avatar, Button, Card, Checkbox, Col, Form, Input, Layout, List, Modal, notification, Popover, Progress, Row, Select, Space, Switch, Tooltip, Typography, Upload } from 'antd' import { useForm } from 'antd/es/form/Form' import prettyBytes from 'pretty-bytes' import pwaInstallHandler from 'pwa-install-handler' import React, { useEffect, useState } from 'react' import { useThemeSwitcher } from 'react-css-theme-switcher' import { useHistory } from 'react-router-dom' import useSWR from 'swr' import { Api } from 'telegram' import * as serviceWorkerRegistration from '../serviceWorkerRegistration' import { VERSION } from '../utils/Constant' import { apiUrl, fetcher, req } from '../utils/Fetcher' import { telegramClient } from '../utils/Telegram' interface Props { me?: any, mutate?: any, error?: any } const Settings: React.FC = ({ me, mutate, error }) => { const history = useHistory() const [expandableRows, setExpandableRows] = useState() const [logoutConfirmation, setLogoutConfirmation] = useState(false) const [removeConfirmation, setRemoveConfirmation] = useState(false) const [expFeatures, setExpFeatures] = useState(false) const [loadingChangeServer, setLoadingChangeServer] = useState(false) const [loadingRemove, setLoadingRemove] = useState(false) const [destroySession, setDestroySession] = useState(false) const [reportBug, setReportBug] = useState(false) const [pwa, setPwa] = useState<{ canInstall: boolean, install: () => Promise }>() const [dc, setDc] = useState() const [form] = useForm() const [formRemoval] = useForm() const { currentTheme } = useThemeSwitcher() const { data: dialogs } = useSWR('/dialogs?limit=75&offset=0', fetcher) const { data: stats } = useSWR('/files/stats', fetcher) const save = async (settings: any): Promise => { try { await req.patch('/users/me/settings', { settings }) notification.success({ message: 'Saved' }) mutate() } catch ({ response }) { if ((response as any).status === 402) { return notification.error({ message: 'Premium Feature', description: 'Please upgrade your plan for using this feature' }) } return notification.error({ message: 'Something error. Please try again.' }) } } useEffect(() => { if (me) { setExpandableRows(me.user?.settings?.expandable_rows) } }, [me]) useEffect(() => { if (error) { return history.push('/login') } }, [error]) useEffect(() => { pwaInstallHandler.addListener(canInstall => { setPwa({ canInstall, install: pwaInstallHandler.install }) }) }, []) useEffect(() => { if (window.location.host === 'ge.teledriveapp.com') { setDc('ge') localStorage.setItem('dc', 'ge') } else if (window.location.host === 'us.teledriveapp.com') { setDc('us') localStorage.setItem('dc', 'us') } else { setDc('sg') localStorage.setItem('dc', 'sg') } }, []) useEffect(() => { if (dc) { form.setFieldsValue({ change_server: dc }) } }, [dc]) useEffect(() => { if (me) { form.setFieldsValue({ saved_location: me?.user.settings?.saved_location || 'me' }) } }, [me]) const logout = async () => { await req.post('/auth/logout', {}, destroySession ? { params: { destroySession: 1 } } : undefined) window.localStorage.removeItem('experimental') return window.location.replace('/') } const remove = async () => { setLoadingRemove(true) const { agreement, reason } = formRemoval.getFieldsValue() try { await req.post('/users/me/delete', { agreement, reason }) setRemoveConfirmation(false) setLoadingRemove(false) return window.location.replace('/') } catch (error: any) { setLoadingRemove(false) return notification.error({ message: 'Error', description: <> {error?.response?.data?.error || error.message || 'Something error'} {JSON.stringify(error?.response?.data || error?.data || error, null, 2)} }) } } const exportFilesData = async () => { setLoadingChangeServer(true) const { data } = await req.get('/files', { params: { full_properties: 1, sort: 'created_at', offset: 0, limit: 10 } }) const files = data?.files || [] while (files?.length < data.length) { const { data } = await req.get('/files', { params: { full_properties: 1, sort: 'created_at', offset: 0, limit: 10 } }) files.push(...data.files) } const hiddenElement = document.createElement('a') hiddenElement.href = 'data:attachment/text,' + encodeURI(JSON.stringify(files) || '') hiddenElement.target = '_blank' hiddenElement.download = 'files.json' hiddenElement.click() setLoadingChangeServer(false) } const downloadLogs = async () => { const hiddenElement = document.createElement('a') hiddenElement.href = 'data:attachment/text,' + encodeURI(sessionStorage.getItem('requests') || '') hiddenElement.target = '_blank' hiddenElement.download = 'logs.json' hiddenElement.click() } const emailLink = () => `mailto:bug@teledriveapp.com?subject=TeleDrive%20-%20Bug%20Report&body=User%3A%20${decodeURIComponent(me?.user.username)}%0D%0AOrigin%3A%20${decodeURIComponent(window.location.origin)}%0D%0ADevice%3A%20${decodeURIComponent(navigator.userAgent)}%0D%0AProblem%3A%20%3CPlease%20describe%20your%20problem%20here%3E%0D%0AExpectation%3A%20%3CPlease%20describe%20your%20expectation%20here%3E` const buildPathDialog = (dialog: any) => { const peerType = dialog.isUser ? 'user' : dialog.isChannel ? 'channel' : 'chat' return `${peerType}/${dialog.entity?.id}/_${dialog.entity?.accessHash ? `/${dialog.entity?.accessHash}` : ''}` } return <> Settings } title={<>{me?.user.name} {me?.user?.plan === 'premium' && Premium}> }} description={me?.user.username} />} actions={[ v{VERSION} ]}>
{stats?.stats && <> {prettyBytes(Number(stats.stats.totalUserFilesSize))} User Files Size } percent={Number((Number(stats.stats.totalUserFilesSize) / Number(stats.stats.totalFilesSize) * 100).toFixed(1))} /> <> {prettyBytes(Number(stats.stats.totalFilesSize))} Total Files Size } percent={100} /> } /> } /> } /> } {pwa?.canInstall && ]}> <>Install App} description="Install TeleDrive to your device" /> } { setExpandableRows(val) save({ expandable_rows: val }) }} checked={expandableRows} defaultChecked={expandableRows} /> ]}> <>Expandable Rows} description="Show file details in row table" /> save({ theme: val ? 'dark' : 'light' }).then(window.location.reload)} checked={currentTheme === 'dark'} defaultChecked={currentTheme === 'dark'} /> ]}> <>Dark Mode} description="Join the dark side" /> {dialogs?.dialogs && ]}> <>Upload Destination} description="Select where to save files" /> } ]}> <>Check Updates} description="Reload to checking for updates" /> ]}> <>Report Bug} description="Send your activities for reporting" /> ]}> <>Save Data} description="Export your files ref data as JSON" /> ]}> <>Import Data} description="Import your files ref data" /> ]}> <>Experimental} description="Join to the experimental features" /> ]}> <>Delete Account} description="Delete your account permanently" />
Logout Confirmation } visible={logoutConfirmation} onCancel={() => setLogoutConfirmation(false)} onOk={logout} cancelButtonProps={{ shape: 'round' }} okButtonProps={{ danger: true, type: 'primary', shape: 'round' }}> Are you sure to logout? setDestroySession(target.checked)}> Also delete my active session This action cannot be undone } visible={removeConfirmation} onCancel={() => setRemoveConfirmation(false)} onOk={formRemoval.submit} cancelButtonProps={{ shape: 'round' }} okButtonProps={{ danger: true, type: 'primary', shape: 'round', loading: loadingRemove }}>
Please type permanently removed for your confirmation} rules={[{ required: true, message: 'Please input the confirmation' }]}>
{/* Change Server Confirmation } visible={!!changeDCConfirmation} onCancel={() => setChangeDCConfirmation(undefined)} onOk={changeServer} cancelButtonProps={{ shape: 'round' }} okButtonProps={{ danger: true, type: 'primary', shape: 'round', loading: loadingChangeServer }}> Are you sure to change the server region to {changeDCConfirmation === 'ge' ? 'Frankfurt' : changeDCConfirmation === 'us' ? 'New York' : 'Singapore'}? You'll be logged out and redirected to the new server. Please login again to that new server. */} Report Bugs } visible={reportBug} onCancel={() => setReportBug(false)} onOk={undefined} okText="Send Email" cancelButtonProps={{ shape: 'round' }} okButtonProps={{ type: 'primary', shape: 'round', href: emailLink() }}> Please follow these instructions:
  1. Download your logs
  2. Send an email to bug@teledriveapp.com with logs and additional screenshots in the attachment
Join Experimental } visible={expFeatures} onCancel={() => { localStorage.removeItem('experimental') setExpFeatures(false) }} onOk={() => { localStorage.setItem('experimental', 'true') setExpFeatures(false) window.open(`${window.location.origin}/login`, '_blank', 'location=yes,height=720,width=520,scrollbars=yes,status=yes,top=100,left=300') }} cancelButtonProps={{ shape: 'round' }} okButtonProps={{ type: 'primary', shape: 'round' }}> You will get this experimental features:
  • Ultra Upload Your files will directly upload to the Telegram servers and the speed will follow your internet connection.
  • Fast Download Same like Ultra Upload, your files will be downloaded directly from the Telegram servers. But, it will have some limitations:
    • Only works with chrome-based browsers
    • The max download size is 2GB for free users or follows your device memory
Note. Those features may have bugs please report them to bug@teledriveapp.com and you can always revoke from experimental features anytime. You need to be logged in again to TeleDrive. Continue?
} export default Settings