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 buildUrl

Kiran K47f4fe114d55
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-events.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-events.tsx
index 4a0ff72abe..6749e07239 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-events.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-events.tsx
@@ -4,7 +4,13 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { campaignEventSchema } from "@/lib/zod/schemas/campaigns";
import { GroupColorCircle } from "@/ui/partners/groups/group-color-circle";
import { Table, ToggleGroup, Tooltip, useTable } from "@dub/ui";
-import { fetcher, formatDateTime, OG_AVATAR_URL, timeAgo } from "@dub/utils";
+import {
+ buildUrl,
+ fetcher,
+ formatDateTime,
+ OG_AVATAR_URL,
+ timeAgo,
+} from "@dub/utils";
import { useParams, useRouter } from "next/navigation";
import { useMemo, useState } from "react";
import useSWR from "swr";
@@ -14,21 +20,6 @@ type EventStatus = "delivered" | "opened" | "bounced";
export type CampaignEvent = z.infer<typeof campaignEventSchema>;
-const getPartnerUrl = ({
- workspaceSlug,
- id,
-}: {
- workspaceSlug: string;
- id: string;
-}) => `/${workspaceSlug}/program/partners/${id}`;
-
-const getTimestamp = (event: CampaignEvent) => {
- if (event.deliveredAt) return event.deliveredAt;
- if (event.openedAt) return event.openedAt;
- if (event.bouncedAt) return event.bouncedAt;
- return null;
-};
-
export function CampaignEvents() {
const router = useRouter();
const { campaignId } = useParams<{ campaignId: string }>();
@@ -41,7 +32,11 @@ export function CampaignEvents() {
isLoading,
} = useSWR<CampaignEvent[]>(
campaignId && workspaceId
- ? `/api/campaigns/${campaignId}/events?workspaceId=${workspaceId}&status=${status}&pageSize=10`
+ ? buildUrl(`/api/campaigns/${campaignId}/events`, {
+ workspaceId,
+ status,
+ pageSize: 10,
+ })
: null,
fetcher,
);
@@ -172,3 +167,27 @@ export function CampaignEvents() {
</>
);
}
+
+const getPartnerUrl = ({
+ workspaceSlug,
+ id,
+}: {
+ workspaceSlug: string;
+ id: string;
+}) => `/${workspaceSlug}/program/partners/${id}`;
+
+const getTimestamp = (event: CampaignEvent) => {
+ if (event.deliveredAt) {
+ return event.deliveredAt;
+ }
+
+ if (event.openedAt) {
+ return event.openedAt;
+ }
+
+ if (event.bouncedAt) {
+ return event.bouncedAt;
+ }
+
+ return null;
+};
diff --git a/packages/utils/src/functions/urls.ts b/packages/utils/src/functions/urls.ts
index 1d9ef8603d..5e41426544 100644
--- a/packages/utils/src/functions/urls.ts
+++ b/packages/utils/src/functions/urls.ts
@@ -178,3 +178,23 @@ export const normalizeUrl = (url: string): string => {
return url;
}
};
+
+export function buildUrl(
+ baseUrl: string,
+ params?: Record<string, string | number | boolean | null | undefined>,
+) {
+ const url = new URL(
+ baseUrl,
+ typeof window !== "undefined" ? window.location.origin : "http://localhost",
+ );
+
+ if (params) {
+ Object.entries(params).forEach(([key, value]) => {
+ if (value !== null && value !== undefined && value !== "") {
+ url.searchParams.append(key, String(value));
+ }
+ });
+ }
+
+ return url.toString();
+}