From 91b478e7bf69c2b4fe52185a0dd35ce8035b534b Mon Sep 17 00:00:00 2001
From: alyssaf16 <alyssa_feinberg@brown.edu>
Date: Mon, 13 Jan 2025 16:52:46 -0500
Subject: commit chatbox

---
 .../nodes/chatbot/chatboxcomponents/ChatBox.tsx    | 108 ++++++++-------------
 1 file changed, 40 insertions(+), 68 deletions(-)

(limited to 'src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx')

diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
index 7859eae01..542d8ea58 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
@@ -16,7 +16,7 @@ import { v4 as uuidv4 } from 'uuid';
 import { ClientUtils } from '../../../../../ClientUtils';
 import { Doc, DocListCast } from '../../../../../fields/Doc';
 import { DocData, DocViews } from '../../../../../fields/DocSymbols';
-import { CsvCast, DocCast, PDFCast, RTFCast, StrCast } from '../../../../../fields/Types';
+import { CsvCast, DocCast, PDFCast, RTFCast, StrCast, NumCast } from '../../../../../fields/Types';
 import { Networking } from '../../../../Network';
 import { DocUtils } from '../../../../documents/DocUtils';
 import { DocumentType } from '../../../../documents/DocumentTypes';
@@ -400,29 +400,17 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
      * @param options Other optional document options (e.g. color)
      * @param id The unique ID for the document.
      */
-
-    // @action
-    // createDocInDash = async (docs: string[]) => {
-    //     console.log('DOCS HERE' + docs);
-    //     docs.forEach(doc => {
-    //         const parsedDoc = JSON.parse(doc);
-    //         this.createIndivDocInDash(parsedDoc.doc_type, parsedDoc.data, parsedDoc.options, '');
-    //     });
-    // };
     @action
-    private createCollectionWithChildren = async (data: any): Promise<Doc[]> => {
-        console.log('Creating collection with nested documents');
-
+    private createCollectionWithChildren = async (data: any, insideCol: boolean): Promise<Doc[]> => {
         // Create an array of promises for each document
         const childDocPromises = data.map(async doc => {
             const parsedDoc = doc;
-            console.log('Parse #3: ' + parsedDoc);
             if (parsedDoc.doc_type !== 'collection') {
                 // Handle non-collection documents
-                return await this.whichDoc(parsedDoc.doc_type, parsedDoc.data, { backgroundColor: parsedDoc.backgroundColor, _width: parsedDoc.width, _height: parsedDoc.height }, parsedDoc.id);
+                return await this.whichDoc(parsedDoc.doc_type, parsedDoc.data, { backgroundColor: parsedDoc.backgroundColor, _width: parsedDoc.width, _height: parsedDoc.height }, parsedDoc.id, insideCol);
             } else {
                 // Recursively process collections
-                const nestedDocs = await this.createCollectionWithChildren(parsedDoc.data);
+                const nestedDocs = await this.createCollectionWithChildren(parsedDoc.data, true);
                 const collectionOptions: DocumentOptions = {
                     title: parsedDoc.title,
                     backgroundColor: parsedDoc.backgroundColor,
@@ -432,16 +420,14 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                     _freeform_backgroundGrid: true,
                 };
                 const collectionDoc = DocCast(Docs.Create.FreeformDocument(nestedDocs, collectionOptions));
-                return collectionDoc; // Return th
+                return collectionDoc;
             }
         });
 
         // Await all child document creations concurrently
         const nestedResults = await Promise.all(childDocPromises);
-        console.log('n' + nestedResults);
         // Flatten any nested arrays from recursive collection calls
         const childDocs = nestedResults.flat() as Doc[];
-        console.log('c' + childDocs);
         childDocs.forEach(doc => {
             console.log(DocCast(doc));
             console.log(DocCast(doc)[DocData].data);
@@ -456,7 +442,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
     // }
 
     @action
-    whichDoc = async (doc_type: string, data: string, options: DocumentOptions, id: string): Promise<Doc> => {
+    whichDoc = async (doc_type: string, data: string, options: DocumentOptions, id: string, insideCol: boolean): Promise<Doc> => {
         let doc;
         switch (doc_type) {
             case 'text':
@@ -472,20 +458,16 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                 doc = DocCast(Docs.Create.ImageDocument(data, options));
                 break;
             case 'equation':
-                // make more advanced
                 doc = DocCast(Docs.Create.EquationDocument(data, options));
                 break;
             case 'noteboard':
-                // COME BACK
                 doc = DocCast(Docs.Create.NoteTakingDocument([], options));
                 break;
             case 'simulation':
-                // make more advanced
                 doc = DocCast(Docs.Create.SimulationDocument(options));
                 break;
             case 'collection': {
-                // COME BACK
-                const arr = await this.createCollectionWithChildren(data);
+                const arr = await this.createCollectionWithChildren(data, true);
                 options._layout_fitWidth = true;
                 options._freeform_backgroundGrid = true;
                 if (options.type_collection == 'tree') {
@@ -513,11 +495,9 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                 doc = this.createComparison(data, options);
                 break;
             case 'diagram':
-                // come back
                 doc = Docs.Create.DiagramDocument(options);
                 break;
             case 'audio':
-                // come back
                 doc = Docs.Create.AudioDocument(data, options);
                 break;
             case 'map':
@@ -553,32 +533,42 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
             default:
                 doc = DocCast(Docs.Create.TextDocument(data, options));
         }
+        doc!.x = NumCast(options.x ?? 0) + (insideCol ? 0 : NumCast(this.layoutDoc.x) + NumCast(this.layoutDoc.width)) + 100;
+        doc!.y = NumCast(options.y) + (insideCol ? 0 : NumCast(this.layoutDoc.y));
         return doc;
     };
 
+    /**
+     * Creates a document in the dashboard.
+     *
+     * @param {string} doc_type - The type of document to create.
+     * @param {string} data - The data used to generate the document.
+     * @param {DocumentOptions} options - Configuration options for the document.
+     * @param {string} id - Unique identifier for the document.
+     * @returns {Promise<void>} A promise that resolves once the document is created and displayed.
+     */
     @action
     createDocInDash = async (doc_type: string, data: string, options: DocumentOptions, id: string) => {
-        console.log('INDIV DOC' + doc_type);
-
         const doc = await this.whichDoc(doc_type, data, options, id);
-
-        console.log('DOC' + doc_type);
         const linkDoc = Docs.Create.LinkDocument(this.Document, doc);
         LinkManager.Instance.addLink(linkDoc);
-
         doc && this._props.addDocument?.(doc);
         await DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
     };
 
+    /**
+     * Creates a deck of flashcards.
+     *
+     * @param {any} data - The data used to generate the flashcards. Can be a string or an object.
+     * @param {DocumentOptions} options - Configuration options for the flashcard deck.
+     * @returns {Doc} A carousel document containing the flashcard deck.
+     */
     @action
     createDeck = (data: any, options: DocumentOptions) => {
         const flashcardDeck: Doc[] = [];
-
         // Parse `data` only if it’s a string
         const deckData = typeof data === 'string' ? JSON.parse(data) : data;
-        console.log('Parsed Deck Data:', deckData);
         const flashcardArray = Array.isArray(deckData) ? deckData : Object.values(deckData);
-        console.log(typeof flashcardArray);
         // Process each flashcard document in the `deckData` array
         if (flashcardArray.length == 2 && flashcardArray[0].doc_type == 'text' && flashcardArray[1].doc_type == 'text') {
             this.createFlashcard(flashcardArray, options);
@@ -599,28 +589,22 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                 _layout_autoHeight: true,
             })
         );
-
         return carouselDoc;
     };
+
+    /**
+     * Creates a single flashcard document.
+     *
+     * @param {any} data - The data used to generate the flashcard. Can be a string or an object.
+     * @param {any} options - Configuration options for the flashcard.
+     * @returns {Doc | undefined} The created flashcard document, or undefined if the flashcard cannot be created.
+     */
     @action
     createFlashcard = (data: any, options: any) => {
-        // const flashcardDeck: Doc[] = [];
-
-        // Process each flashcard item in the data array
-        // const p = JSON.parse(data);
-
         const deckData = typeof data === 'string' ? JSON.parse(data) : data;
         const flashcardArray = Array.isArray(deckData) ? deckData : Object.values(deckData)[2];
-        console.log(typeof flashcardArray);
-
         const [front, back] = flashcardArray;
 
-        // Check that both front and back are text documents
-        console.log('DATA' + data);
-        console.log('front' + front);
-        console.log('back' + back);
-        console.log(front.doc_type);
-        console.log(back.doc_type);
         if (front.doc_type === 'text' && back.doc_type === 'text') {
             const sideOptions: DocumentOptions = {
                 backgroundColor: options.backgroundColor,
@@ -635,36 +619,24 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
             // Create the flashcard document with both sides
             const flashcardDoc = DocCast(Docs.Create.FlashcardDocument(data.title, side1, side2, sideOptions));
             return flashcardDoc;
-            // this._props.addDocument?.(flashcardDoc);
-            // flashcardDeck.push(flashcardDoc);
         }
-
-        // Create a carousel to contain the flashcard deck
-        // const carouselDoc = DocCast(
-        //     Docs.Create.CarouselDocument(flashcardDeck, {
-        //         title: options.title || data.title,
-        //         _width: data.width || 300,
-        //         _height: data.height || 300,
-        //         _layout_fitWidth: false,
-        //         _layout_autoHeight: true,
-        //     })
-        // );
-
-        // return carouselDoc;
     };
 
+    /**
+     * Creates a comparison document.
+     *
+     * @param {any} doc - The document data containing left and right components for comparison.
+     * @param {any} options - Configuration options for the comparison document.
+     * @returns {Doc} The created comparison document.
+     */
     @action
     createComparison = (doc: any, options: any) => {
         const comp = Docs.Create.ComparisonDocument(options.title, { _width: options.width, _height: options.height | 300, backgroundColor: options.backgroundColor });
         const [left, right] = doc;
-        console.log(DocCast(comp.dataDoc));
-        console.log(DocCast(comp[DocData]));
-        console.log(DocCast(comp[DocData].data_back));
         const docLeft = DocCast(Docs.Create.TextDocument(left.data, { backgroundColor: left.backgroundColor, _width: left.width, _height: left.height }));
         const docRight = DocCast(Docs.Create.TextDocument(right.data, { backgroundColor: right.backgroundColor, _width: right.width, _height: right.height }));
         comp[DocData].data_back = docLeft;
         comp[DocData].data_front = docRight;
-
         return comp;
     };
 
-- 
cgit v1.2.3-70-g09d2

