diff --git a/src/oauth.ts b/src/oauth.ts index 9c71025..fffd4d3 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -1,4 +1,4 @@ -import { LocalStorage, OAuth, getPreferenceValues } from "@raycast/api"; +import { LocalStorage, OAuth, getPreferenceValues,Cache } from "@raycast/api"; import { Preference } from "./types"; import apiServer from "./api"; @@ -44,13 +44,12 @@ const refreshToken = async ( return tokenResponse; }; -export const authorize = async (): Promise => { +export const authorize = async (cache: Cache): Promise => { const { instance } = getPreferenceValues(); const tokenSet = await client.getTokens(); if (tokenSet?.accessToken) { if (tokenSet.refreshToken && tokenSet.isExpired()) { - LocalStorage.clear(); const { client_id, client_secret } = await apiServer.createApp(); await client.setTokens(await refreshToken(client_id, client_secret, tokenSet.refreshToken)); } @@ -67,7 +66,6 @@ export const authorize = async (): Promise => { const { authorizationCode } = await client.authorize(authRequest); await client.setTokens(await requestAccessToken(client_id, client_secret, authRequest, authorizationCode)); - const { fqn, avatar_static } = await apiServer.fetchAccountInfo(); - await LocalStorage.setItem("account-fqn", fqn); - await LocalStorage.setItem("account-avator", avatar_static); + const { fqn } = await apiServer.fetchAccountInfo(); + cache.set("account-fqn", fqn); }; diff --git a/src/simple-status.tsx b/src/simple-status.tsx index dfb2c6a..076b52e 100644 --- a/src/simple-status.tsx +++ b/src/simple-status.tsx @@ -8,7 +8,6 @@ import { Toast, Cache, Icon, - LocalStorage, getPreferenceValues, LaunchProps, } from "@raycast/api"; @@ -30,33 +29,50 @@ 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", + minute: "numeric", + day: "numeric", + month: "long", + weekday: "long", + dayPeriod: "narrow", + }).format(time); +}; + export default function SimpleCommand(props: CommandProps) { const { instance } = getPreferenceValues(); const { draftValues } = props; - const [cw, setCw] = useState(draftValues?.spoiler_text || ""); - const [isMarkdown, setIsMarkdown] = useState(true); - const [sensitive, setSensitive] = useState(false); - const [openActionText, setOpenActionText] = useState("Open the last published status"); - const [fqn, setFqn] = useState(""); - const cached = cache.get("latest_published_status"); - const [statusInfo, setStatusInfo] = useState(cached ? JSON.parse(cached) : null); + const [state, setState] = useState({ + cw: draftValues?.spoiler_text || "", + isMarkdown: true, + sensitive: false, + openActionText: "Open the last published status", + fqn: "", + }); + + const cachedInfo = cache.get("latest_published_status"); + const [statusInfo, setStatusInfo] = useState(cachedInfo ? JSON.parse(cachedInfo) : null); const cwRef = useRef(null); useEffect(() => { - const init = async () => { - authorize(); - const newFqn = await LocalStorage.getItem("account-fqn"); - newFqn ? setFqn(newFqn) : setFqn(""); - }; - init(); + init(cache, (fqn) => setState((prevState) => ({ ...prevState, fqn }))); }, []); const handleSubmit = async ({ spoiler_text, status, scheduled_at, visibility, files, description }: StatusForm) => { try { if (!status && !files) throw new Error("You might forget the content, right ? |・ω・)"); - showToast(Toast.Style.Animated, "Publishing to the Fediverse ... ᕕ( ᐛ )ᕗ"); const mediaIds = await Promise.all( @@ -71,9 +87,9 @@ export default function SimpleCommand(props: CommandProps) { status, scheduled_at, visibility, - content_type: isMarkdown ? "text/markdown" : "text/plain", + content_type: state.isMarkdown ? "text/markdown" : "text/plain", media_ids: mediaIds, - sensitive, + sensitive: state.sensitive, }; const response = await apiServer.postNewStatus(newStatus); @@ -85,9 +101,12 @@ export default function SimpleCommand(props: CommandProps) { } setStatusInfo(response); - setOpenActionText("View the status in Browser"); + setState((prevState) => ({ + ...prevState, + openActionText: "View the status in Browser", + cw: "", + })); cache.set("latest_published_status", JSON.stringify(response)); - setCw(""); setTimeout(() => popToRoot, 2000); } catch (error) { const requestErr = error as AkkomaError; @@ -95,19 +114,11 @@ export default function SimpleCommand(props: CommandProps) { } }; - const labelText = (time: Date) => { - return new Intl.DateTimeFormat("default", { - hour: "numeric", - minute: "numeric", - day: "numeric", - month: "long", - weekday: "long", - dayPeriod: "narrow", - }).format(time); - }; - const handleCw = (value: boolean) => { - setSensitive(value); + setState((prevState) => ({ + ...prevState, + sensitive: value, + })); cwRef.current?.focus(); }; @@ -117,27 +128,34 @@ export default function SimpleCommand(props: CommandProps) { actions={ - {statusInfo && } + {statusInfo && } } > - - {sensitive && ( + + {state.sensitive && ( setState((prevState) => ({ ...prevState, cw: value }))} ref={cwRef} /> )} - + {!props.children && } {props.children} - - + setState((prevState) => ({ ...prevState, isMarkdown: value }))} + storeValue + /> + ); }