@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
111 lines • 8.19 kB
JavaScript
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';
import { CheckIconOutline } from '../../icons/generated/heroIcons/CheckIconOutline';
import { XMarkIconOutline } from '../../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 (_jsx("div", Object.assign({ ref: ref, style: style, className: classNames('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 (_jsxs("div", Object.assign({ ref: ref, style: style, className: classNames('ndl-ai-preview-header', className) }, restProps, htmlAttributes, { children: [_jsx(Typography, { variant: "code", as: "div", className: "ndl-ai-preview-header-leading-content", children: children }), actions && (_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 = classNames('ndl-ai-preview-confirmation', className, {
'ndl-ai-preview-confirmation-footer': isFooter,
});
return (_jsxs("div", Object.assign({ ref: ref, className: classes, style: style }, restProps, htmlAttributes, { children: [!(isFooter && status !== 'pending') && (_jsx(Typography, { as: "div", variant: isFooter ? 'subheading-small' : 'body-medium', className: "ndl-ai-preview-confirmation-content", children: children })), _jsxs(Typography, { variant: "body-medium", as: "div", className: "ndl-ai-preview-confirmation-actions", children: [status === 'pending' && actions, status === 'confirmed' && (_jsxs("div", { className: "ndl-ai-preview-confirmation-confirm-reject", children: [_jsx(CheckIconOutline, { className: "ndl-ai-preview-confirmation-action-icon" }), confirmedActionText] })), status === 'rejected' && (_jsxs("div", { className: "ndl-ai-preview-confirmation-confirm-reject", children: [_jsx(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] = 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-preview-code', className) }, restProps, htmlAttributes, { children: [isLoading && (_jsxs("div", { className: "ndl-ai-preview-code-loading", children: [_jsx(LoadingSpinner, { size: "medium" }), _jsxs(Typography, { variant: "code", children: ["WRITING ", language.toUpperCase()] })] })), !isLoading && (_jsxs(_Fragment, { children: [_jsxs(PreviewHeader, { className: "ndl-ai-preview-code-header", actions: _jsxs(_Fragment, { children: [_jsx(ClipboardButton, { textToCopy: code, type: "clean-icon-button", size: "small" }), headerActions] }), children: [language, " ", language.toLowerCase() === 'cypher' ? 'QUERY' : '', label && (_jsx(StatusLabel, { variant: label === 'write' ? 'warning' : 'info', size: "small", fill: "semi-filled", children: label.toUpperCase() }))] }), _jsx("div", { className: "ndl-ai-preview-code-content", 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-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 (_jsx("div", Object.assign({ ref: ref, style: style, className: classNames('ndl-ai-preview-content', className) }, restProps, htmlAttributes, { children: children })));
};
export const Preview = Object.assign(PreviewComponent, {
Code: PreviewCode,
Confirmation: PreviewConfirmation,
Content: PreviewContent,
Header: PreviewHeader,
});
//# sourceMappingURL=Preview.js.map