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 link export UI

Tim Wilson440f0417775c
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
index e90c6ec9e0..eeb1509730 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/page-client.tsx
@@ -3,7 +3,11 @@
import useWorkspace from "@/lib/swr/use-workspace";
import LinksContainer from "@/ui/links/links-container";
import { useAddEditLinkModal } from "@/ui/modals/add-edit-link-modal";
+import { useExportLinksModal } from "@/ui/modals/export-links-modal";
+import { ThreeDots } from "@/ui/shared/icons";
import {
+ AnimatedSizeContainer,
+ Button,
IconMenu,
MaxWidthWrapper,
Popover,
@@ -11,9 +15,16 @@ import {
TooltipContent,
useRouterStuff,
} from "@dub/ui";
-import { ChevronDown, FilePlus2, Sheet } from "lucide-react";
+import { CloudUpload, Download } from "@dub/ui/src/icons";
+import { FilePlus2, Sheet } from "lucide-react";
import { useRouter } from "next/navigation";
-import { useState } from "react";
+import {
+ Dispatch,
+ ReactNode,
+ SetStateAction,
+ useEffect,
+ useState,
+} from "react";
export default function WorkspaceLinksClient() {
const { AddEditLinkModal, AddEditLinkButton } = useAddEditLinkModal();
@@ -27,9 +38,9 @@ export default function WorkspaceLinksClient() {
<h1 className="text-2xl font-semibold tracking-tight text-black">
Links
</h1>
- <div className="flex">
+ <div className="flex gap-2">
<AddEditLinkButton />
- <AddLinkOptions />
+ <MoreLinkOptions />
</div>
</div>
</MaxWidthWrapper>
@@ -39,202 +50,185 @@ export default function WorkspaceLinksClient() {
);
}
-const AddLinkOptions = () => {
+const MoreLinkOptions = () => {
const router = useRouter();
- const { slug, nextPlan, exceededLinks } = useWorkspace();
+ const { slug } = useWorkspace();
const [openPopover, setOpenPopover] = useState(false);
- const { queryParams } = useRouterStuff();
+ const [state, setState] = useState<"default" | "import">("default");
+ const { ExportLinksModal, setShowExportLinksModal } = useExportLinksModal();
+
+ useEffect(() => {
+ if (!openPopover) setState("default");
+ }, [openPopover]);
return (
- <Popover
- content={
- <div className="w-full divide-y divide-gray-200 md:w-52">
- <div className="p-2">
- {exceededLinks ? (
- <Tooltip
- content={
- <TooltipContent
- title="Your workspace has exceeded its monthly links limit. We're still collecting data on your existing links, but you need to upgrade to add more links."
- cta={`Upgrade to ${nextPlan.name}`}
- onClick={() => {
- setOpenPopover(false);
- queryParams({
- set: {
- upgrade: nextPlan.name.toLowerCase(),
- },
- });
- }}
- />
- }
- >
- <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400">
- <IconMenu
- text="Import from Bitly"
- icon={
- <img
- src="/_static/icons/bitly.svg"
- alt="Bitly logo"
- className="h-4 w-4 rounded-full grayscale"
+ <>
+ <ExportLinksModal />
+ <Popover
+ content={
+ <AnimatedSizeContainer width height>
+ <div className="w-full divide-y divide-gray-200 md:w-52">
+ <div className="p-2">
+ {state === "default" && (
+ <>
+ <button
+ onClick={() => setState("import")}
+ className="w-full rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"
+ >
+ <IconMenu
+ text="Import links"
+ icon={<CloudUpload className="h-4 w-4" />}
/>
- }
- />
- </div>
- </Tooltip>
- ) : (
- <button
- onClick={() => {
- setOpenPopover(false);
- router.push(`/${slug}?import=bitly`);
- }}
- className="w-full rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"
- >
- <IconMenu
- text="Import from Bitly"
- icon={
- <img
- src="/_static/icons/bitly.svg"
- alt="Bitly logo"
- className="h-4 w-4"
- />
- }
- />
- </button>
- )}
- {exceededLinks ? (
- <Tooltip
- content={
- <TooltipContent
- title="Your workspace has exceeded its monthly links limit. We're still collecting data on your existing links, but you need to upgrade to add more links."
- cta={`Upgrade to ${nextPlan.name}`}
- onClick={() => {
- setOpenPopover(false);
- queryParams({
- set: {
- upgrade: nextPlan.name.toLowerCase(),
- },
- });
- }}
- />
- }
- >
- <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400">
- <IconMenu
- text="Import from Rebrandly"
- icon={
- <img
- src="/_static/icons/rebrandly.svg"
- alt="Rebrandly logo"
- className="h-4 w-4 grayscale"
+ </button>
+ <button
+ onClick={() => {
+ setOpenPopover(false);
+ setShowExportLinksModal(true);
+ }}
+ className="w-full rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"
+ >
+ <IconMenu
+ text="Export links"
+ icon={<Download className="h-4 w-4" />}
/>
- }
- />
- </div>
- </Tooltip>
- ) : (
- <button
- onClick={() => {
- setOpenPopover(false);
- router.push(`/${slug}?import=rebrandly`);
- }}
- className="w-full rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"
- >
- <IconMenu
- text="Import from Rebrandly"
- icon={
- <img
- src="/_static/icons/rebrandly.svg"
- alt="Rebrandly logo"
- className="h-4 w-4"
- />
- }
- />
- </button>
- )}
- {exceededLinks ? (
- <Tooltip
- content={
- <TooltipContent
- title="Your workspace has exceeded its monthly links limit. We're still collecting data on your existing links, but you need to upgrade to add more links."
- cta={`Upgrade to ${nextPlan.name}`}
- onClick={() => {
- setOpenPopover(false);
- queryParams({
- set: {
- upgrade: nextPlan.name.toLowerCase(),
- },
- });
- }}
- />
- }
- >
- <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400">
- <IconMenu
- text="Import from Short.io"
- icon={
- <img
- src="/_static/icons/short.svg"
- alt="Short.io logo"
- className="h-4 w-4 grayscale"
+ </button>
+ </>
+ )}
+ {state === "import" && (
+ <>
+ <ImportOption
+ onClick={() => {
+ setOpenPopover(false);
+ router.push(`/${slug}?import=bitly`);
+ }}
+ setOpenPopover={setOpenPopover}
+ >
+ <IconMenu
+ text="Import from Bitly"
+ icon={
+ <img
+ src="/_static/icons/bitly.svg"
+ alt="Bitly logo"
+ className="h-4 w-4"
+ />
+ }
/>
- }
- />
- </div>
- </Tooltip>
- ) : (
- <button
- onClick={() => {
- setOpenPopover(false);
- router.push(`/${slug}?import=short`);
- }}
- className="w-full rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"
- >
- <IconMenu
- text="Import from Short.io"
- icon={
- <img
- src="/_static/icons/short.svg"
- alt="Short.io logo"
- className="h-4 w-4"
- />
- }
- />
- </button>
- )}
- <Tooltip content="This feature is still in development – we'll let you know when it's ready!">
- <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400">
- <IconMenu
- text="Import from CSV"
- icon={<Sheet className="h-4 w-4" />}
- />
+ </ImportOption>
+ <ImportOption
+ onClick={() => {
+ setOpenPopover(false);
+ router.push(`/${slug}?import=rebrandly`);
+ }}
+ setOpenPopover={setOpenPopover}
+ >
+ <IconMenu
+ text="Import from Rebrandly"
+ icon={
+ <img
+ src="/_static/icons/rebrandly.svg"
+ alt="Rebrandly logo"
+ className="h-4 w-4"
+ />
+ }
+ />
+ </ImportOption>
+
+ <ImportOption
+ onClick={() => {
+ setOpenPopover(false);
+ router.push(`/${slug}?import=short`);
+ }}
+ setOpenPopover={setOpenPopover}
+ >
+ <IconMenu
+ text="Import from Short.io"
+ icon={
+ <img
+ src="/_static/icons/short.svg"
+ alt="Short.io logo"
+ className="h-4 w-4"
+ />
+ }
+ />
+ </ImportOption>
+ <Tooltip content="This feature is still in development – we'll let you know when it's ready!">
+ <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400">
+ <IconMenu
+ text="Import from CSV"
+ icon={<Sheet className="h-4 w-4" />}
+ />
+ </div>
+ </Tooltip>
+ </>
+ )}
</div>
- </Tooltip>
- </div>
- <div className="p-2">
- <Tooltip content="This feature is still in development – we'll let you know when it's ready!">
- <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400">
- <IconMenu
- text="Bulk create links"
- icon={<FilePlus2 className="h-4 w-4" />}
- />
+ <div className="p-2">
+ <Tooltip content="This feature is still in development – we'll let you know when it's ready!">
+ <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400">
+ <IconMenu
+ text="Bulk create links"
+ icon={<FilePlus2 className="h-4 w-4" />}
+ />
+ </div>
+ </Tooltip>
</div>
- </Tooltip>
- </div>
- </div>
- }
- openPopover={openPopover}
- setOpenPopover={setOpenPopover}
- align="end"
- >
- <button
- onClick={() => setOpenPopover(!openPopover)}
- className="group ml-2 flex items-center justify-between space-x-2 rounded-md border border-gray-200 bg-white p-2.5 shadow transition-all duration-75"
+ </div>
+ </AnimatedSizeContainer>
+ }
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
+ align="end"
>
- <ChevronDown
- className={`h-4 w-4 shrink-0 ${
- openPopover ? "rotate-180 text-gray-700" : "text-gray-400"
- } transition-all group-hover:text-gray-700`}
+ <Button
+ onClick={() => setOpenPopover(!openPopover)}
+ variant="secondary"
+ className="w-auto px-1.5"
+ icon={<ThreeDots className="h-5 w-5 text-gray-500" />}
/>
- </button>
- </Popover>
+ </Popover>
+ </>
);
};
+
+function ImportOption({
+ children,
+ setOpenPopover,
+ onClick,
+}: {
+ children: ReactNode;
+ setOpenPopover: Dispatch<SetStateAction<boolean>>;
+ onClick: () => void;
+}) {
+ const { queryParams } = useRouterStuff();
+ const { exceededLinks, nextPlan } = useWorkspace();
+
+ return exceededLinks ? (
+ <Tooltip
+ content={
+ <TooltipContent
+ title="Your workspace has exceeded its monthly links limit. We're still collecting data on your existing links, but you need to upgrade to add more links."
+ cta={`Upgrade to ${nextPlan.name}`}
+ onClick={() => {
+ setOpenPopover(false);
+ queryParams({
+ set: {
+ upgrade: nextPlan.name.toLowerCase(),
+ },
+ });
+ }}
+ />
+ }
+ >
+ <div className="flex w-full cursor-not-allowed items-center justify-between space-x-2 rounded-md p-2 text-sm text-gray-400 [&_img]:grayscale">
+ {children}
+ </div>
+ </Tooltip>
+ ) : (
+ <button
+ onClick={onClick}
+ className="w-full rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"
+ >
+ {children}
+ </button>
+ );
+}
diff --git a/apps/web/package.json b/apps/web/package.json
index 3a3598eb57..fe88b64132 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -85,6 +85,7 @@
"react-dom-confetti": "^0.2.0",
"react-email": "^2.0.0",
"react-highlight-words": "^0.20.0",
+ "react-hook-form": "^7.52.1",
"react-medium-image-zoom": "^5.1.7",
"react-parallax-tilt": "^1.7.70",
"react-spring": "^9.5.5",
diff --git a/apps/web/ui/modals/export-links-modal.tsx b/apps/web/ui/modals/export-links-modal.tsx
new file mode 100644
index 0000000000..f90cadb076
--- /dev/null
+++ b/apps/web/ui/modals/export-links-modal.tsx
@@ -0,0 +1,226 @@
+import { INTERVAL_DATA, INTERVAL_DISPLAYS } from "@/lib/analytics/constants";
+import useWorkspace from "@/lib/swr/use-workspace";
+import {
+ Button,
+ Checkbox,
+ DateRangePicker,
+ Logo,
+ Modal,
+ useMediaQuery,
+} from "@dub/ui";
+import va from "@vercel/analytics";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useId,
+ useMemo,
+ useState,
+} from "react";
+import { Controller, useForm } from "react-hook-form";
+import { toast } from "sonner";
+
+const columns = [
+ { id: "link", label: "Short link" },
+ { id: "url", label: "Destination URL" },
+ { id: "clicks", label: "Clicks" },
+ { id: "createdAt", label: "Created at" },
+ { id: "updatedAt", label: "Updated at" },
+ { id: "tags", label: "Tags" },
+ { id: "archived", label: "Archived" },
+];
+
+const defaultColumns = ["link", "url", "clicks", "createdAt"];
+
+type FormData = {
+ columns: string[];
+ dateRange: {
+ from?: Date;
+ to?: Date;
+ interval?: string;
+ };
+};
+
+function ExportLinksModal({
+ showExportLinksModal,
+ setShowExportLinksModal,
+}: {
+ showExportLinksModal: boolean;
+ setShowExportLinksModal: Dispatch<SetStateAction<boolean>>;
+}) {
+ const { id: workspaceId } = useWorkspace();
+ const { isMobile } = useMediaQuery();
+ const dateRangePickerId = useId();
+ const checkboxId = useId();
+
+ const {
+ register,
+ control,
+ handleSubmit,
+ formState: { errors },
+ } = useForm<FormData>({
+ defaultValues: {
+ columns: defaultColumns,
+ dateRange: {
+ interval: "all",
+ },
+ },
+ });
+
+ const [loading, setLoading] = useState(false);
+
+ const onSubmit = handleSubmit((data) => {
+ setLoading(true);
+ const queryString = new URLSearchParams({
+ ...(workspaceId && { workspaceId }),
+ ...(data.dateRange.from && data.dateRange.to
+ ? {
+ from: data.dateRange.from.toISOString(),
+ to: data.dateRange.to.toISOString(),
+ }
+ : {
+ interval: data.dateRange.interval ?? "all",
+ }),
+ columns: (data.columns.length ? data.columns : defaultColumns).join(","),
+ }).toString();
+ fetch(`/api/links/export?${queryString}`, {
+ method: "GET",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(data),
+ })
+ .then(async (res) => {
+ if (res.status === 200 || res.status === 201) {
+ va.track("Exported links");
+ toast.success("Links exported successfully");
+ setShowExportLinksModal(false);
+ } else {
+ const { error } = await res.json();
+ toast.error(error.message);
+ }
+ })
+ // TODO: catch
+ .finally(() => setLoading(false));
+ });
+
+ return (
+ <Modal
+ showModal={showExportLinksModal}
+ setShowModal={setShowExportLinksModal}
+ >
+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 sm:px-16">
+ <Logo />
+ <div className="flex flex-col space-y-1 text-center">
+ <h3 className="text-lg font-medium">Export links</h3>
+ <p className="text-sm text-gray-500">
+ Export this workspace's links to a CSV file
+ </p>
+ </div>
+ </div>
+
+ <form
+ onSubmit={onSubmit}
+ className="flex flex-col gap-6 bg-gray-50 px-4 py-8 text-left sm:rounded-b-2xl sm:px-16"
+ >
+ <Controller
+ name="dateRange"
+ control={control}
+ render={({ field }) => (
+ <div className="flex flex-col gap-2">
+ <label
+ htmlFor={dateRangePickerId}
+ className="block text-sm font-medium text-gray-700"
+ >
+ Link creation
+ </label>
+ <DateRangePicker
+ id={dateRangePickerId}
+ value={
+ field.value?.from && field.value?.to
+ ? {
+ from: field.value.from,
+ to: field.value.to,
+ }
+ : undefined
+ }
+ presetId={
+ !field.value.from || !field.value.to
+ ? field.value.interval ?? "all"
+ : undefined
+ }
+ onChange={(dateRange, preset) =>
+ field.onChange(preset ? { interval: preset.id } : dateRange)
+ }
+ presets={INTERVAL_DISPLAYS.map(({ display, value }) => ({
+ id: value,
+ label: display,
+ dateRange: {
+ from: INTERVAL_DATA[value].startDate,
+ to: new Date(),
+ },
+ }))}
+ />
+ </div>
+ )}
+ />
+
+ <div>
+ <p className="block text-sm font-medium text-gray-700">Columns</p>
+ <Controller
+ name="columns"
+ control={control}
+ render={({ field }) => (
+ <div className="mt-2 grid grid-cols-1 gap-x-4 gap-y-2 sm:grid-cols-2">
+ {columns.map(({ id, label }) => (
+ <div key={id} className="group flex gap-2">
+ <Checkbox
+ value={id}
+ id={`${checkboxId}-${id}`}
+ checked={field.value.includes(id)}
+ onCheckedChange={(checked) => {
+ field.onChange(
+ checked
+ ? [...field.value, id]
+ : field.value.filter((value) => value !== id),
+ );
+ }}
+ />
+ <label
+ htmlFor={`${checkboxId}-${id}`}
+ className="select-none text-sm font-medium text-gray-600 group-hover:text-gray-800"
+ >
+ {label}
+ </label>
+ </div>
+ ))}
+ </div>
+ )}
+ />
+ </div>
+ <Button loading={loading} text="Export links" />
+ </form>
+ </Modal>
+ );
+}
+
+export function useExportLinksModal() {
+ const [showExportLinksModal, setShowExportLinksModal] = useState(false);
+
+ const ExportLinksModalCallback = useCallback(() => {
+ return (
+ <ExportLinksModal
+ showExportLinksModal={showExportLinksModal}
+ setShowExportLinksModal={setShowExportLinksModal}
+ />
+ );
+ }, [showExportLinksModal, setShowExportLinksModal]);
+
+ return useMemo(
+ () => ({
+ setShowExportLinksModal,
+ ExportLinksModal: ExportLinksModalCallback,
+ }),
+ [setShowExportLinksModal, ExportLinksModalCallback],
+ );
+}
diff --git a/packages/ui/src/icons/nucleo/cloud-upload.tsx b/packages/ui/src/icons/nucleo/cloud-upload.tsx
new file mode 100644
index 0000000000..fe2234b375
--- /dev/null
+++ b/packages/ui/src/icons/nucleo/cloud-upload.tsx
@@ -0,0 +1,43 @@
+import { SVGProps } from "react";
+
+export function CloudUpload(props: SVGProps<SVGSVGElement>) {
+ return (
+ <svg
+ height="18"
+ width="18"
+ viewBox="0 0 18 18"
+ xmlns="http://www.w3.org/2000/svg"
+ {...props}
+ >
+ <g fill="currentColor">
+ <path
+ d="M13.464,6.891c-.186-2.314-2.102-4.141-4.464-4.141-2.485,0-4.5,2.015-4.5,4.5,0,.35,.049,.686,.124,1.013-1.597,.067-2.874,1.374-2.874,2.987,0,1.657,1.343,3,3,3h7.75c2.071,0,3.75-1.679,3.75-3.75,0-1.736-1.185-3.182-2.786-3.609Z"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <polyline
+ fill="none"
+ points="6.75 9 9 6.75 11.25 9"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="9"
+ x2="9"
+ y1="6.75"
+ y2="11.75"
+ />
+ </g>
+ </svg>
+ );
+}
diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.ts
index 81d9d42a76..f7764a5936 100644
--- a/packages/ui/src/icons/nucleo/index.ts
+++ b/packages/ui/src/icons/nucleo/index.ts
@@ -8,6 +8,7 @@ export * from "./circle-half-dotted-clock";
export * from "./circle-info";
export * from "./circle-warning";
export * from "./circle-xmark";
+export * from "./cloud-upload";
export * from "./code";
export * from "./crosshairs";
export * from "./cube";
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d13de468f9..9fbc7f60dc 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -251,6 +251,9 @@ importers:
react-highlight-words:
specifier: ^0.20.0
version: 0.20.0([email protected])
+ react-hook-form:
+ specifier: ^7.52.1
+ version: 7.52.1([email protected])
react-medium-image-zoom:
specifier: ^5.1.7
@@ -16521,6 +16524,15 @@ packages:
react: 18.2.0
dev: false
+ resolution: {integrity: sha512-uNKIhaoICJ5KQALYZ4TOaOLElyM+xipord+Ha3crEFhTntdLvWZqVY49Wqd/0GiVCA/f9NjemLeiNPjG7Hpurg==}
+ engines: {node: '>=12.22.0'}
+ peerDependencies:
+ react: ^16.8.0 || ^17 || ^18 || ^19
+ dependencies:
+ react: 18.2.0
+ dev: false
+
resolution: {integrity: sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==}