UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

84 lines 5.21 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; /** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import { ClipboardButton, LoadingSpinner, StatusLabel, Typography, useNeedleTheme, } from '@neo4j-ndl/react'; import classNames from 'classnames'; import { useEffect, useState } from 'react'; import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter'; import { Languages, loadLanguage, } from '../../code-block/code-languages'; import ndlCodeDark from '../../code-block/themes/ndl-code-dark'; import ndlCodeLight from '../../code-block/themes/ndl-code-light'; /** * The component is used to display code that is generated by an LLM. * * @alpha - Changes to this component may be breaking. */ export const CodePreview = (_a) => { var { code, language, label, isLoading = false, headerActions, theme, ref, style, className, htmlAttributes } = _a, restProps = __rest(_a, ["code", "language", "label", "isLoading", "headerActions", "theme", "ref", "style", "className", "htmlAttributes"]); const [loadedLanguage, setLoadedLanguage] = useState(null); const { theme: ndlTheme } = useNeedleTheme(); useEffect(() => { if (!language || !Languages.includes(language)) { setLoadedLanguage('text'); return; } loadLanguage(language) .then((module) => { SyntaxHighlighter.registerLanguage(language, module.default); setLoadedLanguage(language); }) .catch((err) => console.error(err)); }, [language]); const getTheme = () => { switch (theme) { case 'ndl-code-dark': return ndlCodeDark; case 'ndl-code-light': return ndlCodeLight; default: return ndlTheme === 'light' ? ndlCodeLight : ndlCodeDark; } }; return (_jsxs("div", Object.assign({ ref: ref, style: style, className: classNames('ndl-ai-code-preview', className) }, restProps, htmlAttributes, { children: [_jsxs("div", { className: "ndl-ai-code-preview-header", children: [_jsx("div", { className: "ndl-ai-code-preview-title", children: isLoading ? (_jsxs(_Fragment, { children: [_jsx(LoadingSpinner, { size: "medium" }), _jsxs(Typography, { variant: "code", children: ["WRITING ", language.toUpperCase()] })] })) : (_jsxs(_Fragment, { children: [_jsxs(Typography, { variant: "code", children: [language.toUpperCase(), ' ', language.toLowerCase() === 'cypher' ? 'QUERY' : ''] }), label && (_jsx(StatusLabel, { variant: label === 'write' ? 'warning' : 'info', size: "small", fill: "outlined", children: label }))] })) }), !isLoading && (_jsxs("div", { className: "ndl-ai-code-preview-actions", children: [_jsx(ClipboardButton, { textToCopy: code, type: "clean-icon-button", size: "small" }), headerActions] }))] }), !isLoading && (_jsx("div", { className: "ndl-ai-code-preview-content", children: _jsx("div", { className: "ndl-code-content-container", children: _jsx(SyntaxHighlighter, { language: loadedLanguage !== null && loadedLanguage !== void 0 ? loadedLanguage : 'text', style: Object.assign(Object.assign({}, getTheme()), { 'pre[class*="language-"]': { border: 0, color: 'var(--theme-color-neutral-text-default)', lineHeight: '1', overflowX: 'auto', padding: 'var(--space-12)', width: 'fit-content', } }), // Turn on 'shouldShowLineNumbers' & 'wrapLongLines' at the same time, the display is wrong // https://github.com/react-syntax-highlighter/react-syntax-highlighter/issues/402 // wrapLongLines codeTagProps: { className: 'n-ai-code-preview' }, showLineNumbers: false, children: code }) }) }))] }))); }; //# sourceMappingURL=CodePreview.js.map