49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { MenuEntry } from "@islands/KMenu/types.ts";
|
|
import { fetchStream, isValidUrl } from "@lib/helpers.ts";
|
|
import { getCookie } from "@lib/string.ts";
|
|
|
|
export const createNewArticle: MenuEntry = {
|
|
title: "Create new article",
|
|
meta: "",
|
|
icon: "IconSquareRoundedPlus",
|
|
cb: (state) => {
|
|
state.menus["input_link"] = {
|
|
title: "Link:",
|
|
entries: [],
|
|
};
|
|
|
|
state.activeMenu.value = "input_link";
|
|
state.activeState.value = "input";
|
|
|
|
const unsub = state.commandInput.subscribe((value) => {
|
|
if (isValidUrl(value)) {
|
|
unsub();
|
|
|
|
state.activeState.value = "loading";
|
|
|
|
fetchStream("/api/articles/create?url=" + value, (chunk) => {
|
|
if (chunk.type === "error") {
|
|
state.activeState.value = "error";
|
|
state.loadingText.value = chunk.message;
|
|
} else if (chunk.type === "finished") {
|
|
state.loadingText.value = "Finished";
|
|
setTimeout(() => {
|
|
globalThis.location.href = "/articles/" + chunk.url;
|
|
}, 500);
|
|
} else {
|
|
state.loadingText.value = chunk.message;
|
|
}
|
|
});
|
|
}
|
|
});
|
|
},
|
|
visible: () => {
|
|
if (!getCookie("session_cookie")) return false;
|
|
if (
|
|
!globalThis?.location?.pathname?.includes("article") &&
|
|
globalThis?.location?.pathname !== "/"
|
|
) return false;
|
|
return true;
|
|
},
|
|
};
|