diff --git a/src/api.ts b/src/api.ts index 0c18044..fd223b1 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,6 @@ import fetch from "node-fetch"; import { OAuth, getPreferenceValues } from "@raycast/api"; -import { Credentials, Preference, Status ,StatusResponse} from "./types"; +import { Credentials, Preference, Status, StatusResponse } from "./types"; import { client } from "./oauth"; export const fetchToken = async (params: URLSearchParams, errorMessage: string): Promise => { @@ -12,8 +12,7 @@ export const fetchToken = async (params: URLSearchParams, errorMessage: string): }); if (!response.ok) { - console.error(errorMessage, await response.text()); - throw new Error(response.statusText); + throw new Error(errorMessage); } return (await response.json()) as OAuth.TokenResponse; @@ -35,9 +34,7 @@ export const createApp = async (): Promise => { }), }); - if (!response.ok) { - throw new Error("Failed to create Akkoma app"); - } + if (!response.ok) throw new Error("Failed to create Akkoma app"); return (await response.json()) as Credentials; }; @@ -69,9 +66,10 @@ export const postNewStatus = async ({ }), }); - if (!response.ok) { - throw new Error("Failed to pulish new status!"); + if (!response.ok) { + throw new Error("Failed to pulish :("); } + return (await response.json()) as StatusResponse; }; diff --git a/src/components/StatusContent.tsx b/src/components/StatusContent.tsx index 6dbdd1b..3ac8eac 100644 --- a/src/components/StatusContent.tsx +++ b/src/components/StatusContent.tsx @@ -9,21 +9,17 @@ interface statusProps { const StatusContent = ({ isMarkdown, draftStatus }: statusProps) => { const [error, setError] = useState(false); const [statusContent, setStatusContent] = useState(draftStatus || ""); - + return ( <> { - setError(!statusContent.trim()); - }} /> ); diff --git a/src/simple-status.tsx b/src/simple-status.tsx index f35e12f..1fc1f57 100644 --- a/src/simple-status.tsx +++ b/src/simple-status.tsx @@ -1,8 +1,9 @@ import { useEffect, useState } from "react"; import { Form, ActionPanel, Action, showToast, popToRoot, LaunchProps, Toast } from "@raycast/api"; import { postNewStatus } from "./api"; -import { Status } from "./types"; +import { Status, AkkomaError } from "./types"; import { authorize } from "./oauth"; + import VisibilityDropdown from "./components/VisibilityDropdown"; import StatusContent from "./components/statusContent"; @@ -17,17 +18,19 @@ export default function Command(props: LaunchProps<{ draftValues: Partial) => { try { + if (!values.status) throw new Error("You might forget the content, right ? |・ω・)"); + showToast(Toast.Style.Animated, "Publishing to the Fediverse ... ᕕ( ᐛ )ᕗ"); + await postNewStatus({ ...values, content_type: isMarkdown ? "text/markdown" : "text/plain", - }); + }) - setCw(""); - showToast({ title: "Success", message: "Status has been posted!" }); + showToast(Toast.Style.Success, "Status has been published (≧∇≦)/ ! "); popToRoot(); } catch (error) { - console.error(error); - showToast({ title: "Error", message: "Something went wrong!", style: Toast.Style.Failure }); + const requestErr = error as AkkomaError; + showToast(Toast.Style.Failure, "Error", requestErr.message); } }; @@ -40,7 +43,7 @@ export default function Command(props: LaunchProps<{ draftValues: Partial } > - +