add pin to hover view + add number of pins to dashboard + bug fixes

This commit is contained in:
daniel31x13
2024-09-11 01:38:38 -04:00
parent fb1869ca7a
commit 9b1506a64e
11 changed files with 198 additions and 123 deletions
@@ -107,9 +107,15 @@ export default async function getDashboardData(
const links = [...recentlyAddedLinks, ...pinnedLinks].sort(
(a, b) => new Date(b.id).getTime() - new Date(a.id).getTime()
);
// Make sure links are unique
const uniqueLinks = links.filter(
(link, index, self) => index === self.findIndex((t) => t.id === link.id)
);
return {
data: {
links,
links: uniqueLinks,
numberOfPinnedLinks,
},
message: "Dashboard data fetched successfully.",
@@ -124,7 +124,7 @@ export default async function updateLinkById(
})),
},
pinnedBy:
data?.pinnedBy && data.pinnedBy[0]
data?.pinnedBy && data.pinnedBy[0].id === userId
? { connect: { id: userId } }
: { disconnect: { id: userId } },
},
+47
View File
@@ -0,0 +1,47 @@
import { useUpdateLink } from "@/hooks/store/links";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import toast from "react-hot-toast";
import { useTranslation } from "next-i18next";
import { useUser } from "@/hooks/store/user";
const usePinLink = () => {
const { t } = useTranslation();
const updateLink = useUpdateLink();
const { data: user = {} } = useUser();
// Return a function that can be used to pin/unpin the link
const pinLink = async (link: LinkIncludingShortenedCollectionAndTags) => {
const isAlreadyPinned = link?.pinnedBy && link.pinnedBy[0] ? true : false;
const load = toast.loading(t("updating"));
try {
await updateLink.mutateAsync(
{
...link,
pinnedBy: isAlreadyPinned ? [{ id: undefined }] : [{ id: user.id }],
},
{
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
toast.success(
isAlreadyPinned ? t("link_unpinned") : t("link_pinned")
);
}
},
}
);
} catch (e) {
toast.dismiss(load);
console.error(e);
}
};
return pinLink;
};
export default usePinLink;