The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix: move export button and add export endpoints
flooorianflof17307f26266
diff --git a/apps/web/app/api/analytics/[endpoint]/export/route.ts b/apps/web/app/api/analytics/[endpoint]/export/route.tsindex 4e30853a9d..4e7ad377fa 100644--- a/apps/web/app/api/analytics/[endpoint]/export/route.ts+++ b/apps/web/app/api/analytics/[endpoint]/export/route.ts@@ -39,6 +39,15 @@ export const GET = withAuth(...parsedParams,});+ console.log(response.length);++ // if there is no data to export so return 204+ if (response.length === 0) {+ return new Response(undefined, {+ status: 204,+ });+ }+// convert the response to CSVconst csv = json2csv(endpoint === "timeseries"diff --git a/apps/web/ui/analytics/clicks.tsx b/apps/web/ui/analytics/clicks.tsxindex 7e3abe7822..f3454b9166 100644--- a/apps/web/ui/analytics/clicks.tsx+++ b/apps/web/ui/analytics/clicks.tsx@@ -15,10 +15,9 @@ import { useSearchParams } from "next/navigation";import { useContext } from "react";import { AnalyticsContext } from ".";import ClicksChart from "./clicks-chart";-import ExportButton from "./export-button";export default function Clicks() {- const { totalClicks, basePath } = useContext(AnalyticsContext);+ const { totalClicks } = useContext(AnalyticsContext);const searchParams = useSearchParams();const domain = searchParams?.get("domain");const key = searchParams?.get("key");@@ -26,8 +25,6 @@ export default function Clicks() {const { queryParams } = useRouterStuff();const { tags } = useTags();- const isPublicStatsPage = basePath.startsWith("/stats");-return (<div className="max-w-4xl overflow-hidden border border-gray-200 bg-white p-5 sm:rounded-lg sm:border-gray-100 sm:p-10 sm:shadow-lg"><div className="mb-5 flex items-start justify-between space-x-4">@@ -123,7 +120,6 @@ export default function Clicks() {</Link>);})}- {!isPublicStatsPage && <ExportButton />}</div></div><ClicksChart />diff --git a/apps/web/ui/analytics/export-button.tsx b/apps/web/ui/analytics/export-button.tsxindex f1022262eb..8e921bdb55 100644--- a/apps/web/ui/analytics/export-button.tsx+++ b/apps/web/ui/analytics/export-button.tsx@@ -15,6 +15,10 @@ export default function ExportButton() {"top_urls","device","referer",+ "city",+ "browser",+ "os",+ "top_links",];const exportData = async () => {@@ -32,7 +36,9 @@ export default function ExportButton() {},);- if (response.status === 200) {+ if (response.ok) {+ if (response.status === 204) continue;+const data = await response.blob();zipFile.file(`${endpoint}.csv`, data);} else {@@ -60,7 +66,7 @@ export default function ExportButton() {<Tooltip content={<TooltipContent title="No data available" />}><buttondisabled={loading || totalClicks === 0 || !totalClicks}- className="flex items-center space-x-1 rounded-lg border border-gray-200 bg-gray-50 px-3 py-1 text-sm text-gray-300"+ className="flex w-full items-center justify-between space-x-2 rounded-md p-2 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed disabled:text-gray-400 disabled:hover:bg-white disabled:active:bg-white"onClick={async () => {setLoading(true);await exportData();@@ -73,7 +79,7 @@ export default function ExportButton() {) : (<buttondisabled={loading}- className="flex items-center space-x-1 rounded-lg border border-gray-200 bg-gray-50 px-3 py-1 text-sm text-gray-500 transition-all hover:bg-gray-100 active:scale-95 disabled:cursor-progress disabled:text-gray-400 disabled:hover:shadow disabled:active:scale-100"+ className="flex w-full items-center justify-between space-x-2 rounded-md p-2 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-progress disabled:text-gray-400 disabled:hover:bg-white disabled:active:bg-white"onClick={async () => {setLoading(true);await exportData();diff --git a/apps/web/ui/analytics/share-popover.tsx b/apps/web/ui/analytics/share-popover.tsxindex f039eed082..0491314814 100644--- a/apps/web/ui/analytics/share-popover.tsx+++ b/apps/web/ui/analytics/share-popover.tsx@@ -119,7 +119,7 @@ export default function SharePopover() {><buttononClick={() => setopenSharePopoverPopover(!openSharePopover)}- className="flex w-24 items-center justify-center space-x-2 rounded-md bg-white px-3 py-2.5 shadow transition-all duration-75 hover:shadow-md active:scale-95"+ className="flex w-24 items-center justify-center space-x-2 rounded-md bg-white px-3 py-2.5 shadow transition-all hover:shadow-md"><IconMenu text="Share" icon={<Share2 className="h-4 w-4" />} /></button>diff --git a/apps/web/ui/analytics/toggle.tsx b/apps/web/ui/analytics/toggle.tsxindex 8c9496e641..b948f2777f 100644--- a/apps/web/ui/analytics/toggle.tsx+++ b/apps/web/ui/analytics/toggle.tsx@@ -28,6 +28,7 @@ import { useParams, useSearchParams } from "next/navigation";import punycode from "punycode/";import { useContext, useMemo, useState } from "react";import { AnalyticsContext } from ".";+import ExportButton from "./export-button";import SharePopover from "./share-popover";import TagSelector from "./tag-selector";@@ -38,6 +39,7 @@ export default function Toggle() {const { basePath, domain, key, url, interval } = useContext(AnalyticsContext);const [openDatePopover, setOpenDatePopover] = useState(false);+ const [openMorePopover, setOpenMorePopover] = useState(false);const selectedInterval = useMemo(() => {return INTERVALS.find((s) => s.value === interval) || INTERVALS[1];@@ -178,6 +180,26 @@ export default function Toggle() {/></button></Popover>++ {!isPublicStatsPage && (+ <Popover+ align="end"+ content={+ <div className="grid w-full p-2 md:w-48">+ <ExportButton />+ </div>+ }+ openPopover={openMorePopover}+ setOpenPopover={setOpenMorePopover}+ >+ <button+ onClick={() => setOpenMorePopover(!openMorePopover)}+ className="flex h-10 w-10 items-center justify-center rounded-md bg-white shadow transition-all hover:shadow-md"+ >+ ...+ </button>+ </Popover>+ )}</div></div></div>