From 4e34335e600b5d9d29e8a4af99fda6b2a6d3ba69 Mon Sep 17 00:00:00 2001
From: aidahosa1 <aisosa_idahosa@brown.edu>
Date: Tue, 10 Sep 2024 10:54:29 -0400
Subject: more chat features

---
 .../views/collections/CollectionCardDeckView.tsx   | 221 ++++++++++++---------
 1 file changed, 123 insertions(+), 98 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 52f01e239..5dafb1088 100644
--- a/src/client/views/collections/CollectionCardDeckView.tsx
+++ b/src/client/views/collections/CollectionCardDeckView.tsx
@@ -28,6 +28,8 @@ import { SettingsManager } from '../../util/SettingsManager';
 import { Tooltip } from '@mui/material';
 import { dropActionType } from '../../util/DropActionTypes';
 import { List } from '../../../fields/List';
+import { DocCast } from '../../../fields/Types';
+import { SelectionManager } from '../../util/SelectionManager';
 
 enum cardSortings {
     Time = 'time',
@@ -35,6 +37,7 @@ enum cardSortings {
     Color = 'color',
     Custom = 'custom',
     Chat = 'chat',
+    Tag = 'tag',
     None = '',
 }
 @observer
@@ -91,7 +94,9 @@ export class CollectionCardView extends CollectionSubView() {
     childPairStringListAndUpdateSortDesc = async () => {
         const sortDesc = await this.childPairStringList(); // Await the promise to get the string result
         GPTPopup.Instance.setSortDesc(sortDesc.join());
-        GPTPopup.Instance.onSortComplete = (sortResult: string) => this.processGptOutput(sortResult);
+        GPTPopup.Instance.onSortComplete = (sortResult: string, questionType: string, tag?: string) => this.processGptOutput(sortResult, questionType, tag);
+        GPTPopup.Instance.onQuizRandom = () => this.quizMode()
+
     };
 
     componentDidMount(): void {
@@ -103,9 +108,17 @@ export class CollectionCardView extends CollectionSubView() {
         });
 
         
+        // Reaction to cardSort changes
         this._disposers.sort = reaction(
-            () => ({ cardSort: this.cardSort }),
-            ({ cardSort}) => (cardSort === cardSortings.Chat ? this.openChatPopup() : GPTPopup.Instance.setVisible(false))
+            () => this.cardSort,
+            (cardSort) => {
+                if (cardSort === cardSortings.Chat) {
+                    this.openChatPopup();
+                } else {
+                    GPTPopup.Instance.setVisible(false);
+                }
+                // Force re-render to see if this resolves the sorting issue
+            }
         );
 
        
@@ -121,6 +134,7 @@ export class CollectionCardView extends CollectionSubView() {
     }
 
     @computed get cardSort() {
+        // console.log(StrCast(this.Document.cardSort) as any as cardSortings)
         return StrCast(this.Document.cardSort) as any as cardSortings;
     }
     
@@ -151,6 +165,16 @@ export class CollectionCardView extends CollectionSubView() {
         return regularDocs;
     }
 
+    quizMode () {
+
+        const randomIndex = Math.floor(Math.random() * this.childDocs.length)
+        console.log('hiiiiiii')
+
+        SelectionManager.DeselectAll()
+        DocumentView.SelectView(DocumentView.getDocumentView(this.childDocs[randomIndex]), false)
+
+    }
+
    
 
     /**
@@ -290,13 +314,48 @@ export class CollectionCardView extends CollectionSubView() {
     };
 
 
-     @computed get sortedDocs() {
+    get sortedDocs() {
         // console.log("hi hi hi")
         // console.log(this.layoutDoc.cardSort_isDesc + "layoutdoc desc")
-        console.log(this.cardSort + "card sort")
+        // console.log(this.cardSort + "card sort")
   
-        return this.sort(this.childDocsWithoutLinks, this.cardSort, BoolCast(this.layoutDoc.cardSort_isDesc), this._docDraggedIndex);
+        return this.sort(this.childDocsWithoutLinks, this.cardSort, BoolCast(this.Document.cardSort_isDesc), this._docDraggedIndex);
     }
+
+    tagValue(doc: Doc) {
+        const keys = StrListCast(Doc.UserDoc().myFilterHotKeyTitles);
+    
+        const isTagActive = (buttonID: number) => {
+            return BoolCast(doc[StrCast(Doc.UserDoc()[keys[buttonID]])]);
+        };
+    
+        let base = '';
+        let fraction = '';
+    
+        for (let i = 0; i < keys.length; i++) {
+            if (isTagActive(i)) {
+                if (base === '') {
+                    base = i.toString(); // First active tag becomes the base
+                } else {
+                    fraction += i.toString(); // Subsequent active tags become part of the fraction
+                }
+            }
+        }
+    
+        // If no tag was active, return 0 by default
+        if (base === '') {
+            return 0;
+        }
+    
+        // Construct the final number by appending the fraction if it exists
+        const numberString = fraction ? `${base}.${fraction}` : base;
+    
+        // Convert the result to a number and return
+        return Number(numberString);
+    }
+    
+
+
     /**
      * Called in the sortedDocsType method. Compares the cards' value in regards to the desired sort type-- earlier cards are move to the
      * front, latter cards to the back
@@ -305,7 +364,8 @@ export class CollectionCardView extends CollectionSubView() {
      * @param isDesc
      * @returns
      */
-    sort = (docs: Doc[], sortType: cardSortings, isDesc: boolean, dragIndex: number) => {
+    @action sort = (docs: Doc[], sortType: cardSortings, isDesc: boolean, dragIndex: number) => {
+        // console.log('hiiii')
         // if (sortType === cardSortings.None) return docs;
         
         // if(sortType !== cardSortings.None){
@@ -319,12 +379,13 @@ export class CollectionCardView extends CollectionSubView() {
                     case cardSortings.Color: 
                         return [ClientUtils.hexToHsv(StrCast(docA.backgroundColor)),
                                 ClientUtils.hexToHsv(StrCast(docB.backgroundColor))];
-                    case cardSortings.Custom:
-                        return [CollectionCardView.getButtonGroup(this.cardSort_customField, docA) ?? 9999,
-                                CollectionCardView.getButtonGroup(this.cardSort_customField, docB) ?? 9999];
+                    case cardSortings.Tag:
+                        return [this.tagValue(docA) ?? 9999,
+                                this.tagValue(docB) ?? 9999];
                     case cardSortings.Chat:
-                        return [NumCast(docA.chat) ?? 9999,
-                                NumCast(docB.chat) ?? 9999]
+                        return [NumCast(docA.chatIndex) ?? 9999,
+                                NumCast(docB.chatIndex) ?? 9999]
+                    
 
                     default:
                         return [StrCast(docA.type), StrCast(docB.type)]
@@ -487,12 +548,19 @@ export class CollectionCardView extends CollectionSubView() {
      * Converts the gpt output into a hashmap that can be used for sorting. lists are seperated by ==== while elements within the list are seperated by ~~~~~~
      * @param gptOutput
      */
-    @action processGptOutput = (gptOutput: string) => {
+    @action processGptOutput = (gptOutput: string, questionType: string, tag?: string) => {
 
         console.log("HIIII")
         console.log(StrCast(this.Document.cardSort) + "cardSort")
         // Split the string into individual list items
         const listItems = gptOutput.split('======').filter(item => item.trim() !== '');
+
+
+        if (questionType == '2' || questionType == '4'){
+            this.childDocs.forEach(d => {
+                d['chatFilter'] = false
+            })
+        }
         // console.log(listItems + " LISTT");
     
         // Debug: print the map contents
@@ -509,10 +577,47 @@ export class CollectionCardView extends CollectionSubView() {
             // console.log("ITEM:", normalizedItem);
     
             if (doc) {
-                doc.chat = index;
+                switch (questionType){
+                    case '6': 
+                        doc.chatIndex = index
+                        console.log(index)
+                        break
+                    case '1':
+                        const allHotKeys = StrListCast(Doc.UserDoc().myFilterHotKeyTitles)
+
+                        let myTag = ''
+
+                        if (tag){
+                            for (let i=0; i< allHotKeys.length; i++){
+                                if (tag.includes(allHotKeys[i])){
+                                    myTag = StrCast(Doc.UserDoc()[allHotKeys[i]])
+                                    break
+                                }
+
+                                else if (tag.includes(StrCast(Doc.UserDoc()[allHotKeys[i]]))){
+                                    myTag =  StrCast(Doc.UserDoc()[allHotKeys[i]])
+                                    break
+                                }
+                            }
+
+                            if (myTag != ''){
+                                doc[myTag] = true
+                            }         
+                        }
+                        break
+                    case '2':
+                    case '4':
+                        doc['chatFilter'] = true
+                        Doc.setDocFilter(DocCast(this.Document.embedContainer), 'chatFilter', true, 'match');
+                        break
+
+
+                
+                }
             } else {
                 console.warn(`No matching document found for item: ${normalizedItem}`);
             }
+
         });
 
     }
@@ -528,100 +633,20 @@ export class CollectionCardView extends CollectionSubView() {
         const sortDesc = await this.childPairStringList(); // Await the promise to get the string result
         GPTPopup.Instance.setCardsDoneLoading(true); // Set dataDoneLoading to true after data is loaded
         GPTPopup.Instance.setSortDesc(sortDesc.join());
-        GPTPopup.Instance.onSortComplete = (sortResult: string) => this.processGptOutput(sortResult);
-    };
-
-    /**
-     * Renders the buttons to customize sorting depending on which group the card belongs to and the amount of total groups
-     * @param doc
-     * @param cardSort
-     * @returns
-     */
-    renderButtons = (doc: Doc, cardSort: cardSortings): JSX.Element | null => {
-        // if (cardSort !== cardSortings.Custom) return null;
-
-        const amButtons = 4
-
-        // const amButtons = Math.max(
-        //     4,
-        //     this.childDocs?.reduce((set, d) => {
-        //         if (this.cardSort_customField) {
-        //             set.add(NumCast(d[this.cardSort_customField]));
-        //         }
-        //         return set;
-        //     }, new Set<number>()).size ?? 0
-        // );
-
-        // const activeButtonIndex = CollectionCardView.getButtonGroup(this.cardSort_customField, doc);
-
-        const totalWidth = amButtons * 72 + amButtons * 2 * 5 + 6;
-
-        const iconMap: { [key: number]: any } = {
-            0: 'star',
-            1: 'heart',
-            2: 'cloud',
-            3: 'bolt'
-        };
-
-        return (
-            <div className="card-button-container" style={{ width: `${totalWidth}px`, fontSize: '50px' }}>
-                {numberRange(amButtons).map(i => (
-                    <Tooltip key={i} title={<div className="dash-tooltip">Click to add/remove this card from the {iconMap[i]} group</div>}>
-                        <button type="button" onClick={() => this.toggleButton(doc, iconMap[i] )}>
-                            {this.getButtonIcon(doc, iconMap[i])}
-                        </button>
-                    </Tooltip>
-                ))}
-            </div>
-        );
-    };
-
-      /**
-     * Toggles the buttons between on and off when creating custom sort groupings/changing those created by gpt
-     * @param childPairIndex
-     * @param buttonID
-     * @param doc
-     */
-      toggleButton = undoable((doc: Doc, icon: string) => {
-
-
-
-        // this.cardSort_customField && (doc[this.cardSort_customField] = buttonID);
-
-        // doc.cardSort_activeIcons = new List<string>()
-
-
-        // const list = StrListCast(doc.cardSort_activeIcons);
-        // doc.cardSort_activeIcons = new List<string>(list.includes(icon) ? list.filter(d => d !== icon) : [...list, icon]);
-
-        BoolCast(doc[icon]) ? doc[icon] = false : doc[icon] = true
-
-
-
-        // StrListCast(doc.cardSort_activeIcons).push(iconMap[buttonID])
-    }, 'toggle card tag');
-
-
-    getButtonIcon = (doc: Doc, icon: any): JSX.Element => {
-
-        // const isActive = StrListCast(doc.cardSort_activeIcons).includes(icon)
-        const isActive = doc[icon]
-
-        // console.log(StrListCast(doc.cardSort_activeIcons))
-        const color = isActive ? '#4476f7' : '#323232';
-
-        return <FontAwesomeIcon icon={icon} size="lg" style={{ color }} />;
+        GPTPopup.Instance.onSortComplete = (sortResult: string, questionType: string, tag?: string) => this.processGptOutput(sortResult, questionType, tag);
+        GPTPopup.Instance.onQuizRandom = () => this.quizMode()
     };
 
     /**
      * Actually renders all the cards
      */
     renderCards = () => {
+        // console.log('christ')
         const anySelected = this.childDocs.some(doc => DocumentView.SelectedDocs().includes(doc));
         const isEmpty = this.childDocsWithoutLinks.length === 0;
         const isDesc = BoolCast(this.Document.cardSort_isDesc)
 
-        console.log(this.childDocsWithoutLinks.length + "length")
+        // console.log(this.childDocsWithoutLinks.length + "length")
 
         if (isEmpty) {
             return (
-- 
cgit v1.2.3-70-g09d2

