aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/components/CurrentAlerts.tsx
blob: 38d1b6b6b0a8dad30e41af6b2be46b24f7bbb57e (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
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
/*
 This file is part of GNU Taler
 (C) 2022 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

import { ComponentChildren, Fragment, h, VNode } from "preact";
import { useState } from "preact/hooks";
import {
  Alert as AlertNotification,
  useAlertContext,
} from "../context/alert.js";
import { Alert } from "../mui/Alert.js";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { ButtonHandler } from "../mui/handlers.js";
import { Button } from "../mui/Button.js";

/**
 *
 * @author sebasjm
 */

function AlertContext({
  context,
  cause,
}: {
  cause: unknown;
  context: undefined | object;
}): VNode {
  const [more, setMore] = useState(false);
  const [wrap, setWrap] = useState(false);
  const { i18n } = useTranslationContext();
  if (!more) {
    return (
      <div style={{ display: "flex", justifyContent: "right" }}>
        <a
          onClick={() => setMore(true)}
          style={{ cursor: "pointer", textDecoration: "underline" }}
        >
          <i18n.Translate>more info</i18n.Translate>
        </a>
      </div>
    );
  }
  const errorInfo = JSON.stringify(
    context === undefined ? { cause } : { context, cause },
    undefined,
    2,
  );
  return (
    <Fragment>
      <div style={{ display: "flex", justifyContent: "right" }}>
        <a
          onClick={() => setWrap(!wrap)}
          style={{ cursor: "pointer", textDecoration: "underline" }}
        >
          <i18n.Translate>wrap text</i18n.Translate>
        </a>
        &nbsp;&nbsp;
        <a
          onClick={() => navigator.clipboard.writeText(errorInfo)}
          style={{ cursor: "pointer", textDecoration: "underline" }}
        >
          <i18n.Translate>copy content</i18n.Translate>
        </a>
        &nbsp;&nbsp;
        <a
          onClick={() => setMore(false)}
          style={{ cursor: "pointer", textDecoration: "underline" }}
        >
          <i18n.Translate>less info</i18n.Translate>
        </a>
      </div>
      <pre
        style={
          wrap
            ? {
                whiteSpace: "pre-wrap",
                overflowWrap: "anywhere",
              }
            : {
                overflow: "overlay",
              }
        }
      >
        {errorInfo}
      </pre>
    </Fragment>
  );
}

export function ErrorAlertView({
  error,
  retry,
  onClose,
}: {
  error: AlertNotification;
  retry?: ButtonHandler;
  onClose?: () => Promise<void>;
}): VNode {
  const { i18n } = useTranslationContext();
  return (
    <Wrapper>
      {!retry ? undefined : (
        <section>
          <Button variant="contained" color="success" onClick={retry.onClick}>
            <i18n.Translate>Retry operation</i18n.Translate>
          </Button>
        </section>
      )}
      <AlertView alert={error} onClose={onClose} />
    </Wrapper>
  );
}

export function AlertView({
  alert,
  onClose,
}: {
  alert: AlertNotification;
  onClose?: () => Promise<void>;
}): VNode {
  return (
    <Alert title={alert.message} severity={alert.type} onClose={onClose}>
      <div style={{ display: "flex", flexDirection: "column" }}>
        <div>{alert.description}</div>
        {alert.type === "error" && alert.cause !== undefined ? (
          <AlertContext context={alert.context} cause={alert.cause} />
        ) : undefined}
      </div>
    </Alert>
  );
}

export function CurrentAlerts(): VNode {
  const { alerts, removeAlert } = useAlertContext();
  if (alerts.length === 0) return <Fragment />;
  return (
    <Wrapper>
      {alerts.map((n, i) => (
        <AlertView key={i} alert={n} onClose={async () => removeAlert(n)} />
      ))}
    </Wrapper>
  );
}

function Wrapper({ children }: { children: ComponentChildren }): VNode {
  return <div style={{ margin: "1em" }}>{children}</div>;
}