diff --git a/src/oauth.ts b/src/oauth.ts index fffd4d3..327db5f 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -44,7 +44,7 @@ const refreshToken = async ( return tokenResponse; }; -export const authorize = async (cache: Cache): Promise => { +export const authorize = async (): Promise => { const { instance } = getPreferenceValues(); const tokenSet = await client.getTokens(); @@ -66,6 +66,6 @@ export const authorize = async (cache: Cache): Promise => { const { authorizationCode } = await client.authorize(authRequest); await client.setTokens(await requestAccessToken(client_id, client_secret, authRequest, authorizationCode)); - const { fqn } = await apiServer.fetchAccountInfo(); - cache.set("account-fqn", fqn); + const { fqn } = await apiServer.fetchAccountInfo(); + await LocalStorage.setItem("account-fqn", fqn); }; diff --git a/src/simple-status.tsx b/src/simple-status.tsx index 076b52e..7fb3b54 100644 --- a/src/simple-status.tsx +++ b/src/simple-status.tsx @@ -10,6 +10,7 @@ import { Icon, getPreferenceValues, LaunchProps, + LocalStorage, } from "@raycast/api"; import apiServer from "./api"; import { AkkomaError, StatusResponse, Preference, Status } from "./types"; @@ -29,15 +30,6 @@ interface StatusForm extends Status { description?: string; } -const init = async (cache: Cache, setFqn: (fqn: string) => void) => { - try { - await authorize(cache); - setFqn(cache.get("account-fqn") ?? ""); - } catch (error) { - console.error("Error during authorization or fetching account-fqn:", error); - } -}; - const labelText = (time: Date) => { return new Intl.DateTimeFormat("default", { hour: "numeric", @@ -61,13 +53,25 @@ export default function SimpleCommand(props: CommandProps) { fqn: "", }); - const cachedInfo = cache.get("latest_published_status"); - const [statusInfo, setStatusInfo] = useState(cachedInfo ? JSON.parse(cachedInfo) : null); + const cached = cache.get("latest_published_status"); + const [statusInfo, setStatusInfo] = useState(cached ? JSON.parse(cached) : null); const cwRef = useRef(null); useEffect(() => { - init(cache, (fqn) => setState((prevState) => ({ ...prevState, fqn }))); + const init = async () => { + try { + await authorize(); + const newFqn = (await LocalStorage.getItem("account-fqn")) ?? ""; + setState((prevState) => ({ + ...prevState, + fqn: newFqn, + })); + } catch (error) { + console.error("Error during authorization or fetching account-fqn:", error); + } + }; + init(); }, []); const handleSubmit = async ({ spoiler_text, status, scheduled_at, visibility, files, description }: StatusForm) => {