Skip to content

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

add remove highlight server side

daniel31x13d703ff072c7f
diff --git a/components/Preservation/ReadableView.tsx b/components/Preservation/ReadableView.tsx
index 0b63bb63..773549c4 100644
--- a/components/Preservation/ReadableView.tsx
+++ b/components/Preservation/ReadableView.tsx
@@ -16,6 +16,7 @@ import ClickAwayHandler from "@/components/ClickAwayHandler";
import {
useGetLinkHighlights,
usePostHighlight,
+ useRemoveHighlight,
} from "@/hooks/store/highlights";
import { Highlight } from "@prisma/client";
@@ -29,8 +30,9 @@ export default function ReadableView({ link }: Props) {
const isPublicRoute = router.pathname.startsWith("/public");
const permissions = usePermissions(link?.collection?.id as number);
- const postHighlight = usePostHighlight();
+ const postHighlight = usePostHighlight(link?.id as number);
const { data: linkHighlights } = useGetLinkHighlights(link?.id as number);
+ const deleteHighlight = useRemoveHighlight(link?.id as number);
const [linkContent, setLinkContent] = useState("");
const [showSelectionMenu, setShowSelectionMenu] = useState(false);
diff --git a/hooks/store/highlights.tsx b/hooks/store/highlights.tsx
index ffcce096..6e84b7f5 100644
--- a/hooks/store/highlights.tsx
+++ b/hooks/store/highlights.tsx
@@ -50,11 +50,13 @@ const useUpdateHighlight = () => {
highlight.id === data.id ? data : highlight
)
);
+
+ // queryClient.invalidateQueries({ queryKey: ["highlights", data.linkId] });
},
});
};
-const usePostHighlight = () => {
+const usePostHighlight = (linkId: number) => {
const queryClient = useQueryClient();
return useMutation({
@@ -72,26 +74,18 @@ const usePostHighlight = () => {
return data.response;
},
- onSuccess: (data: Highlight[]) => {
- queryClient.setQueryData(["highlights"], (oldData: Highlight[] = []) => {
- const updatedHighlights = oldData.map((highlight) => {
- const updatedHighlight = data.find((t) => t.id === highlight.id);
- return updatedHighlight
- ? { ...highlight, ...updatedHighlight }
- : highlight;
- });
-
- const newHighlights = data.filter(
- (t) => !oldData.some((highlight) => highlight.id === t.id)
- );
-
- return [...updatedHighlights, ...newHighlights];
- });
+ onSuccess: (data: Highlight) => {
+ queryClient.setQueryData(
+ ["highlights", linkId],
+ (oldData: Highlight[]) => {
+ return [...oldData, data];
+ }
+ );
},
});
};
-const useRemoveHighlight = () => {
+const useRemoveHighlight = (linkId: number) => {
const queryClient = useQueryClient();
return useMutation({
@@ -105,9 +99,9 @@ const useRemoveHighlight = () => {
return data.response;
},
- onSuccess: (data, variables) => {
- queryClient.setQueryData(["highlights"], (oldData: any) =>
- oldData.filter((highlight: Highlight) => highlight.id !== variables)
+ onSuccess: (data) => {
+ queryClient.setQueryData(["highlights", linkId], (oldData: any) =>
+ oldData.filter((highlight: Highlight) => highlight.id !== data)
);
},
});
diff --git a/lib/api/controllers/highlights/deleteHighlightById.ts b/lib/api/controllers/highlights/deleteHighlightById.ts
new file mode 100644
index 00000000..fc2033ce
--- /dev/null
+++ b/lib/api/controllers/highlights/deleteHighlightById.ts
@@ -0,0 +1,18 @@
+import { prisma } from "@/lib/api/db";
+
+export default async function deleteHighlightById(
+ userId: number,
+ highlightId: number
+) {
+ if (!highlightId)
+ return { response: "Please choose a valid highlight.", status: 401 };
+
+ const targetHighlight = await prisma.highlight.delete({
+ where: {
+ id: highlightId,
+ userId,
+ },
+ });
+
+ return { response: targetHighlight.id, status: 200 };
+}
diff --git a/pages/api/v1/highlights/[id].ts b/pages/api/v1/highlights/[id].ts
index e69de29b..a37b4f83 100644
--- a/pages/api/v1/highlights/[id].ts
+++ b/pages/api/v1/highlights/[id].ts
@@ -0,0 +1,22 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+import verifyUser from "@/lib/api/verifyUser";
+import deleteHighlightById from "@/lib/api/controllers/highlights/deleteHighlightById";
+
+export default async function highlights(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ const user = await verifyUser({ req, res });
+ if (!user) return;
+
+ if (req.method === "DELETE") {
+ const highlights = await deleteHighlightById(
+ user.id,
+ Number(req.query.id as string)
+ );
+
+ return res
+ .status(highlights?.status || 500)
+ .json({ response: highlights?.response });
+ }
+}