Skip to content

Commit

Permalink
feature: Allow attaching custom banners to notes. Fixes: #106
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Oct 5, 2024
1 parent 99c6232 commit 8a13095
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
18 changes: 17 additions & 1 deletion apps/web/components/dashboard/bookmarks/TextCard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"use client";

import Image from "next/image";
import Link from "next/link";
import { MarkdownComponent } from "@/components/ui/markdown-component";
import { bookmarkLayoutSwitch } from "@/lib/userLocalSettings/bookmarksLayout";
import { cn } from "@/lib/utils";

import type { ZBookmarkTypeText } from "@hoarder/shared/types/bookmarks";
import { getAssetUrl } from "@hoarder/shared-react/utils/assetUtils";
import { getSourceUrl } from "@hoarder/shared-react/utils/bookmarkUtils";

import { BookmarkLayoutAdaptingCard } from "./BookmarkLayoutAdaptingCard";
Expand All @@ -19,6 +22,8 @@ export default function TextCard({
}) {
const bookmarkedText = bookmark.content;

const banner = bookmark.assets.find((a) => a.assetType == "bannerImage");

return (
<>
<BookmarkLayoutAdaptingCard
Expand All @@ -38,7 +43,18 @@ export default function TextCard({
grid: null,
masonry: null,
compact: null,
list: (
list: banner ? (
<div className="relative size-full flex-1">
<Link href={`/dashboard/preview/${bookmark.id}`}>
<Image
alt="card banner"
fill={true}
className={cn("flex-1", className)}
src={getAssetUrl(banner.id)}
/>
</Link>
</div>
) : (
<div
className={cn(
"flex size-full items-center justify-center bg-accent text-center",
Expand Down
12 changes: 9 additions & 3 deletions apps/web/components/dashboard/preview/AttachmentBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import {
useReplaceBookmarkAsset,
} from "@hoarder/shared-react/hooks/bookmarks";
import { getAssetUrl } from "@hoarder/shared-react/utils/assetUtils";
import { ZAssetType, ZBookmark } from "@hoarder/shared/types/bookmarks";
import {
BookmarkTypes,
ZAssetType,
ZBookmark,
} from "@hoarder/shared/types/bookmarks";
import {
humanFriendlyNameForAssertType,
isAllowedToAttachAsset,
Expand Down Expand Up @@ -94,10 +98,12 @@ export default function AttachmentBox({ bookmark }: { bookmark: ZBookmark }) {
},
});

if (!bookmark.assets.length) {
bookmark.assets.sort((a, b) => a.assetType.localeCompare(b.assetType));

if (bookmark.content.type == BookmarkTypes.ASSET) {
// Currently, we don't allow attaching assets to assets types.
return null;
}
bookmark.assets.sort((a, b) => a.assetType.localeCompare(b.assetType));

return (
<Collapsible>
Expand Down
18 changes: 18 additions & 0 deletions apps/web/components/dashboard/preview/TextContentSection.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import Image from "next/image";
import { MarkdownComponent } from "@/components/ui/markdown-component";
import { ScrollArea } from "@radix-ui/react-scroll-area";

import { getAssetUrl } from "@hoarder/shared-react/utils/assetUtils";
import { BookmarkTypes, ZBookmark } from "@hoarder/shared/types/bookmarks";

export function TextContentSection({ bookmark }: { bookmark: ZBookmark }) {
if (bookmark.content.type != BookmarkTypes.TEXT) {
throw new Error("Invalid content type");
}
const banner = bookmark.assets.find(
(asset) => asset.assetType == "bannerImage",
);

return (
<ScrollArea className="h-full">
{banner && (
<div className="relative h-52 min-w-full">
<Image
alt="banner"
src={getAssetUrl(banner.id)}
width={0}
height={0}
layout="fill"
objectFit="cover"
/>
</div>
)}
<MarkdownComponent>{bookmark.content.text}</MarkdownComponent>
</ScrollArea>
);
Expand Down

0 comments on commit 8a13095

Please sign in to comment.