All files / app dictionaryMenu.tsx

89.28% Statements 25/28
90% Branches 9/10
84.61% Functions 11/13
88.88% Lines 24/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                                              2626x 2626x 2626x 2626x   2626x   2626x 2626x 2626x 2626x 2626x 9x 9x 9x     2626x   344x 228x                                 4x             2626x                                                                                                       740x                                           84x 9x       70x                                     5x               4x        
import {Close, Menu, RemoveCircleOutline} from '@mui/icons-material'
import {
  Button,
  Card,
  CardActions,
  CardContent,
  CardHeader,
  Drawer,
  FormControl,
  IconButton,
  InputLabel,
  List,
  ListItem,
  ListItemButton,
  ListItemText,
  MenuItem,
  Select,
  SelectChangeEvent,
  Stack,
  TextField,
  Typography,
} from '@mui/material'
import {type ChangeEvent, type KeyboardEvent, useContext, useMemo, useState} from 'react'
import {AllCategories, BuildEditContext, CategoryEditContext, ManageDictionaries, SettingsContext} from './building'
import {ImportMenu} from './import'
import {ExportMenu} from './export'
import {History} from './history'
import {Confirm} from './confirmDialog'
import {CategoryEditor} from './categoryEditor'
 
export function DictionaryMenu() {
  const settings = useContext(SettingsContext)
  const [menuOpen, setMenuOpen] = useState(false)
  const toggleMenu = () => setMenuOpen(!menuOpen)
  const [editingCategory, setEditingCategory] = useState('')
 
  const manageDictionaries = useContext(ManageDictionaries)
 
  const categories = useContext(AllCategories)
  const editCategories = useContext(CategoryEditContext)
  const dictionaryAction = useContext(BuildEditContext)
  const [newCategory, setNewCategory] = useState('')
  const add = () => {
    Eif (newCategory && -1 === categories.indexOf(newCategory)) {
      editCategories({type: 'add', cat: newCategory})
      setNewCategory('')
    }
  }
  const cats = useMemo(
    () =>
      categories.map(cat => (
        <ListItem
          key={cat}
          disablePadding
          disableGutters
          secondaryAction={
            <IconButton
              aria-label="delete category"
              sx={{opacity: 0.5}}
              onClick={() => {
                dictionaryAction({type: 'remove_category', name: cat})
                editCategories({type: 'remove', cat: cat})
              }}
            >
              <RemoveCircleOutline />
            </IconButton>
          }
        >
          <ListItemButton dense onClick={() => setEditingCategory(cat)}>
            <ListItemText primary={cat} />
          </ListItemButton>
        </ListItem>
      )),
    [categories, dictionaryAction, editCategories]
  )
  return (
    <>
      <Stack direction="row">
        <IconButton onClick={toggleMenu} aria-label="open dictionary menu">
          <Menu />
        </IconButton>
        <Typography
          sx={{
            display: {sm: 'block', xs: 'none'},
            fontSize: {md: '1.2em', sm: '.8em'},
            maxWidth: {md: '285px', sm: '150px'},
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            alignSelf: 'center',
          }}
          noWrap
        >
          {settings.selected}
        </Typography>
      </Stack>
      {menuOpen && (
        <Drawer anchor="left" open={menuOpen} onClose={toggleMenu}>
          <Card
            sx={{
              height: '100%',
              width: '20em',
              display: 'flex',
              flexDirection: 'column',
              justifyContent: 'space-between',
            }}
          >
            <CardHeader
              title={<Typography fontWeight="bold">Dictionary Menu</Typography>}
              action={
                <IconButton onClick={toggleMenu} aria-label="close dictionary menu" className="close-button">
                  <Close />
                </IconButton>
              }
            />
            <Stack direction="row" sx={{mb: 2}}>
              <FormControl fullWidth>
                <InputLabel id="dictionary_select">Current</InputLabel>
                <Select
                  labelId="dictionary_select"
                  label="Current"
                  size="small"
                  value={settings.selected}
                  onChange={(e: SelectChangeEvent<string>) => {
                    manageDictionaries({type: 'set', name: e.target.value})
                  }}
                >
                  {settings.dictionary_names.map(name => (
                    <MenuItem key={name} value={name}>
                      {name}
                    </MenuItem>
                  ))}
                </Select>
              </FormControl>
              <ImportMenu>New</ImportMenu>
            </Stack>
            <CardContent sx={{alignContent: 'left', mb: 'auto', pt: 0, pb: 1, height: '100%', overflow: 'hidden'}}>
              <Stack spacing={1} sx={{height: '100%', overflowY: 'auto'}}>
                <Card elevation={5} sx={{height: '40%', minHeight: '300px', display: 'flex', flexDirection: 'column'}}>
                  <CardHeader title={<Typography fontWeight="bold">Categories</Typography>} sx={{pb: 0}} />
                  <CardContent sx={{p: 0, mb: 'auto', overflowY: 'auto'}}>
                    <List dense>{cats}</List>
                  </CardContent>
                  <CardActions>
                    <Stack direction="row">
                      <TextField
                        label="New Category Name"
                        size="small"
                        value={newCategory}
                        onKeyDown={(e: KeyboardEvent) => {
                          if (e.code === 'Enter' || e.code === 'NumpadEnter') {
                            add()
                          }
                        }}
                        onChange={(e: ChangeEvent<HTMLInputElement>) => {
                          setNewCategory(e.target.value)
                        }}
                      ></TextField>
                      <Button variant="contained" onClick={add}>
                        Add
                      </Button>
                    </Stack>
                  </CardActions>
                </Card>
                <History />
              </Stack>
            </CardContent>
            <CardActions>
              <Stack direction="column" sx={{width: '100%'}} spacing={1}>
                <ExportMenu />
                <Confirm
                  label="Delete"
                  message={'Delete the ' + settings.selected + ' dictionary?'}
                  onConfirm={() => {
                    manageDictionaries({type: 'delete', name: settings.selected})
                  }}
                />
              </Stack>
            </CardActions>
          </Card>
        </Drawer>
      )}
      <CategoryEditor category={editingCategory} onClose={() => setEditingCategory('')} />
    </>
  )
}