The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add loading states for events list
Steven Tey8a210dcae48c
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/activity-list.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/activity-list.tsxindex 17f0596990..9fc231c199 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/activity-list.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/activity-list.tsx@@ -1,39 +1,27 @@"use client";+import { EventType } from "@/lib/analytics/types";import { clickEventEnrichedSchema } from "@/lib/zod/schemas/clicks";import { leadEventEnrichedSchema } from "@/lib/zod/schemas/leads";import { saleEventEnrichedSchema } from "@/lib/zod/schemas/sales";import { EventList } from "@/ui/blocks/event-list";-import { useRouterStuff } from "@dub/ui";import { CursorRays, Globe, InvoiceDollar, UserCheck } from "@dub/ui/src/icons";import { COUNTRIES, timeAgo } from "@dub/utils";+import { useSearchParams } from "next/navigation";import { z } from "zod";-type ActivityListProps = {- page: number;- hasNextPage?: boolean;-} & (- | {- event: "clicks";- events: z.infer<typeof clickEventEnrichedSchema>[];- }- | {- event: "leads";- events: z.infer<typeof leadEventEnrichedSchema>[];- }- | {- event: "sales";- events: z.infer<typeof saleEventEnrichedSchema>[];- }-);-export function ActivityList({- event,- page,- hasNextPage,events,-}: ActivityListProps) {- const { queryParams } = useRouterStuff();+ totalEvents,+}: {+ events:+ | z.infer<typeof clickEventEnrichedSchema>[]+ | z.infer<typeof leadEventEnrichedSchema>[]+ | z.infer<typeof saleEventEnrichedSchema>[];+ totalEvents: number;+}) {+ const searchParams = useSearchParams();+ const event = (searchParams.get("event") || "clicks") as EventType;return (<EventList@@ -57,12 +45,7 @@ export function ActivityList({),};})}- onPrevious={() =>- queryParams({ set: { page: Math.max(page - 1, 1).toString() } })- }- onNext={() => queryParams({ set: { page: (page + 1).toString() } })}- hasPrevious={page > 1}- hasNext={hasNextPage}+ totalEvents={totalEvents}/>);}diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/event-tabs.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/event-tabs.tsxindex de3c9bd2b1..4e6f769b51 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/event-tabs.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/event-tabs.tsx@@ -1,11 +1,10 @@"use client";-import { EventType } from "@/lib/analytics/types";import { useRouterStuff } from "@dub/ui";import { ToggleGroup } from "@dub/ui/src/toggle-group";-export function EventTabs({ event }: { event: EventType }) {- const { queryParams } = useRouterStuff();+export function EventTabs() {+ const { queryParams, searchParams } = useRouterStuff();return (<ToggleGroupoptions={[@@ -13,7 +12,7 @@ export function EventTabs({ event }: { event: EventType }) {{ value: "leads", label: "Leads" },{ value: "sales", label: "Sales" },]}- selected={event}+ selected={searchParams.get("event") ?? "clicks"}selectAction={(event) => queryParams({ set: { event } })}/>);diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsxindex 4a3adcc467..8ce626a090 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page.tsx@@ -1,10 +1,11 @@-export const dynamic = "force-dynamic";--import { EVENT_TYPES } from "@/lib/analytics/constants";+import { EventType } from "@/lib/analytics/types";import { dub } from "@/lib/dub";+import { EventListSkeleton } from "@/ui/blocks/event-list";import { CopyButton, Logo } from "@dub/ui";import { Check } from "@dub/ui/src/icons";import { getPrettyUrl } from "@dub/utils";+import { ClicksCount } from "dub/dist/commonjs/models/components";+import { Suspense } from "react";import { ActivityList } from "./activity-list";import { EventTabs } from "./event-tabs";import { InviteButton } from "./invite-button";@@ -18,26 +19,9 @@ export default async function ReferralsPage({searchParams: { event?: string; page?: string };}) {const link = `https://refer.dub.co/${slug}`;- const event = EVENT_TYPES.find((e) => e === searchParams.event) ?? "clicks";+ const event = (searchParams.event ?? "clicks") as EventType;const page = parseInt(searchParams.page ?? "1") || 1;- const eventsParams = {- domain: "refer.dub.co",- key: slug,- event,- };-- const events = await dub.events.list({- ...eventsParams,- page: page - 1,- });-- // Ideally the Dub SDK/API would give us hasNext info, but at least this could speed things up with caching- const nextEvents = await dub.events.list({- ...eventsParams,- page: page,- });-return (<ReferralsPageClient><div>@@ -103,16 +87,47 @@ export default async function ReferralsPage({<div className="mt-12"><div className="mb-5 flex flex-wrap items-end justify-between gap-4"><h2 className="text-xl font-semibold text-gray-800">Activity</h2>- <EventTabs event={event} />+ <EventTabs /></div>- <ActivityList- event={event}- page={page}- hasNextPage={nextEvents.length > 0}- events={events as any}- />+ <Suspense+ key={`${slug}-${event}-${page}`}+ fallback={<EventListSkeleton />}+ >+ <ActivityListRSC workspaceSlug={slug} event={event} page={page} />+ </Suspense></div></div></ReferralsPageClient>);}++async function ActivityListRSC({+ workspaceSlug,+ event,+ page,+}: {+ workspaceSlug: string;+ event: EventType;+ page: number;+}) {+ const eventsParams = {+ domain: "refer.dub.co",+ key: workspaceSlug,+ event,+ };++ const events = await dub.events.list({+ ...eventsParams,+ interval: "all",+ page: page - 1,+ });++ const totalEvents = (await dub.analytics.retrieve({+ ...eventsParams,+ interval: "all_unfiltered",+ })) as ClicksCount;++ return (+ <ActivityList events={events as any} totalEvents={totalEvents[event]} />+ );+}diff --git a/apps/web/ui/blocks/event-list.tsx b/apps/web/ui/blocks/event-list.tsxindex af3d218286..9f50ca815c 100644--- a/apps/web/ui/blocks/event-list.tsx+++ b/apps/web/ui/blocks/event-list.tsx@@ -1,13 +1,12 @@"use client";+import { PAGINATION_LIMIT } from "@dub/utils";import { PropsWithChildren, ReactNode } from "react";+import usePagination from "../analytics/events/use-pagination";export type EventListProps = PropsWithChildren<{events: { icon: ReactNode; content: ReactNode; right?: ReactNode }[];- hasNext?: boolean;- hasPrevious?: boolean;- onNext?: () => void;- onPrevious?: () => void;+ totalEvents: number;}>;const buttonClassName = [@@ -16,13 +15,8 @@ const buttonClassName = ["disabled:cursor-not-allowed disabled:border-gray-200 disabled:text-gray-400 disabled:bg-white",].join(" ");-export function EventList({- events,- hasNext,- hasPrevious,- onNext,- onPrevious,-}: EventListProps) {+export function EventList({ events, totalEvents }: EventListProps) {+ const { pagination, setPagination } = usePagination(PAGINATION_LIMIT);return (<div className="rounded-xl border border-gray-200"><div className="flex flex-col divide-y divide-gray-200">@@ -41,30 +35,71 @@ export function EventList({</div>))}</div>- {onPrevious && onNext && (- <div className="flex items-center justify-end gap-2 border-t border-gray-200 px-3.5 py-2">- {onPrevious && (- <button- type="button"- className={buttonClassName}- onClick={onPrevious}- disabled={!hasPrevious}- >- Previous- </button>- )}- {onNext && (- <button- type="button"- className={buttonClassName}- onClick={onNext}- disabled={!hasNext}- >- Next- </button>- )}+ <div className="flex items-center justify-between gap-2 border-t border-gray-200 px-3.5 py-2 text-sm leading-6 text-gray-600">+ <div>+ <span className="hidden sm:inline-block">Viewing</span>{" "}+ <span className="font-medium">+ {pagination.pageIndex * pagination.pageSize + 1}-+ {Math.min(+ pagination.pageIndex * pagination.pageSize + pagination.pageSize,+ totalEvents,+ )}+ </span>{" "}+ of <span className="font-medium">{totalEvents}</span> events+ </div>+ <div className="flex items-center gap-2">+ <button+ type="button"+ className={buttonClassName}+ onClick={() =>+ setPagination({+ pageIndex: pagination.pageIndex - 1,+ pageSize: pagination.pageSize,+ })+ }+ disabled={pagination.pageIndex === 0}+ >+ Previous+ </button>+ <button+ type="button"+ className={buttonClassName}+ onClick={() =>+ setPagination({+ pageIndex: pagination.pageIndex + 1,+ pageSize: pagination.pageSize,+ })+ }+ disabled={+ pagination.pageIndex * pagination.pageSize ++ pagination.pageSize >=+ totalEvents+ }+ >+ Next+ </button></div>- )}+ </div>+ </div>+ );+}++export function EventListSkeleton() {+ return (+ <div className="rounded-xl border border-gray-200">+ <div className="flex flex-col divide-y divide-gray-200">+ {[...Array(5)].map((_, index) => (+ <div+ className="flex items-center justify-between gap-5 px-3.5 py-4"+ key={index}+ >+ <div className="flex items-center gap-2.5 text-gray-500">+ <div className="h-4 w-4 animate-pulse rounded-full bg-gray-200" />+ <div className="h-4 w-28 animate-pulse rounded-full bg-gray-200" />+ </div>+ </div>+ ))}+ </div></div>);}