Skip to content

Commit bc60926

Browse files
committed
refactor: show notification when empty key
1 parent 763e8a5 commit bc60926

4 files changed

Lines changed: 63 additions & 41 deletions

File tree

src/api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ async function postProof(text: string, apiKey: string): Promise<ApiResponse> {
3333
updateEmoji("$(debug-disconnect)");
3434
throw new Error("NetworkError");
3535
}
36-
console.log("API Key: ", apiKey);
3736
const response = await fetch("https://api.longdo.com/spell-checker/proof", {
3837
method: "POST",
3938
headers: {

src/extension.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import { ErrorsResult } from "./interface/types";
66
import { Diagnostics } from "./diagnostics";
77
import { Configuration } from "./configuration";
88
import { showStatusBar, updateEmoji } from "./statusbar";
9+
import { showErrorAPIKey, showWarningAPIKey } from "./notification";
910

1011
let errorsResult: ErrorsResult[] = [];
1112
let markCheckList: ErrorsResult[] = [];
13+
let checkCountWithDefaultKey = 0;
14+
const DEFAULT_API_KEY = "longdospellcheckervscodedemo";
15+
const NOTIFICATION_THRESHOLD = 3;
1216

1317
export function activate(context: vscode.ExtensionContext) {
1418
showStatusBar(context);
@@ -171,10 +175,6 @@ export function activate(context: vscode.ExtensionContext) {
171175
context.subscriptions.push(listenerDocumentSaved());
172176
}
173177

174-
let checkCountWithDefaultKey = 0;
175-
const DEFAULT_API_KEY = "longdospellcheckervscodedemo";
176-
const NOTIFICATION_THRESHOLD = 3;
177-
178178
async function onSpellCheck() {
179179
errorsResult = [];
180180
Diagnostics.clearDiagnostics();
@@ -192,19 +192,7 @@ async function onSpellCheck() {
192192
checkCountWithDefaultKey++;
193193

194194
if (checkCountWithDefaultKey >= NOTIFICATION_THRESHOLD) {
195-
const action = await vscode.window.showWarningMessage(
196-
"You're using the default API key. For best results, please set your own API key.",
197-
"Set API Key", "Get API Key", "Dismiss"
198-
);
199-
200-
if (action === "Set API Key") {
201-
vscode.commands.executeCommand(Command.OpenSetKey);
202-
return;
203-
} else if (action === "Get API Key") {
204-
vscode.commands.executeCommand(Command.openWebAPI);
205-
return;
206-
}
207-
195+
showWarningAPIKey();
208196
checkCountWithDefaultKey = 0;
209197
}
210198
}
@@ -213,7 +201,14 @@ async function onSpellCheck() {
213201
await textProcessor.processDocument({ document });
214202

215203
try {
216-
let results = await spellCheckPromises(apiKey);
204+
const textData = await textProcessor.getTextData();
205+
if (textData.length === 0) {
206+
return;
207+
}
208+
if (textData.length >= 3 && apiKey === DEFAULT_API_KEY) {
209+
showWarningAPIKey();
210+
}
211+
let results = await spellCheckPromises(apiKey, textData);
217212
results = results.filter(
218213
(error) => !markCheckList.some((mark) => mark.word === error.word)
219214
);
@@ -231,20 +226,9 @@ async function onSpellCheck() {
231226
? error.message
232227
: "An error occurred while checking spelling.";
233228
const isErrorNetwork = errorMessage.includes("NetworkError");
234-
235229
const errorApiKeyEmpty = errorMessage.includes("API key is not set");
236230
if (errorApiKeyEmpty) {
237-
const actionItems = ["Yes", "No", "Get API Key"];
238-
const notification = await vscode.window.showWarningMessage(
239-
"API key is not set. Do you want to set it now?",
240-
...actionItems
241-
);
242-
243-
if (notification === "Yes") {
244-
vscode.commands.executeCommand(Command.OpenSetKey);
245-
} else if (notification === "Get API Key") {
246-
vscode.commands.executeCommand(Command.openWebAPI);
247-
}
231+
showErrorAPIKey();
248232
return;
249233
}
250234

src/notification.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as vscode from "vscode";
2+
import { Command } from "./command";
3+
4+
export async function showWarningAPIKey() {
5+
const action = await vscode.window.showWarningMessage(
6+
"You're using the default API key. For best results, please set your own API key.",
7+
"Set API Key",
8+
"Get API Key",
9+
"Dismiss"
10+
);
11+
12+
if (action === "Set API Key") {
13+
vscode.commands.executeCommand(Command.OpenSetKey);
14+
return;
15+
} else if (action === "Get API Key") {
16+
vscode.commands.executeCommand(Command.openWebAPI);
17+
return;
18+
}
19+
}
20+
21+
export async function showErrorAPIKey() {
22+
const actionItems = ["Yes", "No", "Get API Key"];
23+
const notification = await vscode.window.showWarningMessage(
24+
"API key is not set. Do you want to set it now?",
25+
...actionItems
26+
);
27+
28+
if (notification === "Yes") {
29+
vscode.commands.executeCommand(Command.OpenSetKey);
30+
} else if (notification === "Get API Key") {
31+
vscode.commands.executeCommand(Command.openWebAPI);
32+
}
33+
}

src/spell.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { postProof } from "./api";
2-
import { Position, ProofResponse } from "./interface/types";
2+
import { Position, ProofResponse, TextIndex } from "./interface/types";
33
import { textProcessor } from "./text";
44

55
type SpellCheckResult = ProofResponse & { originalPosition: Position };
@@ -8,27 +8,33 @@ type SpellCheckResult = ProofResponse & { originalPosition: Position };
88
* Performs spell checking on all processed text segments
99
* @returns Promise resolving to an array of spell check results with their original positions
1010
*/
11-
export async function spellCheckPromises(apiKey: string): Promise<SpellCheckResult[]> {
11+
export async function spellCheckPromises(
12+
apiKey: string,
13+
textData: { text: string; indices: TextIndex[] }[] = []
14+
): Promise<SpellCheckResult[]> {
1215
try {
13-
const textData = textProcessor.getTextData();
14-
1516
const spellCheckPromises = textData.map(async (data) => {
16-
const spell = await postProof(data.text,apiKey );
17+
const spell = await postProof(data.text, apiKey);
1718
const results = spell?.result || [];
1819

1920
return results
2021
.map((item: ProofResponse) => ({
2122
...item,
22-
originalPosition: textProcessor.findOriginalPosition(item.index, data.indices),
23+
originalPosition: textProcessor.findOriginalPosition(
24+
item.index,
25+
data.indices
26+
),
2327
}))
24-
.filter((result: any): result is SpellCheckResult =>
25-
result.originalPosition !== undefined && result.originalPosition !== null
28+
.filter(
29+
(result: any): result is SpellCheckResult =>
30+
result.originalPosition !== undefined &&
31+
result.originalPosition !== null
2632
);
2733
});
28-
34+
2935
const allResults = await Promise.all(spellCheckPromises);
3036
return allResults.flat();
3137
} catch (error) {
32-
throw error;
38+
throw error;
3339
}
3440
}

0 commit comments

Comments
 (0)