Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

Archival tags post route

Isaacc90b53376d6b
diff --git a/components/InputSelect/types.ts b/components/InputSelect/types.ts
index 4ac48498..20c7c570 100644
--- a/components/InputSelect/types.ts
+++ b/components/InputSelect/types.ts
@@ -11,6 +11,7 @@ export interface ArchivalTagOption extends Option {
archiveAsReadable: boolean;
archiveAsWaybackMachine: boolean;
aiTag: boolean;
+ newTag: boolean;
}
export type ArchivalOptionKeys = keyof Omit<ArchivalTagOption, "label" | "value" | "__isNew__">;
diff --git a/hooks/store/tags.tsx b/hooks/store/tags.tsx
index 93840d2c..067d35ad 100644
--- a/hooks/store/tags.tsx
+++ b/hooks/store/tags.tsx
@@ -2,6 +2,7 @@ import { useQuery, useMutation, useQueryClient, UseQueryResult } from "@tanstack
import { TagIncludingLinkCount } from "@/types/global";
import { useSession } from "next-auth/react";
import { Tag } from "@prisma/client";
+import { ArchivalTagOption } from "@/components/InputSelect/types";
const useTags = (): UseQueryResult<Tag[], Error> => {
const { status } = useSession();
@@ -47,6 +48,30 @@ const useUpdateTag = () => {
});
};
+const useUpdateArchivalTags = () => {
+ const queryClient = useQueryClient();
+
+ return useMutation({
+ mutationFn: async (tags: ArchivalTagOption[]) => {
+ const response = await fetch("/api/v1/tags/archival", {
+ body: JSON.stringify(tags),
+ headers: {
+ "Content-Type": "application/json",
+ },
+ method: "POST",
+ });
+
+ const data = await response.json();
+ if (!response.ok) throw new Error(data.response);
+
+ return data.response;
+ },
+ onSettled: () => {
+ queryClient.invalidateQueries({ queryKey: ["tags"] });
+ },
+ });
+}
+
const useRemoveTag = () => {
const queryClient = useQueryClient();
@@ -69,4 +94,4 @@ const useRemoveTag = () => {
});
};
-export { useTags, useUpdateTag, useRemoveTag };
+export { useTags, useUpdateTag, useUpdateArchivalTags, useRemoveTag };
diff --git a/hooks/useArchivalTags.ts b/hooks/useArchivalTags.ts
index 65e9fb80..070cc81d 100644
--- a/hooks/useArchivalTags.ts
+++ b/hooks/useArchivalTags.ts
@@ -28,6 +28,7 @@ const useArchivalTags = (initialTags: Tag[]) => {
archiveAsReadable: tag.archiveAsReadable || false,
archiveAsWaybackMachine: tag.archiveAsWaybackMachine || false,
aiTag: tag.aiTag || false,
+ newTag: false,
});
const archival = initialTags.filter(isArchivalTag).map(transformTag);
@@ -38,17 +39,18 @@ const useArchivalTags = (initialTags: Tag[]) => {
}, [initialTags]);
const addTags = (newTags: ArchivalTagOption[]) => {
- console.log("newTags", newTags);
const uniqueNewTags = newTags
.filter(newTag => !archivalTags.some(existing => existing.label === newTag.label))
- .map(tag => ({
+ .map(({ __isNew__, ...tag }) => ({
...tag,
- archiveAsScreenshot: tag.__isNew__ ? false : tag.archiveAsScreenshot,
- archiveAsMonolith: tag.__isNew__ ? false : tag.archiveAsMonolith,
- archiveAsPDF: tag.__isNew__ ? false : tag.archiveAsPDF,
- archiveAsReadable: tag.__isNew__ ? false : tag.archiveAsReadable,
- archiveAsWaybackMachine: tag.__isNew__ ? false : tag.archiveAsWaybackMachine,
- aiTag: tag.__isNew__ ? false : tag.aiTag,
+ value: __isNew__ ? undefined : tag.value,
+ archiveAsScreenshot: __isNew__ ? false : tag.archiveAsScreenshot,
+ archiveAsMonolith: __isNew__ ? false : tag.archiveAsMonolith,
+ archiveAsPDF: __isNew__ ? false : tag.archiveAsPDF,
+ archiveAsReadable: __isNew__ ? false : tag.archiveAsReadable,
+ archiveAsWaybackMachine: __isNew__ ? false : tag.archiveAsWaybackMachine,
+ aiTag: __isNew__ ? false : tag.aiTag,
+ newTag: __isNew__ ? true : false,
}));
setArchivalTags(prev => [...prev, ...uniqueNewTags]);
diff --git a/lib/api/controllers/tags/tagId/updateTagById.ts b/lib/api/controllers/tags/tagId/updateTagById.ts
index f4bdea90..fc13706b 100644
--- a/lib/api/controllers/tags/tagId/updateTagById.ts
+++ b/lib/api/controllers/tags/tagId/updateTagById.ts
@@ -19,7 +19,7 @@ export default async function updateTagById(
};
}
- const { name, archiveAsScreenshot, archiveAsMonolith, archiveAsPDF, archiveAsReadable, archiveAsWaybackMachine } = dataValidation.data;
+ const { name } = dataValidation.data;
const tagNameIsTaken = await prisma.tag.findFirst({
where: {
@@ -52,11 +52,6 @@ export default async function updateTagById(
},
data: {
name: name,
- archiveAsScreenshot: archiveAsScreenshot,
- archiveAsMonolith: archiveAsMonolith,
- archiveAsPDF: archiveAsPDF,
- archiveAsReadable: archiveAsReadable,
- archiveAsWaybackMachine: archiveAsWaybackMachine,
},
});
diff --git a/lib/shared/schemaValidation.ts b/lib/shared/schemaValidation.ts
index 2d6fbb1b..5377224d 100644
--- a/lib/shared/schemaValidation.ts
+++ b/lib/shared/schemaValidation.ts
@@ -213,11 +213,6 @@ export type UpdateCollectionSchemaType = z.infer<typeof UpdateCollectionSchema>;
export const UpdateTagSchema = z.object({
name: z.string().trim().max(50),
- archiveAsScreenshot: z.boolean().nullable(),
- archiveAsMonolith: z.boolean().nullable(),
- archiveAsPDF: z.boolean().nullable(),
- archiveAsReadable: z.boolean().nullable(),
- archiveAsWaybackMachine: z.boolean().nullable(),
});
export type UpdateTagSchemaType = z.infer<typeof UpdateTagSchema>;
@@ -228,3 +223,19 @@ export const PostRssSubscriptionSchema = z.object({
collectionId: z.number().optional(),
collectionName: z.string().max(50).optional(),
});
+
+export const PostArchivalTagSchema = z.object({
+ tags: z.array(
+ z.object({
+ value: z.number().optional(),
+ label: z.string(),
+ newTag: z.boolean(),
+ archiveAsScreenshot: z.boolean(),
+ archiveAsMonolith: z.boolean(),
+ archiveAsPDF: z.boolean(),
+ archiveAsReadable: z.boolean(),
+ archiveAsWaybackMachine: z.boolean(),
+ aiTag: z.boolean()
+ })
+ ),
+});
\ No newline at end of file
diff --git a/pages/api/v1/tags/archival/index.ts b/pages/api/v1/tags/archival/index.ts
index 04090d9b..a29c97d1 100644
--- a/pages/api/v1/tags/archival/index.ts
+++ b/pages/api/v1/tags/archival/index.ts
@@ -1,19 +1,82 @@
+import { prisma } from "@/lib/api/db";
import verifyUser from "@/lib/api/verifyUser";
+import { PostArchivalTagSchema } from "@/lib/shared/schemaValidation";
import { NextApiRequest, NextApiResponse } from "next";
export default async function archivalTags(req: NextApiRequest, res: NextApiResponse) {
const user = await verifyUser({ req, res });
if (!user) return;
- // The body should be like this
- // {
- // tags: [{ id: 1, archiveAsScreenshot: true }, { id: 2, archiveAsMonolith: true }]
- //}
+ if (req.method === "POST") {
+ const dataValidation = PostArchivalTagSchema.safeParse(req.body);
- // Update or create
- // Will sometimes sent ID, sometimes not
+ if (!dataValidation.success) {
+ return res.status(400).json({
+ response: `Error: ${dataValidation.error.issues[0].message
+ } [${dataValidation.error.issues[0].path.join(", ")}]`,
+ });
+ }
- if (req.method === "POST") {
+ const { tags } = dataValidation.data;
+
+ try {
+ await prisma.$transaction(async (tx) => {
+ for (const tag of tags) {
+ if (tag.newTag) {
+ const tagNameIsTaken = await tx.tag.findFirst({
+ where: {
+ ownerId: user.id,
+ name: tag.label,
+ },
+ });
+
+ if (tagNameIsTaken) {
+ throw new Error(`Tag name "${tag.label}" already exists`);
+ }
+
+ await tx.tag.create({
+ data: {
+ name: tag.label,
+ ownerId: user.id,
+ archiveAsScreenshot: tag.archiveAsScreenshot,
+ archiveAsMonolith: tag.archiveAsMonolith,
+ archiveAsPDF: tag.archiveAsPDF,
+ archiveAsReadable: tag.archiveAsReadable,
+ archiveAsWaybackMachine: tag.archiveAsWaybackMachine,
+ aiTag: tag.aiTag,
+ },
+ });
+ } else if (tag.value) {
+ const targetTag = await tx.tag.findUnique({
+ where: { id: tag.value },
+ });
+
+ if (!targetTag) {
+ throw new Error(`Tag with ID ${tag.value} not found`);
+ }
+
+ if (targetTag.ownerId !== user.id) {
+ throw new Error(`Permission denied for tag ID ${tag.value}`);
+ }
+
+ await tx.tag.update({
+ where: { id: tag.value },
+ data: {
+ archiveAsScreenshot: tag.archiveAsScreenshot,
+ archiveAsMonolith: tag.archiveAsMonolith,
+ archiveAsPDF: tag.archiveAsPDF,
+ archiveAsReadable: tag.archiveAsReadable,
+ archiveAsWaybackMachine: tag.archiveAsWaybackMachine,
+ aiTag: tag.aiTag,
+ },
+ });
+ }
+ }
+ return res.status(200).json({ response: "Tags updated successfully" });
+ });
+ } catch (error: any) {
+ return res.status(400).json({ response: error.message });
+ }
}
}