diff --git a/app/(app)/event/[naddr]/layout.tsx b/app/(app)/event/[naddr]/layout.tsx new file mode 100644 index 0000000..dbc37cc --- /dev/null +++ b/app/(app)/event/[naddr]/layout.tsx @@ -0,0 +1,53 @@ +import type { Metadata, ResolvingMetadata } from "next"; +import { get } from "@/lib/server-actions/events/cache"; + +type Props = { + params: { naddr: string }; + searchParams: { [key: string]: string | string[] | undefined }; +}; + +export async function generateMetadata( + { params, searchParams }: Props, + parent: ResolvingMetadata, +): Promise { + // read route params + const identifier = params.naddr; + + // fetch data + const event = await get(identifier); + + // optionally access and extend (rather than replace) parent metadata + const previousImages = (await parent).openGraph?.images || []; + if (!event) { + return { + title: "Flockstr Event", + openGraph: { + images: previousImages, + }, + }; + } + const title = `${event.name} | Flockstr`; + return { + title: title, + description: event.description, + openGraph: { + title: title, + description: event.description, + images: [event.image, ...previousImages], + }, + twitter: { + title: title, + description: event.description, + images: [event.image], + card: "summary_large_image", + }, + }; +} + +export default function metadataLayout({ + children, +}: { + children: React.ReactNode; +}) { + <>{children}; +} diff --git a/app/(app)/event/[naddr]/page.tsx b/app/(app)/event/[naddr]/page.tsx index c2d7364..3b798b0 100644 --- a/app/(app)/event/[naddr]/page.tsx +++ b/app/(app)/event/[naddr]/page.tsx @@ -1,7 +1,5 @@ "use client"; -import { useState } from "react"; -import Image from "next/image"; -import Link from "next/link"; +import { useEffect } from "react"; import { nip19 } from "nostr-tools"; import useEvents from "@/lib/hooks/useEvents"; import Spinner from "@/components/spinner"; @@ -19,6 +17,8 @@ import LocationContainer from "./_components/LocationContainer"; import AnnouncementsContainer from "./_components/AnnouncementsContainer"; import DiscussionContainer from "./_components/DiscussionContainer"; import AttendeesContainer from "./_components/AttendeesContainer"; +import { add, get } from "@/lib/server-actions/events/cache"; +import { BANNER } from "@/constants"; export default function EventPage({ params: { naddr }, @@ -41,6 +41,29 @@ export default function EventPage({ }, }); const event = events[0]; + useEffect(() => { + console.log("EFFECT CALLED", event); + if (event) { + const { tags, content } = event; + const name = getTagValues("name", tags) ?? "Untitled"; + const image = + getTagValues("image", tags) ?? + getTagValues("picture", tags) ?? + getTagValues("banner", tags) ?? + BANNER; + console.log("setting event"); + add({ + identifier: naddr, + name: name, + description: content, + image: image, + }); + (async () => { + const res = await get(naddr); + console.log("RESPONE", res); + })(); + } + }, [event]); if (!event) { return ( diff --git a/bun.lockb b/bun.lockb index e3ed9cf..a3d38b2 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/lib/clients/redis/index.ts b/lib/clients/redis/index.ts new file mode 100644 index 0000000..02005a9 --- /dev/null +++ b/lib/clients/redis/index.ts @@ -0,0 +1,8 @@ +import { Redis } from "@upstash/redis"; + +const redis = new Redis({ + url: "https://us1-new-mako-37785.upstash.io", + token: process.env.UPSTASH_REDIS_REST_TOKEN as string, +}); + +export { redis }; diff --git a/lib/server-actions/events/cache/index.ts b/lib/server-actions/events/cache/index.ts new file mode 100644 index 0000000..4210828 --- /dev/null +++ b/lib/server-actions/events/cache/index.ts @@ -0,0 +1,34 @@ +"use server"; +import { redis } from "@/lib/clients/redis"; +import { z } from "zod"; + +const eventSchema = z.object({ + identifier: z.string(), + name: z.string(), + description: z.string(), + image: z.string(), +}); + +export async function get(identifier: string) { + const data = await redis.get(identifier); + console.log("Data", data); + try { + const parsedData = eventSchema.parse({ identifier, ...(data as Object) }); + return parsedData; + } catch (err) { + console.log("Error", err); + return; + } +} +export async function add(data: z.infer) { + let { identifier, name, description, image } = data; + try { + redis.set(identifier, { name, description, image }); + return { + success: true, + }; + } catch (err) { + console.log("Error", err); + return; + } +} diff --git a/next.config.js b/next.config.js index d9e9f2a..f1f6e03 100644 --- a/next.config.js +++ b/next.config.js @@ -1,5 +1,9 @@ /** @type {import('next').NextConfig} */ const nextConfig = { + experimental: { + serverActions: true, + swcPlugins: ["next-superjson-plugin"], + }, async rewrites() { return [ { diff --git a/package.json b/package.json index 1cb74e7..3b3371e 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "@radix-ui/react-tooltip": "^1.0.7", "@react-google-maps/api": "^2.19.2", "@tailwindcss/container-queries": "^0.1.1", + "@upstash/redis": "^1.24.3", "aws-sdk": "^2.1475.0", "buffer": "^6.0.3", "class-variance-authority": "^0.7.0",