⚡️⚡️⚡️ 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
update highlight functionality
daniel31x130de7988b2941
diff --git a/components/Preservation/ReadableView.tsx b/components/Preservation/ReadableView.tsxindex 0da4bfae..3c378dde 100644--- a/components/Preservation/ReadableView.tsx+++ b/components/Preservation/ReadableView.tsx@@ -174,12 +174,22 @@ export default function ReadableView({ link }: Props) {}, [linkContent, linkHighlights]);const handleHighlightSelection = async (- color: "yellow" | "red" | "blue" | "green"+ color: "yellow" | "red" | "blue" | "green",+ highlightId: number | null) => {- const selection = getHighlightedSection(color);- if (!selection) return;+ let selection = getHighlightedSection(color);- postHighlight.mutate(selection, {+ if (highlightId) {+ selection = linkHighlights?.find(+ (h) => h.id === selectionMenu.highlightId+ );++ if (selection) selection.color = color;+ }++ if (!selection && !highlightId) return;++ postHighlight.mutate(selection as Highlight, {onSuccess: (data) => {if (data) {setSelectionMenu({@@ -260,7 +270,8 @@ export default function ReadableView({ link }: Props) {key={color}onClick={() =>handleHighlightSelection(- color as "yellow" | "red" | "blue" | "green"+ color as "yellow" | "red" | "blue" | "green",+ selectionMenu.highlightId)}className={`w-5 h-5 rounded-full bg-${color}-500 hover:opacity-70 duration-100 relative`}@@ -272,7 +283,7 @@ export default function ReadableView({ link }: Props) {linkHighlights?.find((h) => h.id === selectionMenu.highlightId)?.color === color && (- <i className="bi-check text-black absolute inset-0 flex items-center justify-center" />+ <i className="bi-check2 text-sm text-black absolute inset-0 flex items-center justify-center" />)}</button>))}diff --git a/hooks/store/highlights.tsx b/hooks/store/highlights.tsxindex 6e84b7f5..3d23ca09 100644--- a/hooks/store/highlights.tsx+++ b/hooks/store/highlights.tsx@@ -26,36 +26,6 @@ const useGetLinkHighlights = (});};-const useUpdateHighlight = () => {- const queryClient = useQueryClient();-- return useMutation({- mutationFn: async (highlight: Highlight) => {- const response = await fetch(`/api/v1/highlights/${highlight.id}`, {- body: JSON.stringify(highlight),- headers: {- "Content-Type": "application/json",- },- method: "PUT",- });-- const data = await response.json();- if (!response.ok) throw new Error(data.response);-- return data.response;- },- onSuccess: (data) => {- queryClient.setQueryData(["highlights"], (oldData: any) =>- oldData.map((highlight: Highlight) =>- highlight.id === data.id ? data : highlight- )- );-- // queryClient.invalidateQueries({ queryKey: ["highlights", data.linkId] });- },- });-};-const usePostHighlight = (linkId: number) => {const queryClient = useQueryClient();@@ -78,7 +48,14 @@ const usePostHighlight = (linkId: number) => {queryClient.setQueryData(["highlights", linkId],(oldData: Highlight[]) => {- return [...oldData, data];+ const index = oldData.findIndex((h) => h?.id === data?.id);+ if (index !== -1) {+ const newData = [...oldData];+ newData[index] = data;+ return newData;+ } else {+ return [...oldData, data];+ }});},@@ -107,9 +84,4 @@ const useRemoveHighlight = (linkId: number) => {});};-export {- useGetLinkHighlights,- useUpdateHighlight,- usePostHighlight,- useRemoveHighlight,-};+export { useGetLinkHighlights, usePostHighlight, useRemoveHighlight };diff --git a/lib/api/controllers/highlights/postHighlight.ts b/lib/api/controllers/highlights/postOrUpdateHighlight.tssimilarity index 66%rename from lib/api/controllers/highlights/postHighlight.tsrename to lib/api/controllers/highlights/postOrUpdateHighlight.tsindex 47652b76..64dadd51 100644--- a/lib/api/controllers/highlights/postHighlight.ts+++ b/lib/api/controllers/highlights/postOrUpdateHighlight.ts@@ -1,7 +1,7 @@import { prisma } from "@/lib/api/db";import { PostHighlightSchemaType } from "@/lib/shared/schemaValidation";-export default async function postHighlight(+export default async function postOrUpdateHighlight(userId: number,body: PostHighlightSchemaType) {@@ -37,22 +37,22 @@ export default async function postHighlight(OR: [{startOffset: {- lte: body.startOffset,+ lt: body.startOffset,},endOffset: {- gte: body.endOffset,+ gt: body.endOffset,},},{startOffset: {- gte: body.startOffset,- lte: body.endOffset,+ gt: body.startOffset,+ lt: body.endOffset,},},{endOffset: {- gte: body.startOffset,- lte: body.endOffset,+ gt: body.startOffset,+ lt: body.endOffset,},},],@@ -66,6 +66,32 @@ export default async function postHighlight(};}+ const existingHighlight = await prisma.highlight.findFirst({+ where: {+ userId,+ linkId: body.linkId,+ startOffset: body.startOffset,+ endOffset: body.endOffset,+ },+ });++ if (existingHighlight) {+ const updatedHighlight = await prisma.highlight.update({+ where: {+ id: existingHighlight.id,+ },+ data: {+ color: body.color,+ comment: body.comment,+ },+ });++ return {+ status: 200,+ response: updatedHighlight,+ };+ }+// create highlightconst highlight = await prisma.highlight.create({data: {diff --git a/pages/api/v1/highlights/index.ts b/pages/api/v1/highlights/index.tsindex 81a05d65..15d7f012 100644--- a/pages/api/v1/highlights/index.ts+++ b/pages/api/v1/highlights/index.ts@@ -1,7 +1,7 @@import type { NextApiRequest, NextApiResponse } from "next";import verifyUser from "@/lib/api/verifyUser";import { PostHighlightSchema } from "@/lib/shared/schemaValidation";-import postHighlight from "@/lib/api/controllers/highlights/postHighlight";+import postOrUpdateHighlight from "@/lib/api/controllers/highlights/postOrUpdateHighlight";export default async function highlights(req: NextApiRequest,@@ -23,7 +23,7 @@ export default async function highlights(const body = dataValidation.data;- const highlights = await postHighlight(user.id, body);+ const highlights = await postOrUpdateHighlight(user.id, body);return res.status(highlights.status)