File size: 1,567 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Avatar from '@mui/material/Avatar';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';

import React from 'react';
import type { PluginDrawerLabels } from '..';
import {
  useDisplayModeReferenceNodeId,
  useInsertNew,
  useUiTranslator,
} from '../../../core/components/hooks';
import type { CellPlugin, InsertNewCell } from '../../../core/types';
import Draggable from '../Draggable/index';

type ItemProps = {
  plugin: CellPlugin;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  insert: InsertNewCell;
  translations: PluginDrawerLabels;
};

const Item: React.FC<ItemProps> = ({ plugin, insert }) => {
  const title = plugin.title ?? plugin.text;
  const { t } = useUiTranslator();
  if (!plugin.icon && !title) {
    return null;
  }

  const referenceNodeId = useDisplayModeReferenceNodeId();
  const insertNew = useInsertNew(referenceNodeId);
  const insertIt = React.useCallback(
    () => insertNew(insert),
    [insertNew, referenceNodeId, insert]
  );

  return (
    <Draggable insert={insert}>
      <ListItem
        title={
          t('Click to add or drag and drop it somewhere on your page!') ?? ''
        }
        className="react-page-plugin-drawer-item"
        onClick={insertIt}
      >
        <Avatar
          children={plugin.icon || title?.[0]}
          style={{
            marginRight: 16,
          }}
        />
        <ListItemText primary={t(title)} secondary={t(plugin.description)} />
      </ListItem>
    </Draggable>
  );
};

export default Item;