diff --git a/apps/web/components/dashboard/preview/AttachmentBox.tsx b/apps/web/components/dashboard/preview/AttachmentBox.tsx new file mode 100644 index 00000000..9d581015 --- /dev/null +++ b/apps/web/components/dashboard/preview/AttachmentBox.tsx @@ -0,0 +1,209 @@ +import Link from "next/link"; +import { ActionButton } from "@/components/ui/action-button"; +import ActionConfirmingDialog from "@/components/ui/action-confirming-dialog"; +import { Button } from "@/components/ui/button"; +import { + Collapsible, + CollapsibleContent, + CollapsibleTrigger, +} from "@/components/ui/collapsible"; +import FilePickerButton from "@/components/ui/file-picker-button"; +import { toast } from "@/components/ui/use-toast"; +import useUpload from "@/lib/hooks/upload-file"; +import { + Archive, + Camera, + ChevronsDownUp, + Download, + Image, + Pencil, + Plus, + Trash2, +} from "lucide-react"; + +import { + useAttachBookmarkAsset, + useDetachBookmarkAsset, + useReplaceBookmarkAsset, +} from "@hoarder/shared-react/hooks/bookmarks"; +import { getAssetUrl } from "@hoarder/shared-react/utils/assetUtils"; +import { ZAssetType, ZBookmark } from "@hoarder/shared/types/bookmarks"; +import { + humanFriendlyNameForAssertType, + isAllowedToAttachAsset, +} from "@hoarder/trpc/lib/attachments"; + +export default function AttachmentBox({ bookmark }: { bookmark: ZBookmark }) { + const typeToIcon: Record = { + screenshot: , + fullPageArchive: , + bannerImage: , + }; + + const { mutate: attachAsset, isPending: isAttaching } = + useAttachBookmarkAsset({ + onSuccess: () => { + toast({ + description: "Attachment has been attached!", + }); + }, + onError: (e) => { + toast({ + description: e.message, + variant: "destructive", + }); + }, + }); + + const { mutate: replaceAsset, isPending: isReplacing } = + useReplaceBookmarkAsset({ + onSuccess: () => { + toast({ + description: "Attachment has been replaced!", + }); + }, + onError: (e) => { + toast({ + description: e.message, + variant: "destructive", + }); + }, + }); + + const { mutate: detachAsset, isPending: isDetaching } = + useDetachBookmarkAsset({ + onSuccess: () => { + toast({ + description: "Attachment has been detached!", + }); + }, + onError: (e) => { + toast({ + description: e.message, + variant: "destructive", + }); + }, + }); + + const { mutate: uploadAsset } = useUpload({ + onError: (e) => { + toast({ + description: e.error, + variant: "destructive", + }); + }, + }); + + if (!bookmark.assets.length) { + return null; + } + bookmark.assets.sort((a, b) => a.assetType.localeCompare(b.assetType)); + + return ( + + + Attachments + + + + {bookmark.assets.map((asset) => ( +
+ + {typeToIcon[asset.assetType]} +

{humanFriendlyNameForAssertType(asset.assetType)}

+ +
+ + + + {isAllowedToAttachAsset(asset.assetType) && ( + + uploadAsset(file, { + onSuccess: (resp) => { + replaceAsset({ + bookmarkId: bookmark.id, + oldAssetId: asset.id, + newAssetId: resp.assetId, + }); + }, + }) + } + > + + + )} + ( + + detachAsset( + { bookmarkId: bookmark.id, assetId: asset.id }, + { onSettled: () => setDialogOpen(false) }, + ) + } + > + + Delete + + )} + > + + +
+
+ ))} + {!bookmark.assets.some((asset) => asset.assetType == "bannerImage") && ( + + uploadAsset(file, { + onSuccess: (resp) => { + attachAsset({ + bookmarkId: bookmark.id, + asset: { + id: resp.assetId, + assetType: "bannerImage", + }, + }); + }, + }) + } + > + + Attach a Banner + + )} +
+
+ ); +} diff --git a/apps/web/components/dashboard/preview/BookmarkPreview.tsx b/apps/web/components/dashboard/preview/BookmarkPreview.tsx index 13d3c9d8..1f099725 100644 --- a/apps/web/components/dashboard/preview/BookmarkPreview.tsx +++ b/apps/web/components/dashboard/preview/BookmarkPreview.tsx @@ -25,6 +25,7 @@ import { BookmarkTypes, ZBookmark } from "@hoarder/shared/types/bookmarks"; import ActionBar from "./ActionBar"; import { AssetContentSection } from "./AssetContentSection"; +import AttachmentBox from "./AttachmentBox"; import { EditableTitle } from "./EditableTitle"; import LinkContentSection from "./LinkContentSection"; import { NoteEditor } from "./NoteEditor"; @@ -153,6 +154,7 @@ export default function BookmarkPreview({

Note

+ diff --git a/apps/web/components/dashboard/settings/ImportExport.tsx b/apps/web/components/dashboard/settings/ImportExport.tsx index 4827df93..a19db7fd 100644 --- a/apps/web/components/dashboard/settings/ImportExport.tsx +++ b/apps/web/components/dashboard/settings/ImportExport.tsx @@ -171,6 +171,7 @@ export function Import() {
{ +interface FilePickerButtonProps extends Omit { onFileSelect?: (file: File) => void; accept?: string; multiple?: boolean; @@ -35,7 +35,7 @@ const FilePickerButton: React.FC = ({ return (
-