-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae8aa6d
commit 209df57
Showing
15 changed files
with
631 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@use '../../styles/mixins.scss'; | ||
|
||
.ydb-connect-to-db { | ||
&__dialog-tabs { | ||
margin-top: var(--g-spacing-4); | ||
} | ||
|
||
&__docs { | ||
margin-top: var(--g-spacing-4); | ||
} | ||
|
||
&__syntax-highlighter-wrapper { | ||
position: relative; | ||
z-index: 0; | ||
|
||
height: 270px; | ||
} | ||
|
||
&__sticky-container { | ||
z-index: 1; | ||
top: 52px; | ||
@include mixins.sticky-top(); | ||
} | ||
|
||
&__snippet-copy { | ||
position: absolute; | ||
top: 13px; | ||
right: 14px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React from 'react'; | ||
|
||
import NiceModal from '@ebay/nice-modal-react'; | ||
import {Dialog, Tabs} from '@gravity-ui/uikit'; | ||
|
||
import {lazyComponent} from '../../utils/lazyComponent'; | ||
import {LinkWithIcon} from '../LinkWithIcon/LinkWithIcon'; | ||
|
||
import {getDocsLink} from './getDocsLink'; | ||
import i18n from './i18n'; | ||
import {b} from './shared'; | ||
import {getSnippetCode} from './snippets'; | ||
import type {SnippetLanguage, SnippetParams} from './types'; | ||
|
||
import './ConnectToDB.scss'; | ||
|
||
const ConnectToDBSyntaxHighlighter = lazyComponent( | ||
() => import('./ConnectToDBSyntaxHighlighter'), | ||
'ConnectToDBSyntaxHighlighter', | ||
); | ||
|
||
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')} | ||
/> | ||
<ConnectToDBSyntaxHighlighter language={activeTab} text={snippet} /> | ||
{docsLink ? ( | ||
<LinkWithIcon | ||
className={b('docs')} | ||
title={i18n('documentation')} | ||
url={docsLink} | ||
/> | ||
) : null} | ||
</Dialog.Body> | ||
<Dialog.Footer onClickButtonCancel={onClose} textButtonCancel={i18n('close')} /> | ||
</Dialog> | ||
); | ||
} | ||
|
||
export 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, | ||
}); | ||
} |
84 changes: 84 additions & 0 deletions
84
src/components/ConnectToDB/ConnectToDBSyntaxHighlighter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import {ClipboardButton, useThemeValue} from '@gravity-ui/uikit'; | ||
import {PrismLight as SyntaxHighlighter} from 'react-syntax-highlighter'; | ||
import bash from 'react-syntax-highlighter/dist/esm/languages/prism/bash'; | ||
import cpp from 'react-syntax-highlighter/dist/esm/languages/prism/cpp'; | ||
import csharp from 'react-syntax-highlighter/dist/esm/languages/prism/csharp'; | ||
import go from 'react-syntax-highlighter/dist/esm/languages/prism/go'; | ||
import java from 'react-syntax-highlighter/dist/esm/languages/prism/java'; | ||
import javascript from 'react-syntax-highlighter/dist/esm/languages/prism/javascript'; | ||
import php from 'react-syntax-highlighter/dist/esm/languages/prism/php'; | ||
import python from 'react-syntax-highlighter/dist/esm/languages/prism/python'; | ||
import {vscDarkPlus} from 'react-syntax-highlighter/dist/esm/styles/prism'; | ||
|
||
import {dark, light} from '../YqlHighlighter/yql'; | ||
|
||
import i18n from './i18n'; | ||
import {b} from './shared'; | ||
import type {SnippetLanguage} from './types'; | ||
|
||
SyntaxHighlighter.registerLanguage('bash', bash); | ||
SyntaxHighlighter.registerLanguage('cpp', cpp); | ||
SyntaxHighlighter.registerLanguage('csharp', csharp); | ||
SyntaxHighlighter.registerLanguage('go', go); | ||
SyntaxHighlighter.registerLanguage('java', java); | ||
SyntaxHighlighter.registerLanguage('javascript', javascript); | ||
SyntaxHighlighter.registerLanguage('php', php); | ||
SyntaxHighlighter.registerLanguage('python', python); | ||
|
||
interface ConnectToDBSyntaxHighlighterProps { | ||
text: string; | ||
language: SnippetLanguage; | ||
className?: string; | ||
} | ||
const darkTheme: Record<string, React.CSSProperties> = { | ||
...dark, | ||
'pre[class*="language-"]': { | ||
...dark['pre[class*="language-"]'], | ||
background: vscDarkPlus['pre[class*="language-"]'].background, | ||
scrollbarColor: `var(--g-color-scroll-handle) transparent`, | ||
}, | ||
'code[class*="language-"]': { | ||
...dark['code[class*="language-"]'], | ||
whiteSpace: 'pre', | ||
}, | ||
}; | ||
|
||
const lightTheme: Record<string, React.CSSProperties> = { | ||
...light, | ||
'pre[class*="language-"]': { | ||
...light['pre[class*="language-"]'], | ||
background: 'var(--g-color-base-misc-light)', | ||
scrollbarColor: `var(--g-color-scroll-handle) transparent`, | ||
}, | ||
'code[class*="language-"]': { | ||
...light['code[class*="language-"]'], | ||
whiteSpace: 'pre', | ||
}, | ||
}; | ||
|
||
export function ConnectToDBSyntaxHighlighter({text, language}: ConnectToDBSyntaxHighlighterProps) { | ||
const themeValue = useThemeValue(); | ||
const isDark = themeValue === 'dark' || themeValue === 'dark-hc'; | ||
|
||
return ( | ||
<div className={b('syntax-highlighter-wrapper')}> | ||
<div className={b('sticky-container')}> | ||
<ClipboardButton | ||
view="flat-secondary" | ||
size="s" | ||
className={b('snippet-copy')} | ||
text={text} | ||
> | ||
{i18n('copy')} | ||
</ClipboardButton> | ||
</div> | ||
<SyntaxHighlighter | ||
language={language} | ||
style={isDark ? darkTheme : lightTheme} | ||
customStyle={{height: '100%'}} | ||
> | ||
{text} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import i18n from './i18n'; | ||
import type {SnippetLanguage} from './types'; | ||
|
||
export function getDocsLink(snippetLang: SnippetLanguage) { | ||
switch (snippetLang) { | ||
case 'bash': { | ||
return i18n('docs_bash'); | ||
} | ||
case 'cpp': { | ||
return i18n('docs_cpp'); | ||
} | ||
case 'csharp': { | ||
return i18n('docs_dotnet'); | ||
} | ||
case 'go': { | ||
return i18n('docs_go'); | ||
} | ||
case 'java': { | ||
return i18n('docs_java'); | ||
} | ||
case 'javascript': { | ||
return i18n('docs_nodejs'); | ||
} | ||
case 'php': { | ||
return i18n('docs_php'); | ||
} | ||
case 'python': { | ||
return i18n('docs_python'); | ||
} | ||
default: { | ||
return undefined; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"header": "Connection string", | ||
"connection-info-message": "Use the following string to connect to you database", | ||
|
||
"documentation": "Documentation", | ||
|
||
"close": "Close", | ||
"copy": "Copy", | ||
|
||
"docs_bash": "https://ydb.tech/docs/en/concepts/connect", | ||
"docs_cpp": "https://ydb.tech/docs/en/dev/example-app/example-cpp", | ||
"docs_dotnet": "https://ydb.tech/docs/en/dev/example-app/example-dotnet", | ||
"docs_go": "https://ydb.tech/docs/en/dev/example-app/go", | ||
"docs_java": "https://ydb.tech/docs/en/dev/example-app/java", | ||
"docs_nodejs": "https://ydb.tech/docs/en/dev/example-app/example-nodejs", | ||
"docs_php": "https://ydb.tech/docs/en/dev/example-app/example-php", | ||
"docs_python": "https://ydb.tech/docs/en/dev/example-app/python" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {registerKeysets} from '../../../utils/i18n'; | ||
|
||
import en from './en.json'; | ||
|
||
const COMPONENT = 'ydb-connect-to-db'; | ||
|
||
export default registerKeysets(COMPONENT, {en}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {cn} from '../../utils/cn'; | ||
|
||
export const b = cn('ydb-connect-to-db'); |
Oops, something went wrong.