The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Analytics Options Dropdown
Steven Teye244037ba6b0
diff --git a/apps/web/ui/analytics/analytics-options.tsx b/apps/web/ui/analytics/analytics-options.tsxnew file mode 100644index 0000000000..c6334c3acc--- /dev/null+++ b/apps/web/ui/analytics/analytics-options.tsx@@ -0,0 +1,54 @@+import { AnimatedSizeContainer, Popover, useMediaQuery } from "@dub/ui";+import { Button } from "@dub/ui/src/button";+import { ReferredVia } from "@dub/ui/src/icons";+import { useContext, useState } from "react";+import { AnalyticsContext } from ".";+import { ThreeDots } from "../shared/icons";+import ExportButton from "./export-button";+import ShareAnalytics from "./share-analytics";++export default function AnalyticsOptions() {+ const { isMobile } = useMediaQuery();+ const { key } = useContext(AnalyticsContext);++ const [openPopover, setOpenPopover] = useState(false);++ const [state, setState] = useState<"default" | "share">("default");++ return (+ <Popover+ align="end"+ content={+ <AnimatedSizeContainer width={!isMobile} height>+ {key && state === "share" && <ShareAnalytics />}++ {state === "default" && (+ <div className="grid gap-px p-2 sm:w-44">+ <ExportButton setOpenPopover={setOpenPopover} />+ {key && (+ <Button+ text="Share Analytics"+ variant="outline"+ icon={<ReferredVia className="h-4 w-4" />}+ className="h-9 justify-start px-2 text-black"+ onClick={() => setState("share")}+ />+ )}+ </div>+ )}+ </AnimatedSizeContainer>+ }+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <div>+ <Button+ variant="secondary"+ onClick={() => setOpenPopover(!openPopover)}+ icon={<ThreeDots className="h-5 w-5 text-gray-500" />}+ className="h-10 space-x-0 px-1 py-2"+ />+ </div>+ </Popover>+ );+}diff --git a/apps/web/ui/analytics/export-button.tsx b/apps/web/ui/analytics/export-button.tsxindex d3e4fca499..da020d28a0 100644--- a/apps/web/ui/analytics/export-button.tsx+++ b/apps/web/ui/analytics/export-button.tsx@@ -1,10 +1,14 @@-import { LoadingSpinner } from "@dub/ui";+import { Button } from "@dub/ui/src/button";import { Download } from "lucide-react";-import { useContext, useState } from "react";+import { Dispatch, SetStateAction, useContext, useState } from "react";import { toast } from "sonner";import { AnalyticsContext } from ".";-export default function ExportButton() {+export default function ExportButton({+ setOpenPopover,+}: {+ setOpenPopover: Dispatch<SetStateAction<boolean>>;+}) {const [loading, setLoading] = useState(false);const { queryString } = useContext(AnalyticsContext);@@ -36,22 +40,20 @@ export default function ExportButton() {}return (- <button- disabled={!queryString || loading}- className="flex h-10 w-10 flex-shrink-0 items-center justify-center rounded-md border border-gray-200 bg-white transition-all focus:border-gray-500 focus:outline-none focus:ring-4 focus:ring-gray-200 disabled:cursor-progress disabled:text-gray-400 disabled:hover:bg-white disabled:active:bg-white"+ <Button+ text="Export CSV"+ variant="outline"+ icon={<Download className="h-4 w-4" />}+ className="h-9 justify-start px-2 text-black"onClick={() => {+ setOpenPopover(false);toast.promise(exportData(), {loading: "Exporting files...",success: "Exported successfully",error: (error) => error,});}}- >- {loading ? (- <LoadingSpinner className="h-4 w-4" />- ) : (- <Download className="h-4 w-4" />- )}- </button>+ loading={loading}+ />);}diff --git a/apps/web/ui/analytics/share-analytics.tsx b/apps/web/ui/analytics/share-analytics.tsxnew file mode 100644index 0000000000..949d8b8aac--- /dev/null+++ b/apps/web/ui/analytics/share-analytics.tsx@@ -0,0 +1,99 @@+import { Copy, Switch, Tick, useOptimisticUpdate } from "@dub/ui";+import { linkConstructor } from "@dub/utils";+import { useContext, useState } from "react";+import { toast } from "sonner";+import { AnalyticsContext } from ".";++export default function ShareAnalytics() {+ const { queryString, domain, key } = useContext(AnalyticsContext) as {+ queryString: string;+ domain: string;+ key: string; // coerce to string since <SharePopover is not shown if key is undefined)+ };++ const { data, isLoading, update } = useOptimisticUpdate<{+ publicStats: boolean;+ }>(`/api/analytics/public-stats?${queryString}`, {+ loading: "Updating...",+ success: "Successfully updated stats page visibility!",+ error: "Something went wrong",+ });++ const handleUpdate = async (checked: boolean) => {+ const res = await fetch(`/api/analytics/public-stats?${queryString}`, {+ method: "PUT",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ publicStats: checked,+ }),+ });+ if (!res.ok) {+ throw new Error("Failed to update email preferences");+ }+ if (res.status === 200) {+ checked &&+ navigator.clipboard.writeText(+ `https://${domain}/stats/${encodeURIComponent(key)}`,+ );+ }+ return { publicStats: checked };+ };++ const [copied, setCopied] = useState(false);++ return (+ <div className="w-full divide-y divide-gray-200 text-sm md:w-72">+ <div className="p-4">+ <p className="text-gray-500">Share analytics for</p>+ <p className="truncate font-semibold text-gray-800">+ {linkConstructor({ key, domain, pretty: true })}+ </p>+ </div>+ <div className="p-4">+ <div className="mb-2 flex items-center justify-between">+ <p className="font-semibold text-gray-800">Public Stats Page</p>+ <Switch+ checked={data?.publicStats}+ loading={isLoading}+ fn={(checked: boolean) => {+ update(() => handleUpdate(checked), { publicStats: checked });+ }}+ />+ </div>+ <p className="text-gray-500">+ Making stats public will allow anyone with the link to see the stats+ for this short link.+ </p>+ </div>+ <div className="p-4">+ <p className="font-semibold text-gray-800">Share Link</p>+ <div className="divide-x-200 mt-2 flex items-center justify-between divide-x overflow-hidden rounded-md border border-gray-200 bg-gray-100">+ <div className="scrollbar-hide overflow-scroll pl-2">+ <p className="whitespace-nowrap text-gray-600">+ https://{domain}/stats/{encodeURIComponent(key)}+ </p>+ </div>+ <button+ className="h-8 flex-none border-l bg-white px-2 hover:bg-gray-50 active:bg-gray-100"+ onClick={() => {+ navigator.clipboard.writeText(+ `https://${domain}/stats/${encodeURIComponent(key)}`,+ );+ setCopied(true);+ toast.success("Copied to clipboard");+ setTimeout(() => setCopied(false), 3000);+ }}+ >+ {copied ? (+ <Tick className="h-4 w-4 text-gray-500" />+ ) : (+ <Copy className="h-4 w-4 text-gray-500" />+ )}+ </button>+ </div>+ </div>+ </div>+ );+}diff --git a/apps/web/ui/analytics/share-popover.tsx b/apps/web/ui/analytics/share-popover.tsxdeleted file mode 100644index 56377dc002..0000000000--- a/apps/web/ui/analytics/share-popover.tsx+++ /dev/null@@ -1,129 +0,0 @@-import {- Copy,- IconMenu,- Popover,- Switch,- Tick,- useOptimisticUpdate,-} from "@dub/ui";-import { cn, linkConstructor } from "@dub/utils";-import { Share2 } from "lucide-react";-import { useContext, useState } from "react";-import { toast } from "sonner";-import { AnalyticsContext } from ".";--export default function SharePopover() {- const [openSharePopover, setopenSharePopoverPopover] = useState(false);-- const { queryString, domain, key } = useContext(AnalyticsContext) as {- queryString: string;- domain: string;- key: string; // coerce to string since <SharePopover is not shown if key is undefined)- };-- const { data, isLoading, update } = useOptimisticUpdate<{- publicStats: boolean;- }>(`/api/analytics/public-stats?${queryString}`, {- loading: "Updating...",- success: "Successfully updated stats page visibility!",- error: "Something went wrong",- });-- const handleUpdate = async (checked: boolean) => {- const res = await fetch(`/api/analytics/public-stats?${queryString}`, {- method: "PUT",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify({- publicStats: checked,- }),- });- if (!res.ok) {- throw new Error("Failed to update email preferences");- }- if (res.status === 200) {- checked &&- navigator.clipboard.writeText(- `https://${domain}/stats/${encodeURIComponent(key)}`,- );- }- return { publicStats: checked };- };-- const [copied, setCopied] = useState(false);-- if (!data) return null;-- return (- <Popover- content={- <div className="w-full divide-y divide-gray-200 text-sm md:w-60">- <div className="p-4">- <p className="text-gray-500">Share stats for</p>- <p className="truncate font-semibold text-gray-800">- {linkConstructor({ key, domain, pretty: true })}- </p>- </div>- <div className="p-4">- <div className="mb-2 flex items-center justify-between">- <p className="font-semibold text-gray-800">Public Stats Page</p>- <Switch- checked={data?.publicStats}- loading={isLoading}- fn={(checked: boolean) => {- update(() => handleUpdate(checked), { publicStats: checked });- }}- />- </div>- <p className="text-gray-500">- Making stats public will allow anyone with the link to see the- stats for this short link.- </p>- </div>- <div className="p-4">- <p className="font-semibold text-gray-800">Share Link</p>- <div className="divide-x-200 mt-2 flex items-center justify-between divide-x overflow-hidden rounded-md border border-gray-200 bg-gray-100">- <div className="scrollbar-hide overflow-scroll pl-2">- <p className="whitespace-nowrap text-gray-600">- https://{domain}/stats/{encodeURIComponent(key)}- </p>- </div>- <button- className="h-8 flex-none border-l bg-white px-2 hover:bg-gray-50 active:bg-gray-100"- onClick={() => {- navigator.clipboard.writeText(- `https://${domain}/stats/${encodeURIComponent(key)}`,- );- setCopied(true);- toast.success("Copied to clipboard");- setTimeout(() => setCopied(false), 3000);- }}- >- {copied ? (- <Tick className="h-4 w-4 text-gray-500" />- ) : (- <Copy className="h-4 w-4 text-gray-500" />- )}- </button>- </div>- </div>- </div>- }- align="end"- openPopover={openSharePopover}- setOpenPopover={setopenSharePopoverPopover}- >- <button- onClick={() => setopenSharePopoverPopover(!openSharePopover)}- className={cn(- "flex w-24 items-center justify-center space-x-2 rounded-md border bg-white px-3 py-2.5 outline-none transition-all sm:text-sm",- "border-gray-200 bg-white text-gray-900 placeholder-gray-400 transition-all",- "focus-visible:border-gray-500 data-[state=open]:border-gray-500 data-[state=open]:ring-4 data-[state=open]:ring-gray-200",- )}- >- <IconMenu text="Share" icon={<Share2 className="h-4 w-4" />} />- </button>- </Popover>- );-}diff --git a/apps/web/ui/analytics/toggle.tsx b/apps/web/ui/analytics/toggle.tsxindex 78e3b9adcd..25cf9a0479 100644--- a/apps/web/ui/analytics/toggle.tsx+++ b/apps/web/ui/analytics/toggle.tsx@@ -59,10 +59,9 @@ import {import { AnalyticsContext } from ".";import LinkLogo from "../links/link-logo";import { COLORS_LIST } from "../links/tag-badge";+import AnalyticsOptions from "./analytics-options";import DeviceIcon from "./device-icon";-import ExportButton from "./export-button";import RefererIcon from "./referer-icon";-import SharePopover from "./share-popover";import { useAnalyticsFilterOption } from "./utils";export default function Toggle() {@@ -479,7 +478,6 @@ export default function Toggle() {"w-full md:w-auto": key,})}>- {!isPublicStatsPage && key && <SharePopover />}<Filter.SelectclassName="w-full"filters={filters}@@ -606,7 +604,7 @@ export default function Toggle() {};})}/>- {!isPublicStatsPage && <ExportButton />}+ {!isPublicStatsPage && <AnalyticsOptions />}</div></div></div>diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsxindex 50d0cf21ee..01618b6eec 100644--- a/packages/ui/src/index.tsx+++ b/packages/ui/src/index.tsx@@ -36,6 +36,7 @@ export * from "./nav-mobile";export * from "./content";// misc+export * from "./animated-size-container";export * from "./blur-image";export * from "./client-only";export * from "./copy-button";