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.

Buton to remove the shared dashboard

Kiran K441612dae0b1
diff --git a/apps/web/app/api/analytics/share/[id]/route.ts b/apps/web/app/api/analytics/share/[id]/route.ts
new file mode 100644
index 0000000000..bda4061db0
--- /dev/null
+++ b/apps/web/app/api/analytics/share/[id]/route.ts
@@ -0,0 +1,22 @@
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import { NextResponse } from "next/server";
+
+// DELETE /api/analytics/share/[id] – delete a shared dashboard for a link
+export const DELETE = withWorkspace(
+ async ({ params, workspace }) => {
+ const { id } = params;
+
+ await prisma.sharedDashboard.delete({
+ where: {
+ id,
+ projectId: workspace.id,
+ },
+ });
+
+ return NextResponse.json({ id });
+ },
+ {
+ requiredPermissions: ["links.write"],
+ },
+);
diff --git a/apps/web/app/api/analytics/share/route.ts b/apps/web/app/api/analytics/share/route.ts
index ca5e50552a..bee2c0712c 100644
--- a/apps/web/app/api/analytics/share/route.ts
+++ b/apps/web/app/api/analytics/share/route.ts
@@ -7,21 +7,26 @@ import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
// GET /api/analytics/share – get a shared dashboard for a link
-export const GET = withWorkspace(async ({ searchParams, workspace }) => {
- const { domain, key } = domainKeySchema.parse(searchParams);
+export const GET = withWorkspace(
+ async ({ searchParams, workspace }) => {
+ const { domain, key } = domainKeySchema.parse(searchParams);
- const link = await getLinkOrThrow({
- workspace,
- domain,
- key,
- });
+ const link = await getLinkOrThrow({
+ workspace,
+ domain,
+ key,
+ });
- const sharedDashboard = await prisma.sharedDashboard.findUnique({
- where: { linkId: link.id },
- });
+ const sharedDashboard = await prisma.sharedDashboard.findUnique({
+ where: { linkId: link.id },
+ });
- return NextResponse.json(sharedDashboard);
-});
+ return NextResponse.json(sharedDashboard);
+ },
+ {
+ requiredPermissions: ["links.read"],
+ },
+);
// POST /api/analytics/share – create a shared dashboard for a link
export const POST = withWorkspace(
diff --git a/apps/web/ui/analytics/share-popover.tsx b/apps/web/ui/analytics/share-popover.tsx
index 45eff3aaa2..5f718d00f8 100644
--- a/apps/web/ui/analytics/share-popover.tsx
+++ b/apps/web/ui/analytics/share-popover.tsx
@@ -1,3 +1,4 @@
+import useWorkspace from "@/lib/swr/use-workspace";
import {
AnimatedSizeContainer,
Button,
@@ -13,6 +14,8 @@ import useSWR from "swr";
import { AnalyticsContext } from "./analytics-provider";
export default function SharePopover() {
+ const { id: workspaceId } = useWorkspace();
+ const [removing, setRemoving] = useState(false);
const [openSharePopover, setopenSharePopoverPopover] = useState(false);
const { queryString } = useContext(AnalyticsContext) as {
@@ -50,6 +53,31 @@ export default function SharePopover() {
setIsCreating(false);
};
+ const handleRemove = async () => {
+ if (!data) {
+ return;
+ }
+
+ setRemoving(true);
+
+ const res = await fetch(
+ `/api/analytics/share/${data.id}?workspaceId=${workspaceId}`,
+ {
+ method: "DELETE",
+ },
+ );
+
+ if (!res.ok) {
+ toast.error("Failed to remove shared dashboard.");
+ return;
+ }
+
+ await mutate();
+ toast.success("Removed shared dashboard.");
+ setRemoving(false);
+ setopenSharePopoverPopover(false);
+ };
+
if (isLoading) {
return null;
}
@@ -66,31 +94,44 @@ export default function SharePopover() {
Share this link's analytics with anyone via a shared dashboard –
no signup required.
</p>
+
{data ? (
- <div className="divide-x-200 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-3">
- <p className="whitespace-nowrap text-gray-400">
- {getPrettyUrl(`${APP_DOMAIN}/share/${data.id}`)}
- </p>
+ <>
+ <div className="divide-x-200 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-3">
+ <p className="whitespace-nowrap text-gray-400">
+ {getPrettyUrl(`${APP_DOMAIN}/share/${data.id}`)}
+ </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(
+ `${APP_DOMAIN}/share/${data.id}`,
+ );
+ 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 className="flex justify-start">
+ <Button
+ text={removing ? "Removing..." : "Remove link"}
+ variant="outline"
+ className="w-fit"
+ loading={removing}
+ onClick={handleRemove}
+ />
</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(
- `${APP_DOMAIN}/share/${data.id}`,
- );
- 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>
+ </>
) : (
<Button
text="Create dashboard"