From 6928800a604f05ef62234cb5c3ee1e60fb27ea1a Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 9 Jun 2024 21:05:21 +0000 Subject: refactor: Extract the bookmark polling logic into a separate shared component --- .../components/dashboard/bookmarks/AssetCard.tsx | 36 ++----------- .../dashboard/bookmarks/BookmarkCard.tsx | 59 ++++++++++++++++++++++ .../dashboard/bookmarks/BookmarksGrid.tsx | 34 ++++--------- .../components/dashboard/bookmarks/LinkCard.tsx | 30 +---------- .../components/dashboard/bookmarks/TextCard.tsx | 29 ++--------- 5 files changed, 75 insertions(+), 113 deletions(-) create mode 100644 apps/web/components/dashboard/bookmarks/BookmarkCard.tsx (limited to 'apps') diff --git a/apps/web/components/dashboard/bookmarks/AssetCard.tsx b/apps/web/components/dashboard/bookmarks/AssetCard.tsx index f110e0c4..c235e4c8 100644 --- a/apps/web/components/dashboard/bookmarks/AssetCard.tsx +++ b/apps/web/components/dashboard/bookmarks/AssetCard.tsx @@ -2,14 +2,9 @@ import Image from "next/image"; import Link from "next/link"; -import { api } from "@/lib/trpc"; -import type { - ZBookmark, - ZBookmarkTypeAsset, -} from "@hoarder/shared/types/bookmarks"; +import type { ZBookmarkTypeAsset } from "@hoarder/shared/types/bookmarks"; import { getAssetUrl } from "@hoarder/shared-react/utils/assetUtils"; -import { isBookmarkStillTagging } from "@hoarder/shared-react/utils/bookmarkUtils"; import { BookmarkLayoutAdaptingCard } from "./BookmarkLayoutAdaptingCard"; @@ -51,37 +46,12 @@ function AssetImage({ } export default function AssetCard({ - bookmark: initialData, + bookmark: bookmarkedAsset, className, }: { - bookmark: ZBookmark; + bookmark: ZBookmarkTypeAsset; className?: string; }) { - const { data: bookmark } = api.bookmarks.getBookmark.useQuery( - { - bookmarkId: initialData.id, - }, - { - initialData, - refetchInterval: (query) => { - const data = query.state.data; - if (!data) { - return false; - } - if (isBookmarkStillTagging(data)) { - return 1000; - } - return false; - }, - }, - ); - - if (bookmark.content.type != "asset") { - throw new Error("Unexpected bookmark type"); - } - - const bookmarkedAsset = { ...bookmark, content: bookmark.content }; - return ( { + const data = query.state.data; + if (!data) { + return false; + } + if (isBookmarkStillLoading(data)) { + return 1000; + } + return false; + }, + }, + ); + + switch (bookmark.content.type) { + case "link": + return ( + + ); + case "text": + return ( + + ); + case "asset": + return ( + + ); + } +} diff --git a/apps/web/components/dashboard/bookmarks/BookmarksGrid.tsx b/apps/web/components/dashboard/bookmarks/BookmarksGrid.tsx index b44dea33..540546fe 100644 --- a/apps/web/components/dashboard/bookmarks/BookmarksGrid.tsx +++ b/apps/web/components/dashboard/bookmarks/BookmarksGrid.tsx @@ -11,12 +11,10 @@ import resolveConfig from "tailwindcss/resolveConfig"; import type { ZBookmark } from "@hoarder/shared/types/bookmarks"; -import AssetCard from "./AssetCard"; +import BookmarkCard from "./BookmarkCard"; import EditorCard from "./EditorCard"; -import LinkCard from "./LinkCard"; -import TextCard from "./TextCard"; -function BookmarkCard({ children }: { children: React.ReactNode }) { +function StyledBookmarkCard({ children }: { children: React.ReactNode }) { return ( {children} @@ -36,24 +34,6 @@ function getBreakpointConfig() { return breakpointColumnsObj; } -function renderBookmark(bookmark: ZBookmark) { - let comp; - switch (bookmark.content.type) { - case "link": - comp = ; - break; - case "text": - comp = ; - break; - case "asset": - comp = ( - - ); - break; - } - return {comp}; -} - export default function BookmarksGrid({ bookmarks, hasNextPage = false, @@ -76,11 +56,15 @@ export default function BookmarksGrid({ const children = [ showEditorCard && ( - + - + ), - ...bookmarks.map((b) => renderBookmark(b)), + ...bookmarks.map((b) => ( + + + + )), ]; return ( <> diff --git a/apps/web/components/dashboard/bookmarks/LinkCard.tsx b/apps/web/components/dashboard/bookmarks/LinkCard.tsx index 1446b031..7212940b 100644 --- a/apps/web/components/dashboard/bookmarks/LinkCard.tsx +++ b/apps/web/components/dashboard/bookmarks/LinkCard.tsx @@ -2,13 +2,11 @@ import Image from "next/image"; import Link from "next/link"; -import { api } from "@/lib/trpc"; import type { ZBookmarkTypeLink } from "@hoarder/shared/types/bookmarks"; import { getBookmarkLinkImageUrl, isBookmarkStillCrawling, - isBookmarkStillLoading, } from "@hoarder/shared-react/utils/bookmarkUtils"; import { BookmarkLayoutAdaptingCard } from "./BookmarkLayoutAdaptingCard"; @@ -80,38 +78,12 @@ function LinkUrl({ bookmark }: { bookmark: ZBookmarkTypeLink }) { } export default function LinkCard({ - bookmark: initialData, + bookmark: bookmarkLink, className, }: { bookmark: ZBookmarkTypeLink; className?: string; }) { - 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 (isBookmarkStillLoading(data)) { - return 1000; - } - return false; - }, - }, - ); - - if (bookmark.content.type !== "link") { - throw new Error("Invalid bookmark type"); - } - - const bookmarkLink = { ...bookmark, content: bookmark.content }; - return ( } diff --git a/apps/web/components/dashboard/bookmarks/TextCard.tsx b/apps/web/components/dashboard/bookmarks/TextCard.tsx index b18efc3d..ffaa18a4 100644 --- a/apps/web/components/dashboard/bookmarks/TextCard.tsx +++ b/apps/web/components/dashboard/bookmarks/TextCard.tsx @@ -2,46 +2,23 @@ import { useState } from "react"; import { MarkdownComponent } from "@/components/ui/markdown-component"; -import { api } from "@/lib/trpc"; import { bookmarkLayoutSwitch } from "@/lib/userLocalSettings/bookmarksLayout"; import { cn } from "@/lib/utils"; -import type { ZBookmark } from "@hoarder/shared/types/bookmarks"; -import { isBookmarkStillTagging } from "@hoarder/shared-react/utils/bookmarkUtils"; +import type { ZBookmarkTypeText } from "@hoarder/shared/types/bookmarks"; import { BookmarkedTextViewer } from "./BookmarkedTextViewer"; import { BookmarkLayoutAdaptingCard } from "./BookmarkLayoutAdaptingCard"; export default function TextCard({ - bookmark: initialData, + bookmark, className, }: { - bookmark: ZBookmark; + bookmark: ZBookmarkTypeText; className?: string; }) { - const { data: bookmark } = api.bookmarks.getBookmark.useQuery( - { - bookmarkId: initialData.id, - }, - { - initialData, - refetchInterval: (query) => { - const data = query.state.data; - if (!data) { - return false; - } - if (isBookmarkStillTagging(data)) { - return 1000; - } - return false; - }, - }, - ); const [previewModalOpen, setPreviewModalOpen] = useState(false); const bookmarkedText = bookmark.content; - if (bookmarkedText.type != "text") { - throw new Error("Unexpected bookmark type"); - } return ( <> -- cgit v1.2.3-70-g09d2