-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathConnectToDBDialog.tsx
99 lines (83 loc) · 3.11 KB
/
ConnectToDBDialog.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
import React from 'react';
import NiceModal from '@ebay/nice-modal-react';
import {Dialog, Tabs} from '@gravity-ui/uikit';
import {cn} from '../../utils/cn';
import {LinkWithIcon} from '../LinkWithIcon/LinkWithIcon';
import {ConnectToDBSyntaxHighlighterLazy} from './ConnectToDBSyntaxHighlighter/lazy';
import {getDocsLink} from './getDocsLink';
import i18n from './i18n';
import {getSnippetCode} from './snippets';
import type {SnippetLanguage, SnippetParams} from './types';
import './ConnectToDB.scss';
const b = cn('ydb-connect-to-db');
const connectionTabs: {id: SnippetLanguage; title: string}[] = [
{id: 'bash', title: 'Bash'},
{id: 'cpp', title: 'C++'},
{id: 'csharp', title: 'C# (.NET)'},
{id: 'go', title: 'Go'},
{id: 'java', title: 'Java'},
{id: 'javascript', title: 'Node JS'},
{id: 'php', title: 'PHP'},
{id: 'python', title: 'Python'},
];
interface ConnectToDBDialogProps extends SnippetParams {
open: boolean;
onClose: VoidFunction;
}
function ConnectToDBDialog({open, onClose, database, endpoint}: ConnectToDBDialogProps) {
const [activeTab, setActiveTab] = React.useState<SnippetLanguage>('bash');
const snippet = getSnippetCode(activeTab, {database, endpoint});
const docsLink = getDocsLink(activeTab);
return (
<Dialog open={open} hasCloseButton={true} onClose={onClose} size="l">
<Dialog.Header caption={i18n('header')} />
<Dialog.Body>
<div>{i18n('connection-info-message')}</div>
<Tabs
size="m"
allowNotSelected={false}
activeTab={activeTab}
items={connectionTabs}
onSelectTab={(tab) => setActiveTab(tab as SnippetLanguage)}
className={b('dialog-tabs')}
/>
<div className={b('snippet-container')}>
<ConnectToDBSyntaxHighlighterLazy language={activeTab} text={snippet} />
</div>
{docsLink ? (
<LinkWithIcon
className={b('docs')}
title={i18n('documentation')}
url={docsLink}
/>
) : null}
</Dialog.Body>
<Dialog.Footer onClickButtonCancel={onClose} textButtonCancel={i18n('close')} />
</Dialog>
);
}
const ConnectToDBDialogNiceModal = NiceModal.create((props: SnippetParams) => {
const modal = NiceModal.useModal();
const handleClose = () => {
modal.hide();
modal.remove();
};
return (
<ConnectToDBDialog
{...props}
onClose={() => {
modal.resolve(false);
handleClose();
}}
open={modal.visible}
/>
);
});
const CONNECT_TO_DB_DIALOG = 'connect-to-db-dialog';
NiceModal.register(CONNECT_TO_DB_DIALOG, ConnectToDBDialogNiceModal);
export async function getConnectToDBDialog(params: SnippetParams): Promise<boolean> {
return await NiceModal.show(CONNECT_TO_DB_DIALOG, {
id: CONNECT_TO_DB_DIALOG,
...params,
});
}