💄 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 { 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> {
// eslint-disable-next-line no-async-promise-executor
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) {
lastUpdate = Date.now()
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.period = InstallProgressPeriod.Downloading
task.progress.details = `${downloaded}/${len}`
task.progress.details = `${downloadedFormatted}/${lenFormatted} ${byteSizeUnits[lenUnitIndex]}`
updateInstallTask(task)
}
}

View File

@ -53,7 +53,13 @@ export function MaAppBar(): JSX.Element {
<List sx={{ width: '320px' }}>
{functionLinks.map((l) => (
<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>
<ListItemText primary={l.title} />
</ListItemButton>

View File

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