Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Implement ShareExportButton component and remove ShareSite and ExportButton components

Bill Yang512c8e7e12d9
diff --git a/client/src/app/[site]/components/SubHeader/Export/ExportButton.tsx b/client/src/app/[site]/components/SubHeader/Export/ExportButton.tsx
deleted file mode 100644
index 5c3a0fb39..000000000
--- a/client/src/app/[site]/components/SubHeader/Export/ExportButton.tsx
+++ /dev/null
@@ -1,95 +0,0 @@
-"use client";
-
-import { Download, FileArchive, FileText, Loader2 } from "lucide-react";
-import { useExtracted } from "next-intl";
-import { useState } from "react";
-import { toast } from "@/components/ui/sonner";
-import { Button } from "../../../../../components/ui/button";
-import {
- DropdownMenu,
- DropdownMenuContent,
- DropdownMenuItem,
- DropdownMenuTrigger,
-} from "../../../../../components/ui/dropdown-menu";
-import { Tooltip, TooltipContent, TooltipTrigger } from "../../../../../components/ui/tooltip";
-import { getTimezone, useStore } from "../../../../../lib/store";
-import { exportCsv } from "./exportCsv";
-import { exportPdf } from "./exportPdf";
-import { useStripeSubscription } from "../../../../../lib/subscription/useStripeSubscription";
-
-export function ExportButton() {
- const t = useExtracted();
- const [isExportingCsv, setIsExportingCsv] = useState(false);
- const [isExportingPdf, setIsExportingPdf] = useState(false);
- const { site, time, filters } = useStore();
- const { data: subscription } = useStripeSubscription();
-
- const handleExportPdf = async () => {
- if (!site) {
- toast.error(t("No site selected"));
- return;
- }
-
- setIsExportingPdf(true);
-
- try {
- await exportPdf({ site, time, filters, timeZone: getTimezone() });
- toast.success(t("PDF report downloaded"));
- } catch (error) {
- console.error("PDF export failed:", error);
- toast.error(error instanceof Error ? error.message : t("Failed to generate PDF report"));
- } finally {
- setIsExportingPdf(false);
- }
- };
-
- const handleExportCsv = async () => {
- if (!site) {
- toast.error(t("No site selected"));
- return;
- }
-
- setIsExportingCsv(true);
-
- try {
- const fileCount = await exportCsv({ site, time, filters, timeZone: getTimezone() });
- toast.success(t("Exported {fileCount} files", { fileCount: String(fileCount) }));
- } catch (error) {
- console.error("Export failed:", error);
- toast.error(error instanceof Error ? error.message : t("Export failed. Please try again."));
- } finally {
- setIsExportingCsv(false);
- }
- };
-
- const isExporting = isExportingCsv || isExportingPdf;
-
- return (
- <DropdownMenu>
- <Tooltip>
- <TooltipTrigger asChild>
- <DropdownMenuTrigger asChild>
- <Button variant="secondary" size="icon" disabled={isExporting} className="h-8 w-8">
- {isExporting ? <Loader2 className="h-4 w-4 animate-spin" /> : <Download className="h-4 w-4" />}
- </Button>
- </DropdownMenuTrigger>
- </TooltipTrigger>
- <TooltipContent>
- <p>{t("Export data")}</p>
- </TooltipContent>
- </Tooltip>
- <DropdownMenuContent align="end">
- {subscription?.planName !== "free" && !["appsumo-1", "appsumo-2"].includes(subscription?.planName ?? "") &&
- <DropdownMenuItem onClick={handleExportPdf} disabled={isExportingPdf}>
- <FileText className="h-4 w-4 mr-2" />
- {isExportingPdf ? t("Generating PDF...") : t("Export as PDF Report")}
- </DropdownMenuItem>
- }
- <DropdownMenuItem onClick={handleExportCsv} disabled={isExportingCsv}>
- <FileArchive className="h-4 w-4 mr-2" />
- {isExportingCsv ? t("Exporting...") : t("Export as CSV (ZIP)")}
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
- );
-}
diff --git a/client/src/app/[site]/components/SubHeader/Export/index.ts b/client/src/app/[site]/components/SubHeader/Export/index.ts
index 576c484c2..5923b93ad 100644
--- a/client/src/app/[site]/components/SubHeader/Export/index.ts
+++ b/client/src/app/[site]/components/SubHeader/Export/index.ts
@@ -1,3 +1,2 @@
-export { ExportButton } from "./ExportButton";
export { exportCsv } from "./exportCsv";
export { exportPdf } from "./exportPdf";
diff --git a/client/src/app/[site]/components/SubHeader/ShareExportButton.tsx b/client/src/app/[site]/components/SubHeader/ShareExportButton.tsx
new file mode 100644
index 000000000..e23be5a84
--- /dev/null
+++ b/client/src/app/[site]/components/SubHeader/ShareExportButton.tsx
@@ -0,0 +1,167 @@
+"use client";
+
+import { Copy, Download, FileArchive, FileText, Loader2, Share } from "lucide-react";
+import { useExtracted } from "next-intl";
+import { useParams } from "next/navigation";
+import { useState } from "react";
+import { toast } from "@/components/ui/sonner";
+import {
+ useGeneratePrivateLinkKey,
+ useGetPrivateLinkConfig,
+ useRevokePrivateLinkKey,
+} from "../../../../api/admin/hooks/usePrivateLink";
+import { Button } from "../../../../components/ui/button";
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuSeparator,
+ DropdownMenuTrigger,
+} from "../../../../components/ui/dropdown-menu";
+import { Input } from "../../../../components/ui/input";
+import { Tooltip, TooltipContent, TooltipTrigger } from "../../../../components/ui/tooltip";
+import { authClient } from "../../../../lib/auth";
+import { getTimezone, useStore } from "../../../../lib/store";
+import { useStripeSubscription } from "../../../../lib/subscription/useStripeSubscription";
+import { exportCsv, exportPdf } from "./Export";
+
+export function ShareExportButton() {
+ const t = useExtracted();
+ const session = authClient.useSession();
+ const params = useParams();
+ const siteId = Number(params.site);
+ const canShare = !!session.data;
+
+ const [isExportingCsv, setIsExportingCsv] = useState(false);
+ const [isExportingPdf, setIsExportingPdf] = useState(false);
+ const { site, time, filters } = useStore();
+ const { data: subscription } = useStripeSubscription();
+
+ const { data: privateLink, isLoading: isLoadingPrivateLink } = useGetPrivateLinkConfig(canShare ? siteId : 0);
+ const { mutate: generatePrivateLinkKey, isPending: isGeneratingPrivateLink } = useGeneratePrivateLinkKey();
+ const { mutate: revokePrivateLinkKey } = useRevokePrivateLinkKey();
+
+ const handleExportPdf = async () => {
+ if (!site) {
+ toast.error(t("No site selected"));
+ return;
+ }
+
+ setIsExportingPdf(true);
+
+ try {
+ await exportPdf({ site, time, filters, timeZone: getTimezone() });
+ toast.success(t("PDF report downloaded"));
+ } catch (error) {
+ console.error("PDF export failed:", error);
+ toast.error(error instanceof Error ? error.message : t("Failed to generate PDF report"));
+ } finally {
+ setIsExportingPdf(false);
+ }
+ };
+
+ const handleExportCsv = async () => {
+ if (!site) {
+ toast.error(t("No site selected"));
+ return;
+ }
+
+ setIsExportingCsv(true);
+
+ try {
+ const fileCount = await exportCsv({ site, time, filters, timeZone: getTimezone() });
+ toast.success(t("Exported {fileCount} files", { fileCount: String(fileCount) }));
+ } catch (error) {
+ console.error("Export failed:", error);
+ toast.error(error instanceof Error ? error.message : t("Export failed. Please try again."));
+ } finally {
+ setIsExportingCsv(false);
+ }
+ };
+
+ const isExporting = isExportingCsv || isExportingPdf;
+ const canExportPdf =
+ subscription?.planName !== "free" && !["appsumo-1", "appsumo-2"].includes(subscription?.planName ?? "");
+
+ return (
+ <div className={canShare ? undefined : "hidden md:block"}>
+ <DropdownMenu>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <DropdownMenuTrigger asChild>
+ <Button variant="secondary" size="icon" className="h-8 w-8">
+ {isExporting ? (
+ <Loader2 className="h-4 w-4 animate-spin" />
+ ) : canShare ? (
+ <Share className="h-4 w-4" />
+ ) : (
+ <Download className="h-4 w-4" />
+ )}
+ </Button>
+ </DropdownMenuTrigger>
+ </TooltipTrigger>
+ <TooltipContent>{canShare ? t("Share or export data") : t("Export data")}</TooltipContent>
+ </Tooltip>
+ <DropdownMenuContent align="end" className="max-w-[400px]">
+ {canShare && (
+ <>
+ <div className="flex flex-col p-2">
+ <span className="text-sm font-medium pb-2">{t("Share this dashboard")}</span>
+ {!isLoadingPrivateLink && !privateLink?.privateLinkKey && (
+ <Button onClick={() => generatePrivateLinkKey(siteId)} disabled={isGeneratingPrivateLink}>
+ {isGeneratingPrivateLink ? t("Generating...") : t("Generate Private Link")}
+ </Button>
+ )}
+ {privateLink?.privateLinkKey && (
+ <>
+ <div className="flex items-center">
+ <Input
+ value={`${globalThis.location.protocol}//${globalThis.location.host}/${siteId}/${privateLink?.privateLinkKey}`}
+ readOnly
+ className="rounded-r-none bg-white dark:bg-neutral-900"
+ />
+ <Button
+ size="icon"
+ onClick={() => {
+ const fullUrl = `${globalThis.location.protocol}//${globalThis.location.host}/${siteId}/${privateLink?.privateLinkKey}`;
+ navigator.clipboard.writeText(fullUrl);
+ toast.success(t("Copied to clipboard"));
+ }}
+ className="w-10 rounded-l-none"
+ >
+ <Copy />
+ </Button>
+ </div>
+ <div
+ className="text-xs text-neutral-500 dark:text-neutral-500 mt-1 cursor-pointer hover:text-neutral-600 dark:hover:text-neutral-400"
+ onClick={() => {
+ revokePrivateLinkKey(siteId);
+ toast.success(t("Private link revoked"));
+ }}
+ >
+ {t("Revoke this link")}
+ </div>
+ </>
+ )}
+ <span className="text-xs text-neutral-600 dark:text-neutral-300 mt-2">
+ {t("Generate a private link to share a read-only view of this dashboard with your team.")}
+ </span>
+ </div>
+ <DropdownMenuSeparator />
+ </>
+ )}
+ {canExportPdf && (
+ <DropdownMenuItem onClick={handleExportPdf} disabled={isExportingPdf}>
+ <FileText className="h-4 w-4 mr-2" />
+ {isExportingPdf ? t("Generating PDF...") : t("Export as PDF Report")}
+ </DropdownMenuItem>
+ )}
+ <DropdownMenuItem onClick={handleExportCsv} disabled={isExportingCsv}>
+ <FileArchive className="h-4 w-4 mr-2" />
+ {isExportingCsv ? t("Exporting...") : t("Export as CSV (ZIP)")}
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ );
+}
diff --git a/client/src/app/[site]/components/SubHeader/ShareSite.tsx b/client/src/app/[site]/components/SubHeader/ShareSite.tsx
deleted file mode 100644
index ecc182ed6..000000000
--- a/client/src/app/[site]/components/SubHeader/ShareSite.tsx
+++ /dev/null
@@ -1,78 +0,0 @@
-import { Copy, Share } from "lucide-react";
-import { useExtracted } from "next-intl";
-import { useParams } from "next/navigation";
-import { toast } from "@/components/ui/sonner";
-import {
- useGeneratePrivateLinkKey,
- useGetPrivateLinkConfig,
- useRevokePrivateLinkKey,
-} from "../../../../api/admin/hooks/usePrivateLink";
-import { Button } from "../../../../components/ui/button";
-import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from "../../../../components/ui/dropdown-menu";
-import { Input } from "../../../../components/ui/input";
-import { Tooltip, TooltipContent, TooltipTrigger } from "../../../../components/ui/tooltip";
-
-export function ShareSite() {
- const t = useExtracted();
- const { site } = useParams();
- const { data: privateLink, isLoading: isLoadingPrivateLink } = useGetPrivateLinkConfig(Number(site));
- const { mutate: generatePrivateLinkKey, isPending: isGeneratingPrivateLink } = useGeneratePrivateLinkKey();
- const { mutate: revokePrivateLinkKey } = useRevokePrivateLinkKey();
-
- return (
- <DropdownMenu>
- <Tooltip>
- <TooltipTrigger asChild>
- <DropdownMenuTrigger asChild>
- <Button variant="secondary" size="icon" onClick={() => {}} className="h-8 w-8">
- <Share className="h-4 w-4" />
- </Button>
- </DropdownMenuTrigger>
- </TooltipTrigger>
- <TooltipContent>{t("Share a private link")}</TooltipContent>
- </Tooltip>
- <DropdownMenuContent align="end" className="flex flex-col p-3 max-w-[400px]">
- <span className="text-sm font-medium pb-2">{t("Share this dashboard")}</span>
- {!isLoadingPrivateLink && !privateLink?.privateLinkKey && (
- <Button onClick={() => generatePrivateLinkKey(Number(site))} disabled={isGeneratingPrivateLink}>
- {isGeneratingPrivateLink ? t("Generating...") : t("Generate Private Link")}
- </Button>
- )}
- {privateLink?.privateLinkKey && (
- <>
- <div className="flex items-center">
- <Input
- value={`${globalThis.location.protocol}//${globalThis.location.host}/${site}/${privateLink?.privateLinkKey}`}
- readOnly
- className="rounded-r-none bg-white dark:bg-neutral-900"
- />
- <Button
- size="icon"
- onClick={() => {
- const fullUrl = `${globalThis.location.protocol}//${globalThis.location.host}/${site}/${privateLink?.privateLinkKey}`;
- navigator.clipboard.writeText(fullUrl);
- toast.success(t("Copied to clipboard"));
- }}
- className="w-10 rounded-l-none"
- >
- <Copy />
- </Button>
- </div>
- <div
- className="text-xs text-neutral-500 dark:text-neutral-500 mt-1 cursor-pointer hover:text-neutral-600 dark:hover:text-neutral-400"
- onClick={() => {
- revokePrivateLinkKey(Number(site));
- toast.success(t("Private link revoked"));
- }}
- >
- {t("Revoke this link")}
- </div>
- </>
- )}
- <span className="text-xs text-neutral-600 dark:text-neutral-300 mt-2">
- {t("Generate a private link to share a read-only view of this dashboard with your team.")}
- </span>
- </DropdownMenuContent>
- </DropdownMenu>
- );
-}
diff --git a/client/src/app/[site]/components/SubHeader/SubHeader.tsx b/client/src/app/[site]/components/SubHeader/SubHeader.tsx
index 698179773..0ace67cf4 100644
--- a/client/src/app/[site]/components/SubHeader/SubHeader.tsx
+++ b/client/src/app/[site]/components/SubHeader/SubHeader.tsx
@@ -6,16 +6,13 @@ import { ChevronLeft, ChevronRight } from "lucide-react";
import { Filters } from "./Filters/Filters";
import { DateSelector } from "../../../../components/DateSelector/DateSelector";
-import { authClient } from "../../../../lib/auth";
import { MobileSidebar } from "../Sidebar/MobileSidebar";
-import { ExportButton } from "./Export";
import { NewFilterButton } from "./Filters/NewFilterButton";
import { LiveUserCount } from "./LiveUserCount";
-import { ShareSite } from "./ShareSite";
+import { ShareExportButton } from "./ShareExportButton";
export function SubHeader({ availableFilters }: { availableFilters?: FilterParameter[] }) {
const { time, setTime } = useStore();
- const session = authClient.useSession();
return (
<div>
@@ -28,10 +25,7 @@ export function SubHeader({ availableFilters }: { availableFilters?: FilterParam
</div>
<div className="flex items-center gap-2">
<LiveUserCount />
- {session.data && <ShareSite />}
- <div className="hidden md:block">
- <ExportButton />
- </div>
+ <ShareExportButton />
<DateSelector time={time} setTime={setTime} />
<div className="flex items-center">
<Button