|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import type { ApiKey } from "@3rdweb-sdk/react/hooks/useApi"; |
| 5 | +import { ApiKeysMenu } from "components/settings/ApiKeys/Menu"; |
| 6 | +import Link from "next/link"; |
| 7 | +import { useMemo, useState } from "react"; |
| 8 | +import { ConnectSDKCard } from "../../../../../components/shared/ConnectSDKCard"; |
| 9 | +import { ConnectAnalyticsDashboard } from "../../../../team/[team_slug]/[project_slug]/connect/analytics/ConnectAnalyticsDashboard"; |
| 10 | + |
| 11 | +export function ConnectAnalyticsPage(props: { |
| 12 | + defaultClientId: string | undefined; |
| 13 | + apiKeys: ApiKey[]; |
| 14 | +}) { |
| 15 | + const { defaultClientId, apiKeys } = props; |
| 16 | + const [selectedKey_, setSelectedKey] = useState<undefined | ApiKey>(); |
| 17 | + |
| 18 | + const selectedKey = useMemo(() => { |
| 19 | + if (selectedKey_) { |
| 20 | + return selectedKey_; |
| 21 | + } |
| 22 | + if (apiKeys.length) { |
| 23 | + if (defaultClientId) { |
| 24 | + return apiKeys.find((k) => k.key === defaultClientId); |
| 25 | + } |
| 26 | + return apiKeys[0]; |
| 27 | + } |
| 28 | + return undefined; |
| 29 | + }, [apiKeys, defaultClientId, selectedKey_]); |
| 30 | + |
| 31 | + return ( |
| 32 | + <div> |
| 33 | + <div className="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between"> |
| 34 | + <div> |
| 35 | + <h1 className="mb-1 font-semibold text-3xl tracking-tight"> |
| 36 | + Connect Analytics |
| 37 | + </h1> |
| 38 | + <p className="text-muted-foreground"> |
| 39 | + Visualize how users are connecting to your apps. |
| 40 | + </p> |
| 41 | + </div> |
| 42 | + |
| 43 | + {/* API selector */} |
| 44 | + {selectedKey && ( |
| 45 | + <div> |
| 46 | + <ApiKeysMenu |
| 47 | + apiKeys={apiKeys} |
| 48 | + selectedKey={selectedKey} |
| 49 | + onSelect={(key) => { |
| 50 | + // add clientId search param to the url without affecting router |
| 51 | + window.history.replaceState( |
| 52 | + {}, |
| 53 | + "", |
| 54 | + `/dashboard/connect/analytics?clientId=${key.key}`, |
| 55 | + ); |
| 56 | + setSelectedKey(key); |
| 57 | + }} |
| 58 | + /> |
| 59 | + </div> |
| 60 | + )} |
| 61 | + </div> |
| 62 | + |
| 63 | + <div className="h-4 lg:h-8" /> |
| 64 | + |
| 65 | + {selectedKey ? ( |
| 66 | + <ConnectAnalyticsDashboard clientId={selectedKey.key} /> |
| 67 | + ) : ( |
| 68 | + <NoAPIFoundCard /> |
| 69 | + )} |
| 70 | + |
| 71 | + <div className="h-4 lg:h-8" /> |
| 72 | + <ConnectSDKCard description="Add the Connect SDK to your app to start collecting analytics." /> |
| 73 | + </div> |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +function NoAPIFoundCard() { |
| 78 | + return ( |
| 79 | + <div className="flex flex-col items-center rounded-lg border border-border bg-muted/50 px-4 py-10 lg:px-6"> |
| 80 | + <h3 className="mb-3 font-semibold text-2xl">No API keys found</h3> |
| 81 | + <p className="mb-6 text-muted-foreground text-sm"> |
| 82 | + Start using the Connect SDK in your app with a free API key. |
| 83 | + </p> |
| 84 | + <Button asChild variant="primary"> |
| 85 | + <Link href="/dashboard/settings/api-keys">Create API Key</Link> |
| 86 | + </Button> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
0 commit comments