Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

feat: Allow links to be removed from the archive

Snazzahf99c932c16c0
diff --git a/components/app/links/link-card.tsx b/components/app/links/link-card.tsx
index 715908c1bf..77e83e581d 100644
--- a/components/app/links/link-card.tsx
+++ b/components/app/links/link-card.tsx
@@ -1,7 +1,7 @@
import Link from "next/link";
import { useRouter } from "next/router";
import { useState } from "react";
-import useSWR from "swr";
+import useSWR, { mutate } from "swr";
import { useAddEditLinkModal } from "@/components/app/modals/add-edit-link-modal";
import { useArchiveLinkModal } from "@/components/app/modals/archive-link-modal";
import { useDeleteLinkModal } from "@/components/app/modals/delete-link-modal";
@@ -26,6 +26,7 @@ import { LinkProps } from "@/lib/types";
import {
fetcher,
getApexDomain,
+ getQueryString,
linkConstructor,
nFormatter,
timeAgo,
@@ -67,6 +68,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {
props,
});
const [openPopover, setOpenPopover] = useState(false);
+ const [unarchiving, setUnarchiving] = useState(false);
const expired = expiresAt && new Date() > new Date(expiresAt);
@@ -150,7 +152,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {
</p>
<Popover
content={
- <div className="grid w-full gap-1 p-2 sm:w-40">
+ <div className="grid w-full gap-1 p-2 sm:w-48">
{slug && exceededUsage ? (
<Tooltip
content={
@@ -181,14 +183,35 @@ export default function LinkCard({ props }: { props: LinkProps }) {
</button>
)}
<button
- onClick={() => {
- setOpenPopover(false);
- setShowArchiveLinkModal(true);
+ onClick={(e) => {
+ e.preventDefault();
+ if (!archived) {
+ setOpenPopover(false);
+ setShowArchiveLinkModal(true);
+ return;
+ }
+ setUnarchiving(true);
+ fetch(
+ domain
+ ? `/api/projects/${slug}/domains/${domain}/links/${props.key}/archive`
+ : `/api/links/${props.key}/archive`,
+ { method: 'DELETE' }
+ ).then(async (res) => {
+ setUnarchiving(false);
+ setOpenPopover(false);
+ if (res.status === 200) {
+ mutate(domain
+ ? `/api/projects/${slug}/domains/${domain}/links${getQueryString(router)}`
+ : `/api/links${getQueryString(router)}`
+ );
+ setShowArchiveLinkModal(false);
+ }
+ });
}}
className="w-full rounded-md p-2 text-left text-sm font-medium text-gray-500 transition-all duration-75 hover:bg-gray-100"
>
<IconMenu
- text="Archive"
+ text={!archived ? 'Archive' : unarchiving ? 'Unarchiving...' : 'Remove from archive'}
icon={<Archive className="h-4 w-4" />}
/>
</button>
diff --git a/lib/api/links.ts b/lib/api/links.ts
index d06a888d96..89a38d237c 100644
--- a/lib/api/links.ts
+++ b/lib/api/links.ts
@@ -304,7 +304,7 @@ export async function deleteLink(domain: string, key: string) {
]);
}
-export async function archiveLink(domain: string, key: string) {
+export async function archiveLink(domain: string, key: string, archived = true) {
return await prisma.link.update({
where: {
domain_key: {
@@ -313,7 +313,7 @@ export async function archiveLink(domain: string, key: string) {
},
},
data: {
- archived: true,
+ archived,
},
});
}
diff --git a/pages/api/links/[key]/archive.ts b/pages/api/links/[key]/archive.ts
index 34e6391eeb..c8d6bbdc12 100644
--- a/pages/api/links/[key]/archive.ts
+++ b/pages/api/links/[key]/archive.ts
@@ -27,6 +27,9 @@ export default withUserAuth(
if (req.method === "PUT") {
const response = await archiveLink(domain, key);
return res.status(200).json(response);
+ } else if (req.method === "DELETE") {
+ const response = await archiveLink(domain, key, false);
+ return res.status(200).json(response);
} else {
res.setHeader("Allow", ["PUT"]);
return res
diff --git a/pages/api/projects/[slug]/domains/[domain]/links/[key]/archive.ts b/pages/api/projects/[slug]/domains/[domain]/links/[key]/archive.ts
index 9077c26070..824efdab78 100644
--- a/pages/api/projects/[slug]/domains/[domain]/links/[key]/archive.ts
+++ b/pages/api/projects/[slug]/domains/[domain]/links/[key]/archive.ts
@@ -12,6 +12,9 @@ export default withProjectAuth(
if (req.method === "PUT") {
const response = await archiveLink(domain, key);
return res.status(200).json(response);
+ } else if (req.method === "DELETE") {
+ const response = await archiveLink(domain, key, false);
+ return res.status(200).json(response);
} else {
res.setHeader("Allow", ["PUT"]);
return res