⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
fix deleting archival tags
Isaac671f27ccde68
diff --git a/components/InputSelect/types.ts b/components/InputSelect/types.tsindex 20c7c570..3313f8d4 100644--- a/components/InputSelect/types.ts+++ b/components/InputSelect/types.ts@@ -5,13 +5,13 @@ export interface Option {}export interface ArchivalTagOption extends Option {- archiveAsScreenshot: boolean;- archiveAsMonolith: boolean;- archiveAsPDF: boolean;- archiveAsReadable: boolean;- archiveAsWaybackMachine: boolean;- aiTag: boolean;- newTag: boolean;+ archiveAsScreenshot: boolean | null;+ archiveAsMonolith: boolean | null;+ archiveAsPDF: boolean | null;+ archiveAsReadable: boolean | null;+ archiveAsWaybackMachine: boolean | null;+ aiTag: boolean | null;+ newTag: boolean | null;}export type ArchivalOptionKeys = keyof Omit<ArchivalTagOption, "label" | "value" | "__isNew__">;diff --git a/hooks/store/tags.tsx b/hooks/store/tags.tsxindex 067d35ad..da290e94 100644--- a/hooks/store/tags.tsx+++ b/hooks/store/tags.tsx@@ -54,7 +54,7 @@ const useUpdateArchivalTags = () => {return useMutation({mutationFn: async (tags: ArchivalTagOption[]) => {const response = await fetch("/api/v1/tags/archival", {- body: JSON.stringify(tags),+ body: JSON.stringify({ tags }),headers: {"Content-Type": "application/json",},diff --git a/hooks/useArchivalTags.ts b/hooks/useArchivalTags.tsindex 070cc81d..9d0e5060 100644--- a/hooks/useArchivalTags.ts+++ b/hooks/useArchivalTags.ts@@ -3,13 +3,13 @@ import { Tag } from '@prisma/client';import { useState, useEffect } from 'react';import { useTranslation } from 'react-i18next';-const isArchivalTag = (tag: Tag) =>- tag.archiveAsScreenshot ||- tag.archiveAsMonolith ||- tag.archiveAsPDF ||- tag.archiveAsReadable ||- tag.archiveAsWaybackMachine ||- tag.aiTag;+export const isArchivalTag = (tag: ArchivalTagOption | Tag) =>+ typeof tag.archiveAsScreenshot === "boolean" ||+ typeof tag.archiveAsMonolith === "boolean" ||+ typeof tag.archiveAsPDF === "boolean" ||+ typeof tag.archiveAsReadable === "boolean" ||+ typeof tag.archiveAsWaybackMachine === "boolean" ||+ typeof tag.aiTag === "boolean";const useArchivalTags = (initialTags: Tag[]) => {const [archivalTags, setArchivalTags] = useState<ArchivalTagOption[]>([]);@@ -64,7 +64,9 @@ const useArchivalTags = (initialTags: Tag[]) => {};const removeTag = (tagToDelete: ArchivalTagOption) => {- setArchivalTags(prev => prev.filter(tag => tag.label !== tagToDelete.label));+ setArchivalTags(prev => prev.map(t =>+ t.label === tagToDelete.label ? { ...t, archiveAsScreenshot: null, archiveAsMonolith: null, archiveAsPDF: null, archiveAsReadable: null, archiveAsWaybackMachine: null, aiTag: null } : t+ ));if (!tagToDelete.__isNew__) {const resetTag: ArchivalTagOption = {diff --git a/lib/shared/schemaValidation.ts b/lib/shared/schemaValidation.tsindex 5377224d..0f226964 100644--- a/lib/shared/schemaValidation.ts+++ b/lib/shared/schemaValidation.ts@@ -230,12 +230,12 @@ export const PostArchivalTagSchema = 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()+ archiveAsScreenshot: z.boolean().nullable(),+ archiveAsMonolith: z.boolean().nullable(),+ archiveAsPDF: z.boolean().nullable(),+ archiveAsReadable: z.boolean().nullable(),+ archiveAsWaybackMachine: z.boolean().nullable(),+ aiTag: z.boolean().nullable()})),});\ No newline at end of filediff --git a/pages/api/v1/tags/archival/index.ts b/pages/api/v1/tags/archival/index.tsindex a29c97d1..5e342a1c 100644--- a/pages/api/v1/tags/archival/index.ts+++ b/pages/api/v1/tags/archival/index.ts@@ -19,64 +19,66 @@ export default async function archivalTags(req: NextApiRequest, res: NextApiRespconst { 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,- },- });+ console.log(tags);- if (tagNameIsTaken) {- throw new Error(`Tag name "${tag.label}" already exists`);- }+ // This is still broken+ tags.forEach(async (tag) => {+ if (tag.newTag) {+ const tagNameIsTaken = await prisma.tag.findFirst({+ where: {+ ownerId: user.id,+ name: tag.label,+ },+ });- 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 (tagNameIsTaken)+ return {+ response: "Tag names should be unique.",+ status: 400,+ };- if (!targetTag) {- throw new Error(`Tag with ID ${tag.value} not found`);- }+ await prisma.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 prisma.tag.findUnique({+ where: {+ id: tag.value,+ },+ });- 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,- },- });- }+ if (targetTag?.ownerId !== user.id) {+ return {+ response: "Permission denied.",+ status: 401,+ };}- return res.status(200).json({ response: "Tags updated successfully" });- });- } catch (error: any) {- return res.status(400).json({ response: error.message });- }+ await prisma.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." });}}diff --git a/pages/settings/preference.tsx b/pages/settings/preference.tsxindex 63a29d1c..190c596c 100644--- a/pages/settings/preference.tsx+++ b/pages/settings/preference.tsx@@ -10,10 +10,10 @@ import { AiTaggingMethod, LinksRouteTo } from "@prisma/client";import { useUpdateUser, useUser } from "@/hooks/store/user";import ArchivalTagSelection from "@/components/InputSelect/ArchivalTagSelection";import { useConfig } from "@/hooks/store/config";-import { useTags } from "@/hooks/store/tags";+import { useTags, useUpdateArchivalTags } from "@/hooks/store/tags";import { cn } from "@/lib/client/utils";import TagSelection from "@/components/InputSelect/TagSelection";-import { useArchivalTags } from "@/hooks/useArchivalTags";+import { isArchivalTag, useArchivalTags } from "@/hooks/useArchivalTags";export default function Preference() {const { t } = useTranslation();@@ -21,6 +21,7 @@ export default function Preference() {const [submitLoader, setSubmitLoader] = useState(false);const { data: account } = useUser();const { data: tags } = useTags();+ const updateArchivalTags = useUpdateArchivalTags();const { ARCHIVAL_OPTIONS, archivalTags, options, addTags, toggleOption, removeTag } = useArchivalTags(tags ? tags : []);const updateUser = useUpdateUser();const [user, setUser] = useState(account);@@ -111,21 +112,32 @@ export default function Preference() {const load = toast.loading(t("applying_settings"));- await updateUser.mutateAsync(- { ...user },- {- onSettled: (_, error) => {- setSubmitLoader(false);- toast.dismiss(load);-- if (error) {- toast.error(error.message);- } else {- toast.success(t("settings_applied"));- }- },- }- );+ try {+ await updateUser.mutateAsync({ ...user });+ await updateArchivalTags.mutateAsync(archivalTags);+ toast.success(t("settings_applied"));+ } catch (error: any) {+ toast.error(error.message);+ } finally {+ setSubmitLoader(false);+ toast.dismiss(load);+ }++ // await updateUser.mutateAsync(+ // { ...user },+ // {+ // onSettled: (_, error) => {+ // setSubmitLoader(false);+ // toast.dismiss(load);++ // if (error) {+ // toast.error(error.message);+ // } else {+ // toast.success(t("settings_applied"));+ // }+ // },+ // }+ // );};return (@@ -344,7 +356,7 @@ export default function Preference() {<div className="p-3"><ArchivalTagSelection onChange={addTags} options={options} /><div className="flex flex-col gap-2">- {archivalTags && archivalTags.map((tag) => (+ {archivalTags && archivalTags.filter(isArchivalTag).map((tag) => (<div key={tag.label} className="w-full flex items-center justify-between bg-base-200 p-2 rounded first-of-type:mt-4"><span className="text-xl sm:text-lg text-white truncate max-w-[10rem]">{tag.label}</span><div className="flex items-center gap-1">