From dccb6a6ecf31e72ab4fbd6394a22e09ec8f63859 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 8 Aug 2023 11:03:20 +0200 Subject: [PATCH] feat: some shit --- fresh.gen.ts | 18 ++++---- islands/KMenu/commands/add_series_infos.ts | 52 ++++++++++++++++++++++ islands/Search.tsx | 6 +-- lib/hooks/useEventListener.ts | 2 +- lib/types.ts | 8 ++-- lib/typesense.ts | 11 +++++ routes/api/resources.ts | 3 +- routes/api/tmdb/query.ts | 10 +++-- routes/index.tsx | 4 ++ 9 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 islands/KMenu/commands/add_series_infos.ts diff --git a/fresh.gen.ts b/fresh.gen.ts index 5d3b23e..41b7cc3 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -39,10 +39,11 @@ import * as $$1 from "./islands/IngredientsList.tsx"; import * as $$2 from "./islands/KMenu.tsx"; import * as $$3 from "./islands/KMenu/commands.ts"; import * as $$4 from "./islands/KMenu/commands/add_movie_infos.ts"; -import * as $$5 from "./islands/KMenu/commands/create_article.ts"; -import * as $$6 from "./islands/KMenu/commands/create_movie.ts"; -import * as $$7 from "./islands/KMenu/types.ts"; -import * as $$8 from "./islands/Search.tsx"; +import * as $$5 from "./islands/KMenu/commands/add_series_infos.ts"; +import * as $$6 from "./islands/KMenu/commands/create_article.ts"; +import * as $$7 from "./islands/KMenu/commands/create_movie.ts"; +import * as $$8 from "./islands/KMenu/types.ts"; +import * as $$9 from "./islands/Search.tsx"; const manifest = { routes: { @@ -85,10 +86,11 @@ const manifest = { "./islands/KMenu.tsx": $$2, "./islands/KMenu/commands.ts": $$3, "./islands/KMenu/commands/add_movie_infos.ts": $$4, - "./islands/KMenu/commands/create_article.ts": $$5, - "./islands/KMenu/commands/create_movie.ts": $$6, - "./islands/KMenu/types.ts": $$7, - "./islands/Search.tsx": $$8, + "./islands/KMenu/commands/add_series_infos.ts": $$5, + "./islands/KMenu/commands/create_article.ts": $$6, + "./islands/KMenu/commands/create_movie.ts": $$7, + "./islands/KMenu/types.ts": $$8, + "./islands/Search.tsx": $$9, }, baseUrl: import.meta.url, }; diff --git a/islands/KMenu/commands/add_series_infos.ts b/islands/KMenu/commands/add_series_infos.ts new file mode 100644 index 0000000..58a405b --- /dev/null +++ b/islands/KMenu/commands/add_series_infos.ts @@ -0,0 +1,52 @@ +import { MenuEntry } from "@islands/KMenu/types.ts"; +import { TMDBMovie } from "@lib/types.ts"; +import { getCookie } from "@lib/string.ts"; +import { Series } from "@lib/resource/series.ts"; + +export const addMovieInfos: MenuEntry = { + title: "Add Movie infos", + meta: "", + icon: "IconReportSearch", + cb: async (state, context) => { + state.activeState.value = "loading"; + const movie = context as Series; + + const query = movie.name; + + const response = await fetch( + `/api/tmdb/query?q=${encodeURIComponent(query)}&type=serie`, + ); + + 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"; + await fetch(`/api/movies/enhance/${movie.name}/`, { + method: "POST", + body: JSON.stringify({ tmdbId: m.id }), + }); + state.visible.value = false; + state.activeState.value = "normal"; + window.location.reload(); + }, + })), + }; + + state.activeMenu.value = menuID; + + state.activeState.value = "normal"; + }, + visible: () => { + const loc = globalThis["location"]; + if (!getCookie("session_cookie")) return false; + return (loc?.pathname?.includes("movie") && + !loc.pathname.endsWith("movies")) || + (loc?.pathname?.includes("series") && !loc.pathname.endsWith("series")); + }, +}; diff --git a/islands/Search.tsx b/islands/Search.tsx index 56de3b9..0ae018c 100644 --- a/islands/Search.tsx +++ b/islands/Search.tsx @@ -14,14 +14,13 @@ import { isLocalImage } from "@lib/string.ts"; export const RedirectSearchHandler = () => { useEventListener("keydown", (e: KeyboardEvent) => { if (e?.target?.nodeName == "INPUT") return; - if ( e.key === "?" && window.location.search === "" ) { window.location.href += "?q="; } - }); + }, document?.body); return <>; }; @@ -136,13 +135,14 @@ const SearchComponent = ( debouncedFetchData(q); }, []); + console.log({ data, isLoading }); return (
- {isLoading + {isLoading && searchQuery ? : } ( eventName: string, handler: (event: T) => void, - element = window, + element: Window | HTMLElement = window, ) { console.log("Add Eventlistener", { eventName, element, handler }); // Create a ref that stores handler diff --git a/lib/types.ts b/lib/types.ts index 114104a..b5fb9d8 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -27,8 +27,8 @@ export interface GiteaOauthUser { groups: any; } -export type SearchResult = SearchResponse<{ - id:string; +export type TypesenseDocument = { + id: string; name: string; type: keyof typeof resources; date?: string; @@ -36,4 +36,6 @@ export type SearchResult = SearchResponse<{ tags: string[]; description?: string; image?: string; -}>; +}; + +export type SearchResult = SearchResponse; diff --git a/lib/typesense.ts b/lib/typesense.ts index acccc40..c281200 100644 --- a/lib/typesense.ts +++ b/lib/typesense.ts @@ -6,6 +6,7 @@ import { getAllArticles } from "@lib/resource/articles.ts"; import { createLogger } from "@lib/log.ts"; import { debounce } from "https://deno.land/std@0.193.0/async/mod.ts"; import { getAllSeries } from "@lib/resource/series.ts"; +import { TypesenseDocument } from "@lib/types.ts"; const log = createLogger("typesense"); @@ -94,6 +95,16 @@ async function initializeTypesense() { const init = initializeTypesense(); +export async function createTypesenseDocument(doc: TypesenseDocument) { + const client = await getTypeSenseClient(); + if (!client) return; + + await client.collections("resources").documents().create( + doc, + { action: "upsert" }, + ); +} + async function synchronizeWithTypesense() { await init; try { diff --git a/routes/api/resources.ts b/routes/api/resources.ts index d692b8b..6812d96 100644 --- a/routes/api/resources.ts +++ b/routes/api/resources.ts @@ -12,7 +12,8 @@ export const handler: Handlers = { throw new BadRequestError('Query parameter "q" is required.'); } - const query_by = url.searchParams.get("query_by") || "name,description"; + const query_by = url.searchParams.get("query_by") || + "name,description,author,tags"; let filter_by = ""; const type = url.searchParams.get("type"); diff --git a/routes/api/tmdb/query.ts b/routes/api/tmdb/query.ts index a47dd22..598e931 100644 --- a/routes/api/tmdb/query.ts +++ b/routes/api/tmdb/query.ts @@ -1,5 +1,5 @@ import { HandlerContext, Handlers } from "$fresh/server.ts"; -import { searchMovie } from "@lib/tmdb.ts"; +import { searchMovie, searchTVShow } from "@lib/tmdb.ts"; import * as cache from "@lib/cache/cache.ts"; import { AccessDeniedError, BadRequestError } from "@lib/errors.ts"; import { json } from "@lib/helpers.ts"; @@ -28,7 +28,9 @@ const GET = async ( throw new BadRequestError(); } - const cacheId = `/movie/query/${query}`; + const type = u.searchParams.get("type") || "movie"; + + const cacheId = `/${type}/query/${query}`; const cachedResponse = await cache.get(cacheId); if ( @@ -37,7 +39,9 @@ const GET = async ( return json(cachedResponse.data); } - const res = await searchMovie(query); + const res = type === "movie" + ? await searchMovie(query) + : await searchTVShow(query); cache.set( cacheId, diff --git a/routes/index.tsx b/routes/index.tsx index bb89640..483f2ea 100644 --- a/routes/index.tsx +++ b/routes/index.tsx @@ -3,6 +3,8 @@ import { MainLayout } from "@components/layouts/main.tsx"; import { Card } from "@components/Card.tsx"; import { PageProps } from "$fresh/server.ts"; import { resources } from "@lib/resources.ts"; +import { RedirectSearchHandler } from "@islands/Search.tsx"; +import { KMenu } from "@islands/KMenu.tsx"; export default function Home(props: PageProps) { return ( @@ -10,6 +12,8 @@ export default function Home(props: PageProps) { app + +
{Object.values(resources).map((m) => {