feat: some shit

This commit is contained in:
max_richter 2023-08-07 13:42:00 +02:00
parent aff3019768
commit 990db3ae50
8 changed files with 36 additions and 25 deletions

View File

@ -1,3 +1,6 @@
import { isYoutubeLink } from "@lib/string.ts";
import { IconBrandYoutube } from "@components/icons.tsx";
export function Card( export function Card(
{ link, title, image }: { link?: string; title?: string; image?: string }, { link, title, image }: { link?: string; title?: string; image?: string },
) { ) {
@ -18,7 +21,8 @@ export function Card(
<div> <div>
{/* Recipe Card content */} {/* Recipe Card content */}
</div> </div>
<div class="mt-2"> <div class="mt-2 flex items-center gap-2">
{isYoutubeLink(link || "") && <IconBrandYoutube />}
{title} {title}
</div> </div>
</div> </div>

View File

@ -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 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 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 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";

View File

@ -136,7 +136,6 @@ const SearchComponent = (
debouncedFetchData(q); debouncedFetchData(q);
}, []); }, []);
console.log({ data, isLoading });
return ( return (
<div class="mt-2"> <div class="mt-2">
<div <div

View File

@ -12,6 +12,7 @@ import * as cache from "@lib/cache/documents.ts";
import { SILVERBULLET_SERVER } from "@lib/env.ts"; import { SILVERBULLET_SERVER } from "@lib/env.ts";
import { fixRenderedMarkdown } from "@lib/helpers.ts"; import { fixRenderedMarkdown } from "@lib/helpers.ts";
import { createLogger } from "@lib/log.ts"; import { createLogger } from "@lib/log.ts";
import * as typesense from "@lib/typesense.ts";
export type Document = { export type Document = {
name: string; name: string;
@ -37,6 +38,8 @@ export async function getDocuments(): Promise<Document[]> {
const documents = await response.json(); const documents = await response.json();
cache.setDocuments(documents); cache.setDocuments(documents);
typesense.synchronize();
return documents; return documents;
} }

View File

@ -5,6 +5,7 @@ export function useEventListener<T extends Event>(
handler: (event: T) => void, handler: (event: T) => void,
element = window, element = window,
) { ) {
console.log("Add Eventlistener", { eventName, element, handler });
// Create a ref that stores handler // Create a ref that stores handler
const savedHandler = useRef<(event: Event) => void>(); const savedHandler = useRef<(event: Event) => void>();

View File

@ -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 { TYPESENSE_API_KEY, TYPESENSE_URL } from "@lib/env.ts";
import { getAllMovies } from "@lib/resource/movies.ts"; import { getAllMovies } from "@lib/resource/movies.ts";
import { getAllRecipes } from "@lib/resource/recipes.ts"; import { getAllRecipes } from "@lib/resource/recipes.ts";
import { getAllArticles } from "@lib/resource/articles.ts"; import { getAllArticles } from "@lib/resource/articles.ts";
import { createLogger } from "@lib/log.ts"; import { createLogger } from "@lib/log.ts";
import { debounce } from "https://deno.land/std@0.193.0/async/mod.ts";
const log = createLogger("typesense"); const log = createLogger("typesense");
@ -73,6 +74,7 @@ async function initializeTypesense() {
{ name: "name", type: "string" }, { name: "name", type: "string" },
{ name: "type", type: "string", facet: true }, { name: "type", type: "string", facet: true },
{ name: "date", type: "string", optional: true }, { name: "date", type: "string", optional: true },
{ name: "author", type: "string", facet: true },
{ name: "rating", type: "int32", facet: true }, { name: "rating", type: "int32", facet: true },
{ name: "tags", type: "string[]", facet: true }, { name: "tags", type: "string[]", facet: true },
{ name: "description", type: "string", optional: true }, { name: "description", type: "string", optional: true },
@ -111,10 +113,11 @@ async function synchronizeWithTypesense() {
description: sanitizeStringForTypesense( description: sanitizeStringForTypesense(
resource?.description || resource?.content || "", resource?.description || resource?.content || "",
), ),
image: resource?.meta?.image, author: resource.meta?.author,
image: resource.meta?.image,
tags: resource?.tags || [], tags: resource?.tags || [],
rating: resource?.meta?.rating || 0, rating: resource.meta?.rating || 0,
date: resource?.meta?.date?.toString() || "", date: resource.meta?.date?.toString() || "",
type: resource.type, type: resource.type,
}; };
}); });
@ -133,21 +136,19 @@ async function synchronizeWithTypesense() {
limit_hits: 9999, limit_hits: 9999,
}); });
const documentIds = allTypesenseDocuments.hits?.map((doc) => const deletedDocumentIds = allTypesenseDocuments.hits
doc?.document?.id ?.map((doc) => doc?.document?.id)
) as string[]; ?.filter((id) =>
// Find deleted document IDs by comparing the Typesense document IDs with the current list of resources
// Find deleted document IDs by comparing the Typesense document IDs with the current list of resources !allResources.some((resource) => resource.id.toString() === id)
const deletedDocumentIds = documentIds?.filter((id) => ).map((id) => client.collections("resources").documents(id).delete());
!allResources.some((resource) => resource.id.toString() === id)
);
// Delete the documents with IDs found in deletedDocumentIds // Delete the documents with IDs found in deletedDocumentIds
await Promise.all( if (deletedDocumentIds) {
deletedDocumentIds?.map((id) => await Promise.all(
client.collections("resources").documents(id).delete() deletedDocumentIds,
), );
); }
log.info("data synchronized"); log.info("data synchronized");
} catch (error) { } catch (error) {
@ -157,3 +158,5 @@ async function synchronizeWithTypesense() {
// Call the synchronizeWithTypesense function to trigger the synchronization // Call the synchronizeWithTypesense function to trigger the synchronization
synchronizeWithTypesense(); synchronizeWithTypesense();
export const synchronize = debounce(synchronizeWithTypesense, 1000 * 60 * 5);

View File

@ -43,8 +43,6 @@ async function updateMovieMetadata(
return root; return root;
}); });
console.log({ newDoc });
return createDocument(docId, newDoc); return createDocument(docId, newDoc);
} }

View File

@ -2,10 +2,7 @@ import { Handlers } from "$fresh/server.ts";
import { BadRequestError } from "@lib/errors.ts"; import { BadRequestError } from "@lib/errors.ts";
import { getTypeSenseClient } from "@lib/typesense.ts"; import { getTypeSenseClient } from "@lib/typesense.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
import { getArticle } from "@lib/resource/articles.ts"; import { extractHashTags } from "@lib/string.ts";
import { getMovie } from "@lib/tmdb.ts";
import { getRecipe } from "@lib/resource/recipes.ts";
import { getDocument } from "@lib/documents.ts";
export const handler: Handlers = { export const handler: Handlers = {
async GET(req, _ctx) { async GET(req, _ctx) {
@ -23,6 +20,11 @@ export const handler: Handlers = {
filter_by = `type:=${type}`; filter_by = `type:=${type}`;
} }
const hashTags = extractHashTags(query);
if (hashTags?.length) {
//filter_by += `tags:=${}`
}
const typesenseClient = await getTypeSenseClient(); const typesenseClient = await getTypeSenseClient();
if (!typesenseClient) { if (!typesenseClient) {
throw new Error("Query not available"); throw new Error("Query not available");