Files
linux-cloud-router/web/src/DockerPanel.tsx
T

73 lines
2.8 KiB
TypeScript

import Refresh from '@mui/icons-material/Refresh'
import { Alert, Box, Button, Typography } from '@mui/material'
import { Board, BoardButtons, type BoardPanel } from './PanelBoard.tsx'
import { useBoard } from './useBoard.ts'
import { Pairs } from './ServerPanel.tsx'
import { text } from './format.ts'
import Toolbar from './Toolbar.tsx'
import { DETAILS_SHAPE, STATUS_SHAPE, request, type RouterDetails, type RouterStatus } from './api.ts'
import { useRead } from './useRead.ts'
/** The localStorage key of the tab's layout. */
const LAYOUT_KEY = 'linux-manager-ui.docker-layout'
/** Reads the server's /status/full and /config. */
const readDocker = async () => {
const [status, details] = await Promise.all([
request<RouterStatus>('GET', '/status/full', STATUS_SHAPE),
request<RouterDetails>('GET', '/config', DETAILS_SHAPE),
])
return { status, details }
}
/**
* The state of Docker on the server: whether it is installed, the state of docker.service and of the server's Docker
* rules, from /status/full, and the ports exposed on the external interface and the rules of the LINUX-MANAGER-DOCKER
* chain, from /config. The panels are on a PanelBoard whose layout is kept in the browser.
*/
export default function DockerPanel() {
const { data, error, busy, refresh } = useRead(readDocker)
const status = data?.status
const details = data?.details
const docker = status?.docker
const board = useBoard(LAYOUT_KEY)
const panels: BoardPanel[] = status && details ? [
{
id: 'status',
title: 'Status',
fixed: true,
content: (
<Pairs
rows={[
['Docker', docker === undefined ? 'unknown' : docker.installed ? 'installed' : 'not installed'],
['docker.service', text(docker?.service)],
['Docker rules', text(docker?.rules)],
['Exposed ports', text(details.docker.exposed_ports)],
]}
/>
),
},
{
id: 'rules',
title: 'Rules of LINUX-MANAGER-DOCKER',
content: details.docker.rules === null
? <Typography variant="body2" color="text.secondary">Not available.</Typography>
: (
<Box component="pre" sx={{ m: 0, overflow: 'auto', fontFamily: 'monospace', fontSize: 12 }}>
{details.docker.rules.length === 0 ? '(none)' : details.docker.rules.join('\n')}
</Box>
),
},
] : []
return (
<Box component="section">
<Toolbar description="Docker and its firewall rules.">
<BoardButtons board={board} />
<Button variant="outlined" startIcon={<Refresh />} disabled={busy} onClick={() => void refresh()}>Refresh</Button>
</Toolbar>
{error && <Alert severity="error" sx={{ mb: 2, whiteSpace: 'pre-line' }}>{error}</Alert>}
<Board board={board} panels={panels} />
</Box>
)
}