-
Notifications
You must be signed in to change notification settings - Fork 36
/
CustomSubmitApiDrawer.tsx
122 lines (113 loc) · 4.52 KB
/
CustomSubmitApiDrawer.tsx
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
import React, { ReactElement, useEffect, useState } from 'react';
import { Drawer, DrawerHeader, DrawerNavigation, PostHogAction, toast } from '@lace/common';
import { Typography } from 'antd';
import styles from './SettingsLayout.module.scss';
import { useTranslation } from 'react-i18next';
import { Button, TextBox } from '@lace/ui';
import { getBackgroundStorage } from '@lib/scripts/background/storage';
import { useWalletManager } from '@hooks';
import { useWalletStore } from '@stores';
import { isValidURL } from '@utils/is-valid-url';
import { useAnalyticsContext } from '@providers';
import SwitchIcon from '@assets/icons/switch.component.svg';
import ErrorIcon from '@assets/icons/address-error-icon.component.svg';
import PlayIcon from '@assets/icons/play-icon.component.svg';
import PauseIcon from '@assets/icons/pause-icon.component.svg';
const { Text } = Typography;
interface CustomSubmitApiDrawerProps {
visible: boolean;
onClose: () => void;
popupView?: boolean;
}
const LEARN_SUBMIT_API_URL = 'https://github.com/IntersectMBO/cardano-node/tree/master/cardano-submit-api';
const DEFAULT_SUBMIT_API = 'http://localhost:8090/api/submit/tx';
export const CustomSubmitApiDrawer = ({
visible,
onClose,
popupView = false
}: CustomSubmitApiDrawerProps): ReactElement => {
const { t } = useTranslation();
const { enableCustomNode } = useWalletManager();
const { isCustomSubmitApiEnabled } = useWalletStore();
const analytics = useAnalyticsContext();
const [customSubmitTxUrl, setCustomSubmitTxUrl] = useState<string>(DEFAULT_SUBMIT_API);
const [isValidationError, setIsValidationError] = useState<boolean>(false);
useEffect(() => {
getBackgroundStorage()
.then((storage) => {
setCustomSubmitTxUrl(storage.customSubmitTxUrl || DEFAULT_SUBMIT_API);
})
.catch(console.error);
}, []);
const handleCustomTxSubmitEndpoint = async (enable: boolean) => {
if (enable && !isValidURL(customSubmitTxUrl)) {
setIsValidationError(true);
return;
}
setIsValidationError(false);
const value = enable ? customSubmitTxUrl : undefined;
try {
await enableCustomNode(value);
toast.notify({
text: value
? t('browserView.settings.wallet.customSubmitApi.usingCustomTxSubmitEndpoint')
: t('browserView.settings.wallet.customSubmitApi.usingStandardTxSubmitEndpoint'),
withProgressBar: true,
icon: SwitchIcon
});
if (enable) {
void analytics.sendEventToPostHog(PostHogAction.SettingsCustomSubmitApiEnableClick);
}
} catch (error) {
console.error('Error switching TxSubmit endpoint', error);
toast.notify({ text: t('general.errors.somethingWentWrong'), icon: ErrorIcon });
}
};
return (
<Drawer
open={visible}
onClose={onClose}
title={<DrawerHeader popupView={popupView} title={t('browserView.settings.wallet.customSubmitApi.title')} />}
navigation={
<DrawerNavigation
title={t('browserView.settings.heading')}
onCloseIconClick={!popupView ? onClose : undefined}
onArrowIconClick={popupView ? onClose : undefined}
/>
}
popupView={popupView}
>
<div className={popupView ? styles.popupContainer : undefined}>
<Text className={styles.drawerDescription}>
{t('browserView.settings.wallet.customSubmitApi.description')}{' '}
<a href={LEARN_SUBMIT_API_URL} target="_blank" rel="noopener noreferrer">
{t('browserView.settings.wallet.customSubmitApi.descriptionLink')}
</a>
</Text>
<div className={styles.customApiContainer}>
<TextBox
label={t('browserView.settings.wallet.customSubmitApi.inputLabel')}
w="$fill"
value={customSubmitTxUrl}
onChange={(event) => setCustomSubmitTxUrl(event.target.value)}
disabled={isCustomSubmitApiEnabled}
/>
<Button.Primary
label={
isCustomSubmitApiEnabled
? t('browserView.settings.wallet.customSubmitApi.disable')
: t('browserView.settings.wallet.customSubmitApi.enable')
}
icon={isCustomSubmitApiEnabled ? <PauseIcon /> : <PlayIcon />}
onClick={() => handleCustomTxSubmitEndpoint(!isCustomSubmitApiEnabled)}
/>
</div>
{isValidationError && (
<Text className={styles.validationError}>
{t('browserView.settings.wallet.customSubmitApi.validationError')}
</Text>
)}
</div>
</Drawer>
);
};