Skip to content

Commit 12912a0

Browse files
feat: add connect to DB dialog
1 parent dec5b95 commit 12912a0

File tree

12 files changed

+509
-24
lines changed

12 files changed

+509
-24
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@use '../../styles/mixins.scss';
2+
3+
.ydb-connect-to-db {
4+
&__dialog-body {
5+
height: 600px;
6+
padding-top: 0px;
7+
}
8+
&__dialog-tabs {
9+
padding-top: 10px;
10+
11+
@include mixins.sticky-top();
12+
}
13+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import React from 'react';
2+
3+
import NiceModal from '@ebay/nice-modal-react';
4+
import {Dialog, Tabs} from '@gravity-ui/uikit';
5+
6+
import {cn} from '../../utils/cn';
7+
import {lazyComponent} from '../../utils/lazyComponent';
8+
import {LinkWithIcon} from '../LinkWithIcon/LinkWithIcon';
9+
10+
import {getDocsLink} from './documentationLinks';
11+
import i18n from './i18n';
12+
import {getSnippetCode} from './snippets';
13+
import type {SnippetLanguage, SnippetParams} from './types';
14+
15+
import './ConnectToDB.scss';
16+
17+
const ConnectToDBSyntaxHighlighter = lazyComponent(
18+
() => import('./ConnectToDBSyntaxHighlighter'),
19+
'ConnectToDBSyntaxHighlighter',
20+
);
21+
22+
const b = cn('ydb-connect-to-db');
23+
24+
const connectionTabs: {id: SnippetLanguage; title: string}[] = [
25+
{id: 'bash', title: 'Bash'},
26+
{id: 'cpp', title: 'C++'},
27+
{id: 'csharp', title: 'C# (.NET)'},
28+
{id: 'go', title: 'Go'},
29+
{id: 'java', title: 'Java'},
30+
{id: 'javascript', title: 'Node JS'},
31+
{id: 'php', title: 'PHP'},
32+
{id: 'python', title: 'Python'},
33+
];
34+
35+
interface ConnectToDBDialogProps extends SnippetParams {
36+
open: boolean;
37+
onClose: VoidFunction;
38+
}
39+
40+
function ConnectToDBDialog({open, onClose, database, endpoint}: ConnectToDBDialogProps) {
41+
const [activeTab, setActiveTab] = React.useState<SnippetLanguage>('bash');
42+
43+
const snippet = getSnippetCode(activeTab, {database, endpoint});
44+
const docsLink = getDocsLink(activeTab);
45+
46+
return (
47+
<Dialog open={open} hasCloseButton={true} onClose={onClose} size="l">
48+
<Dialog.Header caption={i18n('connect-to-database')} />
49+
<Dialog.Body className={b('dialog-body')}>
50+
<Tabs
51+
size="l"
52+
allowNotSelected={false}
53+
activeTab={activeTab}
54+
items={connectionTabs}
55+
onSelectTab={(tab) => setActiveTab(tab as SnippetLanguage)}
56+
className={b('dialog-tabs')}
57+
/>
58+
<ConnectToDBSyntaxHighlighter language={activeTab} text={snippet} />
59+
{docsLink ? <LinkWithIcon title={i18n('documentation')} url={docsLink} /> : null}
60+
</Dialog.Body>
61+
<Dialog.Footer onClickButtonCancel={onClose} textButtonCancel={i18n('close')} />
62+
</Dialog>
63+
);
64+
}
65+
66+
export const ConnectToDBDialogNiceModal = NiceModal.create((props: SnippetParams) => {
67+
const modal = NiceModal.useModal();
68+
69+
const handleClose = () => {
70+
modal.hide();
71+
modal.remove();
72+
};
73+
74+
return (
75+
<ConnectToDBDialog
76+
{...props}
77+
onClose={() => {
78+
modal.resolve(false);
79+
handleClose();
80+
}}
81+
open={modal.visible}
82+
/>
83+
);
84+
});
85+
86+
const CONNECT_TO_DB_DIALOG = 'connect-to-db-dialog';
87+
88+
NiceModal.register(CONNECT_TO_DB_DIALOG, ConnectToDBDialogNiceModal);
89+
90+
export async function getConnectToDBDialog(params: SnippetParams): Promise<boolean> {
91+
return await NiceModal.show(CONNECT_TO_DB_DIALOG, {
92+
id: CONNECT_TO_DB_DIALOG,
93+
...params,
94+
});
95+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {useThemeValue} from '@gravity-ui/uikit';
2+
import {PrismLight as SyntaxHighlighter} from 'react-syntax-highlighter';
3+
import bash from 'react-syntax-highlighter/dist/esm/languages/prism/bash';
4+
import cpp from 'react-syntax-highlighter/dist/esm/languages/prism/cpp';
5+
import csharp from 'react-syntax-highlighter/dist/esm/languages/prism/csharp';
6+
import go from 'react-syntax-highlighter/dist/esm/languages/prism/go';
7+
import java from 'react-syntax-highlighter/dist/esm/languages/prism/java';
8+
import javascript from 'react-syntax-highlighter/dist/esm/languages/prism/javascript';
9+
import php from 'react-syntax-highlighter/dist/esm/languages/prism/php';
10+
import python from 'react-syntax-highlighter/dist/esm/languages/prism/python';
11+
import {
12+
vscDarkPlus as darkTheme,
13+
materialLight as lightTheme,
14+
} from 'react-syntax-highlighter/dist/esm/styles/prism';
15+
16+
import type {SnippetLanguage} from './types';
17+
18+
SyntaxHighlighter.registerLanguage('bash', bash);
19+
SyntaxHighlighter.registerLanguage('cpp', cpp);
20+
SyntaxHighlighter.registerLanguage('csharp', csharp);
21+
SyntaxHighlighter.registerLanguage('go', go);
22+
SyntaxHighlighter.registerLanguage('java', java);
23+
SyntaxHighlighter.registerLanguage('javascript', javascript);
24+
SyntaxHighlighter.registerLanguage('php', php);
25+
SyntaxHighlighter.registerLanguage('python', python);
26+
27+
interface ConnectToDBSyntaxHighlighterProps {
28+
text: string;
29+
language: SnippetLanguage;
30+
className?: string;
31+
}
32+
33+
export function ConnectToDBSyntaxHighlighter({text, language}: ConnectToDBSyntaxHighlighterProps) {
34+
const themeValue = useThemeValue();
35+
const isDark = themeValue === 'dark' || themeValue === 'dark-hc';
36+
37+
return (
38+
<SyntaxHighlighter language={language} style={isDark ? darkTheme : lightTheme}>
39+
{text}
40+
</SyntaxHighlighter>
41+
);
42+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {currentLang} from '../../utils/i18n';
2+
3+
import type {SnippetLanguage} from './types';
4+
5+
function getBashDocsLink() {
6+
return `https://ydb.tech/docs/${currentLang}/concepts/connect`;
7+
}
8+
function getCPPDocsLink() {
9+
return `https://ydb.tech/docs/${currentLang}/dev/example-app/example-cpp`;
10+
}
11+
function getCSharpDocsLink() {
12+
return `https://ydb.tech/docs/${currentLang}/dev/example-app/example-dotnet`;
13+
}
14+
function getGoDocsLink() {
15+
return `https://ydb.tech/docs/${currentLang}/dev/example-app/go`;
16+
}
17+
function getJavaDocsLink() {
18+
return `https://ydb.tech/docs/${currentLang}/dev/example-app/java`;
19+
}
20+
function getNodeJSDocsLink() {
21+
return `https://ydb.tech/docs/${currentLang}/dev/example-app/example-nodejs`;
22+
}
23+
function getPHPDocsLink() {
24+
return `https://ydb.tech/docs/${currentLang}/dev/example-app/example-php`;
25+
}
26+
function getPythonDocsLink() {
27+
return `https://ydb.tech/docs/${currentLang}/dev/example-app/python`;
28+
}
29+
30+
export function getDocsLink(snippetLang: SnippetLanguage) {
31+
switch (snippetLang) {
32+
case 'bash': {
33+
return getBashDocsLink();
34+
}
35+
case 'cpp': {
36+
return getCPPDocsLink();
37+
}
38+
case 'csharp': {
39+
return getCSharpDocsLink();
40+
}
41+
case 'go': {
42+
return getGoDocsLink();
43+
}
44+
case 'java': {
45+
return getJavaDocsLink();
46+
}
47+
case 'javascript': {
48+
return getNodeJSDocsLink();
49+
}
50+
case 'php': {
51+
return getPHPDocsLink();
52+
}
53+
case 'python': {
54+
return getPythonDocsLink();
55+
}
56+
default: {
57+
return undefined;
58+
}
59+
}
60+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"connect-to-database": "Connect to Database",
3+
4+
"documentation": "Documentation",
5+
6+
"close": "Close"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {registerKeysets} from '../../../utils/i18n';
2+
3+
import en from './en.json';
4+
5+
const COMPONENT = 'ydb-connect-to-db';
6+
7+
export default registerKeysets(COMPONENT, {en});

0 commit comments

Comments
 (0)