import { TreeDataProvider, TreeItem, Command, window, ProviderResult, EventEmitter } from 'vscode'; import * as vscode from 'vscode'; import * as path from "path"; import mock from '../mock'; import { login, checkIsLogin } from '../command/login'; import _ from '../utils/lodash'; import { getTopics, getQuestions } from '../service'; export class Pangfeng implements TreeDataProvider { constructor(private context: vscode.ExtensionContext) { } private _onDidChangeTreeData: EventEmitter = new EventEmitter(); readonly onDidChangeTreeData: vscode.Event = this._onDidChangeTreeData.event; getTreeItem(element: Question | Topic): TreeItem { return element; } getChildren(element?: Question | Topic): ProviderResult { if (element) { const { topic_id, hascomplete, username } = element return this.getQuestions(topic_id, hascomplete, username); } else { return this.getTopic(); } } refresh() { this._onDidChangeTreeData.fire(undefined); } async getQuestions(topic_id: number, topicHasComplete: boolean | number, username: string) { try { const result = await getQuestions(username); const data = _.groupBy(_.get(result, 'data', []), 'topic_id'); const children = data[topic_id]; let arr = children.map((ele: any, index: number) => { return new Question( `${ele.question_id}.${ele.name}`, ele.created_time, vscode.TreeItemCollapsibleState.None, ele.topic_id, username, { command: "panfeng.openQuestion", title: "", arguments: [ele], }, this.context, !topicHasComplete ? index === 0 ? true : false : false, ele.hascomplete, ); }); // await this.handleLogin(); return arr; } catch (e) { // window.showErrorMessage("获取数据失败,请稍后刷新!"); console.log(e); } } async getTopic() { try { const username = await this.handleLogin(); let result = await getTopics(); const data = _.get(result, 'data', []); let arr = data.map((ele: any, index: number) => { return new Topic( ele.topic, ele.created_time.split('T')[0], index === 0 ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed, ele.topic, ele.topic_id, username, { command: "panfeng.openQuestion", title: "", arguments: [ele], }, this.context, index === 0 ? true : false, ele.hascomplete, ); }); return arr; } catch (e) { window.showErrorMessage("获取数据失败,请稍后刷新!"); console.log(e); } } async handleLogin() { try { const { name } = await checkIsLogin(); vscode.window.showInformationMessage('您已登录攀峰😁!'); return name } catch (error) { vscode.window .showWarningMessage("请先点击此处登录", "登录") .then((result) => { if (result === "登录") { login(this.context); } }); } } } export class Question extends TreeItem { constructor( public readonly name: string, public readonly date: string, public readonly collapsibleState: vscode.TreeItemCollapsibleState, public readonly topic_id: number, public readonly username: string, public readonly command?: Command, public readonly context?: vscode.ExtensionContext, public readonly isnew: boolean = false, public readonly hascomplete: boolean = true, public readonly topic?: string, ) { super(name, collapsibleState); this.tooltip = `${this.name}`; 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")) : ""; } export class Topic extends TreeItem { constructor( public readonly name: string, public readonly created_time: string, public readonly collapsibleState: vscode.TreeItemCollapsibleState, public readonly topic: string, public readonly topic_id: number, public readonly username: string, public readonly command?: Command, public readonly context?: vscode.ExtensionContext, public readonly isnew: boolean = false, public readonly hascomplete: boolean = true, ) { super(name, collapsibleState); this.tooltip = `${this.name}`; this.description = `${this.created_time}`; } public iconPath?= this.isnew ? this.context?.asAbsolutePath(path.join("media", "study.svg")) : this.context?.asAbsolutePath(path.join("media", "finish.svg")) }