aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/collections/FlashcardPracticeUI.tsx
blob: 7bf4d86d18b59840414422b146f6a2df44ce1a6d (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
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Tooltip } from '@mui/material';
import { computed, makeObservable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { returnZero } from '../../../ClientUtils';
import { Doc, DocListCast } from '../../../fields/Doc';
import { BoolCast, NumCast, StrCast } from '../../../fields/Types';
import { Transform } from '../../util/Transform';
import { ObservableReactComponent } from '../ObservableReactComponent';
import { DocumentView, DocumentViewProps } from '../nodes/DocumentView';
import './FlashcardPracticeUI.scss';

enum practiceMode {
    PRACTICE = 'practice',
    QUIZ = 'quiz',
}
enum practiceVal {
    MISSED = 'missed',
    CORRECT = 'correct',
}

interface PracticeUIProps {
    fieldKey: string;
    layoutDoc: Doc;
    filteredChildDocs: () => Doc[];
    allChildDocs: () => Doc[];
    curDoc: () => Doc | undefined;
    advance?: (correct: boolean) => void;
    renderDepth: number;
    sideBtnWidth: number;
    uiBtnScaling: number;
    ScreenToLocalBoxXf: () => Transform;
    docViewProps: () => DocumentViewProps;
    setFilterFunc: (func?: (doc: Doc) => boolean) => void;
    practiceBtnOffset?: number;
}
@observer
export class FlashcardPracticeUI extends ObservableReactComponent<PracticeUIProps> {
    constructor(props: PracticeUIProps) {
        super(props);
        makeObservable(this);
        this._props.setFilterFunc(this.tryFilterOut);
    }

    componentWillUnmount(): void {
        this._props.setFilterFunc(undefined);
    }

    get practiceField() { return this._props.fieldKey + "_practice"; } // prettier-ignore

    @computed get filterDoc() { return DocListCast(Doc.MyContextMenuBtns.data).find(doc => doc.title === 'Filter'); } // prettier-ignore
    @computed get practiceMode() {  return this._props.allChildDocs().some(doc => doc._layout_isFlashcard) ? StrCast(this._props.layoutDoc.practiceMode) : ''; } // prettier-ignore

    btnHeight = () => NumCast(this.filterDoc?.height) * Math.min(1, this._props.ScreenToLocalBoxXf().Scale);
    btnWidth = () => (!this.filterDoc ? 1 : (this.btnHeight() * NumCast(this.filterDoc._width)) / NumCast(this.filterDoc._height));

    /**
     * Sets the practice mode answer style for flashcards
     * @param mode practiceMode or undefined for no practice
     */
    setPracticeMode = (mode: practiceMode | undefined) => {
        this._props.layoutDoc.practiceMode = mode;
        this._props.allChildDocs().map(doc => (doc[this.practiceField] = undefined));
    };

    @computed get emptyMessage() {
        const cardCount = this._props.filteredChildDocs().length;
        const practiceMessage = this.practiceMode && !Doc.hasDocFilter(this._props.layoutDoc, 'tags', Doc.FilterAny) && !cardCount ? 'Finished! Click here to view all flashcards.' : '';
        const filterMessage = practiceMessage
            ? ''
            : Doc.hasDocFilter(this._props.layoutDoc, 'tags', Doc.FilterAny) && !cardCount
              ? 'No tagged items. Click here to view all flash cards.'
              : this.practiceMode && !cardCount
                ? 'No flashcards to show! Click here to leave practice mode'
                : '';
        return !practiceMessage && !filterMessage ? null : (
            <p
                className="FlashcardPracticeUI-message"
                style={{ transform: `scale(${this._props.uiBtnScaling})` }}
                onClick={() => {
                    if (filterMessage || practiceMessage) {
                        this.setPracticeMode(undefined);
                        Doc.setDocFilter(this._props.layoutDoc, 'tags', Doc.FilterAny, 'remove');
                    }
                }}>
                {filterMessage || practiceMessage}
            </p>
        );
    }

    @computed get practiceButtons() {
        /*
         * Sets a flashcard to either missed or correct depending on if they got the question right in practice mode.
         */
        const setPracticeVal = (e: React.MouseEvent, val: string) => {
            e.stopPropagation();
            const curDoc = this._props.curDoc();
            curDoc && (curDoc[this.practiceField] = val);
            this._props.advance?.(val === practiceVal.CORRECT);
        };

        return this.practiceMode == practiceMode.PRACTICE && this._props.curDoc() ? (
            <div className="FlashcardPracticeUI-practice" style={{ transform: `scale(${this._props.uiBtnScaling})`, bottom: `${this._props.practiceBtnOffset ?? this._props.sideBtnWidth}px`, height: `${this._props.sideBtnWidth}px` }}>
                <Tooltip title="Incorrect. View again later.">
                    <div key="remove" className="FlashcardPracticeUI-remove" onClick={e => setPracticeVal(e, practiceVal.MISSED)}>
                        <FontAwesomeIcon icon="xmark" color="red" size="1x" />
                    </div>
                </Tooltip>
                <Tooltip title="Correct">
                    <div key="check" className="FlashcardPracticeUI-check" onClick={e => setPracticeVal(e, practiceVal.CORRECT)}>
                        <FontAwesomeIcon icon="check" color="green" size="1x" />
                    </div>
                </Tooltip>
            </div>
        ) : null;
    }
    @computed get practiceModesMenu() {
        const setColor = (mode: practiceMode) => (StrCast(this.practiceMode) === mode ? 'white' : 'light gray');
        const togglePracticeMode = (mode: practiceMode) => this.setPracticeMode(mode === this.practiceMode ? undefined : mode);

        return !this._props.allChildDocs().some(doc => doc._layout_isFlashcard) ? null : (
            <div
                className="FlashcardPracticeUI-practiceModes"
                style={{
                    transformOrigin: `0px ${-this.btnHeight()}px`,
                    transform: `scale(${Math.max(1, 1 / this._props.ScreenToLocalBoxXf().Scale)})`,
                }}>
                <Tooltip title="Practice flashcards using GPT">
                    <div key="back" className="FlashcardPracticeUI-quiz" style={{ width: this.btnWidth(), height: this.btnHeight() }} onClick={() => togglePracticeMode(practiceMode.QUIZ)}>
                        <FontAwesomeIcon icon="file-pen" color={setColor(practiceMode.QUIZ)} size="sm" />
                    </div>
                </Tooltip>
                <Tooltip title={this.practiceMode === practiceMode.PRACTICE ? 'Exit practice mode' : 'Practice flashcards manually'}>
                    <div key="back" className="FlashcardPracticeUI-practice" style={{ width: this.btnWidth(), height: this.btnHeight() }} onClick={() => togglePracticeMode(practiceMode.PRACTICE)}>
                        <FontAwesomeIcon icon="check" color={setColor(practiceMode.PRACTICE)} size="sm" />
                    </div>
                </Tooltip>
            </div>
        );
    }
    tryFilterOut = (doc: Doc) => (this.practiceMode && BoolCast(doc?._layout_isFlashcard) && doc[this.practiceField] === practiceVal.CORRECT ? true : false); // show only cards that aren't marked as correct
    render() {
        return (
            <>
                {this.emptyMessage}
                {this.practiceButtons}
                <div className="FlashcardPracticeUI-menu" style={{ height: this.btnHeight(), width: this.btnHeight(), transform: `scale(${this._props.uiBtnScaling})` }}>
                    {!this.filterDoc || this._props.layoutDoc._chromeHidden ? null : (
                        <DocumentView
                            {...this._props.docViewProps()}
                            Document={this.filterDoc}
                            TemplateDataDocument={undefined}
                            PanelWidth={this.btnWidth}
                            PanelHeight={this.btnHeight}
                            NativeWidth={returnZero}
                            NativeHeight={returnZero}
                            hideDecorations={BoolCast(this._props.layoutDoc.layout_hideDecorations)}
                            hideCaptions={true}
                            hideFilterStatus={true}
                            renderDepth={this._props.renderDepth + 1}
                            fitWidth={undefined}
                            showTags={false}
                            setContentViewBox={undefined}
                        />
                    )}
                    {this.practiceModesMenu}
                </div>
            </>
        );
    }
}