Skip to content

Commit

Permalink
refactor: Start tracking bookmark assets in the assets table
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Oct 6, 2024
1 parent db2d346 commit 1e5c575
Show file tree
Hide file tree
Showing 14 changed files with 1,581 additions and 175 deletions.
11 changes: 11 additions & 0 deletions apps/web/app/api/assets/[assetId]/route.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down
17 changes: 16 additions & 1 deletion apps/web/app/api/assets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
112 changes: 57 additions & 55 deletions apps/web/components/dashboard/preview/AttachmentBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ChevronsDownUp,
Download,
Image,
Paperclip,
Pencil,
Plus,
Trash2,
Expand All @@ -35,13 +36,16 @@ import {
import {
humanFriendlyNameForAssertType,
isAllowedToAttachAsset,
isAllowedToDetachAsset,
} from "@hoarder/trpc/lib/attachments";

export default function AttachmentBox({ bookmark }: { bookmark: ZBookmark }) {
const typeToIcon: Record<ZAssetType, React.ReactNode> = {
screenshot: <Camera className="size-4" />,
fullPageArchive: <Archive className="size-4" />,
bannerImage: <Image className="size-4" />,
bookmarkAsset: <Paperclip className="size-4" />,
unknown: <Paperclip className="size-4" />,
};

const { mutate: attachAsset, isPending: isAttaching } =
Expand Down Expand Up @@ -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 (
<Collapsible>
<CollapsibleTrigger className="flex w-full items-center justify-between gap-2 text-sm text-gray-400">
Expand Down Expand Up @@ -156,59 +155,62 @@ export default function AttachmentBox({ bookmark }: { bookmark: ZBookmark }) {
<Pencil className="size-4" />
</FilePickerButton>
)}
<ActionConfirmingDialog
title="Delete Attachment?"
description={`Are you sure you want to delete the attachment of the bookmark?`}
actionButton={(setDialogOpen) => (
<ActionButton
loading={isDetaching}
variant="destructive"
onClick={() =>
detachAsset(
{ bookmarkId: bookmark.id, assetId: asset.id },
{ onSettled: () => setDialogOpen(false) },
)
}
>
<Trash2 className="mr-2 size-4" />
Delete
</ActionButton>
)}
>
<Button variant="none" size="none" title="Delete">
<Trash2 className="size-4" />
</Button>
</ActionConfirmingDialog>
{isAllowedToDetachAsset(asset.assetType) && (
<ActionConfirmingDialog
title="Delete Attachment?"
description={`Are you sure you want to delete the attachment of the bookmark?`}
actionButton={(setDialogOpen) => (
<ActionButton
loading={isDetaching}
variant="destructive"
onClick={() =>
detachAsset(
{ bookmarkId: bookmark.id, assetId: asset.id },
{ onSettled: () => setDialogOpen(false) },
)
}
>
<Trash2 className="mr-2 size-4" />
Delete
</ActionButton>
)}
>
<Button variant="none" size="none" title="Delete">
<Trash2 className="size-4" />
</Button>
</ActionConfirmingDialog>
)}
</div>
</div>
))}
{!bookmark.assets.some((asset) => asset.assetType == "bannerImage") && (
<FilePickerButton
title="Attach a Banner"
loading={isAttaching}
accept=".jgp,.JPG,.jpeg,.png,.webp"
multiple={false}
variant="ghost"
size="none"
className="flex w-full items-center justify-center gap-2"
onFileSelect={(file) =>
uploadAsset(file, {
onSuccess: (resp) => {
attachAsset({
bookmarkId: bookmark.id,
asset: {
id: resp.assetId,
assetType: "bannerImage",
},
});
},
})
}
>
<Plus className="size-4" />
Attach a Banner
</FilePickerButton>
)}
{!bookmark.assets.some((asset) => asset.assetType == "bannerImage") &&

This comment has been minimized.

Copy link
@CrypticC3s4r

CrypticC3s4r Oct 6, 2024

Contributor

why not using a constant and comparing with === instead ?

bookmark.content.type != BookmarkTypes.ASSET && (
<FilePickerButton
title="Attach a Banner"
loading={isAttaching}
accept=".jgp,.JPG,.jpeg,.png,.webp"
multiple={false}
variant="ghost"
size="none"
className="flex w-full items-center justify-center gap-2"
onFileSelect={(file) =>
uploadAsset(file, {
onSuccess: (resp) => {
attachAsset({
bookmarkId: bookmark.id,
asset: {
id: resp.assetId,
assetType: "bannerImage",
},
});
},
})
}
>
<Plus className="size-4" />
Attach a Banner
</FilePickerButton>
)}
</CollapsibleContent>
</Collapsible>
);
Expand Down
Loading

0 comments on commit 1e5c575

Please sign in to comment.