feat: open the last publish status in browser

This commit is contained in:
sevichecc 2023-04-15 02:08:23 +08:00
parent b5816a37e4
commit 955fbd400e
Signed by untrusted user who does not match committer: SevicheCC
GPG key ID: C577000000000000
2 changed files with 20 additions and 6 deletions

View file

@ -1,17 +1,24 @@
import { useEffect, useState } from "react";
import { Form, ActionPanel, Action, showToast, popToRoot, LaunchProps, Toast } from "@raycast/api";
import { Form, ActionPanel, Action, showToast, popToRoot, LaunchProps, Toast, Cache ,Icon} from "@raycast/api";
import { postNewStatus } from "./api";
import { Status, AkkomaError } from "./types";
import { Status, AkkomaError, StatusResponse } from "./types";
import { authorize } from "./oauth";
import VisibilityDropdown from "./components/VisibilityDropdown";
import StatusContent from "./components/statusContent";
const cache = new Cache();
export default function Command(props: LaunchProps<{ draftValues: Partial<Status> }>) {
const { draftValues } = props;
const [cw, setCw] = useState<string>(draftValues?.spoiler_text || "");
const [isMarkdown, setIsMarkdown] = useState<boolean>(true);
const cached = cache.get("latest-pubished-status");
const [statusInfo, setStatusInfo] = useState<StatusResponse>(cached ? JSON.parse(cached) : "");
const [openActionText, setOpenActionText] = useState<string>("Open the last published status");
useEffect(() => {
authorize();
}, []);
@ -21,13 +28,18 @@ export default function Command(props: LaunchProps<{ draftValues: Partial<Status
if (!values.status) throw new Error("You might forget the content, right ? |・ω・)");
showToast(Toast.Style.Animated, "Publishing to the Fediverse ... ᕕ( ᐛ )ᕗ");
await postNewStatus({
const response = await postNewStatus({
...values,
content_type: isMarkdown ? "text/markdown" : "text/plain",
})
});
setStatusInfo(response);
cache.set("latest-pubished-status", JSON.stringify({ ...response }));
showToast(Toast.Style.Success, "Status has been published (≧∇≦)/ ! ");
popToRoot();
setOpenActionText("Open the status in Browser");
setTimeout(() => {
popToRoot();
}, 1000);
} catch (error) {
const requestErr = error as AkkomaError;
showToast(Toast.Style.Failure, "Error", requestErr.message);
@ -39,7 +51,8 @@ export default function Command(props: LaunchProps<{ draftValues: Partial<Status
enableDrafts
actions={
<ActionPanel>
<Action.SubmitForm onSubmit={handleSubmit} title="Publish!" />
<Action.SubmitForm onSubmit={handleSubmit} title="Publish" icon={Icon.Upload} />
<Action.OpenInBrowser url={statusInfo.url} title={openActionText} />
</ActionPanel>
}
>

View file

@ -68,4 +68,5 @@ export interface StatusResponse {
create_at: Date;
content: string;
application: Application;
url: string;
}