aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/dashboard/bookmarks/BookmarkPreview.tsx
blob: 4209192e75aadf629473f945a45bde5bb4f798e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"use client";

import Image from "next/image";
import Link from "next/link";
import { BackButton } from "@/components/ui/back-button";
import { Skeleton } from "@/components/ui/skeleton";
import { isBookmarkStillCrawling } from "@/lib/bookmarkUtils";
import { api } from "@/lib/trpc";
import { ArrowLeftCircle, CalendarDays, ExternalLink } from "lucide-react";
import Markdown from "react-markdown";

import type { ZBookmark } from "@hoarder/trpc/types/bookmarks";

export default function BookmarkPreview({
  initialData,
}: {
  initialData: ZBookmark;
}) {
  const { data: bookmark } = api.bookmarks.getBookmark.useQuery(
    {
      bookmarkId: initialData.id,
    },
    {
      initialData,
      refetchInterval: (query) => {
        const data = query.state.data;
        if (!data) {
          return false;
        }
        // If the link is not crawled or not tagged
        if (isBookmarkStillCrawling(data)) {
          return 1000;
        }
        return false;
      },
    },
  );

  const linkHeader = bookmark.content.type == "link" && (
    <div className="flex flex-col space-y-2">
      <p className="text-center text-3xl">
        {bookmark.content.title ?? bookmark.content.url}
      </p>
      <Link href={bookmark.content.url} className="mx-auto flex gap-2">
        <span className="my-auto">View Original</span>
        <ExternalLink />
      </Link>
    </div>
  );

  let content;
  switch (bookmark.content.type) {
    case "link": {
      if (!bookmark.content.htmlContent) {
        content = (
          <div className="text-red-500">Failed to fetch link content ...</div>
        );
      } else {
        content = (
          <div
            dangerouslySetInnerHTML={{
              __html: bookmark.content.htmlContent || "",
            }}
            className="prose"
          />
        );
      }
      break;
    }
    case "text": {
      content = <Markdown className="prose">{bookmark.content.text}</Markdown>;
      break;
    }
    case "asset": {
      switch (bookmark.content.assetType) {
        case "image": {
          content = (
            <div className="relative w-full">
              <Image
                alt="asset"
                fill={true}
                src={`/api/assets/${bookmark.content.assetId}`}
              />
            </div>
          );
        }
      }
      break;
    }
  }

  return (
    <div className="m-4 min-h-screen space-y-4 rounded-md border bg-background p-4">
      <div className="flex justify-between">
        <BackButton className="ghost" variant="ghost">
          <ArrowLeftCircle />
        </BackButton>
        <div className="my-auto">
          <span className="my-auto flex gap-2">
            <CalendarDays /> {bookmark.createdAt.toLocaleString()}
          </span>
        </div>
      </div>
      <hr />
      {linkHeader}
      <div className="mx-auto flex h-full border-x p-2 px-4 lg:w-2/3">
        {isBookmarkStillCrawling(bookmark) ? (
          <div className="flex w-full flex-col gap-2">
            <Skeleton className="h-4" />
            <Skeleton className="h-4" />
            <Skeleton className="h-4" />
          </div>
        ) : (
          content
        )}
      </div>
    </div>
  );
}