From 990db3ae50cb475804900ed4aacab295d9145770 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 7 Aug 2023 13:42:00 +0200 Subject: [PATCH] feat: some shit --- components/Card.tsx | 6 ++++- components/icons.tsx | 1 + islands/Search.tsx | 1 - lib/documents.ts | 3 +++ lib/hooks/useEventListener.ts | 1 + lib/typesense.ts | 37 ++++++++++++++++------------- routes/api/movies/enhance/[name].ts | 2 -- routes/api/resources.ts | 10 ++++---- 8 files changed, 36 insertions(+), 25 deletions(-) diff --git a/components/Card.tsx b/components/Card.tsx index d5fc7b2..13389b3 100644 --- a/components/Card.tsx +++ b/components/Card.tsx @@ -1,3 +1,6 @@ +import { isYoutubeLink } from "@lib/string.ts"; +import { IconBrandYoutube } from "@components/icons.tsx"; + export function Card( { link, title, image }: { link?: string; title?: string; image?: string }, ) { @@ -18,7 +21,8 @@ export function Card(
{/* Recipe Card content */}
-
+
+ {isYoutubeLink(link || "") && } {title}
diff --git a/components/icons.tsx b/components/icons.tsx index 21b783b..cd7b5de 100644 --- a/components/icons.tsx +++ b/components/icons.tsx @@ -15,3 +15,4 @@ export { default as IconLogin } from "https://deno.land/x/tabler_icons_tsx@0.0.3 export { default as IconLogout } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/logout.tsx"; export { default as IconSearch } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/search.tsx"; export { default as IconGhost } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/ghost.tsx"; +export { default as IconBrandYoutube } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/brand-youtube.tsx"; diff --git a/islands/Search.tsx b/islands/Search.tsx index 9ea8bda..56de3b9 100644 --- a/islands/Search.tsx +++ b/islands/Search.tsx @@ -136,7 +136,6 @@ const SearchComponent = ( debouncedFetchData(q); }, []); - console.log({ data, isLoading }); return (
{ const documents = await response.json(); cache.setDocuments(documents); + typesense.synchronize(); + return documents; } diff --git a/lib/hooks/useEventListener.ts b/lib/hooks/useEventListener.ts index d3393ef..ba0e1e8 100644 --- a/lib/hooks/useEventListener.ts +++ b/lib/hooks/useEventListener.ts @@ -5,6 +5,7 @@ export function useEventListener( handler: (event: T) => void, element = window, ) { + console.log("Add Eventlistener", { eventName, element, handler }); // Create a ref that stores handler const savedHandler = useRef<(event: Event) => void>(); diff --git a/lib/typesense.ts b/lib/typesense.ts index 4277949..61be77f 100644 --- a/lib/typesense.ts +++ b/lib/typesense.ts @@ -1,9 +1,10 @@ -import { Client } from "https://raw.githubusercontent.com/bradenmacdonald/typesense-deno/main/mod.ts"; +import { Client } from "typesense"; import { TYPESENSE_API_KEY, TYPESENSE_URL } from "@lib/env.ts"; import { getAllMovies } from "@lib/resource/movies.ts"; import { getAllRecipes } from "@lib/resource/recipes.ts"; 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"; const log = createLogger("typesense"); @@ -73,6 +74,7 @@ async function initializeTypesense() { { name: "name", type: "string" }, { name: "type", type: "string", facet: true }, { name: "date", type: "string", optional: true }, + { name: "author", type: "string", facet: true }, { name: "rating", type: "int32", facet: true }, { name: "tags", type: "string[]", facet: true }, { name: "description", type: "string", optional: true }, @@ -111,10 +113,11 @@ async function synchronizeWithTypesense() { description: sanitizeStringForTypesense( resource?.description || resource?.content || "", ), - image: resource?.meta?.image, + author: resource.meta?.author, + image: resource.meta?.image, tags: resource?.tags || [], - rating: resource?.meta?.rating || 0, - date: resource?.meta?.date?.toString() || "", + rating: resource.meta?.rating || 0, + date: resource.meta?.date?.toString() || "", type: resource.type, }; }); @@ -133,21 +136,19 @@ async function synchronizeWithTypesense() { limit_hits: 9999, }); - const documentIds = allTypesenseDocuments.hits?.map((doc) => - doc?.document?.id - ) as string[]; - - // Find deleted document IDs by comparing the Typesense document IDs with the current list of resources - const deletedDocumentIds = documentIds?.filter((id) => - !allResources.some((resource) => resource.id.toString() === id) - ); + const deletedDocumentIds = allTypesenseDocuments.hits + ?.map((doc) => doc?.document?.id) + ?.filter((id) => + // Find deleted document IDs by comparing the Typesense document IDs with the current list of resources + !allResources.some((resource) => resource.id.toString() === id) + ).map((id) => client.collections("resources").documents(id).delete()); // Delete the documents with IDs found in deletedDocumentIds - await Promise.all( - deletedDocumentIds?.map((id) => - client.collections("resources").documents(id).delete() - ), - ); + if (deletedDocumentIds) { + await Promise.all( + deletedDocumentIds, + ); + } log.info("data synchronized"); } catch (error) { @@ -157,3 +158,5 @@ async function synchronizeWithTypesense() { // Call the synchronizeWithTypesense function to trigger the synchronization synchronizeWithTypesense(); + +export const synchronize = debounce(synchronizeWithTypesense, 1000 * 60 * 5); diff --git a/routes/api/movies/enhance/[name].ts b/routes/api/movies/enhance/[name].ts index 5ac7947..7fb0341 100644 --- a/routes/api/movies/enhance/[name].ts +++ b/routes/api/movies/enhance/[name].ts @@ -43,8 +43,6 @@ async function updateMovieMetadata( return root; }); - console.log({ newDoc }); - return createDocument(docId, newDoc); } diff --git a/routes/api/resources.ts b/routes/api/resources.ts index ed0e0a9..d692b8b 100644 --- a/routes/api/resources.ts +++ b/routes/api/resources.ts @@ -2,10 +2,7 @@ import { Handlers } from "$fresh/server.ts"; import { BadRequestError } from "@lib/errors.ts"; import { getTypeSenseClient } from "@lib/typesense.ts"; import { json } from "@lib/helpers.ts"; -import { getArticle } from "@lib/resource/articles.ts"; -import { getMovie } from "@lib/tmdb.ts"; -import { getRecipe } from "@lib/resource/recipes.ts"; -import { getDocument } from "@lib/documents.ts"; +import { extractHashTags } from "@lib/string.ts"; export const handler: Handlers = { async GET(req, _ctx) { @@ -23,6 +20,11 @@ export const handler: Handlers = { filter_by = `type:=${type}`; } + const hashTags = extractHashTags(query); + if (hashTags?.length) { + //filter_by += `tags:=${}` + } + const typesenseClient = await getTypeSenseClient(); if (!typesenseClient) { throw new Error("Query not available");