⚡️⚡️⚡️ 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
feat: enhance useDeleteCollection with improved error handling and optimistic updates
daniel31x13da8761387f44
diff --git a/apps/mobile/components/CollectionListing.tsx b/apps/mobile/components/CollectionListing.tsxindex 499ecdef..88891e63 100644--- a/apps/mobile/components/CollectionListing.tsx+++ b/apps/mobile/components/CollectionListing.tsx@@ -19,7 +19,7 @@ const CollectionListing = ({ collection }: Props) => {const router = useRouter();const { colorScheme } = useColorScheme();- const deleteCollection = useDeleteCollection(auth);+ const deleteCollection = useDeleteCollection({ auth, Alert });return (<ContextMenu.Root>diff --git a/apps/web/components/ModalContent/DeleteCollectionModal.tsx b/apps/web/components/ModalContent/DeleteCollectionModal.tsxindex d053b9f9..813cd7b7 100644--- a/apps/web/components/ModalContent/DeleteCollectionModal.tsx+++ b/apps/web/components/ModalContent/DeleteCollectionModal.tsx@@ -1,5 +1,4 @@import React, { useEffect, useState } from "react";-import TextInput from "@/components/TextInput";import { CollectionIncludingMembersAndLinkCount } from "@linkwarden/types/global";import { useRouter } from "next/router";import usePermissions from "@/hooks/usePermissions";@@ -22,7 +21,6 @@ export default function DeleteCollectionModal({const { t } = useTranslation();const [collection, setCollection] =useState<CollectionIncludingMembersAndLinkCount>(activeCollection);- const [submitLoader, setSubmitLoader] = useState(false);const router = useRouter();const permissions = usePermissions(collection.id as number);@@ -30,32 +28,15 @@ export default function DeleteCollectionModal({setCollection(activeCollection);}, []);- const deleteCollection = useDeleteCollection();+ const deleteCollection = useDeleteCollection({ toast, t });const submit = async () => {- if (!submitLoader) {- setSubmitLoader(true);- if (!collection) return null;+ if (!collection) return null;- setSubmitLoader(true);+ deleteCollection.mutateAsync(collection.id as number);- const load = toast.loading(t("deleting_collection"));-- deleteCollection.mutateAsync(collection.id as number, {- onSettled: (data, error) => {- setSubmitLoader(false);- toast.dismiss(load);-- if (error) {- toast.error(error.message);- } else {- onClose();- toast.success(t("deleted"));- router.push("/collections");- }- },- });- }+ onClose();+ router.push("/collections");};return (diff --git a/packages/router/collections.tsx b/packages/router/collections.tsxindex 0100d88d..90dd0504 100644--- a/packages/router/collections.tsx+++ b/packages/router/collections.tsx@@ -4,6 +4,9 @@ import {MobileAuth,} from "@linkwarden/types/global";import { useSession } from "next-auth/react";+import type toaster from "react-hot-toast";+import { TFunction } from "next-i18next";+import type { Alert as Alert_ } from "react-native";const useCollections = (auth?: MobileAuth) => {let status: "loading" | "authenticated" | "unauthenticated";@@ -109,7 +112,17 @@ const useUpdateCollection = () => {});};-const useDeleteCollection = (auth?: MobileAuth) => {+const useDeleteCollection = ({+ auth,+ Alert,+ toast,+ t,+}: {+ auth?: MobileAuth;+ Alert?: typeof Alert_;+ toast?: typeof toaster;+ t?: TFunction;+}) => {const queryClient = useQueryClient();return useMutation({@@ -133,6 +146,44 @@ const useDeleteCollection = (auth?: MobileAuth) => {return data.response;},+ onMutate: async (id) => {+ await queryClient.cancelQueries({ queryKey: ["collections"] });+ await queryClient.cancelQueries({ queryKey: ["dashboardData"] });++ const previousCollections = queryClient.getQueryData(["collections"]);+ const previousDashboard = queryClient.getQueryData(["dashboardData"]);++ queryClient.setQueryData(["collections"], (oldData: any) => {+ return oldData?.filter((collection: any) => collection.id !== id);+ });++ queryClient.setQueryData(["dashboardData"], (oldData: any) => {+ if (!oldData) return oldData;++ return {+ ...oldData,+ links:+ oldData.links?.filter((link: any) => link.collectionId !== id) ||+ [],+ numberOfPinnedLinks:+ oldData.links?.filter(+ (link: any) => link.collectionId !== id && link.isPinned+ ).length || 0,+ };+ });++ return { previousCollections, previousDashboard };+ },+ onError: (error, _variables, context) => {+ if (toast && t) toast.error(t(error.message));+ else if (Alert)+ Alert.alert("Error", "There was an error adding the link.");++ if (!context) return;++ queryClient.setQueryData(["collections"], context.previousCollections);+ queryClient.setQueryData(["dashboardData"], context.previousDashboard);+ },onSuccess: (data, id) => {queryClient.setQueryData(["collections"], (oldData: any) => {return oldData.filter((collection: any) => collection.id !== id);@@ -153,6 +204,9 @@ const useDeleteCollection = (auth?: MobileAuth) => {).length || 0,};});++ queryClient.invalidateQueries({ queryKey: ["tags"] });+ queryClient.invalidateQueries({ queryKey: ["links"] });},});};