💄 Optimize UX

This commit is contained in:
LittleSheep 2025-01-11 21:29:42 +08:00
parent 65c3249e23
commit 1ed39cbaec
3 changed files with 36 additions and 4 deletions

View File

@ -6,6 +6,17 @@ import unzipper from 'unzipper'
import { InstallProgressPeriod, InstallTask, updateInstallTask } from './tasks' import { InstallProgressPeriod, InstallTask, updateInstallTask } from './tasks'
import { randomUUID } from 'crypto' import { randomUUID } from 'crypto'
const byteSizeUnits = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
function formatBytesWithUnit(bytes: number, unitIndex: number, decimals = 2): string {
if (!+bytes) return '0'
const k = 1024
const dm = decimals < 0 ? 0 : decimals
return `${parseFloat((bytes / Math.pow(k, unitIndex)).toFixed(dm))}`
}
function downloadFile(task: InstallTask, url: string, output: string): Promise<void> { function downloadFile(task: InstallTask, url: string, output: string): Promise<void> {
// eslint-disable-next-line no-async-promise-executor // eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
@ -28,9 +39,15 @@ function downloadFile(task: InstallTask, url: string, output: string): Promise<v
if (Date.now() - lastUpdate >= 100) { if (Date.now() - lastUpdate >= 100) {
lastUpdate = Date.now() lastUpdate = Date.now()
if (task.progress) { if (task.progress) {
const k = 1024
const lenUnitIndex = Math.floor(Math.log(len) / Math.log(k))
const downloadedFormatted = formatBytesWithUnit(downloaded, lenUnitIndex)
const lenFormatted = formatBytesWithUnit(len, lenUnitIndex)
task.progress.value = percent task.progress.value = percent
task.progress.period = InstallProgressPeriod.Downloading task.progress.period = InstallProgressPeriod.Downloading
task.progress.details = `${downloaded}/${len}` task.progress.details = `${downloadedFormatted}/${lenFormatted} ${byteSizeUnits[lenUnitIndex]}`
updateInstallTask(task) updateInstallTask(task)
} }
} }

View File

@ -53,7 +53,13 @@ export function MaAppBar(): JSX.Element {
<List sx={{ width: '320px' }}> <List sx={{ width: '320px' }}>
{functionLinks.map((l) => ( {functionLinks.map((l) => (
<ListItem disablePadding> <ListItem disablePadding>
<ListItemButton selected={window.location.pathname == l.href} onClick={() => navigate(l.href)}> <ListItemButton
selected={window.location.pathname == l.href}
onClick={() => {
setOpen(false)
navigate(l.href)
}}
>
<ListItemIcon>{l.icon}</ListItemIcon> <ListItemIcon>{l.icon}</ListItemIcon>
<ListItemText primary={l.title} /> <ListItemText primary={l.title} />
</ListItemButton> </ListItemButton>

View File

@ -48,17 +48,26 @@ export default function Tasks(): JSX.Element {
{t.progress.details} {t.progress.details}
</Typography> </Typography>
)} )}
{t.progress?.error && (
<Typography variant="body2" component="p" fontFamily="monospace" fontSize={13}>
{t.progress.error}
</Typography>
)}
<Box sx={{ mt: 2 }}> <Box sx={{ mt: 2 }}>
{t.progress?.value ? ( {t.progress?.value ? (
<LinearProgress <LinearProgress
variant="determinate" variant="determinate"
color={t.progress?.done ? 'success' : 'primary'} color={t.progress?.done ? 'success' : t.progress?.error ? 'error' : 'primary'}
value={t.progress.value * 100} value={t.progress.value * 100}
sx={{ borderRadius: 4 }} sx={{ borderRadius: 4 }}
/> />
) : ( ) : (
<LinearProgress variant="indeterminate" sx={{ borderRadius: 4 }} /> <LinearProgress
variant="indeterminate"
color={t.progress?.done ? 'success' : t.progress?.error ? 'error' : 'primary'}
sx={{ borderRadius: 4 }}
/>
)} )}
</Box> </Box>
</CardContent> </CardContent>