mirror of
https://github.com/Sevichecc/raycast-akkoma-extension.git
synced 2025-04-30 06:39:30 +08:00
fix fqn could not show
This commit is contained in:
parent
0f1f8a33d1
commit
cd9a3e5d71
4 changed files with 50 additions and 10 deletions
|
@ -3,7 +3,7 @@ import { Action, ActionPanel, List, Toast, showToast, Cache } from "@raycast/api
|
|||
|
||||
import { Status, AkkomaError } from "./utils/types";
|
||||
import apiServer from "./utils/api";
|
||||
import { authorize } from "./utils/oauth";
|
||||
import { getAccessToken } from "./utils/oauth";
|
||||
import { statusParser } from "./utils/util";
|
||||
|
||||
const cache = new Cache();
|
||||
|
@ -16,7 +16,7 @@ export default function BookmarkCommand() {
|
|||
useEffect(() => {
|
||||
const getBookmark = async () => {
|
||||
try {
|
||||
await authorize();
|
||||
await getAccessToken();
|
||||
showToast(Toast.Style.Animated, "Loading bookmarks..☆ミ(o*・ω・)ノ.");
|
||||
const newBookmarks = await apiServer.fetchBookmarks();
|
||||
setBookmarks(newBookmarks);
|
||||
|
|
|
@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
|
|||
import { Action, ActionPanel, List, Toast, showToast, Cache } from "@raycast/api";
|
||||
import { Status, AkkomaError } from "./utils/types";
|
||||
|
||||
import { authorize } from "./utils/oauth";
|
||||
import { getAccessToken } from "./utils/oauth";
|
||||
import apiServer from "./utils/api";
|
||||
import { statusParser } from "./utils/util";
|
||||
|
||||
|
@ -16,7 +16,7 @@ export default function ViewStatusCommand() {
|
|||
useEffect(() => {
|
||||
const getBookmark = async () => {
|
||||
try {
|
||||
await authorize();
|
||||
await getAccessToken();
|
||||
showToast(Toast.Style.Animated, "Loading Status...ε=ε=┌( >_<)┘");
|
||||
const status = await apiServer.fetchUserStatus();
|
||||
setStatus(status);
|
||||
|
|
|
@ -14,7 +14,7 @@ import {
|
|||
} from "@raycast/api";
|
||||
import apiServer from "./utils/api";
|
||||
import { AkkomaError, StatusResponse, Preference, StatusRequest } from "./utils/types";
|
||||
import { authorize } from "./utils/oauth";
|
||||
import { getAccessToken } from "./utils/oauth";
|
||||
import { dateTimeFormatter } from "./utils/util";
|
||||
|
||||
import VisibilityDropdown from "./components/VisibilityDropdown";
|
||||
|
@ -51,11 +51,11 @@ export default function SimpleCommand(props: CommandProps) {
|
|||
useEffect(() => {
|
||||
const init = async () => {
|
||||
try {
|
||||
await authorize();
|
||||
const newFqn = (await LocalStorage.getItem<string>("account-fqn")) ?? "";
|
||||
await getAccessToken();
|
||||
const fqn = await LocalStorage.getItem<string>("account-fqn") || "";
|
||||
setState((prevState) => ({
|
||||
...prevState,
|
||||
fqn: newFqn,
|
||||
fqn: fqn,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error("Error during authorization or fetching account-fqn:", error);
|
||||
|
|
|
@ -44,7 +44,7 @@ const refreshToken = async (
|
|||
return tokenResponse;
|
||||
};
|
||||
|
||||
export const authorize = async (): Promise<void> => {
|
||||
export const authorize = async (): Promise<string> => {
|
||||
const { instance } = getPreferenceValues<Preference>();
|
||||
const tokenSet = await client.getTokens();
|
||||
|
||||
|
@ -53,7 +53,8 @@ export const authorize = async (): Promise<void> => {
|
|||
const { client_id, client_secret } = await apiServer.createApp();
|
||||
await client.setTokens(await refreshToken(client_id, client_secret, tokenSet.refreshToken));
|
||||
}
|
||||
return;
|
||||
const { fqn } = await apiServer.fetchAccountInfo();
|
||||
return fqn;
|
||||
}
|
||||
|
||||
const { client_id, client_secret } = await apiServer.createApp();
|
||||
|
@ -68,4 +69,43 @@ export const authorize = async (): Promise<void> => {
|
|||
|
||||
const { fqn } = await apiServer.fetchAccountInfo();
|
||||
await LocalStorage.setItem("account-fqn", fqn);
|
||||
return fqn;
|
||||
};
|
||||
|
||||
async function getValidTokens(): Promise<OAuth.TokenSet> {
|
||||
const tokenSet = await client.getTokens();
|
||||
|
||||
if (!tokenSet || !tokenSet.accessToken) {
|
||||
const fqn = await authorize();
|
||||
const updatedTokenSet = await client.getTokens();
|
||||
if (updatedTokenSet && updatedTokenSet.accessToken) {
|
||||
await LocalStorage.setItem("account-fqn", fqn);
|
||||
return updatedTokenSet;
|
||||
} else {
|
||||
throw new Error("Failed to get valid access token");
|
||||
}
|
||||
}
|
||||
|
||||
if (tokenSet.refreshToken && tokenSet.isExpired()) {
|
||||
const { client_id, client_secret } = await apiServer.createApp();
|
||||
const refreshedTokens = await refreshToken(client_id, client_secret, tokenSet.refreshToken);
|
||||
await client.setTokens(refreshedTokens);
|
||||
const updatedTokenSet = await client.getTokens();
|
||||
if (updatedTokenSet && updatedTokenSet.accessToken) {
|
||||
return updatedTokenSet;
|
||||
} else {
|
||||
throw new Error("Failed to refresh access token");
|
||||
}
|
||||
}
|
||||
|
||||
return tokenSet;
|
||||
}
|
||||
|
||||
export async function getAccessToken(): Promise<string> {
|
||||
const validTokenSet = await getValidTokens();
|
||||
if (validTokenSet && validTokenSet.accessToken) {
|
||||
return validTokenSet.accessToken;
|
||||
} else {
|
||||
throw new Error("Failed to get valid access token");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue