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 Export Applications feature

Steven Tey5d4c9006cccd
diff --git a/apps/web/app/(ee)/api/partners/export/route.ts b/apps/web/app/(ee)/api/partners/export/route.ts
index 33f6853f95..73319a7131 100644
--- a/apps/web/app/(ee)/api/partners/export/route.ts
+++ b/apps/web/app/(ee)/api/partners/export/route.ts
@@ -79,7 +79,7 @@ export const GET = withWorkspace(
return new Response(convertToCSV(formattedPartners), {
headers: {
"Content-Type": "text/csv",
- "Content-Disposition": `attachment`,
+ "Content-Disposition": "attachment",
},
});
},
diff --git a/apps/web/app/(ee)/api/programs/[programId]/applications/export/route.ts b/apps/web/app/(ee)/api/programs/[programId]/applications/export/route.ts
new file mode 100644
index 0000000000..ee960d5c24
--- /dev/null
+++ b/apps/web/app/(ee)/api/programs/[programId]/applications/export/route.ts
@@ -0,0 +1,101 @@
+import { convertToCSV } from "@/lib/analytics/utils/convert-to-csv";
+import { withWorkspace } from "@/lib/auth";
+import {
+ exportApplicationColumns,
+ exportApplicationsColumnsDefault,
+} from "@/lib/zod/schemas/partners";
+import { prisma } from "@dub/prisma";
+import { z } from "zod";
+
+const columnIdToLabel = exportApplicationColumns.reduce((acc, column) => {
+ acc[column.id] = column.label;
+ return acc;
+}, {});
+
+const applicationsExportQuerySchema = z.object({
+ columns: z
+ .string()
+ .optional()
+ .transform((v) => v?.split(",") || exportApplicationsColumnsDefault)
+ .default(exportApplicationsColumnsDefault.join(",")),
+});
+
+// GET /api/programs/[programId]/applications/export – export applications to CSV
+export const GET = withWorkspace(
+ async ({ searchParams, params }) => {
+ const { programId } = params;
+ let { columns } = applicationsExportQuerySchema.parse(searchParams);
+
+ const programEnrollments = await prisma.programEnrollment.findMany({
+ where: {
+ programId,
+ status: "pending",
+ },
+ include: {
+ partner: true,
+ application: true,
+ },
+ });
+
+ const applications = programEnrollments.map(
+ ({ partner, application, ...programEnrollment }) => {
+ return {
+ ...partner,
+ createdAt: application?.createdAt || programEnrollment.createdAt,
+ proposal: application?.proposal || "",
+ comments: application?.comments || "",
+ };
+ },
+ );
+
+ const columnOrderMap = exportApplicationColumns.reduce(
+ (acc, column, index) => {
+ acc[column.id] = index + 1;
+ return acc;
+ },
+ {},
+ );
+
+ columns = columns.sort(
+ (a, b) => (columnOrderMap[a] || 999) - (columnOrderMap[b] || 999),
+ );
+
+ const schemaFields = {};
+ columns.forEach((column) => {
+ schemaFields[columnIdToLabel[column]] = z.string().optional().default("");
+ });
+
+ const formattedApplications = applications.map((application) => {
+ const result = {};
+
+ columns.forEach((column) => {
+ if (column === "createdAt") {
+ result[columnIdToLabel[column]] = application[column]
+ ? new Date(application[column]).toISOString()
+ : "";
+ } else {
+ result[columnIdToLabel[column]] = application[column] || "";
+ }
+ });
+
+ return z.object(schemaFields).parse(result);
+ });
+
+ return new Response(convertToCSV(formattedApplications), {
+ headers: {
+ "Content-Type": "text/csv",
+ "Content-Disposition": "attachment",
+ },
+ });
+ },
+ {
+ requiredPlan: [
+ "business",
+ "business extra",
+ "business max",
+ "business plus",
+ "advanced",
+ "enterprise",
+ ],
+ },
+);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/applications-menu.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/applications-menu.tsx
index e4f604afa7..68883d7fc6 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/applications-menu.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/applications-menu.tsx
@@ -5,16 +5,10 @@ import { mutatePrefix } from "@/lib/swr/mutate";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { useConfirmModal } from "@/ui/modals/confirm-modal";
+import { useExportApplicationsModal } from "@/ui/modals/export-applications-modal";
import { ThreeDots } from "@/ui/shared/icons";
-import {
- Button,
- LoadingSpinner,
- MenuItem,
- Popover,
- UserCheck,
- UserXmark,
-} from "@dub/ui";
-import { Command } from "cmdk";
+import { Button, LoadingSpinner, Popover, UserCheck, UserXmark } from "@dub/ui";
+import { Download } from "@dub/ui/icons";
import { useAction } from "next-safe-action/hooks";
import { useRouter } from "next/navigation";
import { useState } from "react";
@@ -70,55 +64,92 @@ export function ApplicationsMenu() {
},
});
+ const { setShowExportApplicationsModal, ExportApplicationsModal } =
+ useExportApplicationsModal();
+
return (
<>
{confirmEnableAutoApproveModal}
{confirmDisableAutoApproveModal}
+ <ExportApplicationsModal />
<Popover
openPopover={isOpen}
setOpenPopover={setIsOpen}
content={
- <Command tabIndex={0} loop className="focus:outline-none">
- <Command.List className="flex w-screen flex-col gap-1 p-1.5 text-sm focus-visible:outline-none sm:w-auto sm:min-w-[200px]">
+ <div className="w-full md:w-56">
+ <div className="grid gap-px p-2">
+ <p className="mb-1.5 mt-1 flex items-center gap-2 px-1 text-xs font-medium text-neutral-500">
+ Application Settings
+ </p>
{program?.autoApprovePartnersEnabledAt ? (
- <MenuItem
- as={Command.Item}
- icon={<UserCheck className="size-4 shrink-0 text-red-600" />}
- onSelect={() => {
+ <button
+ onClick={() => {
setShowConfirmDisableAutoApproveModal(true);
setIsOpen(false);
}}
+ className="w-full rounded-md p-2 hover:bg-neutral-100 active:bg-neutral-200"
>
- Disable auto-approve
- </MenuItem>
+ <div className="flex items-center gap-2 text-left">
+ <UserCheck className="size-4 shrink-0 text-red-600" />
+ <span className="text-sm font-medium">
+ Disable auto-approve
+ </span>
+ </div>
+ </button>
) : (
- <MenuItem
- as={Command.Item}
- icon={
- <UserCheck className="size-4 shrink-0 text-green-600" />
- }
- onSelect={() => {
+ <button
+ onClick={() => {
setShowConfirmEnableAutoApproveModal(true);
setIsOpen(false);
}}
+ className="w-full rounded-md p-2 hover:bg-neutral-100 active:bg-neutral-200"
>
- Enable auto-approve
- </MenuItem>
+ <div className="flex items-center gap-2 text-left">
+ <UserCheck className="size-4 shrink-0 text-green-600" />
+ <span className="text-sm font-medium">
+ Enable auto-approve
+ </span>
+ </div>
+ </button>
)}
- <MenuItem
- as={Command.Item}
- icon={UserXmark}
- onSelect={() => {
+ <button
+ onClick={() => {
router.push(
`/${workspaceSlug}/program/partners/applications/rejected`,
);
setIsOpen(false);
}}
+ className="w-full rounded-md p-2 hover:bg-neutral-100 active:bg-neutral-200"
+ >
+ <div className="flex items-center gap-2 text-left">
+ <UserXmark className="size-4 shrink-0" />
+ <span className="text-sm font-medium">
+ View rejected partners
+ </span>
+ </div>
+ </button>
+ </div>
+
+ <div className="border-t border-neutral-200" />
+
+ <div className="grid gap-px p-2">
+ <p className="mb-1.5 mt-1 flex items-center gap-2 px-1 text-xs font-medium text-neutral-500">
+ Export Applications
+ </p>
+ <button
+ onClick={() => {
+ setShowExportApplicationsModal(true);
+ setIsOpen(false);
+ }}
+ className="w-full rounded-md p-2 hover:bg-neutral-100 active:bg-neutral-200"
>
- View rejected partners
- </MenuItem>
- </Command.List>
- </Command>
+ <div className="flex items-center gap-2 text-left">
+ <Download className="size-4 shrink-0" />
+ <span className="text-sm font-medium">Export as CSV</span>
+ </div>
+ </button>
+ </div>
+ </div>
}
align="end"
>
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index df0edf038b..f31f1aef38 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -73,6 +73,34 @@ export const exportPartnersColumnsDefault = exportPartnerColumns
.filter((column) => column.default)
.map((column) => column.id);
+export const exportApplicationColumns = [
+ { id: "id", label: "ID" },
+ { id: "name", label: "Name" },
+ { id: "email", label: "Email" },
+ { id: "country", label: "Country" },
+ { id: "createdAt", label: "Applied at" },
+ { id: "description", label: "Description" },
+ { id: "website", label: "Website" },
+ { id: "youtube", label: "YouTube" },
+ { id: "twitter", label: "Twitter" },
+ { id: "linkedin", label: "LinkedIn" },
+ { id: "instagram", label: "Instagram" },
+ { id: "tiktok", label: "TikTok" },
+ { id: "proposal", label: "Proposal" },
+ { id: "comments", label: "Comments" },
+];
+
+export const exportApplicationsColumnsDefault = [
+ "id",
+ "name",
+ "email",
+ "country",
+ "createdAt",
+ "website",
+ "youtube",
+ "linkedin",
+];
+
export const getPartnersQuerySchema = z
.object({
status: z
diff --git a/apps/web/ui/modals/export-applications-modal.tsx b/apps/web/ui/modals/export-applications-modal.tsx
new file mode 100644
index 0000000000..423c083549
--- /dev/null
+++ b/apps/web/ui/modals/export-applications-modal.tsx
@@ -0,0 +1,182 @@
+import useProgram from "@/lib/swr/use-program";
+import useWorkspace from "@/lib/swr/use-workspace";
+import {
+ exportApplicationColumns,
+ exportApplicationsColumnsDefault,
+} from "@/lib/zod/schemas/partners";
+import { Button, Checkbox, Modal, useRouterStuff } from "@dub/ui";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useId,
+ useMemo,
+ useState,
+} from "react";
+import { Controller, useForm } from "react-hook-form";
+import { toast } from "sonner";
+
+interface FormData {
+ columns: string[];
+}
+
+function ExportApplicationsModal({
+ showExportApplicationsModal,
+ setShowExportApplicationsModal,
+}: {
+ showExportApplicationsModal: boolean;
+ setShowExportApplicationsModal: Dispatch<SetStateAction<boolean>>;
+}) {
+ const columnCheckboxId = useId();
+ const { program } = useProgram();
+ const { id: workspaceId } = useWorkspace();
+ const { getQueryString } = useRouterStuff();
+
+ const {
+ control,
+ handleSubmit,
+ formState: { isSubmitting },
+ } = useForm<FormData>({
+ defaultValues: {
+ columns: exportApplicationsColumnsDefault,
+ },
+ });
+
+ const onSubmit = handleSubmit(async (data) => {
+ if (!workspaceId || !program?.id) {
+ return;
+ }
+
+ const lid = toast.loading("Exporting applications...");
+
+ try {
+ const response = await fetch(
+ `/api/programs/${program.id}/applications/export?${new URLSearchParams({
+ workspaceId: workspaceId,
+ ...(data.columns.length
+ ? { columns: data.columns.join(",") }
+ : undefined),
+ })}`,
+ {
+ method: "GET",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ },
+ );
+
+ if (!response.ok) {
+ const { error } = await response.json();
+ throw new Error(error.message);
+ }
+
+ const blob = await response.blob();
+ const url = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+
+ a.href = url;
+ a.download = `Dub Applications Export - ${new Date().toISOString()}.csv`;
+ a.click();
+
+ toast.success("Exported successfully");
+ setShowExportApplicationsModal(false);
+ } catch (error) {
+ toast.error(error);
+ } finally {
+ toast.dismiss(lid);
+ }
+ });
+
+ return (
+ <Modal
+ showModal={showExportApplicationsModal}
+ setShowModal={setShowExportApplicationsModal}
+ >
+ <div className="space-y-2 border-b border-neutral-200 p-4 sm:p-6">
+ <h3 className="text-lg font-medium leading-none">
+ Export applications
+ </h3>
+ </div>
+
+ <form onSubmit={onSubmit}>
+ <div className="bg-neutral-50 p-4 sm:p-6">
+ <div className="space-y-4">
+ <div>
+ <p className="mb-2 block text-sm font-medium text-neutral-700">
+ Columns
+ </p>
+ <Controller
+ name="columns"
+ control={control}
+ render={({ field }) => (
+ <div className="xs:grid-cols-2 grid grid-cols-1 gap-x-4 gap-y-2">
+ {exportApplicationColumns.map(({ id, label }) => (
+ <div key={id} className="group flex gap-2">
+ <Checkbox
+ value={id}
+ id={`${columnCheckboxId}-${id}`}
+ checked={field.value.includes(id)}
+ onCheckedChange={(checked) => {
+ field.onChange(
+ checked
+ ? [...field.value, id]
+ : field.value.filter((value) => value !== id),
+ );
+ }}
+ />
+ <label
+ htmlFor={`${columnCheckboxId}-${id}`}
+ className="select-none text-sm font-medium text-neutral-600 group-hover:text-neutral-800"
+ >
+ {label}
+ </label>
+ </div>
+ ))}
+ </div>
+ )}
+ />
+ </div>
+ </div>
+ </div>
+
+ <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={() => setShowExportApplicationsModal(false)}
+ variant="secondary"
+ text="Cancel"
+ className="h-8 w-fit px-3"
+ type="button"
+ />
+ <Button
+ type="submit"
+ loading={isSubmitting}
+ text="Export applications"
+ className="h-8 w-fit px-3"
+ />
+ </div>
+ </form>
+ </Modal>
+ );
+}
+
+export function useExportApplicationsModal() {
+ const [showExportApplicationsModal, setShowExportApplicationsModal] =
+ useState(false);
+
+ const ExportApplicationsModalCallback = useCallback(() => {
+ return (
+ <ExportApplicationsModal
+ showExportApplicationsModal={showExportApplicationsModal}
+ setShowExportApplicationsModal={setShowExportApplicationsModal}
+ />
+ );
+ }, [showExportApplicationsModal, setShowExportApplicationsModal]);
+
+ return useMemo(
+ () => ({
+ setShowExportApplicationsModal,
+ ExportApplicationsModal: ExportApplicationsModalCallback,
+ }),
+ [setShowExportApplicationsModal, ExportApplicationsModalCallback],
+ );
+}
diff --git a/apps/web/ui/modals/export-partners-modal.tsx b/apps/web/ui/modals/export-partners-modal.tsx
index 81894da3a8..5f8329d0b9 100644
--- a/apps/web/ui/modals/export-partners-modal.tsx
+++ b/apps/web/ui/modals/export-partners-modal.tsx
@@ -8,7 +8,6 @@ import {
Button,
Checkbox,
InfoTooltip,
- Logo,
Modal,
Switch,
useRouterStuff,
@@ -106,70 +105,82 @@ function ExportPartnersModal({
showModal={showExportPartnersModal}
setShowModal={setShowExportPartnersModal}
>
- <div className="flex flex-col items-center justify-center space-y-3 border-b border-neutral-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 partners</h3>
- <p className="text-sm text-neutral-500">
- Export this program's partners to a CSV file
- </p>
- </div>
+ <div className="space-y-2 border-b border-neutral-200 p-4 sm:p-6">
+ <h3 className="text-lg font-medium leading-none">Export partners</h3>
</div>
- <form
- onSubmit={onSubmit}
- className="flex flex-col gap-6 bg-neutral-50 px-4 py-8 text-left sm:rounded-b-2xl sm:px-12"
- >
- <div>
- <p className="block text-sm font-medium text-neutral-700">Columns</p>
+ <form onSubmit={onSubmit}>
+ <div className="bg-neutral-50 p-4 sm:p-6">
+ <div className="space-y-4">
+ <div>
+ <p className="mb-2 block text-sm font-medium text-neutral-700">
+ Columns
+ </p>
+ <Controller
+ name="columns"
+ control={control}
+ render={({ field }) => (
+ <div className="xs:grid-cols-2 grid grid-cols-1 gap-x-4 gap-y-2">
+ {exportPartnerColumns.map(({ id, label }) => (
+ <div key={id} className="group flex gap-2">
+ <Checkbox
+ value={id}
+ id={`${columnCheckboxId}-${id}`}
+ checked={field.value.includes(id)}
+ onCheckedChange={(checked) => {
+ field.onChange(
+ checked
+ ? [...field.value, id]
+ : field.value.filter((value) => value !== id),
+ );
+ }}
+ />
+ <label
+ htmlFor={`${columnCheckboxId}-${id}`}
+ className="select-none text-sm font-medium text-neutral-600 group-hover:text-neutral-800"
+ >
+ {label}
+ </label>
+ </div>
+ ))}
+ </div>
+ )}
+ />
+ </div>
+ </div>
+ </div>
+
+ <div className="border-t border-neutral-200 bg-neutral-50 px-4 py-4 sm:px-6">
<Controller
- name="columns"
+ name="useFilters"
control={control}
render={({ field }) => (
- <div className="xs:grid-cols-2 mt-2 grid grid-cols-1 gap-x-4 gap-y-2">
- {exportPartnerColumns.map(({ id, label }) => (
- <div key={id} className="group flex gap-2">
- <Checkbox
- value={id}
- id={`${columnCheckboxId}-${id}`}
- checked={field.value.includes(id)}
- onCheckedChange={(checked) => {
- field.onChange(
- checked
- ? [...field.value, id]
- : field.value.filter((value) => value !== id),
- );
- }}
- />
- <label
- htmlFor={`${columnCheckboxId}-${id}`}
- className="select-none text-sm font-medium text-neutral-600 group-hover:text-neutral-800"
- >
- {label}
- </label>
- </div>
- ))}
+ <div className="flex items-center justify-between gap-2">
+ <span className="flex select-none items-center gap-2 text-sm font-medium text-neutral-600 group-hover:text-neutral-800">
+ Apply current filters
+ <InfoTooltip content="Filter exported partners by your currently selected filters" />
+ </span>
+ <Switch checked={field.value} fn={field.onChange} />
</div>
)}
/>
</div>
- <div className="border-t border-neutral-200" />
-
- <Controller
- name="useFilters"
- control={control}
- render={({ field }) => (
- <div className="flex items-center justify-between gap-2">
- <span className="flex select-none items-center gap-2 text-sm font-medium text-neutral-600 group-hover:text-neutral-800">
- Apply current filters
- <InfoTooltip content="Filter exported partners by your currently selected filters" />
- </span>
- <Switch checked={field.value} fn={field.onChange} />
- </div>
- )}
- />
- <Button loading={isSubmitting} text="Export partners" />
+ <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={() => setShowExportPartnersModal(false)}
+ variant="secondary"
+ text="Cancel"
+ className="h-8 w-fit px-3"
+ type="button"
+ />
+ <Button
+ type="submit"
+ loading={isSubmitting}
+ text="Export partners"
+ className="h-8 w-fit px-3"
+ />
+ </div>
</form>
</Modal>
);