Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopiovanello committed Nov 2, 2023
1 parent 00b3fcc commit f49f072
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 39 deletions.
7 changes: 5 additions & 2 deletions frontend/src/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import ListItemText from '@mui/material/ListItemText'
import Toolbar from '@mui/material/Toolbar'
import Typography from '@mui/material/Typography'
import { grey } from '@mui/material/colors'
import { useMemo, useState } from 'react'
import { Suspense, useMemo, useState } from 'react'
import { Helmet } from 'react-helmet'
import { Link, Outlet } from 'react-router-dom'
import { useRecoilValue } from 'recoil'
Expand Down Expand Up @@ -86,11 +86,14 @@ export default function Layout() {
>
{settings.appTitle}
</Typography>
<FreeSpaceIndicator />
<Suspense fallback={i18n.t('loadingLabel')}>
<FreeSpaceIndicator />
</Suspense>
<div style={{
display: 'flex',
alignItems: 'center',
flexWrap: 'wrap',
marginLeft: '4px',
gap: 3,
}}>
<SettingsEthernet />
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/assets/i18n.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App title
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -89,6 +90,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: Nom de l'application
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -134,6 +136,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: Titolo applicazione
savedTemplates: Template salvati
templatesEditor: Editor template
Expand Down Expand Up @@ -180,6 +183,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App 标题
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -224,6 +228,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App title
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -268,6 +273,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App title
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -312,6 +318,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App title
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -357,6 +364,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App title
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -401,6 +409,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App title
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -445,6 +454,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App title
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down Expand Up @@ -489,6 +499,7 @@ languages:
settingsButtonLabel: Settings
rpcAuthenticationLabel: RPC authentication
themeTogglerLabel: Theme toggler
loadingLabel: Loading...
appTitle: App title
savedTemplates: Saved templates
templatesEditor: Templates editor
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/atoms/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export const isDownloadingState = atom({
default: false
})

// export const freeSpaceBytesState = selector({
// key: 'freeSpaceBytesState',
// get: async ({ get }) => {
// const res = await get(rpcClientState).freeSpace()
// return res.result
// }
// })
export const freeSpaceBytesState = selector({
key: 'freeSpaceBytesState',
get: async ({ get }) => {
const res = await get(rpcClientState).freeSpace()
return res.result
}
})

export const availableDownloadPathsState = selector({
key: 'availableDownloadPathsState',
Expand Down
11 changes: 5 additions & 6 deletions frontend/src/components/DownloadDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Toolbar from '@mui/material/Toolbar'
import Typography from '@mui/material/Typography'
import { TransitionProps } from '@mui/material/transitions'
import {
FC,
forwardRef,
useMemo,
useRef,
Expand Down Expand Up @@ -55,11 +56,7 @@ type Props = {
onDownloadStart: (url: string) => void
}

export default function DownloadDialog({
open,
onClose,
onDownloadStart
}: Props) {
const DownloadDialog: FC<Props> = ({ open, onClose, onDownloadStart }) => {
const settings = useRecoilValue(settingsState)
const isConnected = useRecoilValue(connectedState)
const availableDownloadPaths = useRecoilValue(availableDownloadPathsState)
Expand Down Expand Up @@ -368,4 +365,6 @@ export default function DownloadDialog({
</Box>
</Dialog>
)
}
}

export default DownloadDialog
15 changes: 5 additions & 10 deletions frontend/src/components/FreeSpaceIndicator.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import StorageIcon from '@mui/icons-material/Storage'
import { useEffect, useState } from 'react'
import { useRecoilValue } from 'recoil'
import { freeSpaceBytesState } from '../atoms/status'
import { formatGiB } from '../utils'
import { useRPC } from '../hooks/useRPC'

const FreeSpaceIndicator = () => {
const [freeSpace, setFreeSpace] = useState(0)

const { client } = useRPC()

useEffect(() => {
client.freeSpace().then(r => setFreeSpace(r.result))
}, [client])
const freeSpace = useRecoilValue(freeSpaceBytesState)

return (
<div style={{
display: 'flex',
alignItems: 'center',
flexWrap: 'wrap',
gap: 3
}}>
<StorageIcon />
<span>
&nbsp;{formatGiB(freeSpace)}&nbsp;
{formatGiB(freeSpace)}
</span>
</div>
)
Expand Down
30 changes: 16 additions & 14 deletions frontend/src/components/HomeActions.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useState } from 'react'
import { Suspense, useState } from 'react'
import { useRecoilState } from 'recoil'
import { loadingAtom } from '../atoms/ui'
import { useToast } from '../hooks/toast'
import DownloadDialog from './DownloadDialog'
import HomeSpeedDial from './HomeSpeedDial'
import { useToast } from '../hooks/toast'
import TemplatesEditor from './TemplatesEditor'

const HomeActions: React.FC = () => {
Expand All @@ -20,18 +20,20 @@ const HomeActions: React.FC = () => {
onDownloadOpen={() => setOpenDownload(true)}
onEditorOpen={() => setOpenEditor(true)}
/>
<DownloadDialog
open={openDownload}
onClose={() => {
setOpenDownload(false)
setIsLoading(true)
}}
onDownloadStart={(url) => {
pushMessage(`Requested ${url}`, 'info')
setOpenDownload(false)
setIsLoading(true)
}}
/>
<Suspense>
<DownloadDialog
open={openDownload}
onClose={() => {
setOpenDownload(false)
setIsLoading(true)
}}
onDownloadStart={(url) => {
pushMessage(`Requested ${url}`, 'info')
setOpenDownload(false)
setIsLoading(true)
}}
/>
</Suspense>
<TemplatesEditor
open={openEditor}
onClose={() => setOpenEditor(false)}
Expand Down

0 comments on commit f49f072

Please sign in to comment.