114 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-10-14 18:57:37 -04:00
"use client";
import { useMemo } from "react";
2023-10-16 08:35:53 -04:00
import dynamic from "next/dynamic";
import Image from "next/image";
2023-10-14 18:57:37 -04:00
import { Button } from "@/components/ui/button";
import { RiCloseFill } from "react-icons/ri";
import { Avatar, AvatarImage, AvatarFallback } from "@radix-ui/react-avatar";
import { useRouter } from "next/navigation";
2023-10-27 13:10:07 -04:00
import { formatDate, fromUnix } from "@/lib/utils/dates";
2023-10-14 18:57:37 -04:00
import Actions from "./Actions";
2023-10-16 00:28:09 -04:00
import { NDKEvent } from "@nostr-dev-kit/ndk";
import { getTagAllValues, getTagValues } from "@/lib/nostr/utils";
import useProfile from "@/lib/hooks/useProfile";
import { nip19 } from "nostr-tools";
import { getNameToShow, getTwoLetters } from "@/lib/utils";
type ArticleProps = {
event: NDKEvent;
};
export default function ArticlePage({ event }: ArticleProps) {
2023-10-14 18:57:37 -04:00
const Viewer = useMemo(
() => dynamic(() => import("@/components/LongForm"), { ssr: false }),
[],
);
2023-10-16 00:28:09 -04:00
console.log(event);
2023-10-14 18:57:37 -04:00
const router = useRouter();
2023-10-16 00:28:09 -04:00
const markdown = event.content;
const pubkey = event.pubkey;
const createdAt = getTagValues("published_at", event.tags)
? parseInt(getTagValues("published_at", event.tags) as string)
: event.created_at;
const { profile } = useProfile(pubkey);
const npub = nip19.npubEncode(pubkey);
const title = getTagValues("title", event.tags);
const summary = getTagValues("summary", event.tags);
const tags = getTagAllValues("t", event.tags);
2023-10-16 08:35:53 -04:00
const image = getTagValues("image", event.tags);
2023-10-14 18:57:37 -04:00
return (
<div className="relative @container">
2023-10-14 19:03:44 -04:00
<div className="sticky inset-x-0 top-0 z-10 flex items-center justify-between border-b bg-background pb-4 pt-4">
2023-10-14 18:57:37 -04:00
<div className="center gap-x-3">
<Avatar className="center h-8 w-8 overflow-hidden rounded-sm bg-muted">
2023-10-16 00:28:09 -04:00
<AvatarImage src={profile?.image} alt="user" />
<AvatarFallback className="text-xs">
{getTwoLetters({ profile, npub })}
</AvatarFallback>
2023-10-14 18:57:37 -04:00
</Avatar>
<span className="text-xs uppercase text-muted-foreground">
2023-10-16 00:28:09 -04:00
{getNameToShow({ profile, npub })}
2023-10-14 18:57:37 -04:00
</span>
</div>
<Button
onClick={() => {
if (sessionStorage.getItem("RichHistory")) {
void router.back();
} else {
2023-10-27 17:48:49 -04:00
void router.push("/explore");
2023-10-14 18:57:37 -04:00
}
}}
size="icon"
variant={"outline"}
className=""
>
<RiCloseFill className="h-5 w-5" />
</Button>
</div>
2023-10-14 19:03:44 -04:00
<div className="h-[20px] w-full"></div>
<div className="vmax-h-[calc(100vh_-_100px)] overflow-y-auto">
2023-10-27 13:10:07 -04:00
<article className="prose prose-zinc relative mx-auto max-w-3xl pt-7 dark:prose-invert">
2023-10-14 18:57:37 -04:00
<div className="">
2023-10-29 11:08:12 -04:00
<div className="mb-1.5 flex items-center justify-between gap-1 lg:mb-2">
2023-10-16 00:28:09 -04:00
{tags.map((t) => (
<Button variant={"link"} className="px-0">
{t}
</Button>
))}
2023-10-14 18:57:37 -04:00
<div className="center text-xs text-muted-foreground/50">
2023-10-16 00:28:09 -04:00
{!!createdAt && (
<span className="mr-2.5">
2023-10-27 13:10:07 -04:00
{formatDate(fromUnix(createdAt), "MMMM Do, YYYY")}
2023-10-16 00:28:09 -04:00
</span>
)}
2023-10-14 18:57:37 -04:00
<span className="h-3 w-[1px] rounded-full bg-muted-foreground/50"></span>
</div>
</div>
2023-10-29 11:08:12 -04:00
<h1 className="mb-4 text-5xl font-bold">{title}</h1>
2023-10-16 08:35:53 -04:00
<div className="mb-5 flex items-center justify-end">
2023-10-14 18:57:37 -04:00
<Actions />
</div>
<div className="rounded-r-lg border-l-[4px] border-primary bg-muted p-4">
2023-10-16 00:28:09 -04:00
<p className="m-0">{summary} </p>
2023-10-14 18:57:37 -04:00
</div>
2023-10-16 08:35:53 -04:00
{image && (
<div className="max-h-[400px] w-full">
<Image
src={image}
alt="article banner image"
height="288"
width="288"
unoptimized
className="mb-0 mt-5 max-h-[400px] w-full rounded-lg object-cover sm:mt-8"
/>
</div>
)}
2023-10-14 18:57:37 -04:00
</div>
<Viewer content={markdown} />
</article>
</div>
</div>
);
}