feat: add initial command pallete

This commit is contained in:
2023-07-31 04:19:04 +02:00
parent d47ffb94bf
commit e3df1fbd19
12 changed files with 488 additions and 11 deletions

73
islands/KMenu/commands.ts Normal file
View File

@ -0,0 +1,73 @@
import { Menu } from "@islands/KMenu/types.ts";
import { Movie } from "@lib/movies.ts";
import { TMDBMovie } from "@lib/types.ts";
export const menus: Record<string, Menu> = {
main: {
title: "Run",
entries: [
{
title: "Close menu",
meta: "",
cb: (state) => {
state.visible.value = false;
},
visible: () => false,
},
{
title: "Add Movie infos",
meta: "",
cb: async (state, context) => {
state.activeState.value = "loading";
const movie = context as Movie;
let query = movie.name;
// if (movie.meta.author) {
// query += movie.meta.author;
// }
const response = await fetch(
`/api/tmdb/query?q=${encodeURIComponent(query)}`,
);
console.log(response);
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";
const res = await fetch(`/api/tmdb/${m.id}`);
const j = await res.json();
console.log("Selected", { movie, m, j });
state.visible.value = false;
state.activeState.value = "normal";
},
})),
};
console.log({ state });
state.activeMenu.value = menuID;
state.activeState.value = "normal";
},
visible: () => false,
},
{
title: "Reload Page",
meta: "",
cb: () => {
window.location.reload();
},
visible: () => false,
},
],
},
};

21
islands/KMenu/types.ts Normal file
View File

@ -0,0 +1,21 @@
import { Signal } from "@preact/signals";
export type MenuState = {
activeMenu: Signal<string>;
activeState: Signal<"error" | "normal" | "loading">;
visible: Signal<boolean>;
menus: Record<string, Menu>;
};
export type MenuEntry = {
cb: (state: MenuState, context: unknown) => void;
meta?: string;
visible?: boolean | ((context: unknown) => boolean);
title: string;
icon?: string;
};
export type Menu = {
title: string;
entries: MenuEntry[];
};