-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathTokenChecker.tsx
More file actions
67 lines (56 loc) · 1.95 KB
/
TokenChecker.tsx
File metadata and controls
67 lines (56 loc) · 1.95 KB
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
import type React from "react";
import { useEffect, useState } from "react";
import { t } from "@/i18n";
import { PluginContext } from "@/ui/context";
import { TodoistApiClient } from "../../api";
import { ObsidianFetcher } from "../../api/fetcher";
import { TokenValidation } from "../../token";
import { TokenValidationIcon } from "../components/token-validation-icon";
import { Setting } from "./SettingItem";
type Props = {
tester: (token: string) => Promise<boolean>;
};
export const TokenChecker: React.FC<Props> = ({ tester }) => {
const plugin = PluginContext.use();
const { token: tokenAccessor, todoist, modals } = plugin.services;
const [tokenState, setTokenState] = useState<TokenValidation.Result>({
kind: "in-progress",
});
const [tokenValidationCount, setTokenValidationCount] = useState(0);
// biome-ignore lint/correctness/useExhaustiveDependencies: we are using tokenValidationCount to force a refresh when the modal is closed.
useEffect(() => {
setTokenState({ kind: "in-progress" });
(async () => {
const token = await tokenAccessor.read();
if (token === null) {
setTokenState({
kind: "error",
message: "API token not found",
});
return;
}
setTokenState(await TokenValidation.validate(token, tester));
})();
}, [plugin, tester, tokenValidationCount]);
const openModal = () => {
modals.onboarding({
onTokenSubmit: async (token) => {
setTokenValidationCount((old) => old + 1);
await tokenAccessor.write(token);
await todoist.initialize(new TodoistApiClient(token, new ObsidianFetcher()));
},
});
};
const buttonLabel = t().settings.general.apiToken.buttonLabel;
return (
<>
<TokenValidationIcon status={tokenState} />
<Setting.ButtonControl
label={buttonLabel}
icon="settings"
onClick={openModal}
disabled={tokenState.kind === "in-progress"}
/>
</>
);
};