|
| 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 {lazyComponent} from '../../utils/lazyComponent'; |
| 7 | +import {LinkWithIcon} from '../LinkWithIcon/LinkWithIcon'; |
| 8 | + |
| 9 | +import {getDocsLink} from './getDocsLink'; |
| 10 | +import i18n from './i18n'; |
| 11 | +import {b} from './shared'; |
| 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 connectionTabs: {id: SnippetLanguage; title: string}[] = [ |
| 23 | + {id: 'bash', title: 'Bash'}, |
| 24 | + {id: 'cpp', title: 'C++'}, |
| 25 | + {id: 'csharp', title: 'C# (.NET)'}, |
| 26 | + {id: 'go', title: 'Go'}, |
| 27 | + {id: 'java', title: 'Java'}, |
| 28 | + {id: 'javascript', title: 'Node JS'}, |
| 29 | + {id: 'php', title: 'PHP'}, |
| 30 | + {id: 'python', title: 'Python'}, |
| 31 | +]; |
| 32 | + |
| 33 | +interface ConnectToDBDialogProps extends SnippetParams { |
| 34 | + open: boolean; |
| 35 | + onClose: VoidFunction; |
| 36 | +} |
| 37 | + |
| 38 | +function ConnectToDBDialog({open, onClose, database, endpoint}: ConnectToDBDialogProps) { |
| 39 | + const [activeTab, setActiveTab] = React.useState<SnippetLanguage>('bash'); |
| 40 | + |
| 41 | + const snippet = getSnippetCode(activeTab, {database, endpoint}); |
| 42 | + const docsLink = getDocsLink(activeTab); |
| 43 | + |
| 44 | + return ( |
| 45 | + <Dialog open={open} hasCloseButton={true} onClose={onClose} size="l"> |
| 46 | + <Dialog.Header caption={i18n('header')} /> |
| 47 | + <Dialog.Body> |
| 48 | + <div>{i18n('connection-info-message')}</div> |
| 49 | + <Tabs |
| 50 | + size="m" |
| 51 | + allowNotSelected={false} |
| 52 | + activeTab={activeTab} |
| 53 | + items={connectionTabs} |
| 54 | + onSelectTab={(tab) => setActiveTab(tab as SnippetLanguage)} |
| 55 | + className={b('dialog-tabs')} |
| 56 | + /> |
| 57 | + <ConnectToDBSyntaxHighlighter language={activeTab} text={snippet} /> |
| 58 | + {docsLink ? ( |
| 59 | + <LinkWithIcon |
| 60 | + className={b('docs')} |
| 61 | + title={i18n('documentation')} |
| 62 | + url={docsLink} |
| 63 | + /> |
| 64 | + ) : null} |
| 65 | + </Dialog.Body> |
| 66 | + <Dialog.Footer onClickButtonCancel={onClose} textButtonCancel={i18n('close')} /> |
| 67 | + </Dialog> |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +export const ConnectToDBDialogNiceModal = NiceModal.create((props: SnippetParams) => { |
| 72 | + const modal = NiceModal.useModal(); |
| 73 | + |
| 74 | + const handleClose = () => { |
| 75 | + modal.hide(); |
| 76 | + modal.remove(); |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <ConnectToDBDialog |
| 81 | + {...props} |
| 82 | + onClose={() => { |
| 83 | + modal.resolve(false); |
| 84 | + handleClose(); |
| 85 | + }} |
| 86 | + open={modal.visible} |
| 87 | + /> |
| 88 | + ); |
| 89 | +}); |
| 90 | + |
| 91 | +const CONNECT_TO_DB_DIALOG = 'connect-to-db-dialog'; |
| 92 | + |
| 93 | +NiceModal.register(CONNECT_TO_DB_DIALOG, ConnectToDBDialogNiceModal); |
| 94 | + |
| 95 | +export async function getConnectToDBDialog(params: SnippetParams): Promise<boolean> { |
| 96 | + return await NiceModal.show(CONNECT_TO_DB_DIALOG, { |
| 97 | + id: CONNECT_TO_DB_DIALOG, |
| 98 | + ...params, |
| 99 | + }); |
| 100 | +} |
0 commit comments