summaryrefslogtreecommitdiff
path: root/frontend/src/components/modals/WarnThirdParty.tsx
blob: e7ff456170a8cb89fc248788bc739109812e6336 (plain)
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
import { ConfirmModal } from '@decky/ui';
import { FC, useEffect, useState } from 'react';
import { FaExclamationTriangle } from 'react-icons/fa';

import { WarnThirdPartyType } from '../../utils/globalTypes';
import TranslationHelper, { TranslationClass } from '../../utils/TranslationHelper';

interface WarnThirdPartyProps {
  seconds?: number;
  type: WarnThirdPartyType;
  onOK(): void;
  onCancel(): void;
  closeModal?(): void;
}

const WarnThirdParty: FC<WarnThirdPartyProps> = ({ seconds = 5, type, onOK, onCancel, closeModal }) => {
  const [waitTimer, setWaitTimer] = useState(seconds);

  useEffect(() => {
    // exit early when we reach 0
    if (waitTimer <= 0) return;

    // save intervalId to clear the interval when the
    // component re-renders
    const intervalId = setInterval(() => {
      setWaitTimer(waitTimer - 1);
    }, 1000);

    // clear interval on re-render to avoid memory leaks
    return () => clearInterval(intervalId);
    // add waitTimer as a dependency to re-rerun the effect
    // when we update it
  }, [waitTimer]);

  return (
    <ConfirmModal
      bOKDisabled={waitTimer > 0}
      closeModal={closeModal}
      onOK={async () => {
        await onOK();
      }}
      onCancel={async () => {
        await onCancel();
      }}
      strTitle={
        <div>
          <FaExclamationTriangle />
          <TranslationHelper transClass={TranslationClass.WARN_THIRD_PARTY} transText="title" warnType={type} />
        </div>
      }
      strOKButtonText={
        waitTimer > 0 ? (
          <div>
            <TranslationHelper
              transClass={TranslationClass.WARN_THIRD_PARTY}
              transText="button_processing"
              i18nArgs={{
                timer: waitTimer,
              }}
            />
          </div>
        ) : (
          <div>
            <TranslationHelper transClass={TranslationClass.WARN_THIRD_PARTY} transText="button_idle" />
          </div>
        )
      }
    >
      <span style={{ color: 'red' }}>
        <div>
          <TranslationHelper transClass={TranslationClass.WARN_THIRD_PARTY} transText="desc" warnType={type} />
        </div>
      </span>
    </ConfirmModal>
  );
};

export default WarnThirdParty;