Skip to content

Commit

Permalink
Done environment page
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Jan 12, 2024
1 parent 3671907 commit 73ff5c7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 45 deletions.
15 changes: 6 additions & 9 deletions frontend/src/environments/EnvironmentList.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// import { IconButton } from '@mui/material';
import { IconButton } from '@mui/material';
import { DataGrid, GridColDef } from '@mui/x-data-grid';
import { IEnvironmentData } from './types';
import { memo, useMemo } from 'react';
// import CheckIcon from '@mui/icons-material/Check';
import CheckIcon from '@mui/icons-material/Check';

import { Box } from '@mui/system';
import { RemoveEnvironmentButton } from './RemoveEnvironmentButton';
import { EnvironmentLogButton } from './LogDialog';

const columns: GridColDef[] = [
{
field: 'display_name',
Expand Down Expand Up @@ -52,13 +53,9 @@ const columns: GridColDef[] = [
hideSortIcons: true,
renderCell: params => {
return params.value === 'built' ? (
// <IconButton>
// <CheckIcon color="success" />
// </IconButton>
<EnvironmentLogButton
name={params.row.display_name}
image={params.row.image_name}
/>
<IconButton>
<CheckIcon color="success" />
</IconButton>
) : params.value === 'building' ? (
<EnvironmentLogButton
name={params.row.display_name}
Expand Down
61 changes: 35 additions & 26 deletions frontend/src/environments/LogDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import 'xterm/css/xterm.css';

import CheckIcon from '@mui/icons-material/Check';
import SyncIcon from '@mui/icons-material/Sync';
import { Button, IconButton } from '@mui/material';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import { Fragment, memo, useRef, useState } from 'react';
import SyncIcon from '@mui/icons-material/Sync';
import 'xterm/css/xterm.css';
import urlJoin from 'url-join';
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
import urlJoin from 'url-join';

interface IRemoveEnvironmentButton {
interface IEnvironmentLogButton {
name: string;
image: string;
}

const terminalFactory = () => {
const terminal = new Terminal();
const terminal = new Terminal({ convertEol: true, disableStdin: true });
const fitAddon = new FitAddon();
terminal.loadAddon(fitAddon);
return { terminal, fitAddon };
};

function _EnvironmentLogButton(props: IRemoveEnvironmentButton) {
function _EnvironmentLogButton(props: IEnvironmentLogButton) {
const [open, setOpen] = useState(false);
const [built, setBuilt] = useState(false);
const divRef = useRef<HTMLDivElement>(null);
const terminalRef = useRef<{ terminal: Terminal; fitAddon: FitAddon }>(
terminalFactory()
Expand Down Expand Up @@ -55,11 +58,11 @@ function _EnvironmentLogButton(props: IRemoveEnvironmentButton) {
};

eventSource.onmessage = event => {
var data = JSON.parse(event.data);
console.log('data', data);

const data = JSON.parse(event.data);

if (data.phase === 'built') {
eventSource.close();
setBuilt(true);
return;
}
terminalRef.current.terminal.write(data.message);
Expand All @@ -84,28 +87,34 @@ function _EnvironmentLogButton(props: IRemoveEnvironmentButton) {

return (
<Fragment>
<IconButton onClick={handleOpen}>
<SyncIcon
sx={{
animation: 'spin 2s linear infinite',
'@keyframes spin': {
'0%': {
transform: 'rotate(360deg)'
},
'100%': {
transform: 'rotate(0deg)'
{!built && (
<IconButton onClick={handleOpen}>
<SyncIcon
sx={{
animation: 'spin 2s linear infinite',
'@keyframes spin': {
'0%': {
transform: 'rotate(360deg)'
},
'100%': {
transform: 'rotate(0deg)'
}
}
}
}}
htmlColor="orange"
/>
</IconButton>

}}
htmlColor="orange"
/>
</IconButton>
)}
{built && (
<IconButton>
<CheckIcon color="success" />
</IconButton>
)}
<Dialog
open={open}
onClose={handleClose}
fullWidth
maxWidth={'sm'}
maxWidth={'lg'}
keepMounted={true}
>
<DialogTitle>Creating environment {props.name}</DialogTitle>
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/environments/NewEnvironmentDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ function _NewEnvironmentDialog(props: INewEnvironmentDialogProps) {
onSubmit: async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const data: any = { ...formValues };
data.name = data.name ?? data.repo;
data.repo = data.repo.trim();
data.name =
data.name ??
(data.repo as string)
.replace('http://', '')
.replace('https://', '')
.replace(/\//g, '-')
.replace(/\./g, '-');
data.cpu = data.cpu ?? '2';
data.memory = data.memory ?? '2';
data.username = data.username ?? '';
Expand Down
22 changes: 14 additions & 8 deletions frontend/src/environments/RemoveEnvironmentButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button } from '@mui/material';
import { Button, Typography } from '@mui/material';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
Expand All @@ -15,13 +15,14 @@ interface IRemoveEnvironmentButton {
image: string;
}
const Loading = () => (
<Box sx={{ display: 'flex' }}>
<Box sx={{ display: 'flex', justifyContent: 'center' }}>
<CircularProgress />
</Box>
);
function _RemoveEnvironmentButton(props: IRemoveEnvironmentButton) {
const axios = useAxios();
const [open, setOpen] = useState(false);
const [removing, setRemoving] = useState(false);
const handleOpen = () => {
setOpen(true);
};
Expand All @@ -36,6 +37,7 @@ function _RemoveEnvironmentButton(props: IRemoveEnvironmentButton) {
};

const removeEnv = useCallback(async () => {
setRemoving(true);
const response = await axios.request({
method: 'delete',
path: API_PREFIX,
Expand All @@ -46,7 +48,7 @@ function _RemoveEnvironmentButton(props: IRemoveEnvironmentButton) {
} else {
handleClose();
}
}, [props.image, axios]);
}, [props.image, axios, setRemoving]);

return (
<Fragment>
Expand All @@ -57,11 +59,15 @@ function _RemoveEnvironmentButton(props: IRemoveEnvironmentButton) {
<Dialog open={open} onClose={handleClose} fullWidth maxWidth={'sm'}>
<DialogTitle>Remove environment</DialogTitle>
<DialogContent>
{/* <Typography>
Are you sure you want to remove the following environment?
</Typography>
<pre>{props.name}</pre> */}
<Loading />
{!removing && (
<Box>
<Typography>
Are you sure you want to remove the following environment?
</Typography>
<pre>{props.name}</pre>
</Box>
)}
{removing && <Loading />}
</DialogContent>
<DialogActions>
<Button variant="contained" onClick={handleClose}>
Expand Down
2 changes: 1 addition & 1 deletion tljh_repo2docker/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def post(self):
"Invalid build argument format"
)
extra_buildargs.append(barg)

print('@@@@@@@', repo, ref, name, memory, cpu, username, password, extra_buildargs)
await build_image(repo, ref, name, memory, cpu, username, password, extra_buildargs)

self.set_status(200)
Expand Down

0 comments on commit 73ff5c7

Please sign in to comment.