memorium/islands/KMenu/commands/create_recipe.ts

47 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2025-01-18 00:46:05 +01:00
import { MenuEntry } from "@islands/KMenu/types.ts";
import { fetchStream, isValidUrl } from "@lib/helpers.ts";
import { getCookie } from "@lib/string.ts";
export const createNewRecipe: MenuEntry = {
title: "Create new recipe",
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/recipes/create?url=" + value, (chunk) => {
if (chunk.startsWith("id:")) {
state.loadingText.value = "Finished";
setTimeout(() => {
globalThis.location.href = "/recipes/" +
chunk.replace("id:", "").trim();
}, 500);
} else {
state.loadingText.value = chunk;
}
});
}
});
},
visible: () => {
if (!getCookie("session_cookie")) return false;
if (
!globalThis?.location?.pathname?.includes("recipes") &&
globalThis?.location?.pathname !== "/"
) return false;
return true;
},
};