Skip to content

Commit

Permalink
Fix initial annotations sync
Browse files Browse the repository at this point in the history
  • Loading branch information
phgn0 committed Apr 21, 2022
1 parent 208cf94 commit 6c1e980
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
11 changes: 8 additions & 3 deletions source/common/annotations/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ export async function getUserInfo() {
]);
}

// validate token and save if valid
export async function validateSaveToken(token, forceSave = false) {
const userName = await validateApiToken(token);
if (!userName && !forceSave) {
if (userName === null && !forceSave) {
return false;
}

await saveToken(token, userName);

return userName !== null;
}

export async function saveToken(token: string, userName: string) {
await browser.storage.sync.set({
"hypothesis-api-token": token,
"hypothesis-username": userName,
});

return true;
}

export async function validateApiToken(apiToken) {
Expand Down
11 changes: 2 additions & 9 deletions source/settings-page/HypothesisConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import {
getHypothesisToken,
validateApiToken,
validateSaveToken,
} from "../common/annotations/storage";
import { createRemoteAnnotation } from "../sidebar/common/api";
Expand All @@ -23,12 +22,8 @@ export default function HypothesisConfig() {
return;
}

const tokenValid = (await validateApiToken(token)) !== null;
const tokenValid = await validateSaveToken(token, true);
setTokenValid(tokenValid);

if (tokenValid) {
await validateSaveToken(token, true);
}
}, [token]);

// if this renders, the user has enabled the hypothesis sync
Expand Down Expand Up @@ -71,9 +66,7 @@ export default function HypothesisConfig() {
"flex-grow bg-gray-100 dark:bg-gray-800 shadow-inner py-1 px-2 outline-none rounded border-2 " +
(tokenValid
? "border-green-300 dark:border-green-500"
: token === ""
? "border-red-300 dark:border-red-500"
: "border-white")
: "border-red-300 dark:border-red-500")
}
placeholder="Hypothes.is API token"
spellCheck="false"
Expand Down
11 changes: 10 additions & 1 deletion source/settings-page/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ function OptionsPage({}) {
>
Hypothes.is
</a>{" "}
account
account (
<a
href="https://github.com/lindylearn/unclutter/blob/main/docs/annotations.md"
className="underline"
target="_blank"
rel="noopener noreferrer"
>
why
</a>
)
</FeatureFlagSwitch>
{hypothesisEnabled && <HypothesisConfig />}

Expand Down

0 comments on commit 6c1e980

Please sign in to comment.