@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
117 lines • 9.16 kB
JavaScript
"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.Preview = 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"));
const CheckIconOutline_1 = require("../../icons/generated/heroIcons/CheckIconOutline");
const XMarkIconOutline_1 = require("../../icons/generated/heroIcons/XMarkIconOutline");
/**
* Component to use for previewing certain content in AI chat applications.
*
* @alpha - Changes to this component may be breaking.
*/
const PreviewComponent = (_a) => {
var { children, ref, style, className, htmlAttributes } = _a, restProps = __rest(_a, ["children", "ref", "style", "className", "htmlAttributes"]);
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-ai-preview', className) }, restProps, htmlAttributes, { children: children })));
};
const PreviewHeader = (_a) => {
var { children, actions, ref, style, className, htmlAttributes } = _a, restProps = __rest(_a, ["children", "actions", "ref", "style", "className", "htmlAttributes"]);
return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-ai-preview-header', className) }, restProps, htmlAttributes, { children: [(0, jsx_runtime_1.jsx)(react_1.Typography, { variant: "code", as: "div", className: "ndl-ai-preview-header-leading-content", children: children }), actions && ((0, jsx_runtime_1.jsx)("div", { className: "ndl-ai-preview-header-actions", children: actions }))] })));
};
const PreviewConfirmation = (_a) => {
var { children, actions, isFooter = false, status = 'pending', confirmedActionText = 'Action confirmed', rejectedActionText = 'Action rejected', ref, style, className, htmlAttributes } = _a, restProps = __rest(_a, ["children", "actions", "isFooter", "status", "confirmedActionText", "rejectedActionText", "ref", "style", "className", "htmlAttributes"]);
const classes = (0, classnames_1.default)('ndl-ai-preview-confirmation', className, {
'ndl-ai-preview-confirmation-footer': isFooter,
});
return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ ref: ref, className: classes, style: style }, restProps, htmlAttributes, { children: [!(isFooter && status !== 'pending') && ((0, jsx_runtime_1.jsx)(react_1.Typography, { as: "div", variant: isFooter ? 'subheading-small' : 'body-medium', className: "ndl-ai-preview-confirmation-content", children: children })), (0, jsx_runtime_1.jsxs)(react_1.Typography, { variant: "body-medium", as: "div", className: "ndl-ai-preview-confirmation-actions", children: [status === 'pending' && actions, status === 'confirmed' && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-ai-preview-confirmation-confirm-reject", children: [(0, jsx_runtime_1.jsx)(CheckIconOutline_1.CheckIconOutline, { className: "ndl-ai-preview-confirmation-action-icon" }), confirmedActionText] })), status === 'rejected' && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-ai-preview-confirmation-confirm-reject", children: [(0, jsx_runtime_1.jsx)(XMarkIconOutline_1.XMarkIconOutline, { className: "ndl-ai-preview-confirmation-action-icon" }), rejectedActionText] }))] })] })));
};
const PreviewCode = (_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-preview-code', className) }, restProps, htmlAttributes, { children: [isLoading && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-ai-preview-code-loading", 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()] })] })), !isLoading && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(PreviewHeader, { className: "ndl-ai-preview-code-header", actions: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_1.ClipboardButton, { textToCopy: code, type: "clean-icon-button", size: "small" }), headerActions] }), children: [language, " ", language.toLowerCase() === 'cypher' ? 'QUERY' : '', label && ((0, jsx_runtime_1.jsx)(react_1.StatusLabel, { variant: label === 'write' ? 'warning' : 'info', size: "small", fill: "semi-filled", children: label.toUpperCase() }))] }), (0, jsx_runtime_1.jsx)("div", { className: "ndl-ai-preview-code-content", 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-preview-code' }, showLineNumbers: false, children: code }) })] }))] })));
};
const PreviewContent = (_a) => {
var { children, ref, style, className, htmlAttributes } = _a, restProps = __rest(_a, ["children", "ref", "style", "className", "htmlAttributes"]);
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ ref: ref, style: style, className: (0, classnames_1.default)('ndl-ai-preview-content', className) }, restProps, htmlAttributes, { children: children })));
};
exports.Preview = Object.assign(PreviewComponent, {
Code: PreviewCode,
Confirmation: PreviewConfirmation,
Content: PreviewContent,
Header: PreviewHeader,
});
//# sourceMappingURL=Preview.js.map