File size: 2,478 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
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
import { Button, DialogContent, Table } from '@mui/material';
import Translate from '@mui/icons-material/Translate';

import React, { useCallback } from 'react';
import {
  useCellDataI18nRaw,
  useLang,
  useOption,
  useSetLang,
  useUiTranslator,
  useUpdateCellData,
} from '../../core/components/hooks';
import DraftSwitch from '../DraftSwitch';
import SelectLang from './SelectLang';

const I18nDialog = ({
  nodeId,
  onClose,
}: {
  nodeId: string;
  onClose: () => void;
}) => {
  const currentLang = useLang();
  const languages = useOption('languages');
  const { t } = useUiTranslator();
  const setLang = useSetLang();
  const dataI18n = useCellDataI18nRaw(nodeId);

  const updateCellData = useUpdateCellData(nodeId);
  const reset = useCallback(
    (lang: string) => {
      updateCellData(null, {
        lang,
      });
    },
    [updateCellData]
  );
  const defaultLangLabel = languages?.[0]?.label;
  return (
    <DialogContent>
      <div style={{ display: 'flex', alignItems: 'center' }}>
        <Translate style={{ marginRight: 'auto' }} /> <SelectLang />
      </div>
      <hr />
      <Table>
        <tbody>
          {languages?.map((l, index) => {
            const data = dataI18n?.[l.lang];
            const isCurrent = currentLang === l.lang;
            const hasData = Boolean(data);

            return (
              <tr key={l.lang}>
                <th
                  style={{
                    textAlign: 'left',
                    textDecoration: isCurrent ? 'underline' : undefined,
                  }}
                >
                  <Button onClick={() => setLang(l.lang)}>
                    {l.label} {index === 0 ? t('(default)') : null}
                  </Button>
                </th>

                <td>
                  <DraftSwitch nodeId={nodeId} lang={l.lang} />
                </td>

                <td>{hasData ? '✔️' : ' '}</td>
                <td>
                  {hasData && index !== 0 ? (
                    <Button
                      onClick={() => {
                        reset(l.lang);
                      }}
                    >
                      {t(`Reset to ${defaultLangLabel} ⚠️`)}
                    </Button>
                  ) : null}
                </td>
              </tr>
            );
          })}
        </tbody>
      </Table>
      <Button onClick={() => onClose()}>{t('Close')}</Button>
    </DialogContent>
  );
};

export default I18nDialog;