Skip to content

Commit 0a46e24

Browse files
committed
plugin onboardingModal: improve token input form
- Trim whitespace from token from user input - Prevent stale validation results from showing temporarily - Add debug logging in validation process
1 parent 6f922e2 commit 0a46e24

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

plugin/src/ui/onboardingModal/TokenInputForm.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useEffect, useRef, useState } from "react";
33
import { Button, FieldError, Group, Input, Label, TextField } from "react-aria-components";
44

55
import { t } from "@/i18n";
6+
import debug from "@/log";
67
import { TokenValidation } from "@/token";
78
import { TokenValidationIcon } from "@/ui/components/token-validation-icon";
89

@@ -18,12 +19,17 @@ export const TokenInputForm: React.FC<Props> = ({ onTokenSubmit, tester }) => {
1819
kind: "none",
1920
});
2021

22+
const onTokenChange = (newToken: string) => {
23+
setToken(newToken.trim());
24+
};
25+
2126
// The epoch ensures that only the latest validation request is processed.
2227
// The debounce ensures that we don't fire off too many requests.
2328
const validationEpoch = useRef(0);
2429
const debounceTimeout = useRef<NodeJS.Timeout | null>(null);
2530
// When the token changes, queue up a debounced validation attempt.
2631
useEffect(() => {
32+
validationEpoch.current += 1;
2733
setValidationStatus({ kind: "in-progress" });
2834

2935
if (debounceTimeout.current) {
@@ -32,7 +38,13 @@ export const TokenInputForm: React.FC<Props> = ({ onTokenSubmit, tester }) => {
3238

3339
const timeoutId = setTimeout(() => {
3440
const epoch = ++validationEpoch.current;
41+
42+
debug({ msg: "Validating token", context: { token, epoch } });
3543
TokenValidation.validate(token, tester).then((result) => {
44+
debug({
45+
msg: "Token validation result",
46+
context: { token, epochBefore: epoch, epochAfter: validationEpoch.current, result },
47+
});
3648
if (epoch === validationEpoch.current) {
3749
setValidationStatus(result);
3850
}
@@ -51,7 +63,11 @@ export const TokenInputForm: React.FC<Props> = ({ onTokenSubmit, tester }) => {
5163

5264
return (
5365
<div className="todoist-onboarding-token-form">
54-
<TextField value={token} onChange={setToken} isInvalid={validationStatus.kind === "error"}>
66+
<TextField
67+
value={token}
68+
onChange={onTokenChange}
69+
isInvalid={validationStatus.kind === "error"}
70+
>
5571
<Label>{i18n.tokenInputLabel}</Label>
5672
<Group>
5773
<Input />

0 commit comments

Comments
 (0)