From 2413f0efee2dcb4cd4c9389f5a496d4b3f71335c Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 5 Mar 2024 19:38:34 +0000 Subject: fix: Show loading indicators in the bookmark preview page --- .../[bookmarkId]/components/BookmarkPreview.tsx | 101 +++++++++++++++++++++ .../app/dashboard/preview/[bookmarkId]/page.tsx | 54 +---------- 2 files changed, 103 insertions(+), 52 deletions(-) create mode 100644 packages/web/app/dashboard/preview/[bookmarkId]/components/BookmarkPreview.tsx (limited to 'packages/web/app/dashboard/preview') diff --git a/packages/web/app/dashboard/preview/[bookmarkId]/components/BookmarkPreview.tsx b/packages/web/app/dashboard/preview/[bookmarkId]/components/BookmarkPreview.tsx new file mode 100644 index 00000000..2a8ae1b1 --- /dev/null +++ b/packages/web/app/dashboard/preview/[bookmarkId]/components/BookmarkPreview.tsx @@ -0,0 +1,101 @@ +"use client"; + +import { BackButton } from "@/components/ui/back-button"; +import { Skeleton } from "@/components/ui/skeleton"; +import { isBookmarkStillCrawling } from "@/lib/bookmarkUtils"; +import { api } from "@/lib/trpc"; +import { ZBookmark } from "@hoarder/trpc/types/bookmarks"; +import { ArrowLeftCircle, CalendarDays, ExternalLink } from "lucide-react"; +import Link from "next/link"; +import Markdown from "react-markdown"; + +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" && ( +
+

+ {bookmark.content.title || bookmark.content.url} +

+ + View Original + + +
+ ); + + let content; + switch (bookmark.content.type) { + case "link": { + if (!bookmark.content.htmlContent) { + content = ( +
Failed to fetch link content ...
+ ); + } else { + content = ( +
+ ); + } + break; + } + case "text": { + content = {bookmark.content.text}; + break; + } + } + + return ( +
+
+ + + +
+ + {bookmark.createdAt.toLocaleString()} + +
+
+
+ {linkHeader} +
+ {isBookmarkStillCrawling(bookmark) ? ( +
+ + + +
+ ) : ( + content + )} +
+
+ ); +} diff --git a/packages/web/app/dashboard/preview/[bookmarkId]/page.tsx b/packages/web/app/dashboard/preview/[bookmarkId]/page.tsx index 030ad2df..47aeb891 100644 --- a/packages/web/app/dashboard/preview/[bookmarkId]/page.tsx +++ b/packages/web/app/dashboard/preview/[bookmarkId]/page.tsx @@ -1,8 +1,5 @@ -import { BackButton } from "@/components/ui/back-button"; import { api } from "@/server/api/client"; -import { ArrowLeftCircle, CalendarDays, ExternalLink } from "lucide-react"; -import Link from "next/link"; -import Markdown from "react-markdown"; +import BookmarkPreview from "./components/BookmarkPreview"; export default async function BookmarkPreviewPage({ params, @@ -13,52 +10,5 @@ export default async function BookmarkPreviewPage({ bookmarkId: params.bookmarkId, }); - const linkHeader = bookmark.content.type == "link" && ( -
-

{bookmark.content.title}

- - View Original - - -
- ); - - let content; - switch (bookmark.content.type) { - case "link": { - content = ( -
- ); - break; - } - case "text": { - content = {bookmark.content.text}; - break; - } - } - - return ( -
-
- - - -
- - {bookmark.createdAt.toLocaleString()} - -
-
-
- {linkHeader} -
- {content} -
-
- ); + return ; } -- cgit v1.2.3-70-g09d2