From 1e5c575e16c8a9e6bd7592e83bea53af7f359e15 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 6 Oct 2024 14:33:40 +0000 Subject: refactor: Start tracking bookmark assets in the assets table --- apps/web/app/api/assets/[assetId]/route.ts | 11 ++ apps/web/app/api/assets/route.ts | 17 +++- .../components/dashboard/preview/AttachmentBox.tsx | 112 +++++++++++---------- 3 files changed, 84 insertions(+), 56 deletions(-) (limited to 'apps/web') diff --git a/apps/web/app/api/assets/[assetId]/route.ts b/apps/web/app/api/assets/[assetId]/route.ts index f3cf1ab4..73237d8d 100644 --- a/apps/web/app/api/assets/[assetId]/route.ts +++ b/apps/web/app/api/assets/[assetId]/route.ts @@ -1,5 +1,7 @@ import { createContextFromRequest } from "@/server/api/client"; +import { and, eq } from "drizzle-orm"; +import { assets } from "@hoarder/db/schema"; import { readAsset } from "@hoarder/shared/assetdb"; export const dynamic = "force-dynamic"; @@ -11,6 +13,15 @@ export async function GET( if (!ctx.user) { return Response.json({ error: "Unauthorized" }, { status: 401 }); } + + const assetDb = await ctx.db.query.assets.findFirst({ + where: and(eq(assets.id, params.assetId), eq(assets.userId, ctx.user.id)), + }); + + if (!assetDb) { + return Response.json({ error: "Asset not found" }, { status: 404 }); + } + const { asset, metadata } = await readAsset({ userId: ctx.user.id, assetId: params.assetId, diff --git a/apps/web/app/api/assets/route.ts b/apps/web/app/api/assets/route.ts index 9028f556..0e52ff93 100644 --- a/apps/web/app/api/assets/route.ts +++ b/apps/web/app/api/assets/route.ts @@ -2,6 +2,7 @@ import { createContextFromRequest } from "@/server/api/client"; import { TRPCError } from "@trpc/server"; import type { ZUploadResponse } from "@hoarder/shared/types/uploads"; +import { assets, AssetTypes } from "@hoarder/db/schema"; import { newAssetId, saveAsset, @@ -43,8 +44,22 @@ export async function POST(request: Request) { return Response.json({ error: "Bad request" }, { status: 400 }); } - const assetId = newAssetId(); const fileName = data.name; + const [assetDb] = await ctx.db + .insert(assets) + .values({ + id: newAssetId(), + // Initially, uploads are uploaded for unknown purpose + // And without an attached bookmark. + assetType: AssetTypes.UNKNOWN, + bookmarkId: null, + userId: ctx.user.id, + contentType, + size: data.size, + fileName, + }) + .returning(); + const assetId = assetDb.id; await saveAsset({ userId: ctx.user.id, diff --git a/apps/web/components/dashboard/preview/AttachmentBox.tsx b/apps/web/components/dashboard/preview/AttachmentBox.tsx index a8eaf0f4..436f1026 100644 --- a/apps/web/components/dashboard/preview/AttachmentBox.tsx +++ b/apps/web/components/dashboard/preview/AttachmentBox.tsx @@ -16,6 +16,7 @@ import { ChevronsDownUp, Download, Image, + Paperclip, Pencil, Plus, Trash2, @@ -35,6 +36,7 @@ import { import { humanFriendlyNameForAssertType, isAllowedToAttachAsset, + isAllowedToDetachAsset, } from "@hoarder/trpc/lib/attachments"; export default function AttachmentBox({ bookmark }: { bookmark: ZBookmark }) { @@ -42,6 +44,8 @@ export default function AttachmentBox({ bookmark }: { bookmark: ZBookmark }) { screenshot: , fullPageArchive: , bannerImage: , + bookmarkAsset: , + unknown: , }; const { mutate: attachAsset, isPending: isAttaching } = @@ -100,11 +104,6 @@ export default function AttachmentBox({ bookmark }: { bookmark: ZBookmark }) { 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; - } - return ( @@ -156,59 +155,62 @@ export default function AttachmentBox({ bookmark }: { bookmark: ZBookmark }) { )} - ( - - detachAsset( - { bookmarkId: bookmark.id, assetId: asset.id }, - { onSettled: () => setDialogOpen(false) }, - ) - } - > - - Delete - - )} - > - - + {isAllowedToDetachAsset(asset.assetType) && ( + ( + + 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 - - )} + {!bookmark.assets.some((asset) => asset.assetType == "bannerImage") && + bookmark.content.type != BookmarkTypes.ASSET && ( + + uploadAsset(file, { + onSuccess: (resp) => { + attachAsset({ + bookmarkId: bookmark.id, + asset: { + id: resp.assetId, + assetType: "bannerImage", + }, + }); + }, + }) + } + > + + Attach a Banner + + )} ); -- cgit v1.2.3-70-g09d2