aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/pdf/GPTPopup
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/views/pdf/GPTPopup')
-rw-r--r--src/client/views/pdf/GPTPopup/GPTPopup.scss20
-rw-r--r--src/client/views/pdf/GPTPopup/GPTPopup.tsx200
2 files changed, 154 insertions, 66 deletions
diff --git a/src/client/views/pdf/GPTPopup/GPTPopup.scss b/src/client/views/pdf/GPTPopup/GPTPopup.scss
index c8903e09f..bb43291ee 100644
--- a/src/client/views/pdf/GPTPopup/GPTPopup.scss
+++ b/src/client/views/pdf/GPTPopup/GPTPopup.scss
@@ -9,18 +9,16 @@ $headingHeight: 32px;
.gptPopup-summary-box {
position: fixed;
+ padding-left: 10px;
+ padding-right: 10px;
top: 115px;
left: 75px;
width: 100%;
height: 100%;
top: 0;
left: 0;
- pointer-events: none;
border-top: solid gray 20px;
border-radius: 16px;
- padding: 16px;
- padding-bottom: 0;
- padding-top: 0px;
z-index: 999;
display: flex;
flex-direction: column;
@@ -29,11 +27,13 @@ $headingHeight: 32px;
box-shadow: 0 2px 5px #7474748d;
color: $textgrey;
- .gptPopup-sortBox {
+ .gptPopup-summaryBox-content {
+ padding-right: 16px;
+ padding-left: 16px;
+ position: relative;
+ overflow: hidden;
display: flex;
flex-direction: column;
- height: calc(100% - $inputHeight - $headingHeight);
- pointer-events: all;
}
.summary-heading {
@@ -50,7 +50,6 @@ $headingHeight: 32px;
}
label {
- color: $textgrey;
font-size: 12px;
font-weight: 400;
letter-spacing: 1px;
@@ -65,7 +64,9 @@ $headingHeight: 32px;
.gptPopup-content-wrapper {
padding-top: 10px;
min-height: 50px;
- height: calc(100% - 32px);
+ white-space: pre-line;
+ overflow: auto;
+ margin-bottom: 10px;
}
.inputWrapper {
@@ -73,7 +74,6 @@ $headingHeight: 32px;
justify-content: center;
align-items: center;
height: $inputHeight;
- background-color: white;
width: 100%;
pointer-events: all;
diff --git a/src/client/views/pdf/GPTPopup/GPTPopup.tsx b/src/client/views/pdf/GPTPopup/GPTPopup.tsx
index 4dc45e6a0..c45d8e052 100644
--- a/src/client/views/pdf/GPTPopup/GPTPopup.tsx
+++ b/src/client/views/pdf/GPTPopup/GPTPopup.tsx
@@ -31,6 +31,7 @@ import { OpenWhere } from '../../nodes/OpenWhere';
import { DrawingFillHandler } from '../../smartdraw/DrawingFillHandler';
import { ImageField } from '../../../../fields/URLField';
import { List } from '../../../../fields/List';
+import { ComparisonBox } from '../../nodes/ComparisonBox';
export enum GPTPopupMode {
SUMMARY, // summary of seleted document text
@@ -56,7 +57,7 @@ export class GPTPopup extends ObservableReactComponent<object> {
private _dataJson: string = '';
private _documentDescriptions: Promise<string> | undefined; // a cache of the descriptions of all docs in the selected collection. makes it more efficient when asking GPT multiple questions about the collection.
private _sidebarFieldKey: string = '';
- private _textToSummarize: string = '';
+ private _aiReferenceText: string = '';
private _imageDescription: string = '';
private _textToDocMap = new Map<string, Doc>(); // when GPT answers with a doc's content, this helps us find the Doc
private _addToCollection: ((doc: Doc | Doc[], annotationKey?: string | undefined) => boolean) | undefined;
@@ -79,7 +80,7 @@ export class GPTPopup extends ObservableReactComponent<object> {
};
componentDidUpdate() {
- this._gptProcessing && this.setStopAnimatingResponse(false);
+ //this._gptProcessing && this.setStopAnimatingResponse(false);
}
componentDidMount(): void {
reaction(
@@ -100,14 +101,14 @@ export class GPTPopup extends ObservableReactComponent<object> {
);
}
+ @observable private _showOriginal = true;
+ @observable private _responseText: string = '';
@observable private _conversationArray: string[] = ['Hi! In this pop up, you can ask ChatGPT questions about your documents and filter / sort them. '];
@observable private _fireflyArray: string[] = ['Hi! In this pop up, you can ask Firefly to create images. '];
@observable private _chatEnabled: boolean = false;
@action private setChatEnabled = (start: boolean) => (this._chatEnabled = start);
@observable private _gptProcessing: boolean = false;
@action private setGptProcessing = (loading: boolean) => (this._gptProcessing = loading);
- @observable private _responseText: string = '';
- @action private setResponseText = (text: string) => (this._responseText = text);
@observable private _imgUrls: string[][] = [];
@action private setImgUrls = (imgs: string[][]) => (this._imgUrls = imgs);
@observable private _collectionContext: Doc | undefined = undefined;
@@ -209,7 +210,7 @@ export class GPTPopup extends ObservableReactComponent<object> {
generateFireflyImage = (imgDesc: string) => {
const selView = DocumentView.Selected().lastElement();
const selDoc = selView?.Document;
- if (selDoc && (selView._props.renderDepth > 1 || selDoc[Doc.LayoutFieldKey(selDoc)] instanceof ImageField)) {
+ if (selDoc && (selView._props.renderDepth > 1 || selDoc[Doc.LayoutDataKey(selDoc)] instanceof ImageField)) {
const oldPrompt = StrCast(selDoc.ai_firefly_prompt, StrCast(selDoc.title));
const newPrompt = oldPrompt ? `${oldPrompt} ~~~ ${imgDesc}` : imgDesc;
return DrawingFillHandler.drawingToImage(selDoc, 100, newPrompt, selDoc)
@@ -286,18 +287,30 @@ export class GPTPopup extends ObservableReactComponent<object> {
/**
* Completes an API call to generate a summary of the specified text
*
- * @param text the text to summarizz
+ * @param text the text to summarize
*/
- generateSummary = (text: string) => {
+ private generateSummary = action((text: string) => {
SnappingManager.SetChatVisible(true);
- this._textToSummarize = text;
- this.setMode(GPTPopupMode.SUMMARY);
+ this._showOriginal = false;
this.setGptProcessing(true);
return gptAPICall(text, GPTCallType.SUMMARY)
- .then(res => this.setResponseText(res || 'Something went wrong.'))
+ .then(action(res => (this._responseText = res || 'Something went wrong.')))
.catch(err => console.error(err))
.finally(() => this.setGptProcessing(false));
- };
+ });
+
+ /**
+ * Completes an API call to generate a summary of the specified text
+ *
+ * @param text the text to summarizz
+ */
+ askAIAboutSelection = action((text: string) => {
+ SnappingManager.SetChatVisible(true);
+ this._aiReferenceText = text;
+ this._responseText = '';
+ this._showOriginal = true;
+ this.setMode(GPTPopupMode.SUMMARY);
+ });
/**
* Completes an API call to generate an analysis of
@@ -306,14 +319,16 @@ export class GPTPopup extends ObservableReactComponent<object> {
generateDataAnalysis = () => {
this.setGptProcessing(true);
return gptAPICall(this._dataJson, GPTCallType.DATA, this._dataChatPrompt)
- .then(res => {
- const json = JSON.parse(res! as string);
- const keys = Object.keys(json);
- this._correlatedColumns = [];
- this._correlatedColumns.push(json[keys[0]]);
- this._correlatedColumns.push(json[keys[1]]);
- this.setResponseText(json[keys[2]] || 'Something went wrong.');
- })
+ .then(
+ action(res => {
+ const json = JSON.parse(res! as string);
+ const keys = Object.keys(json);
+ this._correlatedColumns = [];
+ this._correlatedColumns.push(json[keys[0]]);
+ this._correlatedColumns.push(json[keys[1]]);
+ this._responseText = json[keys[2]] || 'Something went wrong.';
+ })
+ )
.catch(err => console.error(err))
.finally(() => this.setGptProcessing(false));
};
@@ -336,6 +351,24 @@ export class GPTPopup extends ObservableReactComponent<object> {
});
}
};
+ /**
+ * Create Flashcards for the selected text
+ */
+ private createFlashcards = action(
+ () =>
+ this.setGptProcessing(true) &&
+ gptAPICall(this._aiReferenceText, GPTCallType.FLASHCARD, undefined, true)
+ .then(res =>
+ ComparisonBox.createFlashcardDeck(res, 250, 200, 'data_front', 'data_back').then(
+ action(newCol => {
+ newCol.zIndex = 1000;
+ DocumentViewInternal.addDocTabFunc(newCol, OpenWhere.addRight);
+ })
+ )
+ )
+ .catch(console.error)
+ .finally(action(() => (this._gptProcessing = false)))
+ );
/**
* Creates a histogram to show the correlation relationship that was found
@@ -378,7 +411,8 @@ export class GPTPopup extends ObservableReactComponent<object> {
tooltip="Ask Firefly to create images"
text="Ask Firefly"
onClick={() => this.setMode(GPTPopupMode.FIREFLY)}
- color={StrCast(Doc.UserDoc().userVariantColor)}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
type={Type.TERT}
style={{
width: '100%',
@@ -393,7 +427,8 @@ export class GPTPopup extends ObservableReactComponent<object> {
tooltip="Ask GPT to sort, tag, define, or filter your Docs!"
text="Ask GPT"
onClick={() => this.setMode(GPTPopupMode.USER_PROMPT)}
- color={StrCast(Doc.UserDoc().userVariantColor)}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
type={Type.TERT}
style={{
width: '100%',
@@ -412,7 +447,8 @@ export class GPTPopup extends ObservableReactComponent<object> {
this.setMode(GPTPopupMode.QUIZ_RESPONSE);
this.onQuizRandom?.();
}}
- color={StrCast(Doc.UserDoc().userVariantColor)}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
type={Type.TERT}
style={{
width: '100%',
@@ -484,6 +520,7 @@ export class GPTPopup extends ObservableReactComponent<object> {
onChange={e => onChange(e.target.value)}
onKeyDown={e => this.handleKeyPress(e, this._mode)}
type="text"
+ style={{ color: SnappingManager.userColor }}
placeholder={placeholder}
/>
<Button //
@@ -491,7 +528,8 @@ export class GPTPopup extends ObservableReactComponent<object> {
type={Type.TERT}
icon={<AiOutlineSend />}
iconPlacement="right"
- color={SnappingManager.userVariantColor}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
onClick={() => this.callGpt(this._mode)}
/>
<DictationButton ref={r => (this._askDictation = r)} setInput={onChange} />
@@ -518,7 +556,7 @@ export class GPTPopup extends ObservableReactComponent<object> {
</div>
</div>
<div key={rawSrc[0] + i + 'btn'} className="btn-container">
- <Button text="Save Image" onClick={() => this.transferToImage(rawSrc[1])} color={StrCast(Doc.UserDoc().userColor)} type={Type.TERT} />
+ <Button text="Save Image" onClick={() => this.transferToImage(rawSrc[1])} color={SettingsManager.userColor} background={SettingsManager.userVariantColor} type={Type.TERT} />
</div>
</>
))}
@@ -528,7 +566,8 @@ export class GPTPopup extends ObservableReactComponent<object> {
tooltip="Generate Again"
onClick={() => this._imgTargetDoc && this.generateImage(this._imageDescription, this._imgTargetDoc, this._addToCollection)}
icon={<FontAwesomeIcon icon="redo-alt" size="lg" />}
- color={StrCast(Doc.UserDoc().userVariantColor)}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
/>
)}
</div>
@@ -536,37 +575,86 @@ export class GPTPopup extends ObservableReactComponent<object> {
summaryBox = () => (
<>
- <div style={{ height: 'calc(100% - 60px)', overflow: 'auto' }}>
- {this.heading('SUMMARY')}
+ <div className="gptPopup-summaryBox-content">
+ <div onClick={action(() => (this._showOriginal = !this._showOriginal))}>{this.heading(this._showOriginal ? 'SELECTION' : 'SUMMARY')}</div>
<div className="gptPopup-content-wrapper">
- {!this._gptProcessing &&
- (!this._stopAnimatingResponse ? (
- <TypeAnimation
- speed={50}
- sequence={[
- this._responseText,
- () => {
- setTimeout(() => this.setStopAnimatingResponse(true), 500);
- },
- ]}
- />
+ {!this._gptProcessing && !this._stopAnimatingResponse && this._responseText ? (
+ <TypeAnimation
+ speed={50}
+ sequence={[
+ this._responseText,
+ () => {
+ setTimeout(() => this.setStopAnimatingResponse(true), 500);
+ },
+ ]}
+ />
+ ) : this._showOriginal ? (
+ this._gptProcessing ? (
+ '...generating cards...'
) : (
- this._responseText
- ))}
+ this._aiReferenceText
+ )
+ ) : (
+ this._responseText || (this._gptProcessing ? '...generating summary...' : '-no ai summary-')
+ )}
</div>
</div>
- {!this._gptProcessing && (
- <div className="btns-wrapper" style={{ position: 'absolute', bottom: 0, width: 'calc(100% - 32px)' }}>
- {this._stopAnimatingResponse ? (
- <>
- <IconButton tooltip="Generate Again" onClick={() => this.generateSummary(this._textToSummarize + ' ')} icon={<FontAwesomeIcon icon="redo-alt" size="lg" />} color={StrCast(SettingsManager.userVariantColor)} />
- <Button tooltip="Transfer to text" text="Transfer To Text" onClick={this.transferToText} color={StrCast(SettingsManager.userVariantColor)} type={Type.TERT} />
- </>
+ {this._gptProcessing ? null : (
+ <div className="btns-wrapper" style={{ position: 'relative', width: 'calc(100% - 32px)' }}>
+ {this._stopAnimatingResponse || !this._responseText ? (
+ <div style={{ display: 'flex' }}>
+ {!this._showOriginal ? (
+ <>
+ <Button
+ tooltip="Show originally selected text" //
+ text="Selection"
+ onClick={action(() => (this._showOriginal = true))}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
+ type={Type.TERT}
+ />
+ <Button
+ tooltip="Create a text Doc with this text and link to the text selection" //
+ text="Transfer To Text"
+ onClick={this.transferToText}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
+ type={Type.TERT}
+ />
+ </>
+ ) : (
+ <>
+ <Button
+ tooltip="Show AI summary of original selection text (Shift+Click to regenerate)"
+ text="Summary"
+ onClick={action(e => {
+ if (e.shiftKey) {
+ this.setStopAnimatingResponse(false);
+ this._aiReferenceText += ' ';
+ this._responseText = '';
+ }
+ this.generateSummary(this._aiReferenceText);
+ })}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
+ type={Type.TERT}
+ />
+ <Button
+ tooltip="Create Flashcards" //
+ text="Create Flashcards"
+ onClick={this.createFlashcards}
+ color={SettingsManager.userColor}
+ background={SettingsManager.userVariantColor}
+ type={Type.TERT}
+ />
+ </>
+ )}
+ </div>
) : (
<div className="summarizing">
- <span>Summarizing</span>
+ <span>{this._showOriginal ? 'Creating Cards...' : 'Summarizing'}</span>
<ReactLoading type="bubbles" color="#bcbcbc" width={20} height={20} />
- <Button text="Stop Animation" onClick={() => this.setStopAnimatingResponse(true)} color={StrCast(SettingsManager.userVariantColor)} type={Type.TERT} />
+ <Button text="Stop Animation" onClick={() => this.setStopAnimatingResponse(true)} color={SettingsManager.userColor} background={SettingsManager.userVariantColor} type={Type.TERT} />
</div>
)}
</div>
@@ -611,19 +699,19 @@ export class GPTPopup extends ObservableReactComponent<object> {
placeholder="Ask GPT a question about the data..."
id="search-input"
className="searchBox-input"
- style={{ width: '100%' }}
+ style={{ width: '100%', color: SnappingManager.userColor }}
/>
) : (
<>
- <Button tooltip="Transfer to text" text="Transfer To Text" onClick={this.transferToText} color={StrCast(SnappingManager.userVariantColor)} type={Type.TERT} />
- <Button tooltip="Chat with AI" text="Chat with AI" onClick={() => this.setChatEnabled(true)} color={StrCast(SnappingManager.userVariantColor)} type={Type.TERT} />
+ <Button tooltip="Transfer to text" text="Transfer To Text" onClick={this.transferToText} color={SettingsManager.userColor} background={SettingsManager.userVariantColor} type={Type.TERT} />
+ <Button tooltip="Chat with AI" text="Chat with AI" onClick={() => this.setChatEnabled(true)} color={SettingsManager.userColor} background={SettingsManager.userVariantColor} type={Type.TERT} />
</>
)
) : (
<div className="summarizing">
<span>Summarizing</span>
<ReactLoading type="bubbles" color="#bcbcbc" width={20} height={20} />
- <Button text="Stop Animation" onClick={() => this.setStopAnimatingResponse(true)} color={StrCast(SnappingManager.userVariantColor)} type={Type.TERT} />
+ <Button text="Stop Animation" onClick={() => this.setStopAnimatingResponse(true)} color={SettingsManager.userColor} background={SettingsManager.userVariantColor} type={Type.TERT} />
</div>
)}
</div>
@@ -640,7 +728,7 @@ export class GPTPopup extends ObservableReactComponent<object> {
);
heading = (headingText: string) => (
- <div className="summary-heading">
+ <div className="summary-heading" style={{ color: SnappingManager.userBackgroundColor }}>
<label className="summary-text">{headingText}</label>
{this._gptProcessing ? (
<ReactLoading type="spin" color="#bcbcbc" width={14} height={14} />
@@ -656,7 +744,7 @@ export class GPTPopup extends ObservableReactComponent<object> {
onClick={() => this._collectionContext && Doc.setDocFilter(this._collectionContext, 'tags', GPTPopup.ChatTag, 'remove')}
/>
{[GPTPopupMode.USER_PROMPT, GPTPopupMode.QUIZ_RESPONSE, GPTPopupMode.FIREFLY].includes(this._mode) && (
- <IconButton color={StrCast(SettingsManager.userVariantColor)} tooltip="back" icon={<CgCornerUpLeft size="16px" />} onClick={() => (this._mode = GPTPopupMode.GPT_MENU)} />
+ <IconButton color={SettingsManager.userVariantColor} background={SettingsManager.userColor} tooltip="back" icon={<CgCornerUpLeft size="16px" />} onClick={() => (this._mode = GPTPopupMode.GPT_MENU)} />
)}
</>
)}
@@ -665,7 +753,7 @@ export class GPTPopup extends ObservableReactComponent<object> {
render() {
return (
- <div className="gptPopup-summary-box" style={{ display: SnappingManager.ChatVisible ? 'flex' : 'none', overflow: 'auto' }}>
+ <div className="gptPopup-summary-box" style={{ background: SnappingManager.userColor, color: SnappingManager.userBackgroundColor, display: SnappingManager.ChatVisible ? 'flex' : 'none', overflow: 'auto' }}>
{(() => {
//prettier-ignore
switch (this._mode) {