From c622a1a33fdbca30f5d8b90d308e2737e2640cee Mon Sep 17 00:00:00 2001
From: Wei Shih <willie5588912@gmail.com>
Date: Tue, 28 May 2024 16:35:45 +0800
Subject: [PATCH] Add tryParseUserInfo() to parse both username and premium
 status.

---
 src/leetCodeManager.ts | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts
index 1b2d4af..fc24920 100644
--- a/src/leetCodeManager.ts
+++ b/src/leetCodeManager.ts
@@ -11,7 +11,7 @@ import { createEnvOption } from "./utils/cpUtils";
 import { DialogType, openUrl, promptForOpenOutputChannel } from "./utils/uiUtils";
 import * as wsl from "./utils/wslUtils";
 import { getLeetCodeEndpoint } from "./commands/plugin";
-import { globalState } from "./globalState";
+import { globalState, UserDataType } from "./globalState";
 import { queryUserData } from "./request/query-user-data";
 import { parseQuery, sleep } from "./utils/toolUtils";
 
@@ -31,8 +31,16 @@ class LeetCodeManager extends EventEmitter {
     public async getLoginStatus(): Promise<void> {
         try {
             const result: string = await leetCodeExecutor.getUserInfo();
-            this.currentUser = this.tryParseUserName(result);
+            const userInfo = this.tryParseUserInfo(result);
             this.userStatus = UserStatus.SignedIn;
+            this.currentUser = userInfo.username;
+            const data: UserDataType | undefined = globalState.getUserStatus();
+            if (data) {
+                globalState.setUserStatus({
+                    ...data,
+                    isPremium: userInfo.isPremium,
+                } as UserDataType);
+            }
         } catch (error) {
             this.currentUser = undefined;
             this.userStatus = UserStatus.SignedOut;
@@ -93,14 +101,16 @@ class LeetCodeManager extends EventEmitter {
         return this.currentUser;
     }
 
-    private tryParseUserName(output: string): string {
-        const reg: RegExp = /^\s*.\s*(.+?)\s*https:\/\/leetcode/m;
+    private tryParseUserInfo(output: string): { username: string, isPremium: boolean } {
+        const reg: RegExp = /^\s*([✔✘]?)\s+(.+?)\s*https:\/\/leetcode/m;
         const match: RegExpMatchArray | null = output.match(reg);
-        if (match && match.length === 2) {
-            return match[1].trim();
+        if (match && match.length === 3) {
+            const isPremium = match[1] === '✔';
+            const username = match[2].trim();
+            return { username, isPremium };
         }
 
-        return "Unknown";
+        return { username: "Unknown", isPremium: false };
     }
 
     public getAuthLoginUrl(): string {