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.

use archive link modal for both archive and unarchive

Steven Tey444f4ecb2b39
diff --git a/components/app/links/link-card.tsx b/components/app/links/link-card.tsx
index ccc2b0f5c3..ea679665c6 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, { mutate } from "swr";
+import useSWR 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";
@@ -27,7 +27,6 @@ import { LinkProps } from "@/lib/types";
import {
fetcher,
getApexDomain,
- getQueryString,
linkConstructor,
nFormatter,
timeAgo,
@@ -73,6 +72,7 @@ export default function LinkCard({ props }: { props: LinkProps }) {
});
const { setShowArchiveLinkModal, ArchiveLinkModal } = useArchiveLinkModal({
props,
+ archived: !archived,
});
const { setShowDeleteLinkModal, DeleteLinkModal } = useDeleteLinkModal({
props,
@@ -207,42 +207,13 @@ export default function LinkCard({ props }: { props: LinkProps }) {
<button
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);
- }
- });
+ setOpenPopover(false);
+ setShowArchiveLinkModal(true);
}}
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={
- !archived
- ? "Archive"
- : unarchiving
- ? "Unarchiving..."
- : "Unarchive"
- }
+ text={archived ? "Unarchive" : "Archive"}
icon={<Archive className="h-4 w-4" />}
/>
</button>
diff --git a/components/app/modals/archive-link-modal.tsx b/components/app/modals/archive-link-modal.tsx
index af944bafad..a58ae24a2b 100644
--- a/components/app/modals/archive-link-modal.tsx
+++ b/components/app/modals/archive-link-modal.tsx
@@ -18,10 +18,12 @@ function ArchiveLinkModal({
showArchiveLinkModal,
setShowArchiveLinkModal,
props,
+ archived,
}: {
showArchiveLinkModal: boolean;
setShowArchiveLinkModal: Dispatch<SetStateAction<boolean>>;
props: LinkProps;
+ archived: boolean;
}) {
const router = useRouter();
const { slug } = router.query;
@@ -51,10 +53,13 @@ function ArchiveLinkModal({
width={20}
height={20}
/>
- <h3 className="text-lg font-medium">Archive {shortlink}</h3>
+ <h3 className="text-lg font-medium">
+ {archived ? "Archive" : "Unarchive"} {shortlink}
+ </h3>
<p className="text-sm text-gray-500">
- Archived links will still work - they just won't show up on your
- main dashboard.
+ {archived
+ ? "Archived links will still work - they just won't show up on your main dashboard."
+ : "By unarchiving this link, it will show up on your main dashboard again."}
</p>
</div>
@@ -68,7 +73,7 @@ function ArchiveLinkModal({
? `/api/projects/${slug}/domains/${domain}/links/${props.key}/archive`
: `/api/links/${props.key}/archive`,
{
- method: "POST",
+ method: archived ? "POST" : "DELETE",
headers: {
"Content-Type": "application/json",
},
@@ -97,7 +102,7 @@ function ArchiveLinkModal({
{archiving ? (
<LoadingDots color="#808080" />
) : (
- <p>Confirm archive</p>
+ <p>Confirm {archived ? "archive" : "unarchive"}</p>
)}
</button>
</div>
@@ -106,7 +111,13 @@ function ArchiveLinkModal({
);
}
-export function useArchiveLinkModal({ props }: { props?: LinkProps }) {
+export function useArchiveLinkModal({
+ props,
+ archived = true,
+}: {
+ props: LinkProps;
+ archived: boolean;
+}) {
const [showArchiveLinkModal, setShowArchiveLinkModal] = useState(false);
const ArchiveLinkModalCallback = useCallback(() => {
@@ -115,6 +126,7 @@ export function useArchiveLinkModal({ props }: { props?: LinkProps }) {
showArchiveLinkModal={showArchiveLinkModal}
setShowArchiveLinkModal={setShowArchiveLinkModal}
props={props}
+ archived={archived}
/>
) : null;
}, [showArchiveLinkModal, setShowArchiveLinkModal]);