import { ProviderResult , Command , TreeItem , TreeDataProvider , Event , EventEmitter , window , } from "vscode" ; import * as vscode from "vscode" ; import * as path from "path" ; import { getProblemList } from "../service" ; import { login } from "../command/login" ; import { filterInvalidPath } from "../shared" ; export class Interview implements TreeDataProvider < Question > { constructor ( private context ) { } private _onDidChangeTreeData : EventEmitter < any > = new EventEmitter < any > ( ) ; readonly onDidChangeTreeData : Event < any > = this . _onDidChangeTreeData . event ; getTreeItem ( element ) { return element ; } getChildren ( element ? ) { return this . getQuestions ( ) ; } refresh ( ) { this . _onDidChangeTreeData . fire ( undefined ) ; } async getQuestions ( ) { try { let result = await getProblemList ( ) ; let arr = result . data . map ( ( ele , index ) => { filterInvalidPath ; return new Question ( `${ ele . day_id }.${ filterInvalidPath ( ele . name ) }` , ele . publish_date , { command : "interview.openQuestion" , title : "" , arguments : [ ele ] , } , this . context , index === 0 ? true : false , ele . answered ) ; } ) ; if ( ! this . context . globalState . get ( "login" ) ) { vscode . window . showWarningMessage ( "请先点击此处登录" , "登录" ) . then ( ( result ) => { if ( result === "登录" ) { login ( this . context ) ; } } ) ; } return arr ; } catch ( e ) { window . showErrorMessage ( "获取数据失败，请稍后刷新！" ) ; console . log ( e ) ; } } } export class Question extends TreeItem { constructor ( public readonly label , public readonly date , public readonly command ? , public readonly context ? , public readonly isNew = false , public readonly hasComplete = true ) { super ( label ) ; this . tooltip = `${ this . label }` ; this . description = `${ this . date }` ; } public iconPath ? = this . hasComplete ? this . context ?. asAbsolutePath ( path . join ( "media" , "checkcircle.svg" ) ) : this . isNew ? this . context ?. asAbsolutePath ( path . join ( "media" , "new.svg" ) ) : "" ; }