import React = require('react'); import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { action, observable } from 'mobx'; import { observer } from 'mobx-react'; import ReactLoading from 'react-loading'; import Typist from 'react-typist'; import { Doc } from '../../../../fields/Doc'; import { Docs } from '../../../documents/Documents'; import './GPTPopup.scss'; export enum GPTPopupMode { SUMMARY, EDIT, } interface GPTPopupProps { visible: boolean; text: string; loading: boolean; mode: GPTPopupMode; callSummaryApi: (e: React.PointerEvent) => Promise; callEditApi: (e: React.PointerEvent) => Promise; replaceText: (replacement: string) => void; highlightRange?: number[]; } @observer export class GPTPopup extends React.Component { static Instance: GPTPopup; @observable private done: boolean = false; @observable private sidebarId: string = ''; @action public setDone = (done: boolean) => { this.done = done; }; @action public setSidebarId = (id: string) => { this.sidebarId = id; }; public addDoc: (doc: Doc | Doc[], sidebarKey?: string | undefined) => boolean = () => false; /** * Transfers the summarization text to a sidebar annotation text document. */ private transferToText = () => { const newDoc = Docs.Create.TextDocument(this.props.text.trim(), { _width: 200, _height: 50, _layout_fitWidth: true, _layout_autoHeight: true, }); this.addDoc(newDoc, this.sidebarId); }; constructor(props: GPTPopupProps) { super(props); GPTPopup.Instance = this; } componentDidUpdate = () => { if (this.props.loading) { this.setDone(false); } }; summaryBox = () => ( <>
{this.heading('SUMMARY')}
{!this.props.loading && (!this.done ? ( { setTimeout(() => { this.setDone(true); }, 500); }}> {this.props.text} ) : ( this.props.text ))}
{!this.props.loading && (
{this.done ? ( <> ) : (
Summarizing
)}
)} ); editBox = () => { const hr = this.props.highlightRange; return ( <>
{this.heading('TEXT EDIT SUGGESTIONS')}
{hr && (
{this.props.text.slice(0, hr[0])} {this.props.text.slice(hr[0], hr[1])} {this.props.text.slice(hr[1])}
)}
{hr && !this.props.loading && ( <>
<>
{this.aiWarning()} )} ); }; aiWarning = () => this.done ? (
AI generated responses can contain inaccurate or misleading content.
) : ( <> ); heading = (headingText: string) => (
{this.props.loading && }
); render() { return (
{this.props.mode === GPTPopupMode.SUMMARY ? this.summaryBox() : this.editBox()}
); } }