From 0b32642fc9c2d33810252cd90e45b8b505ef49b2 Mon Sep 17 00:00:00 2001
From: aidahosa1 <aisosa_idahosa@brown.edu>
Date: Sat, 18 May 2024 00:16:31 -0400
Subject: done :)

---
 .../views/collections/CollectionCardDeckView.tsx   | 188 +++++++++++----------
 1 file changed, 99 insertions(+), 89 deletions(-)

(limited to 'src/client/views/collections/CollectionCardDeckView.tsx')

diff --git a/src/client/views/collections/CollectionCardDeckView.tsx b/src/client/views/collections/CollectionCardDeckView.tsx
index 42c82ca74..c719c9e19 100644
--- a/src/client/views/collections/CollectionCardDeckView.tsx
+++ b/src/client/views/collections/CollectionCardDeckView.tsx
@@ -33,7 +33,8 @@ export class CollectionCardView extends CollectionSubView() {
     // documents themselves
 
     @observable hoveredNodeIndex = -1;
-    //index to group
+
+    //key is the index in the child pair list, value is the id# for the group its in
     @observable customGroupDictionary: Map<number, number>[] = [new Map<number, number>(), new Map<number, number>(), new Map<number, number>()];
 
 
@@ -43,9 +44,9 @@ export class CollectionCardView extends CollectionSubView() {
 
         // console.log("Active Groups:", activeGroups);
 
-        for (let i=0; i< activeGroups.length; i++){
-            console.log("Active Groups" + activeGroups[i])
-        }
+        // for (let i=0; i< activeGroups.length; i++){
+        //     console.log("Active Groups" + activeGroups[i])
+        // }
         // console.log("Current Custom Sort Number:", currCustom);
 
         if (activeGroups.length <= 0) {
@@ -121,10 +122,32 @@ export class CollectionCardView extends CollectionSubView() {
         super(props);
         makeObservable(this);
         // this.rotationDegree(7);
+        const pairs = this.childLayoutPairs.filter(d => d.layout.type != DocumentType.LINK);
+
 
-        if (this._props.Document.customHashMap != undefined){
-            this.customGroupDictionary = this.getCustoms(StrListCast(this._props.Document.customHashMap))
+        for (let i=0; i< pairs.length; i++){
+            if (pairs[i].layout.custom1Group != undefined){
+                this.customGroupDictionary[0].set(i, NumCast(pairs[i].layout.custom1Group))
+            }
+
+            if (pairs[i].layout.custom2Group != undefined){
+                this.customGroupDictionary[1].set(i, NumCast(pairs[i].layout.custom2Group))
+            }
+
+            if (pairs[i].layout.custom3Group != undefined){
+                this.customGroupDictionary[2].set(i, NumCast(pairs[i].layout.custom3Group))
+            }
+
+            if (pairs[i].layout.chatGroup != undefined){
+                this.gptGroups.set(pairs[i].layout, NumCast(pairs[i].layout.chatGroup))
+                this.amGPTGroups = NumCast(this._props.Document.chatAmGroups)
+
+            }
         }
+
+        // if (this._props.Document.customHashMap != undefined){
+        //     this.customGroupDictionary = this.convertListsToArrayOfMaps(DocListCast(this._props.Document.customGroup1),DocListCast(this._props.Document.customGroup2), DocListCast(this._props.Document.customGroup3))
+        // }
         // this._props.Document.visibleGroupNumbers = new List<number>([1,2,3,4])
 
 
@@ -139,32 +162,52 @@ export class CollectionCardView extends CollectionSubView() {
         );
     }
 
+    //converts the customGroupDictionary into a Lists of Docs
+    mapToField(groupNumber: number) {
+        const groupList = new List<Doc>();
+    
+        const pairs = this.childLayoutPairs.filter(d => d.layout.type != DocumentType.LINK);
+        console.log(groupNumber+ "group#")
+    
+        this.customGroupDictionary[groupNumber].forEach((group, index) => {
+            const doc = pairs[index].layout;
+            groupList.push(doc);
+        });
+    
+        this._props.Document[`group${groupNumber}`] = groupList;
+    }
 
-    @computed get mapToField(): List<string> {
-        const resultList = new List<string>(); 
-
-        this.customGroupDictionary.forEach(map => {
-            // Convert each map to a single string with entries formatted as "key:value"
-            const mapString = Array.from(map.entries())
-                                   .map(([key, value]) => `${key}:${value}`)
-                                   .join(','); // Join all key-value pairs with commas
-            resultList.push(mapString); // Add the formatted string of the current map to the list
+    // This function converts three Lists of Docs back into an array of maps
+    convertListsToArrayOfMaps = (group0: Doc[], group1: Doc[], group2: Doc[]): Map<number, number>[] => {
+        const mapsArray: Map<number, number>[] = [
+            new Map<number, number>(),
+            new Map<number, number>(),
+            new Map<number, number>()
+        ];
+        const layoutPairs = this.childLayoutPairs.filter(d => d.layout.type != DocumentType.LINK)
+
+        group0.forEach((doc, index) => {
+            const docIndex = layoutPairs.findIndex(pair => pair.layout === doc);
+            if (docIndex !== -1) {
+                mapsArray[0].set(docIndex, 0);
+            }
         });
 
-        return resultList;
-    }
+        group1.forEach((doc, index) => {
+            const docIndex = layoutPairs.findIndex(pair => pair.layout === doc);
+            if (docIndex !== -1) {
+                mapsArray[1].set(docIndex, 1);
+            }
+        });
 
-    getCustoms = (stringFrom: string[]): Map<number, number>[] => {
-        return stringFrom.map(s => {
-            // Create a new Map object for each string.
-            const map = new Map<number, number>();
-            // Split the string by commas to get key-value pairs, then process each pair.
-            s.split(',').forEach(pair => {
-                const [key, value] = pair.split(':');
-                map.set(Number(key), Number(value));
-            });
-            return map;
+        group2.forEach((doc, index) => {
+            const docIndex = layoutPairs.findIndex(pair => pair.layout === doc);
+            if (docIndex !== -1) {
+                mapsArray[2].set(docIndex, 2);
+            }
         });
+
+        return mapsArray;
     }
 
 
@@ -278,17 +321,6 @@ export class CollectionCardView extends CollectionSubView() {
 
 
     @computed get sortedDocsType() {
-        // if (this._props.Document.cardSort === 'chat'){
-        //     this.openChatPopup()
-        //     const textDesc = await this.childPairStringList();
-        //     GPTPopup.Instance.setSortDesc(textDesc)
-        // }
-
-        // if (this._props.Document.cardSort === 'chat' && this.sortedDocs.length === 0) {
-
-        //     this.smartSort(); // Trigger the sorting if it hasn't been done yet
-        //     return { docs: [] }; // Return an empty array or a loading state
-        // }
 
     
         const desc = BoolCast(this.layoutDoc.sortDesc);
@@ -374,9 +406,9 @@ export class CollectionCardView extends CollectionSubView() {
 
                 // console.log(this.customGroupDictionary[NumCast(this._props.Document.customSortNumber)])
                     typeA = this.gptGroups.get(docA.layout) ?? '9999';
-                    console.log(typeA + "typea")
+                    // console.log(typeA + "typea")
                     typeB = this.gptGroups.get(docB.layout) ?? '9999';
-                    console.log(typeB + "typeB")
+                    // console.log(typeB + "typeB")
 
 
                     // console.log(typeA + "A")
@@ -539,35 +571,45 @@ export class CollectionCardView extends CollectionSubView() {
                     onMouseEnter={() => this.setHoveredNodeIndex(index)}>
                     {this.displayDoc(childPair, childScreenToLocal)}
 
-                    {this._props.Document.cardSort == 'custom' ? this.renderButtons(childPairIndex) : ''}
+                    {this._props.Document.cardSort == 'custom' ? this.renderButtons(childPairIndex, childPair.layout, false) : ''}
                     {this._props.Document.cardSort == 'chat' ? this.renderButtons(childPairIndex, childPair.layout, true) : ''}
 
                 </div>
             );
         });
     }
+  
 
-    
-
-   
-    
-    
-
-    @action toggleButton(childPairIndex: number, buttonID: number, isChat = false, doc?: Doc) {
+    @action toggleButton(childPairIndex: number, buttonID: number, isChat = false, doc: Doc) {
         if (!isChat) {
-            this.customGroupDictionary[NumCast(this._props.Document.customSortNumber)].set(childPairIndex, buttonID);
-            this._props.Document.customHashMap = this.mapToField;
+            const sortNumber = NumCast(this._props.Document.customSortNumber)
+            this.customGroupDictionary[sortNumber].set(childPairIndex, buttonID)
+            switch (sortNumber){
+                case 0:
+                    doc.custom1Group = buttonID
+                    break
+                case 1:
+                    doc.custom2Group = buttonID
+                    break
+                case 2:
+                    doc.custom3Group = buttonID
+                    break
+                default:
+                    break
+            }
+            // this.mapToField(buttonID);
         }
     
         if (isChat && doc) {
             this.gptGroups.set(doc, buttonID);
+            doc.chatGroup = buttonID
         }
     
         // console.log(`Toggled button for childPairIndex: ${childPairIndex}, buttonID: ${buttonID}, isChat: ${isChat}`);
         // console.log(`Updated customGroupDictionary:`, this.customGroupDictionary);
-        if (isChat && doc) {
-            console.log(`Updated gptGroups for doc ${doc}:`, this.gptGroups.get(doc));
-        }
+        // if (isChat && doc) {
+        //     // console.log(`Updated gptGroups for doc ${doc}:`, this.gptGroups.get(doc));
+        // }
     }
     
 
@@ -645,39 +687,6 @@ export class CollectionCardView extends CollectionSubView() {
         }
     }
 
-   
-
-    //a set of all the images that have already been given desc
-    //a map from the text to the Doc (for everything)
-    
-
-    // @action async smartSort() {
-    //     this.isLoading = true;
-    //     console.log("loading");
-        
-    //     // Store the result of childPairStringList in a variable
-    //     const childPairStrings = await this.childPairStringList();
-        
-    //     if (childPairStrings === "") {
-    //         console.log("no child pairs :(");
-    //     } else {
-    //         console.log(childPairStrings + " og list");
-    //         let prompt = childPairStrings;
-        
-    //         let res = await gptAPICall(prompt, GPTCallType.SORT);
-    //         this.isLoading = false;
-    //         if (res == 'Error connecting with API.') {
-    //             // If GPT call failed
-    //             console.error('GPT call failed');
-    //         } else if (res != null) {
-    //             console.log(res);
-    //             this.processGptOutput(res);
-    //             // Update the observable with the sorted documents
-    //             this.sortedDocs = this.sort(this.myChildLayoutPairs, 'gpt', BoolCast(this.layoutDoc.sortDesc)).docs;
-    //         }
-    //         this.isLoading = false;
-    //     }
-    // }
 
     gptGroups = new ObservableMap<Doc, number>
     @observable amGPTGroups = 0
@@ -688,6 +697,7 @@ export class CollectionCardView extends CollectionSubView() {
         // Split the string into individual list items
         const listItems = gptOutput.split('======').filter(item => item.trim() !== '');
         this.amGPTGroups = listItems.length
+        this._props.Document.chatAmGroups = this.amGPTGroups
 
         listItems.forEach((item, index) => {
             // Split the item by '~~~~~~' to get all descriptors
@@ -730,7 +740,7 @@ export class CollectionCardView extends CollectionSubView() {
 
     
 
-    renderButtons(childPairIndex: number, doc?: Doc, isChat = false) {
+    renderButtons(childPairIndex: number, doc: Doc, isChat = false) {
         const buttons = []; // Array to hold the button elements
         const groupNumber = NumCast(this._props.Document.customSortNumber);
     
@@ -744,11 +754,11 @@ export class CollectionCardView extends CollectionSubView() {
             activeButtonIndex = this.gptGroups.get(doc);
         }
     
-        console.log("childPairIndex:", childPairIndex, "activeButtonIndex:", activeButtonIndex, "groupNumber:", groupNumber);
+        // console.log("childPairIndex:", childPairIndex, "activeButtonIndex:", activeButtonIndex, "groupNumber:", groupNumber);
     
         for (let i = 0; i < amButtons; i++) {
             const isActive = activeButtonIndex === i;
-            console.log(`Rendering button ${i} for childPairIndex ${childPairIndex} isActive: ${isActive}`);
+            // console.log(`Rendering button ${i} for childPairIndex ${childPairIndex} isActive: ${isActive}`);
             buttons.push(
                 <button
                     key={i}
-- 
cgit v1.2.3-70-g09d2

