UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

91 lines 5.93 kB
"use strict"; 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CodePreview = void 0; const jsx_runtime_1 = require("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/>. */ const react_1 = require("@neo4j-ndl/react"); const classnames_1 = __importDefault(require("classnames")); const react_2 = require("react"); const react_syntax_highlighter_1 = require("react-syntax-highlighter"); const code_languages_1 = require("../../code-block/code-languages"); const ndl_code_dark_1 = __importDefault(require("../../code-block/themes/ndl-code-dark")); const ndl_code_light_1 = __importDefault(require("../../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. */ 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] = (0, react_2.useState)(null); const { theme: ndlTheme } = (0, react_1.useNeedleTheme)(); (0, react_2.useEffect)(() => { if (!language || !code_languages_1.Languages.includes(language)) { setLoadedLanguage('text'); return; } (0, code_languages_1.loadLanguage)(language) .then((module) => { react_syntax_highlighter_1.PrismLight.registerLanguage(language, module.default); setLoadedLanguage(language); }) .catch((err) => console.error(err)); }, [language]); const getTheme = () => { switch (theme) { case 'ndl-code-dark': return ndl_code_dark_1.default; case 'ndl-code-light': return ndl_code_light_1.default; default: return ndlTheme === 'light' ? ndl_code_light_1.default : ndl_code_dark_1.default; } }; return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-ai-code-preview', className) }, restProps, htmlAttributes, { children: [(0, jsx_runtime_1.jsxs)("div", { className: "ndl-ai-code-preview-header", children: [(0, jsx_runtime_1.jsx)("div", { className: "ndl-ai-code-preview-title", children: isLoading ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_1.LoadingSpinner, { size: "medium" }), (0, jsx_runtime_1.jsxs)(react_1.Typography, { variant: "code", children: ["WRITING ", language.toUpperCase()] })] })) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(react_1.Typography, { variant: "code", children: [language.toUpperCase(), ' ', language.toLowerCase() === 'cypher' ? 'QUERY' : ''] }), label && ((0, jsx_runtime_1.jsx)(react_1.StatusLabel, { variant: label === 'write' ? 'warning' : 'info', size: "small", fill: "outlined", children: label }))] })) }), !isLoading && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-ai-code-preview-actions", children: [(0, jsx_runtime_1.jsx)(react_1.ClipboardButton, { textToCopy: code, type: "clean-icon-button", size: "small" }), headerActions] }))] }), !isLoading && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-ai-code-preview-content", children: (0, jsx_runtime_1.jsx)("div", { className: "ndl-code-content-container", children: (0, jsx_runtime_1.jsx)(react_syntax_highlighter_1.PrismLight, { 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 }) }) }))] }))); }; exports.CodePreview = CodePreview; //# sourceMappingURL=CodePreview.js.map