File size: 1,581 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
import { FormControlLabel, Switch, Tooltip } from '@mui/material';
import VisibleIcon from '@mui/icons-material/Visibility';
import NonVisibleIcon from '@mui/icons-material/VisibilityOff';
import React from 'react';
import {
  useCellProps,
  useLang,
  useSetDraft,
  useUiTranslator,
} from '../../core/components/hooks';

const DraftSwitch = ({ nodeId, lang }: { nodeId: string; lang?: string }) => {
  const { t } = useUiTranslator();
  const cell = useCellProps(nodeId, (c) => ({
    isDraft: c?.isDraft,
    isDraftI18n: c?.isDraftI18n,
  }));
  const setDraft = useSetDraft(nodeId);
  const currentLang = useLang();
  if (!cell) {
    return null;
  }
  const theLang = lang ?? currentLang;
  const hasI18n = Boolean(cell.isDraftI18n);
  const isDraft = cell?.isDraftI18n?.[theLang] ?? cell?.isDraft; // fallback to legacy
  const title = t(isDraft ? 'Content is hidden' : 'Content is visible');
  return cell ? (
    <Tooltip title={title + (hasI18n ? ' in ' + theLang : '')}>
      <FormControlLabel
        style={{ marginRight: 5 }}
        labelPlacement="start"
        control={
          <Switch
            color="primary"
            checked={!isDraft}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
              setDraft(!e.target.checked, theLang);
            }}
          />
        }
        label={
          isDraft ? (
            <NonVisibleIcon style={{ marginTop: 5 }} />
          ) : (
            <VisibleIcon style={{ marginTop: 5 }} />
          )
        }
      />
    </Tooltip>
  ) : null;
};

export default DraftSwitch;