aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/ReportManager.tsx
blob: 89c17e42fe74da79eebca5bf08fca04f466dfc57 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { action, computed, observable, runInAction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { ColorState, SketchPicker } from 'react-color';
import { Doc } from '../../fields/Doc';
import { Id } from '../../fields/FieldSymbols';
import { BoolCast, Cast, StrCast } from '../../fields/Types';
import { addStyleSheet, addStyleSheetRule, Utils } from '../../Utils';
import { GoogleAuthenticationManager } from '../apis/GoogleAuthenticationManager';
import { DocServer } from '../DocServer';
import { Networking } from '../Network';
import { MainViewModal } from '../views/MainViewModal';
import { FontIconBox } from '../views/nodes/FontIconBox/FontIconBox';
import { DragManager } from './DragManager';
import { GroupManager } from './GroupManager';
import './SettingsManager.scss';
import './ReportManager.scss';
import { undoBatch } from './UndoManager';
import { Octokit } from "@octokit/core";
import { CheckBox } from '../views/search/CheckBox';
import ReactLoading from 'react-loading';
import ReactMarkdown from 'react-markdown';
import rehypeRaw from 'rehype-raw';
import remarkGfm from 'remark-gfm';
const higflyout = require('@hig/flyout');
export const { anchorPoints } = higflyout;
export const Flyout = higflyout.default;

@observer
export class ReportManager extends React.Component<{}> {
    public static Instance: ReportManager;
    @observable private isOpen = false;

    private octokit: Octokit;

    @observable public issues: any[] = [];
    @action setIssues = action((issues: any[]) => { this.issues = issues; });
    
    // undefined is the default - null is if the user is making an issue
    @observable public selectedIssue: any = undefined;
    @action setSelectedIssue = action((issue: any) => { this.selectedIssue = issue; });

    // only get the open issues
    @observable public shownIssues = this.issues.filter(issue => issue.state === 'open');
    
    public updateIssueSearch = action((query: string = '') => {
        if (query === '') {
            this.shownIssues = this.issues.filter(issue => issue.state === 'open');
            return;
        }
       this.shownIssues = this.issues.filter(issue => issue.title.toLowerCase().includes(query.toLowerCase()));
    });

    constructor(props: {}) {
        super(props);
        ReportManager.Instance = this;

        this.octokit = new Octokit({
            auth: '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'
        });
    }

    public close = action(() => (this.isOpen = false));
    public open = action(() => {
        if (this.issues.length === 0) {
            // load in the issues if not already loaded
            this.getAllIssues()
                .then(issues => {
                    this.setIssues(issues);
                    this.updateIssueSearch();
                })
                .catch(err => console.log(err));
        }
        (this.isOpen = true)
    });

    @observable private bugTitle = '';
    @action setBugTitle = action((title: string) => { this.bugTitle = title; });
    @observable private bugDescription = '';
    @action setBugDescription = action((description: string) => { this.bugDescription = description; });
    @observable private bugType = '';
    @action setBugType = action((type: string) => { this.bugType = type; });
    @observable private bugPriority = '';
    @action setBugPriority = action((priortiy: string) => { this.bugPriority = priortiy; });

    // private toGithub = false;
    // will always be set to true - no alterntive option yet
    private toGithub = true;

    private formatTitle = (title: string, userEmail: string) => `${title} - ${userEmail.replace('@brown.edu', '')}`; 

    public async getAllIssues() : Promise<any[]> {
        const res = await this.octokit.request('GET /repos/{owner}/{repo}/issues', {
            owner: 'brown-dash',
            repo: 'Dash-Web',
        });

        // 200 status means success
        if (res.status === 200) {
            return res.data;
        } else {
            throw new Error('Error getting issues');
        }
    }

    // turns an upload link into a servable link
    // ex: 
    // C: /Users/dash/Documents/GitHub/Dash-Web/src/server/public/files/images/upload_8008dbc4b6424fbff14da7345bb32eb2.png
    // -> http://localhost:1050/files/images/upload_8008dbc4b6424fbff14da7345bb32eb2_l.png
    private fileLinktoServerLink = (fileLink: string) => {
        const serverUrl = 'https://browndash.com/';

        const regex = 'public'
        const publicIndex = fileLink.indexOf(regex) + regex.length;

        const finalUrl = `${serverUrl}${fileLink.substring(publicIndex + 1).replace('.', '_l.')}`;
        return finalUrl;
    }

    public async reportIssue() {
        if (this.bugTitle === '' || this.bugDescription === ''
            || this.bugType === '' || this.bugPriority === '') {
            alert('Please fill out all required fields to report an issue.');
            return;
        }

        if (this.toGithub) {

            const formattedLinks = (this.fileLinks ?? []).map(this.fileLinktoServerLink)
            
            const req = await this.octokit.request('POST /repos/{owner}/{repo}/issues', {
                owner: 'brown-dash',
                repo: 'Dash-Web',
                title: this.formatTitle(this.bugTitle, Doc.CurrentUserEmail),
                body: `${this.bugDescription} \n\nfiles:\n${formattedLinks.join('\n')}`,
                labels: [
                    'from-dash-app',
                    this.bugType,
                    this.bugPriority
                ]
            });

            // 201 status means success
            if (req.status !== 201) {
                alert('Error creating issue on github.');
                // on error, don't close the modal
                return;
            }
        }
        else {
            // if not going to github issues, not sure what to do yet...
        }

        // if we're down here, then we're good to go. reset the fields.
        this.setBugTitle('');
        this.setBugDescription('');
        // this.toGithub = false;
        this.setFileLinks([]);
        this.setBugType('');
        this.setBugPriority('');
        this.close();
    }

    @observable public fileLinks: any = [];
    @action setFileLinks = action((links: any) => { this.fileLinks = links; });

    private getServerPath = (link: any) => { return link.result.accessPaths.agnostic.server }

    private uploadFiles = (input: any) => {
        // keep null while uploading
        this.setFileLinks(null);
        // upload the files to the server
        if (input.files && input.files.length !== 0) {
            const fileArray: File[] = Array.from(input.files);
            (Networking.UploadFilesToServer(fileArray.map(file =>({file})))).then(links => {
                console.log('finshed uploading', links.map(this.getServerPath));
                this.setFileLinks((links ?? []).map(this.getServerPath));
            })
        }
        
    }


    private renderIssue = (issue: any) => {

        const isReportingIssue = issue === null;

        return isReportingIssue ?
            // report issue
            (<div className="settings-content">
                <h3 style={{ 'textDecoration': 'underline'}}>Report an Issue</h3>
                <label>Please leave a title for the bug.</label><br />
                <input type="text" placeholder='title' onChange={(e) => this.setBugTitle(e.target.value)} required/>
                <br />
                <label>Please leave a description for the bug and how it can be recreated.</label>
                <textarea placeholder='description' onChange={(e) => this.setBugDescription(e.target.value)} required/>
                <br />
                {/* {<label>Send to github issues? </label>
                <input type="checkbox" onChange={(e) => this.toGithub = e.target.checked} />
                <br /> } */}

                <label>Please label the issue</label>
                <div className='flex-select'>
                    <select name="bugType" onChange={e => this.bugType = e.target.value}>
                    <option value="" disabled selected>Type</option>
                    <option value="bug">Bug</option>
                    <option value="cosmetic">Poor Design or Cosmetic</option>
                    <option value="documentation">Poor Documentation</option>
                    </select>

                    <select name="bigPriority" onChange={e => this.bugPriority = e.target.value}>
                        <option value="" disabled selected>Priority</option>
                        <option value="priority-low">Low</option>
                        <option value="priority-medium">Medium</option>
                        <option value="priority-high">High</option>
                    </select>
                </div>


                <div>
                    <label>Upload media that shows the bug (optional)</label>
                    <input type="file" name="file" multiple accept='audio/*, video/*, image/*' onChange={e => this.uploadFiles(e.target)}/>
                </div>
                <br />

                <button onClick={() => this.reportIssue()} disabled={this.fileLinks === null} style={{ backgroundColor: this.fileLinks === null ? 'grey' : '' }}>{this.fileLinks === null ? 'Uploading...' : 'Submit'}</button>
            </div>)
            :
            // view issue
            (
            <div className='issue-container'>
                <h5 style={{'textAlign': "left"}}><a href={issue.html_url} target="_blank">Issue #{issue.number}</a></h5>
                <div className='issue-title'>
                    {issue.title}
                </div>
                <ReactMarkdown children={issue.body} className='issue-body' linkTarget={"_blank"} remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeRaw]} />
            </div>
        );
    }

    private showReportIssueScreen = () => {
        this.setSelectedIssue(null);
    }

    private closeReportIssueScreen = () => {
        this.setSelectedIssue(undefined);
    }

    private get reportInterface() {

        const isReportingIssue = this.selectedIssue === null;

        return (
            <div className="settings-interface">
                <div className='issue-list-wrapper'>
                    <h3>Current Issues</h3>
                    <input type="text" placeholder='search issues' onChange={(e => this.updateIssueSearch(e.target.value))}></input><br />
                    {this.issues.length === 0 ? <ReactLoading className='loading-center'/> : this.shownIssues.map(issue => <div className='issue-list' key={issue.number} onClick={() => this.setSelectedIssue(issue)}>{issue.title}</div>)}

                    {/* <div className="settings-user">
                        <button onClick={() => this.getAllIssues().then(issues => this.issues = issues)}>Poll Issues</button>
                    </div> */}
                </div>
                    
                <div className="close-button" onClick={this.close}>
                    <FontAwesomeIcon icon={'times'} color="black" size={'lg'} />
                </div>

                <div className="issue-content" style={{'paddingTop' : this.selectedIssue === undefined ? '50px' : 'inherit'}}>
                    {this.selectedIssue === undefined ? "no issue selected" : this.renderIssue(this.selectedIssue)}
                </div>

                <div className='report-issue-fab'>
                    <span className='report-disclaimer' hidden={!isReportingIssue}>Note: issue reporting is not anonymous.</span>
                    <button
                        onClick={() => isReportingIssue ? this.closeReportIssueScreen() : this.showReportIssueScreen()}
                    >{isReportingIssue ? 'Cancel' : 'Report New Issue'}</button>
                </div>


            </div>
        );
    }

    render() {
        return (
            <MainViewModal
                contents={this.reportInterface}
                isDisplayed={this.isOpen}
                interactive={true}
                closeOnExternalClick={this.close}
                dialogueBoxStyle={{ width: 'auto', height: '500px', background: Cast(Doc.UserDoc().userColor, 'string', null) }}
            />
        );
    }
}