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.

improve MetadataViewer logic

Steven Teyeddb59a24834
diff --git a/apps/web/ui/analytics/events/metadata-viewer.tsx b/apps/web/ui/analytics/events/metadata-viewer.tsx
new file mode 100644
index 0000000000..49b0117040
--- /dev/null
+++ b/apps/web/ui/analytics/events/metadata-viewer.tsx
@@ -0,0 +1,80 @@
+import { Button, Tooltip, useCopyToClipboard } from "@dub/ui";
+import { truncate } from "@dub/utils";
+import { Fragment } from "react";
+
+// Display the event metadata
+export function MetadataViewer({
+ metadata,
+}: {
+ metadata: Record<string, any>;
+}) {
+ const [copied, copyToClipboard] = useCopyToClipboard();
+
+ const displayEntries = Object.entries(metadata)
+ .map(([key, value]) => {
+ if (typeof value === "object" && value !== null) {
+ // Only show nested properties if the parent object has exactly one property
+ if (Object.keys(metadata).length === 1) {
+ const nestedEntries = Object.entries(value).map(
+ ([nestedKey, nestedValue]) => {
+ const displayValue =
+ typeof nestedValue === "object" && nestedValue !== null
+ ? truncate(JSON.stringify(nestedValue), 20)
+ : truncate(String(nestedValue), 20);
+ return `${key}.${nestedKey}: ${displayValue}`;
+ },
+ );
+ // else show the parent object properties
+ return nestedEntries;
+ }
+ return [`${key}: ${truncate(JSON.stringify(value), 20)}`];
+ }
+ return [`${key}: ${truncate(String(value), 20)}`];
+ })
+ .flat();
+
+ const hasMoreItems = displayEntries.length > 3;
+ const visibleEntries = hasMoreItems
+ ? displayEntries.slice(0, 3)
+ : displayEntries;
+
+ return (
+ <div className="flex items-center gap-2 text-xs text-neutral-600">
+ {visibleEntries.map((entry, i) => (
+ <Fragment key={i}>
+ <span className="rounded-md border border-neutral-200 bg-neutral-100 px-1.5 py-0.5">
+ {entry}
+ </span>
+ </Fragment>
+ ))}
+
+ <Tooltip
+ content={
+ <div className="flex flex-col gap-4 overflow-hidden rounded-md border border-neutral-200 bg-white p-4">
+ <div className="flex h-[200px] w-[280px] overflow-hidden rounded-md border border-neutral-200 bg-white sm:h-[300px] sm:w-[350px]">
+ <div className="w-full overflow-auto">
+ <pre className="p-2 text-xs text-neutral-600">
+ {JSON.stringify(metadata, null, 2)}
+ </pre>
+ </div>
+ </div>
+ <Button
+ text={copied ? "Copied!" : "Copy metadata"}
+ onClick={() => copyToClipboard(JSON.stringify(metadata, null, 2))}
+ />
+ </div>
+ }
+ align="start"
+ >
+ <button
+ type="button"
+ className="rounded-md border border-neutral-200 bg-white px-1.5 py-0.5 hover:bg-neutral-50"
+ >
+ {hasMoreItems
+ ? `+${displayEntries.length - 3} more`
+ : "View metadata"}
+ </button>
+ </Tooltip>
+ </div>
+ );
+}
diff --git a/apps/web/ui/customers/customer-activity-list.tsx b/apps/web/ui/customers/customer-activity-list.tsx
index 6af21e897f..b0de54c806 100644
--- a/apps/web/ui/customers/customer-activity-list.tsx
+++ b/apps/web/ui/customers/customer-activity-list.tsx
@@ -1,15 +1,10 @@
import { CustomerActivityResponse } from "@/lib/types";
-import {
- Button,
- DynamicTooltipWrapper,
- LinkLogo,
- useCopyToClipboard,
-} from "@dub/ui";
+import { DynamicTooltipWrapper, LinkLogo } from "@dub/ui";
import { CursorRays, MoneyBill2, UserCheck } from "@dub/ui/icons";
import { formatDateTimeSmart, getApexDomain, getPrettyUrl } from "@dub/utils";
import Link from "next/link";
import { useParams } from "next/navigation";
-import { Fragment } from "react";
+import { MetadataViewer } from "../analytics/events/metadata-viewer";
const activityData = {
click: {
@@ -128,69 +123,6 @@ const activityData = {
},
};
-// Display the event metadata
-function MetadataViewer({ metadata }: { metadata: Record<string, any> }) {
- const [copied, copyToClipboard] = useCopyToClipboard();
-
- const displayEntries = Object.entries(metadata).map(([key, value]) => {
- const displayValue =
- typeof value === "object" && value !== null
- ? JSON.stringify(value).slice(0, 20) + "..."
- : String(value);
-
- return `${key}: ${displayValue}`;
- });
-
- const hasMoreItems = displayEntries.length > 3;
- const visibleEntries = hasMoreItems
- ? displayEntries.slice(0, 3)
- : displayEntries;
-
- return (
- <div className="flex items-center gap-2 text-xs text-neutral-600">
- {visibleEntries.map((entry, i) => (
- <Fragment key={i}>
- <span className="rounded-md border border-neutral-200 bg-neutral-100 px-1.5 py-0.5">
- {entry}
- </span>
- </Fragment>
- ))}
-
- <DynamicTooltipWrapper
- tooltipProps={{
- content: (
- <div className="flex flex-col gap-4 overflow-hidden rounded-md border border-neutral-200 bg-white p-4">
- <div className="flex h-[200px] w-[280px] overflow-hidden rounded-md border border-neutral-200 bg-white sm:h-[300px] sm:w-[350px]">
- <div className="w-full overflow-auto">
- <pre className="p-2 text-xs text-neutral-600">
- {JSON.stringify(metadata, null, 2)}
- </pre>
- </div>
- </div>
- <Button
- text={copied ? "Copied!" : "Copy metadata"}
- onClick={() =>
- copyToClipboard(JSON.stringify(metadata, null, 2))
- }
- />
- </div>
- ),
- align: "start",
- }}
- >
- <button
- type="button"
- className="rounded-md border border-neutral-200 bg-white px-1.5 py-0.5 hover:bg-neutral-50"
- >
- {hasMoreItems
- ? `+${displayEntries.length - 3} more`
- : "View metadata"}
- </button>
- </DynamicTooltipWrapper>
- </div>
- );
-}
-
export function CustomerActivityList({
activity,
isLoading,