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.

feat: implement tooltip to make user aware

flooorianflod79642d5e4a8
diff --git a/apps/web/ui/analytics/export-button.tsx b/apps/web/ui/analytics/export-button.tsx
index 369753f5a1..c754e2c1b9 100644
--- a/apps/web/ui/analytics/export-button.tsx
+++ b/apps/web/ui/analytics/export-button.tsx
@@ -1,11 +1,13 @@
-import { IconMenu } from "@dub/ui";
+import { IconMenu, Tooltip, TooltipContent } from "@dub/ui";
import zip from "jszip";
import { Download } from "lucide-react";
import { useContext, useState } from "react";
+import { toast } from "sonner";
import { AnalyticsContext } from ".";
export default function ExportButton() {
const [loading, setLoading] = useState(false);
+ const { totalClicks } = useContext(AnalyticsContext);
const { baseApiPath, queryString } = useContext(AnalyticsContext);
const exportableEndpoints = [
"timeseries",
@@ -15,33 +17,60 @@ export default function ExportButton() {
"referer",
];
- return (
+ const exportData = async () => {
+ const zipFile = new zip();
+ for (const endpoint of exportableEndpoints) {
+ try {
+ const data = await fetch(
+ `${baseApiPath}/${endpoint}/export?${queryString}`,
+ {
+ method: "GET",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ },
+ ).then((res) => res.blob());
+ zipFile.file(`${endpoint}.csv`, data);
+ } catch (error) {
+ console.error(error);
+ toast.error("Failed to export");
+ }
+ }
+
+ zipFile.generateAsync({ type: "blob" }).then((blob) => {
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ a.href = url;
+ a.download = "analytics-export.zip";
+ a.click();
+ });
+ };
+
+ // show a tooltip to make the user aware that there is no data to export if there is no data
+ return totalClicks === 0 || !totalClicks ? (
+ <Tooltip content={<TooltipContent title="No data available" />}>
+ <button
+ disabled={loading || totalClicks === 0 || !totalClicks}
+ className="flex cursor-not-allowed items-center justify-center space-x-2 rounded-md bg-white p-4 px-3 py-2.5 text-sm text-gray-400 shadow"
+ onClick={async () => {
+ setLoading(true);
+ await exportData();
+ setLoading(false);
+ toast.success("Exported successfully");
+ }}
+ >
+ <IconMenu text="Export" icon={<Download className="h-4 w-4" />} />
+ </button>
+ </Tooltip>
+ ) : (
<button
- className="flex items-center justify-center space-x-2 rounded-md bg-white p-4 px-3 py-2.5 text-sm shadow transition-all duration-75 hover:shadow-md active:scale-95"
+ disabled={loading}
+ className="disabled: flex items-center justify-center space-x-2 rounded-md bg-white p-4 px-3 py-2.5 text-sm shadow transition-all duration-75 hover:shadow-md active:scale-95 disabled:cursor-progress disabled:text-gray-400 disabled:hover:shadow disabled:active:scale-100"
onClick={async () => {
setLoading(true);
- const zipFile = new zip();
- for (const endpoint of exportableEndpoints) {
- const data = await fetch(
- `${baseApiPath}/${endpoint}/export?${queryString}`,
- {
- method: "GET",
- headers: {
- "Content-Type": "application/json",
- },
- },
- ).then((res) => res.blob());
- zipFile.file(`${endpoint}.csv`, data);
- }
-
- zipFile.generateAsync({ type: "blob" }).then((blob) => {
- const url = URL.createObjectURL(blob);
- const a = document.createElement("a");
- a.href = url;
- a.download = "analytics-export.zip";
- a.click();
- });
+ await exportData();
setLoading(false);
+ toast.success("Exported successfully");
}}
>
{loading ? (