2023-08-04 13:48:12 +02:00
|
|
|
import { MenuEntry } from "@islands/KMenu/types.ts";
|
|
|
|
import { fetchStream, isValidUrl } from "@lib/helpers.ts";
|
2023-08-04 22:35:25 +02:00
|
|
|
import { getCookie } from "@lib/string.ts";
|
2023-08-04 13:48:12 +02:00
|
|
|
|
|
|
|
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) => {
|
|
|
|
console.log({ chunk: chunk.split("\n") });
|
|
|
|
if (chunk.startsWith("id:")) {
|
|
|
|
state.loadingText.value = "Finished";
|
|
|
|
setTimeout(() => {
|
|
|
|
window.location.href = "/articles/" +
|
|
|
|
chunk.replace("id:", "").trim();
|
|
|
|
}, 500);
|
|
|
|
} else {
|
|
|
|
state.loadingText.value = chunk;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2023-08-04 22:35:25 +02:00
|
|
|
visible: () => {
|
|
|
|
if (!getCookie("session_cookie")) return false;
|
|
|
|
return true;
|
|
|
|
},
|
2023-08-04 13:48:12 +02:00
|
|
|
};
|