memorium/islands/KMenu/commands.ts

121 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-07-31 04:19:04 +02:00
import { Menu } from "@islands/KMenu/types.ts";
2023-08-01 17:50:00 +02:00
import { Movie } from "@lib/resource/movies.ts";
2023-07-31 04:19:04 +02:00
import { TMDBMovie } from "@lib/types.ts";
import { fetchStream, isValidUrl } from "@lib/helpers.ts";
2023-07-31 04:19:04 +02:00
export const menus: Record<string, Menu> = {
main: {
title: "Run",
entries: [
{
title: "Close menu",
meta: "",
cb: (state) => {
state.visible.value = false;
},
visible: () => false,
},
2023-08-01 21:35:21 +02:00
{
title: "Create new article",
meta: "",
2023-08-02 13:11:17 +02:00
icon: "IconSquareRoundedPlus",
2023-08-01 21:35:21 +02:00
cb: (state) => {
state.menus["input_link"] = {
title: "Link:",
entries: [],
};
2023-08-02 01:58:03 +02:00
2023-08-01 21:35:21 +02:00
state.activeMenu.value = "input_link";
2023-08-02 01:58:03 +02:00
state.activeState.value = "input";
2023-08-01 21:35:21 +02:00
const unsub = state.commandInput.subscribe((value) => {
2023-08-01 21:35:21 +02:00
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-01 21:35:21 +02:00
}
});
},
visible: () => true,
},
2023-08-01 18:35:35 +02:00
{
title: "Clear Cache",
2023-08-02 13:11:17 +02:00
icon: "IconRefresh",
2023-08-01 18:35:35 +02:00
cb: async (state) => {
state.activeState.value = "loading";
await fetch("/api/cache", {
method: "DELETE",
});
state.activeState.value = "normal";
state.visible.value = false;
},
},
2023-07-31 04:19:04 +02:00
{
title: "Add Movie infos",
meta: "",
2023-08-02 13:11:17 +02:00
icon: "IconReportSearch",
2023-07-31 04:19:04 +02:00
cb: async (state, context) => {
state.activeState.value = "loading";
const movie = context as Movie;
2023-07-31 17:21:17 +02:00
const query = movie.name;
2023-07-31 04:19:04 +02:00
const response = await fetch(
`/api/tmdb/query?q=${encodeURIComponent(query)}`,
);
const json = await response.json() as TMDBMovie[];
const menuID = `result/${movie.name}`;
state.menus[menuID] = {
title: "Select",
entries: json.map((m) => ({
title: `${m.title} released ${m.release_date}`,
cb: async () => {
state.activeState.value = "loading";
2023-08-01 18:35:35 +02:00
await fetch(`/api/movies/enhance/${movie.name}/`, {
2023-07-31 17:21:17 +02:00
method: "POST",
body: JSON.stringify({ tmdbId: m.id }),
});
2023-07-31 04:19:04 +02:00
state.visible.value = false;
state.activeState.value = "normal";
window.location.reload();
2023-07-31 04:19:04 +02:00
},
})),
};
state.activeMenu.value = menuID;
state.activeState.value = "normal";
},
2023-08-01 21:35:21 +02:00
visible: () => {
const loc = globalThis["location"];
return loc?.pathname?.includes("movie");
},
2023-07-31 04:19:04 +02:00
},
{
title: "Reload Page",
meta: "",
cb: () => {
window.location.reload();
},
visible: () => false,
},
],
},
};