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.

Add Archive

Tim Wilson19cbef5d00d2
diff --git a/apps/web/ui/links/links-container.tsx b/apps/web/ui/links/links-container.tsx
index 0ccd99f324..87e96d630e 100644
--- a/apps/web/ui/links/links-container.tsx
+++ b/apps/web/ui/links/links-container.tsx
@@ -137,6 +137,7 @@ function LinksList({
{links && (
<LinksToolbar
loading={!!loading}
+ links={links}
linksCount={count ?? links?.length ?? 0}
/>
)}
diff --git a/apps/web/ui/links/links-toolbar.tsx b/apps/web/ui/links/links-toolbar.tsx
index 126b961208..980da59073 100644
--- a/apps/web/ui/links/links-toolbar.tsx
+++ b/apps/web/ui/links/links-toolbar.tsx
@@ -1,21 +1,48 @@
-import { LoadingSpinner, PaginationControls, usePagination } from "@dub/ui";
+import {
+ BoxArchive,
+ Button,
+ LoadingSpinner,
+ PaginationControls,
+ usePagination,
+} from "@dub/ui";
import { cn } from "@dub/utils";
+import { useArchiveLinkModal } from "../modals/archive-link-modal";
import { X } from "../shared/icons";
import ArchivedLinksHint from "./archived-links-hint";
import { useLinkSelection } from "./link-selection-provider";
+import { ResponseLink } from "./links-container";
export const LinksToolbar = ({
loading,
+ links,
linksCount,
}: {
loading: boolean;
+ links: ResponseLink[];
linksCount: number;
}) => {
const { selectedLinkIds, setSelectedLinkIds } = useLinkSelection();
const { pagination, setPagination } = usePagination();
+ const selectedLinks = links.filter(({ id }) => selectedLinkIds.includes(id));
+ const allArchived = selectedLinks.every(({ archived }) => archived);
+
+ const { setShowArchiveLinkModal, ArchiveLinkModal } = useArchiveLinkModal({
+ props: selectedLinks,
+ onSuccess: () => setSelectedLinkIds([]),
+ });
+
+ const bulkActions = [
+ {
+ label: allArchived ? "Unarchive" : "Archive",
+ icon: BoxArchive,
+ action: () => setShowArchiveLinkModal(true),
+ },
+ ];
+
return (
<>
+ <ArchiveLinkModal />
{/* Leave room at bottom of list */}
<div className="h-[90px]" />
@@ -75,7 +102,18 @@ export const LinksToolbar = ({
</div>
{/* Controls */}
- <div className="flex items-center gap-2"></div>
+ <div className="flex items-center gap-2">
+ {bulkActions.map(({ label, icon: Icon, action }) => (
+ <Button
+ type="button"
+ variant="secondary"
+ className="h-7 px-2.5"
+ icon={<Icon className="size-4" />}
+ text={label}
+ onClick={action}
+ />
+ ))}
+ </div>
</div>
</div>
</div>
diff --git a/apps/web/ui/links/simple-link-card.tsx b/apps/web/ui/links/simple-link-card.tsx
new file mode 100644
index 0000000000..35e6efed58
--- /dev/null
+++ b/apps/web/ui/links/simple-link-card.tsx
@@ -0,0 +1,43 @@
+import { LinkProps } from "@/lib/types";
+import { ArrowTurnRight2, LinkLogo } from "@dub/ui";
+import { getApexDomain, getPrettyUrl } from "@dub/utils";
+import { cn } from "@dub/utils/src";
+
+export function SimpleLinkCard({
+ link,
+ className,
+}: {
+ link: Pick<LinkProps, "shortLink" | "url">;
+ className?: string;
+}) {
+ return (
+ <div
+ className={cn(
+ "flex items-center gap-3 rounded-xl border border-neutral-200 bg-white px-4 py-3.5",
+ className,
+ )}
+ >
+ <div className="relative flex-none rounded-full border border-neutral-200 bg-gradient-to-t from-neutral-100 sm:p-1.5">
+ <LinkLogo
+ apexDomain={getApexDomain(link.url)}
+ className="size-4 shrink-0 sm:size-5"
+ />
+ </div>
+ <div className="flex flex-col text-sm leading-tight">
+ <span className="truncate text-sm font-semibold text-neutral-800">
+ {getPrettyUrl(link.shortLink)}
+ </span>
+ <div className="flex items-center gap-1">
+ <ArrowTurnRight2 className="h-3 w-3 shrink-0 text-neutral-400" />
+ {link.url ? (
+ <span title={link.url} className="truncate text-neutral-500">
+ {getPrettyUrl(link.url)}
+ </span>
+ ) : (
+ <span className="truncate text-neutral-400">No URL configured</span>
+ )}
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/apps/web/ui/modals/archive-link-modal.tsx b/apps/web/ui/modals/archive-link-modal.tsx
index 00b1f88a8f..db181cb204 100644
--- a/apps/web/ui/modals/archive-link-modal.tsx
+++ b/apps/web/ui/modals/archive-link-modal.tsx
@@ -1,8 +1,8 @@
import { mutatePrefix } from "@/lib/swr/mutate";
import useWorkspace from "@/lib/swr/use-workspace";
import { LinkProps } from "@/lib/types";
-import { Button, LinkLogo, Modal, useToastWithUndo } from "@dub/ui";
-import { getApexDomain, linkConstructor } from "@dub/utils";
+import { Button, Modal, useToastWithUndo } from "@dub/ui";
+import { capitalize, pluralize } from "@dub/utils";
import {
Dispatch,
MouseEvent,
@@ -12,19 +12,20 @@ import {
useState,
} from "react";
import { toast } from "sonner";
+import { SimpleLinkCard } from "../links/simple-link-card";
const sendArchiveRequest = ({
- linkId,
+ linkIds,
archive,
workspaceId,
}: {
- linkId: string;
+ linkIds: string[];
archive: boolean;
workspaceId?: string;
}) => {
- return fetch(`/api/links/${linkId}?workspaceId=${workspaceId}`, {
+ return fetch(`/api/links/bulk?workspaceId=${workspaceId}`, {
method: "PATCH",
- body: JSON.stringify({ archived: archive }),
+ body: JSON.stringify({ linkIds, data: { archived: archive } }),
headers: {
"Content-Type": "application/json",
},
@@ -34,7 +35,8 @@ const sendArchiveRequest = ({
type ArchiveLinkModalProps = {
showArchiveLinkModal: boolean;
setShowArchiveLinkModal: Dispatch<SetStateAction<boolean>>;
- props: LinkProps;
+ links: LinkProps[];
+ onSuccess?: () => void;
};
function ArchiveLinkModal(props: ArchiveLinkModalProps) {
@@ -50,31 +52,32 @@ function ArchiveLinkModal(props: ArchiveLinkModalProps) {
function ArchiveLinkModalInner({
setShowArchiveLinkModal,
- props,
+ links,
+ onSuccess,
}: ArchiveLinkModalProps) {
const toastWithUndo = useToastWithUndo();
+ const archived = links.every((link) => link.archived);
+ const actionText = archived ? "unarchive" : "archive";
+
const { id: workspaceId } = useWorkspace();
const [archiving, setArchiving] = useState(false);
- const apexDomain = getApexDomain(props.url);
-
- const { key, domain } = props;
- const shortlink = useMemo(() => {
- return linkConstructor({
- key,
- domain,
- pretty: true,
- });
- }, [key, domain]);
+ // const shortlink = useMemo(() => {
+ // return linkConstructor({
+ // key,
+ // domain,
+ // pretty: true,
+ // });
+ // }, [key, domain]);
const handleArchiveRequest = async (event: MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
setArchiving(true);
const res = await sendArchiveRequest({
- linkId: props.id,
- archive: !props.archived,
+ linkIds: links.map(({ id }) => id),
+ archive: !archived,
workspaceId,
});
setArchiving(false);
@@ -89,17 +92,18 @@ function ArchiveLinkModalInner({
setShowArchiveLinkModal(false);
toastWithUndo({
id: "link-archive-undo-toast",
- message: `Successfully ${props.archived ? "unarchived" : "archived"} link!`,
+ message: `Successfully ${actionText}d ${pluralize("link", links.length)}!`,
undo: undoAction,
duration: 5000,
});
+ onSuccess?.();
};
const undoAction = () => {
toast.promise(
sendArchiveRequest({
- linkId: props.id,
- archive: props.archived,
+ linkIds: links.map(({ id }) => id),
+ archive: archived,
workspaceId,
}),
{
@@ -115,31 +119,54 @@ function ArchiveLinkModalInner({
return (
<>
- <div className="flex flex-col items-center justify-center space-y-3 border-b border-neutral-200 px-4 py-4 pt-8 text-center sm:px-16">
- <LinkLogo apexDomain={apexDomain} />
- <h3 className="text-lg font-medium">
- {props.archived ? "Unarchive" : "Archive"} {shortlink}
+ <div className="space-y-2 border-b border-neutral-200 p-4 sm:p-6">
+ <h3 className="text-lg font-medium leading-none">
+ {capitalize(actionText)}{" "}
+ {links.length > 1 ? `${links.length} links` : "link"}
</h3>
- <p className="text-sm text-neutral-500">
- {props.archived
- ? "By unarchiving this link, it will show up on your main dashboard again."
- : "Archived links will still work - they just won't show up on your main dashboard."}
+ </div>
+
+ <div className="bg-neutral-50 p-4 sm:p-6">
+ <p className="text-sm text-neutral-800">
+ Are you sure you want to {actionText} the following{" "}
+ {pluralize("link", links.length)}?
</p>
+
+ <div className="scrollbar-hide mt-4 flex max-h-[190px] flex-col gap-2 overflow-y-auto rounded-2xl border border-neutral-200 p-2">
+ {links.map((link) => (
+ <SimpleLinkCard key={link.id} link={link} />
+ ))}
+ </div>
</div>
- <div className="flex flex-col space-y-6 bg-neutral-50 px-4 py-8 text-left sm:px-16">
+ {/* <LinkLogo apexDomain={getApexDomain(links[0].url)} /> */}
+
+ <div className="flex items-center justify-end gap-2 border-t border-neutral-200 bg-neutral-50 px-4 py-5 sm:px-6">
+ <Button
+ onClick={() => setShowArchiveLinkModal(false)}
+ variant="secondary"
+ text="Cancel"
+ className="h-8 w-fit px-3"
+ />
<Button
onClick={handleArchiveRequest}
autoFocus
loading={archiving}
- text={`Confirm ${props.archived ? "unarchive" : "archive"}`}
+ text={`${capitalize(actionText)} ${pluralize("link", links.length)}`}
+ className="h-8 w-fit px-3"
/>
</div>
</>
);
}
-export function useArchiveLinkModal({ props }: { props: LinkProps }) {
+export function useArchiveLinkModal({
+ props,
+ onSuccess,
+}: {
+ props: LinkProps | LinkProps[];
+ onSuccess?: () => void;
+}) {
const [showArchiveLinkModal, setShowArchiveLinkModal] = useState(false);
const ArchiveLinkModalCallback = useCallback(() => {
@@ -147,7 +174,8 @@ export function useArchiveLinkModal({ props }: { props: LinkProps }) {
<ArchiveLinkModal
showArchiveLinkModal={showArchiveLinkModal}
setShowArchiveLinkModal={setShowArchiveLinkModal}
- props={props}
+ links={Array.isArray(props) ? props : [props]}
+ onSuccess={onSuccess}
/>
) : null;
}, [showArchiveLinkModal, setShowArchiveLinkModal]);