aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/pdf/GPTPopup/GPTPopup.tsx
blob: 8bd060d4fe17dc9f79e77c558b2cb80d68480e10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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<void>;
    callEditApi: (e: React.PointerEvent) => Promise<void>;
    replaceText: (replacement: string) => void;
    highlightRange?: number[];
}

@observer
export class GPTPopup extends React.Component<GPTPopupProps> {
    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 = () => (
        <>
            <div>
                {this.heading('SUMMARY')}
                <div className="content-wrapper">
                    {!this.props.loading &&
                        (!this.done ? (
                            <Typist
                                key={this.props.text}
                                avgTypingDelay={15}
                                cursor={{ hideWhenDone: true }}
                                onTypingDone={() => {
                                    setTimeout(() => {
                                        this.setDone(true);
                                    }, 500);
                                }}>
                                {this.props.text}
                            </Typist>
                        ) : (
                            this.props.text
                        ))}
                </div>
            </div>
            {!this.props.loading && (
                <div className="btns-wrapper">
                    {this.done ? (
                        <>
                            <button className="icon-btn" onPointerDown={e => this.props.callSummaryApi(e)}>
                                <FontAwesomeIcon icon="redo-alt" size="lg" />
                            </button>
                            <button
                                className="text-btn"
                                onClick={e => {
                                    this.transferToText();
                                }}>
                                Transfer to Text
                            </button>
                        </>
                    ) : (
                        <div className="summarizing">
                            <span>Summarizing</span>
                            <ReactLoading type="bubbles" color="#bcbcbc" width={20} height={20} />
                            <button
                                className="btn-secondary"
                                onClick={e => {
                                    this.setDone(true);
                                }}>
                                Stop Animation
                            </button>
                        </div>
                    )}
                </div>
            )}
        </>
    );

    editBox = () => {
        const hr = this.props.highlightRange;
        return (
            <>
                <div>
                    {this.heading('TEXT EDIT SUGGESTIONS')}
                    <div className="content-wrapper">
                        {hr && (
                            <div>
                                {this.props.text.slice(0, hr[0])} <span className="highlighted-text">{this.props.text.slice(hr[0], hr[1])}</span> {this.props.text.slice(hr[1])}
                            </div>
                        )}
                    </div>
                </div>
                {hr && !this.props.loading && (
                    <>
                        <div className="btns-wrapper">
                            <>
                                <button className="icon-btn" onPointerDown={e => this.props.callEditApi(e)}>
                                    <FontAwesomeIcon icon="redo-alt" size="lg" />
                                </button>
                                <button
                                    className="text-btn"
                                    onClick={e => {
                                        this.props.replaceText(this.props.text);
                                    }}>
                                    Replace Text
                                </button>
                            </>
                        </div>
                        {this.aiWarning()}
                    </>
                )}
            </>
        );
    };

    aiWarning = () =>
        this.done ? (
            <div className="ai-warning">
                <FontAwesomeIcon icon="exclamation-circle" size="sm" style={{ paddingRight: '5px' }} />
                AI generated responses can contain inaccurate or misleading content.
            </div>
        ) : (
            <></>
        );

    heading = (headingText: string) => (
        <div className="summary-heading">
            <label className="summary-text">{headingText}</label>
            {this.props.loading && <ReactLoading type="spin" color="#bcbcbc" width={14} height={14} />}
        </div>
    );

    render() {
        return (
            <div className="summary-box" style={{ display: this.props.visible ? 'flex' : 'none' }}>
                {this.props.mode === GPTPopupMode.SUMMARY ? this.summaryBox() : this.editBox()}
            </div>
        );
    }
}