-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProcessList.tsx
164 lines (156 loc) · 5.1 KB
/
ProcessList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import Drawer from '@material-ui/core/Drawer'
import IconButton from '@material-ui/core/IconButton'
import List from '@material-ui/core/List'
import ListItem from '@material-ui/core/ListItem'
import ListItemIcon from '@material-ui/core/ListItemIcon'
import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction'
import ListItemText from '@material-ui/core/ListItemText'
import Menu from '@material-ui/core/Menu'
import MenuItem from '@material-ui/core/MenuItem'
import { makeStyles, Theme, useTheme } from '@material-ui/core/styles'
import Toolbar from '@material-ui/core/Toolbar'
import MoreVertIcon from '@material-ui/icons/MoreVert'
import OfflineBoltIcon from '@material-ui/icons/OfflineBolt'
import RemoveCircleOutlineIcon from '@material-ui/icons/RemoveCircleOutline'
import clsx from 'clsx'
import PopupState, { bindMenu, bindTrigger } from 'material-ui-popup-state'
import { useRouter } from 'next/router'
import { Fragment, useMemo } from 'react'
import useSWR from 'swr'
import Api from '../util/Api'
export const drawerWidth = 300
const useStyles = makeStyles((theme: Theme) => {
return {
drawer: {
width: drawerWidth,
flexShrink: 0,
},
itemActive: {
background: theme.palette.grey[300],
},
drawerPaper: {
width: drawerWidth,
},
drawerContainer: {
overflow: 'auto',
},
}
})
interface Props {
active?: string
}
const ProcessList = ({ active }: Props) => {
const router = useRouter()
const { data, error, isValidating, revalidate } = useSWR('/all', {
revalidateOnMount: true,
initialData: { data: [] },
})
const classes = useStyles()
const theme = useTheme()
const lists = useMemo(() => {
if (data.data && data.data.length > 0) {
return data.data
}
return []
}, [data])
const callAction = async ({ id, action }, onClose) => {
onClose()
try {
const { data } = await Api().post('/action', { id, action })
revalidate()
} catch (e) {
console.error(e)
}
}
return (
<Drawer
className={classes['drawer']}
variant='permanent'
classes={{
paper: classes.drawerPaper,
}}
>
<Toolbar />
<div className={classes.drawerContainer}>
<List>
{lists.map((item, index) => (
<ListItem
key={index}
button
onClick={() => {
router.push('/process/[id]', `/process/${item.pm_id}`)
}}
className={clsx({
[classes.itemActive]: item.pm_id.toString() === active,
})}
>
<ListItemIcon>
{item.pm2_env.status === 'online' ? (
<OfflineBoltIcon
style={{ color: theme.palette.success.main }}
/>
) : (
<RemoveCircleOutlineIcon />
)}
</ListItemIcon>
<ListItemText
primary={item.name}
primaryTypographyProps={{ noWrap: true }}
secondary={item.pm2_env.status}
/>
<ListItemSecondaryAction>
<PopupState variant='popover' popupId={`process-${index}`}>
{(popupState) => (
<Fragment>
<IconButton {...bindTrigger(popupState)}>
<MoreVertIcon />
</IconButton>
<Menu {...bindMenu(popupState)}>
{item.pm2_env.status === 'online' ? (
<Fragment>
<MenuItem
onClick={() =>
callAction(
{ id: item.name, action: 'stop' },
popupState.close
)
}
>
Stop
</MenuItem>
<MenuItem
onClick={() =>
callAction(
{ id: item.name, action: 'restart' },
popupState.close
)
}
>
Restart
</MenuItem>
</Fragment>
) : (
<MenuItem
onClick={() =>
callAction(
{ id: item.name, action: 'start' },
popupState.close
)
}
>
Start
</MenuItem>
)}
</Menu>
</Fragment>
)}
</PopupState>
</ListItemSecondaryAction>
</ListItem>
))}
</List>
</div>
</Drawer>
)
}
export default ProcessList