All files / app infoDrawer.tsx

54.54% Statements 30/55
55% Branches 11/20
42.85% Functions 6/14
52.83% Lines 28/53

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                                            20x 20x     8x 8x 8x 8x                                                   20x 20x   190x 190x 190x 190x 190x 190x 190x 190x                         190x 190x                 190x 42x 184x                           42x 42x   190x 8x 2x   8x 8x                                                                                                                        
import {
  Box,
  Button,
  Card,
  CardActions,
  CardContent,
  CardHeader,
  Drawer,
  IconButton,
  Stack,
  Typography,
} from '@mui/material'
import {createContext, useCallback, useContext, useEffect, useState} from 'react'
import {BuildContext, BuildEditContext, SettingEditor, SettingsContext} from './building'
import {TermDisplay} from './term'
import {Close} from '@mui/icons-material'
import type {Synset} from './resources'
import {SynsetDisplay} from './synset'
import {EditorTermSetter} from './termEditor'
 
export type InfoDrawerState = {type: 'term'; value: string} | {type: 'synset'; value: string; info: Synset}
export type InfoDrawerActions = {type: 'add'; state: InfoDrawerState} | {type: 'back' | 'reset'}
export const InfoDrawerContext = createContext<InfoDrawerState[]>([])
export const InfoDrawerSetter = createContext<(action: InfoDrawerActions) => void>(() => {})
 
function TermContent({term}: {term: string}) {
  const Dict = useContext(BuildContext)
  const editDictionary = useContext(BuildEditContext)
  const isInDict = term in Dict
  return (
    <>
      <CardContent sx={{overflowY: 'auto', pb: 0, pt: 0, height: '100%'}}>
        <TermDisplay term={term} />
      </CardContent>
      <CardActions sx={{justifyContent: 'flex-end', p: 0}}>
        <Button
          onClick={() => {
            editDictionary(isInDict ? {type: 'remove', term_id: term} : {type: 'add', term, term_type: 'fixed'})
          }}
        >
          {isInDict ? 'Remove' : 'Add'}
        </Button>
      </CardActions>
    </>
  )
}
 
function SynsetContent({info}: {info: Synset}) {
  return (
    <CardContent sx={{overflowY: 'auto', mb: 'auto', pt: 0}}>
      <SynsetDisplay info={info} />
    </CardContent>
  )
}
 
let resizeAnimationFrame = -1
const heightTracker = {value: 0}
export function InfoDrawer({height, setHeight}: {height: number; setHeight: (height: number) => void}) {
  const dict = useContext(BuildContext)
  const edit = useContext(InfoDrawerSetter)
  const settings = useContext(SettingsContext)
  const updateSettings = useContext(SettingEditor)
  const setEditorTerm = useContext(EditorTermSetter)
  const state = useContext(InfoDrawerContext)
  heightTracker.value = height
  const resize = useCallback(
    (e: MouseEvent) => {
      const value = Math.max(18, Math.min(Math.ceil((1 - e.y / window.innerHeight) * 100), 87))
      if (value !== heightTracker.value) {
        cancelAnimationFrame(resizeAnimationFrame)
        resizeAnimationFrame = requestAnimationFrame(() => {
          setHeight(value)
          window.dispatchEvent(new Event('resize'))
        })
      }
    },
    [setHeight],
  )
  const [resizing, setResizing] = useState(false)
  const startResize = useCallback(
    (e: React.MouseEvent) => {
      setResizing(true)
      e.preventDefault()
      document.body.style.cursor = 'ns-resize'
      window.addEventListener('mousemove', resize)
    },
    [resize],
  )
  useEffect(() => {
    const endResize = (e: MouseEvent) => {
      Iif (resizeAnimationFrame !== -1) {
        cancelAnimationFrame(resizeAnimationFrame)
        setResizing(false)
        resizeAnimationFrame = -1
        document.body.style.cursor = 'default'
        window.removeEventListener('mousemove', resize)
        const value = Math.max(18, Math.min(Math.ceil((1 - e.y / window.innerHeight) * 100), 87))
        setHeight(value)
        settings.info_drawer_height = value
        updateSettings({...settings})
        localStorage.setItem('dictionary_builder_settings', JSON.stringify(settings))
        window.dispatchEvent(new Event('resize'))
      }
    }
    window.addEventListener('mouseup', endResize)
    return () => window.removeEventListener('mouseup', endResize)
  }, [resize, settings, updateSettings, setHeight])
  if (!state.length) return
  const close = () => {
    edit({type: 'reset'})
  }
  const currentState = state[0]
  return (
    currentState.value && (
      <Drawer
        open={true}
        onClose={close}
        variant="permanent"
        hideBackdrop={true}
        anchor="bottom"
        sx={{
          '& .MuiPaper-root': {
            height: height + 'vh',
            display: 'flex',
            flexDirection: 'column',
            justifyContent: 'space-between',
          },
        }}
      >
        <Card>
          <Box
            sx={{
              width: '100%',
              backgroundColor: '#515151',
              height: '1px',
              position: 'absolute',
              cursor: 'ns-resize',
              border: resizing ? 'solid 2px #dab4ff' : '',
              '&:hover': {border: 'solid 2px #dab4ff'},
            }}
            onMouseDownCapture={startResize}
          ></Box>
          <CardHeader
            sx={{p: 1}}
            action={
              <IconButton aria-label="Close info drawer" onClick={close} className="close-button">
                <Close />
              </IconButton>
            }
            title={
              <Stack direction="row">
                {state.length > 1 ?
                  <Button onClick={() => edit({type: 'back'})} sx={{opacity: 0.8}}>
                    {state[1].value}
                  </Button>
                : <></>}
                {currentState.value in dict ?
                  <Button sx={{textTransform: 'none', p: 0}} onClick={() => setEditorTerm(currentState.value)}>
                    <Typography variant="h3">{currentState.value}</Typography>
                  </Button>
                : <Typography variant="h3">{currentState.value}</Typography>}
              </Stack>
            }
          />
          {currentState.type === 'term' ?
            <TermContent term={currentState.value} />
          : <SynsetContent info={currentState.info} />}
        </Card>
      </Drawer>
    )
  )
}