mirror of
https://github.com/Sevichecc/raycast-akkoma-extension.git
synced 2025-04-30 22:49:30 +08:00
feat: open the last publish status in browser
This commit is contained in:
parent
b5816a37e4
commit
955fbd400e
2 changed files with 20 additions and 6 deletions
|
@ -1,17 +1,24 @@
|
||||||
import { useEffect, useState } from "react";
|
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 { postNewStatus } from "./api";
|
||||||
import { Status, AkkomaError } from "./types";
|
import { Status, AkkomaError, StatusResponse } from "./types";
|
||||||
import { authorize } from "./oauth";
|
import { authorize } from "./oauth";
|
||||||
|
|
||||||
import VisibilityDropdown from "./components/VisibilityDropdown";
|
import VisibilityDropdown from "./components/VisibilityDropdown";
|
||||||
import StatusContent from "./components/statusContent";
|
import StatusContent from "./components/statusContent";
|
||||||
|
|
||||||
|
const cache = new Cache();
|
||||||
|
|
||||||
export default function Command(props: LaunchProps<{ draftValues: Partial<Status> }>) {
|
export default function Command(props: LaunchProps<{ draftValues: Partial<Status> }>) {
|
||||||
const { draftValues } = props;
|
const { draftValues } = props;
|
||||||
const [cw, setCw] = useState<string>(draftValues?.spoiler_text || "");
|
const [cw, setCw] = useState<string>(draftValues?.spoiler_text || "");
|
||||||
const [isMarkdown, setIsMarkdown] = useState<boolean>(true);
|
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(() => {
|
useEffect(() => {
|
||||||
authorize();
|
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 ? |・ω・)");
|
if (!values.status) throw new Error("You might forget the content, right ? |・ω・)");
|
||||||
showToast(Toast.Style.Animated, "Publishing to the Fediverse ... ᕕ( ᐛ )ᕗ");
|
showToast(Toast.Style.Animated, "Publishing to the Fediverse ... ᕕ( ᐛ )ᕗ");
|
||||||
|
|
||||||
await postNewStatus({
|
const response = await postNewStatus({
|
||||||
...values,
|
...values,
|
||||||
content_type: isMarkdown ? "text/markdown" : "text/plain",
|
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 (≧∇≦)/ ! ");
|
showToast(Toast.Style.Success, "Status has been published (≧∇≦)/ ! ");
|
||||||
popToRoot();
|
setOpenActionText("Open the status in Browser");
|
||||||
|
setTimeout(() => {
|
||||||
|
popToRoot();
|
||||||
|
}, 1000);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const requestErr = error as AkkomaError;
|
const requestErr = error as AkkomaError;
|
||||||
showToast(Toast.Style.Failure, "Error", requestErr.message);
|
showToast(Toast.Style.Failure, "Error", requestErr.message);
|
||||||
|
@ -39,7 +51,8 @@ export default function Command(props: LaunchProps<{ draftValues: Partial<Status
|
||||||
enableDrafts
|
enableDrafts
|
||||||
actions={
|
actions={
|
||||||
<ActionPanel>
|
<ActionPanel>
|
||||||
<Action.SubmitForm onSubmit={handleSubmit} title="Publish!" />
|
<Action.SubmitForm onSubmit={handleSubmit} title="Publish" icon={Icon.Upload} />
|
||||||
|
<Action.OpenInBrowser url={statusInfo.url} title={openActionText} />
|
||||||
</ActionPanel>
|
</ActionPanel>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
|
|
|
@ -68,4 +68,5 @@ export interface StatusResponse {
|
||||||
create_at: Date;
|
create_at: Date;
|
||||||
content: string;
|
content: string;
|
||||||
application: Application;
|
application: Application;
|
||||||
|
url: string;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue