|
1 | 1 | import React from 'react'; |
2 | | -import TableCell from '@material-ui/core/TableCell'; |
3 | | -import TableHead from '@material-ui/core/TableHead'; |
4 | | -import TableRow from '@material-ui/core/TableRow'; |
| 2 | +import { |
| 3 | + TableCell, |
| 4 | + TableHead, |
| 5 | + TableRow, |
| 6 | + Tooltip, |
| 7 | + IconButton, |
| 8 | + Box, |
| 9 | +} from '@material-ui/core'; |
| 10 | +import { |
| 11 | + CheckBox as CheckBoxIcon, |
| 12 | + IndeterminateCheckBox as CheckBoxIndeterminateIcon, |
| 13 | + CheckBoxOutlineBlank as CheckBoxBlankIcon, |
| 14 | +} from '@material-ui/icons'; |
| 15 | + |
| 16 | +import { isInstalled, setEnabled } from '../../services'; |
5 | 17 |
|
6 | 18 | import { modsText } from '../../helpers/static-text'; |
7 | 19 |
|
8 | 20 | type Props = { |
9 | 21 | title: string; |
| 22 | + mods: Mod[]; |
| 23 | +}; |
| 24 | + |
| 25 | +const getEnableTooltip = (mods: Mod[]) => { |
| 26 | + if (mods.every((mod) => mod.isEnabled)) { |
| 27 | + return modsText.actions.disable; |
| 28 | + } |
| 29 | + if (mods.every((mod) => isInstalled(mod))) { |
| 30 | + return modsText.actions.enable; |
| 31 | + } |
| 32 | + return ''; |
| 33 | +}; |
| 34 | + |
| 35 | +const getCheckboxIcon = (mods: Mod[]) => { |
| 36 | + if (mods.every((mod) => mod.isEnabled)) { |
| 37 | + return <CheckBoxIcon />; |
| 38 | + } |
| 39 | + if (mods.some((mod) => mod.isEnabled)) { |
| 40 | + return <CheckBoxIndeterminateIcon />; |
| 41 | + } |
| 42 | + return <CheckBoxBlankIcon />; |
| 43 | +}; |
| 44 | + |
| 45 | +const toggleAllMods = (mods: Mod[]) => { |
| 46 | + const enabled = !mods.every((mod) => mod.isEnabled); |
| 47 | + mods.forEach((mod) => { |
| 48 | + if (mod.isRequired) return; |
| 49 | + setEnabled(mod, enabled); |
| 50 | + }); |
10 | 51 | }; |
11 | 52 |
|
12 | | -const ModTableHead: React.FunctionComponent<Props> = ({ title }) => ( |
| 53 | +const ModTableHead: React.FunctionComponent<Props> = ({ title, mods }) => ( |
13 | 54 | <TableHead> |
14 | 55 | <TableRow> |
15 | 56 | <TableCell>{title}</TableCell> |
16 | 57 | <TableCell width="100px">{modsText.tableHead.downloadCount}</TableCell> |
17 | 58 | <TableCell width="110px" align="center"> |
18 | 59 | {modsText.tableHead.version} |
19 | 60 | </TableCell> |
20 | | - <TableCell width="180px" /> |
| 61 | + <TableCell width="180px"> |
| 62 | + <Box display="flex" justifyContent="space-between"> |
| 63 | + <Tooltip title={getEnableTooltip(mods)}> |
| 64 | + <span> |
| 65 | + <IconButton |
| 66 | + size="small" |
| 67 | + disabled={ |
| 68 | + mods.every((mod) => !isInstalled(mod)) || |
| 69 | + mods.every((mod) => mod.isRequired) |
| 70 | + } |
| 71 | + onClick={() => toggleAllMods(mods)} |
| 72 | + > |
| 73 | + {getCheckboxIcon(mods)} |
| 74 | + </IconButton> |
| 75 | + </span> |
| 76 | + </Tooltip> |
| 77 | + </Box> |
| 78 | + </TableCell> |
21 | 79 | </TableRow> |
22 | 80 | </TableHead> |
23 | 81 | ); |
|
0 commit comments