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.

add <DubDashboard />

Kiran K56e25daa07d5
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
index 85b15a785e..4ff7073ea3 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/referrals/page-client.tsx
@@ -1,7 +1,7 @@
"use client";
import useWorkspace from "@/lib/swr/use-workspace";
-import { RewardDashboard } from "@/ui/embed/referrals-embed";
+import { DubDashboard } from "@dub/embed-react";
import { LoadingSpinner } from "@dub/ui";
import { redirect } from "next/navigation";
import { useCallback, useEffect, useState } from "react";
@@ -37,8 +37,8 @@ export default function ReferralsPageClient() {
}
return publicToken ? (
- <RewardDashboard
- publicToken={publicToken}
+ <DubDashboard
+ linkToken={publicToken}
onTokenExpired={() => createPublicToken()}
/>
) : (
diff --git a/apps/web/package.json b/apps/web/package.json
index 138db04d89..7348950537 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -19,6 +19,7 @@
"@dub/tailwind-config": "workspace:*",
"@dub/ui": "workspace:*",
"@dub/utils": "workspace:*",
+ "@dub/embed-react": "workspace:*",
"@hookform/resolvers": "^3.9.0",
"@next-auth/prisma-adapter": "^1.0.5",
"@planetscale/database": "^1.18.0",
diff --git a/apps/web/public/embed.js b/apps/web/public/embed.js
index e8565c18ab..b067cfe7c2 100644
--- a/apps/web/public/embed.js
+++ b/apps/web/public/embed.js
@@ -1,9 +1,13 @@
+
+
(function () {
window.Dub = window.Dub || {};
// Default configurations
Dub.options = {
linkToken: null,
+ widgetUrl: "http://localhost:8888/embed/widget",
+ dashboardUrl: "http://localhost:8888/embed/dashboard",
};
const buttonStyles = {
@@ -25,10 +29,37 @@
zIndex: "9998",
};
- // Add a floating button to the page
- function createFloatingButton() {
+ const createIframe = (iframeUrl, linkToken) => {
+ const iframe = document.createElement("iframe");
+
+ iframe.src = `${iframeUrl}?token=${linkToken}`;
+ iframe.style.width = "100%";
+ iframe.style.height = "100%";
+ iframe.style.border = "none";
+ iframe.setAttribute("credentialssupport", "");
+ iframe.setAttribute("allow", "same-origin");
+ iframe.crossOrigin = "use-credentials";
+
+ return iframe;
+ };
+
+ // Find the button that should trigger the widget open
+ const initializeDubButton = () => {
+ const button = document.querySelector("[data-dub-token]");
+
+ if (!button) {
+ console.log("No button found with data-dub-token");
+ return;
+ }
+
+ button.addEventListener("click", () => {
+ console.log("Button clicked");
+ });
+ };
+
+ // Add a floating button
+ const createFloatingButton = () => {
const button = document.createElement("div");
- button.id = "floating-widget-button";
Object.assign(button.style, buttonStyles);
button.innerHTML = `
@@ -51,32 +82,25 @@
return;
}
- // Create iframe container
const container = document.createElement("div");
container.id = "dub-widget-container";
Object.assign(container.style, containerStyles);
- // Create iframe
- const iframe = document.createElement("iframe");
- iframe.src = `http://localhost:8888/embed/widget?token=${Dub.options.linkToken}`;
- iframe.style.width = "100%";
- iframe.style.height = "100%";
- iframe.style.border = "none";
- iframe.setAttribute("credentialssupport", "");
- iframe.setAttribute("allow", "same-origin");
- iframe.crossOrigin = "use-credentials";
+ const { iframeUrl, linkToken } = Dub.options;
+
+ const iframe = createIframe(iframeUrl, linkToken);
container.appendChild(iframe);
document.body.appendChild(container);
});
document.body.appendChild(button);
- }
+ };
// Initialize when DOM is ready
function init() {
setTimeout(createFloatingButton, 100);
- console.log("Initialized");
+ // setTimeout(initializeDubButton, 100);
}
if (document.readyState === "loading") {
diff --git a/dub.html b/dub.html
index 3245775e80..fb55b430d0 100644
--- a/dub.html
+++ b/dub.html
@@ -7,12 +7,16 @@
<script async src="http://localhost:8888/embed.js"></script>
</head>
<body>
- <script type="text/javascript">
+ <!-- <script type="text/javascript">
window.addEventListener("load", function () {
window.Dub.options = {
- linkToken: "dub_embed_7cnHQVYxXxr7tzIAuuPwjDVWT5oC0rD2WyUu", // replace with token
+ linkToken: "dub_embed_cUWcbfn76uAwaHbgoq8yuH6QvOgIaAQb0yoc", // replace with token
};
});
- </script>
+ </script> -->
+
+ <button data-dub-token="dub_embed_cUWcbfn76uAwaHbgoq8yuH6QvOgIaAQb0yoc">
+ Click Me
+ </button>
</body>
</html>
diff --git a/packages/embeds/react/README.md b/packages/embeds/react/README.md
new file mode 100644
index 0000000000..05a51af07e
--- /dev/null
+++ b/packages/embeds/react/README.md
@@ -0,0 +1,11 @@
+# `@dub/embed-react`
+
+`@dub/embed-react` is a library of React components that are used embed Dub's widget on third-party websites.
+
+## Installation
+
+To install the package, run:
+
+```bash
+pnpm i @dub/embed-react
+```
diff --git a/packages/widgets/package.json b/packages/embeds/react/package.json
similarity index 91%
rename from packages/widgets/package.json
rename to packages/embeds/react/package.json
index 73f27dc578..13e3cf8d83 100644
--- a/packages/widgets/package.json
+++ b/packages/embeds/react/package.json
@@ -1,18 +1,21 @@
{
- "name": "@dub/widgets",
- "description": "UI widgets for Dub Partners.",
- "version": "0.1.66",
+ "name": "@dub/embed-react",
+ "description": "Embed React components for Dub.",
+ "version": "0.0.1",
"sideEffects": false,
+ "main": "./dist/index.js",
+ "module": "./dist/index.mjs",
+ "types": "./dist/index.d.ts",
"files": [
"dist/**"
],
"scripts": {
"build": "tsup",
"lint": "eslint src/",
+ "dev": "tsup --watch",
"check-types": "tsc --noEmit"
},
"peerDependencies": {
- "next": "14.2.0-canary.47",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
diff --git a/packages/widgets/postcss.config.js b/packages/embeds/react/postcss.config.js
similarity index 100%
rename from packages/widgets/postcss.config.js
rename to packages/embeds/react/postcss.config.js
diff --git a/packages/embeds/react/src/dashboard.tsx b/packages/embeds/react/src/dashboard.tsx
new file mode 100644
index 0000000000..9808cf86b7
--- /dev/null
+++ b/packages/embeds/react/src/dashboard.tsx
@@ -0,0 +1,48 @@
+import { APP_DOMAIN } from "@dub/utils";
+import { useEffect } from "react";
+
+interface DubDashboardProps {
+ linkToken: string;
+ onTokenExpired?: () => void;
+ url?: string;
+}
+
+const iframeStyles = {
+ width: "100%",
+ height: "100vh",
+ border: "none",
+};
+
+export const DubDashboard = ({
+ linkToken,
+ onTokenExpired,
+ url = `${APP_DOMAIN}/embed/dashboard`,
+}: DubDashboardProps) => {
+ // Listen for messages from the iframe
+ useEffect(() => {
+ const handleMessage = (event: MessageEvent) => {
+ if (event.origin !== APP_DOMAIN) {
+ return;
+ }
+
+ if (event.data === "TOKEN_EXPIRED") {
+ console.error("[Dub] Link token is expired.");
+ onTokenExpired?.();
+ }
+ };
+
+ window.addEventListener("message", handleMessage);
+
+ return () => window.removeEventListener("message", handleMessage);
+ }, [onTokenExpired]);
+
+ // If no link token is provided
+ if (!linkToken) {
+ console.error("[Dub] A link token is required to embed the dashboard.");
+ return null;
+ }
+
+ const dashboardUrl = `${url}?token=${linkToken}`;
+
+ return <iframe src={dashboardUrl} style={iframeStyles} />;
+};
diff --git a/packages/widgets/src/embed.js b/packages/embeds/react/src/embed.js
similarity index 100%
rename from packages/widgets/src/embed.js
rename to packages/embeds/react/src/embed.js
diff --git a/packages/embeds/react/src/embed.ts b/packages/embeds/react/src/embed.ts
new file mode 100644
index 0000000000..4b23b7ba78
--- /dev/null
+++ b/packages/embeds/react/src/embed.ts
@@ -0,0 +1,16 @@
+// TODO:
+// - Convert to TS
+// - Add a loading state
+// - Add a close button
+// - Inline embed (dashboard)
+// - Floating button + widget
+// - Open on a button click
+
+interface DubOptions {
+ linkToken: string;
+ widgetUrl: string;
+ dashboardUrl: string;
+}
+
+// <DubDashboard />
+// <DubWidget />
\ No newline at end of file
diff --git a/packages/embeds/react/src/index.ts b/packages/embeds/react/src/index.ts
new file mode 100644
index 0000000000..67633b6453
--- /dev/null
+++ b/packages/embeds/react/src/index.ts
@@ -0,0 +1,2 @@
+export * from "./dashboard";
+export * from "./widget";
diff --git a/packages/embeds/react/src/widget.tsx b/packages/embeds/react/src/widget.tsx
new file mode 100644
index 0000000000..aa8354285f
--- /dev/null
+++ b/packages/embeds/react/src/widget.tsx
@@ -0,0 +1,3 @@
+export const DubWidget = () => {
+ return <div>Dub Widget</div>;
+};
diff --git a/packages/widgets/tailwind.config.ts b/packages/embeds/react/tailwind.config.ts
similarity index 100%
rename from packages/widgets/tailwind.config.ts
rename to packages/embeds/react/tailwind.config.ts
diff --git a/packages/widgets/tsconfig.json b/packages/embeds/react/tsconfig.json
similarity index 100%
rename from packages/widgets/tsconfig.json
rename to packages/embeds/react/tsconfig.json
diff --git a/packages/widgets/tsup.config.ts b/packages/embeds/react/tsup.config.ts
similarity index 91%
rename from packages/widgets/tsup.config.ts
rename to packages/embeds/react/tsup.config.ts
index 6a8a1e5cb0..8c1cd6d5ff 100644
--- a/packages/widgets/tsup.config.ts
+++ b/packages/embeds/react/tsup.config.ts
@@ -1,7 +1,7 @@
import { defineConfig, Options } from "tsup";
export default defineConfig((options: Options) => ({
- entry: ["src/**/*.tsx"],
+ entry: ["src/**/*.ts"],
format: ["esm"],
esbuildOptions(options) {
options.banner = {
diff --git a/packages/widgets/README.md b/packages/widgets/README.md
deleted file mode 100644
index b5721d0597..0000000000
--- a/packages/widgets/README.md
+++ /dev/null
@@ -1,11 +0,0 @@
-# `@dub/ui`
-
-`@dub/ui` is a library of React components that are used across Dub.co's web applications.
-
-## Installation
-
-To install the package, run:
-
-```bash
-pnpm i @dub/ui
-```
diff --git a/packages/widgets/src/index.ts b/packages/widgets/src/index.ts
deleted file mode 100644
index e69de29bb2..0000000000
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d4e0951b3a..de83360b16 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -48,6 +48,9 @@ importers:
'@dub/analytics':
specifier: ^0.0.21
version: 0.0.21
+ '@dub/embed-react':
+ specifier: workspace:*
+ version: link:../../packages/embeds/react
'@dub/tailwind-config':
specifier: workspace:*
version: link:../../packages/tailwind-config
@@ -486,41 +489,7 @@ importers:
specifier: ^5.6.2
version: 5.6.2
- packages/stripe-app:
- dependencies:
- '@stripe/ui-extension-sdk':
- specifier: ^8.8.1
- version: 8.8.1(@remote-ui/[email protected])([email protected])
- stripe:
- specifier: ^13.11.0
- version: 13.11.0
- devDependencies:
- '@stripe/ui-extension-tools':
- specifier: ^0.0.1
- version: 0.0.1(@babel/[email protected])
-
- packages/tailwind-config:
- dependencies:
- '@tailwindcss/forms':
- specifier: ^0.5.6
- version: 0.5.6([email protected])
- '@tailwindcss/typography':
- specifier: ^0.5.9
- version: 0.5.9([email protected])
- tailwind-scrollbar-hide:
- specifier: ^1.1.7
- version: 1.1.7
- tailwindcss-radix:
- specifier: ^2.8.0
- version: 2.8.0
- devDependencies:
- tailwindcss:
- specifier: ^3.4.4
- version: 3.4.4
-
- packages/tsconfig: {}
-
- packages/ui:
+ packages/embeds/react:
dependencies:
'@floating-ui/react':
specifier: ^0.26.20
@@ -533,22 +502,22 @@ importers:
'@radix-ui/react-checkbox':
specifier: ^1.0.4
'@radix-ui/react-dialog':
specifier: 1.0.5
'@radix-ui/react-label':
specifier: ^2.0.2
'@radix-ui/react-navigation-menu':
specifier: ^1.1.2
'@radix-ui/react-popover':
specifier: 1.0.7
'@radix-ui/react-radio-group':
specifier: ^1.1.3
'@radix-ui/react-slot':
specifier: 1.0.1
version: 1.0.1([email protected])
@@ -557,10 +526,10 @@ importers:
'@radix-ui/react-tooltip':
specifier: ^1.0.7
'@radix-ui/react-visually-hidden':
specifier: ^1.1.0
'@react-aria/datepicker':
specifier: ^3.10.0
@@ -593,7 +562,7 @@ importers:
version: 0.7.0
cmdk:
specifier: ^0.2.0
date-fns:
specifier: ^3.6.0
version: 3.6.0
@@ -605,7 +574,7 @@ importers:
version: 8.1.7([email protected])
framer-motion:
specifier: ^10.16.16
js-cookie:
specifier: ^3.0.5
version: 3.0.5
@@ -632,26 +601,26 @@ importers:
swr:
specifier: ^2.1.5
- version: 2.2.4([email protected])
+ version: 2.1.5([email protected])
use-debounce:
specifier: ^8.0.4
version: 8.0.4([email protected])
vaul:
specifier: ^0.9.6
devDependencies:
'@dub/tailwind-config':
specifier: workspace:*
- version: link:../tailwind-config
+ version: link:../../tailwind-config
'@dub/utils':
specifier: workspace:*
- version: link:../utils
+ version: link:../../utils
'@types/js-cookie':
specifier: ^3.0.6
version: 3.0.6
'@types/react':
specifier: ^18.2.47
- version: 18.2.48
+ version: 18.2.47
'@types/react-dom':
specifier: ^18.2.14
version: 18.2.14
@@ -672,67 +641,49 @@ importers:
version: 3.4.4
tsconfig:
specifier: workspace:*
- version: link:../tsconfig
+ version: link:../../tsconfig
tsup:
specifier: ^6.1.3
typescript:
specifier: ^5.1.6
- version: 5.1.6
+ version: 5.6.2
- packages/utils:
+ packages/stripe-app:
dependencies:
- '@sindresorhus/slugify':
- specifier: ^2.2.1
- version: 2.2.1
- chrono-node:
- specifier: ^2.7.5
- version: 2.7.5
- clsx:
- specifier: ^2.1.1
- version: 2.1.1
- ms:
- specifier: ^2.1.3
- version: 2.1.3
- nanoid:
- specifier: ^5.0.1
- version: 5.0.1
- punycode:
- specifier: ^2.3.0
- version: 2.3.0
- tailwind-merge:
- specifier: ^2.4.0
- version: 2.4.0
+ '@stripe/ui-extension-sdk':
+ specifier: ^8.8.1
+ version: 8.8.1(@remote-ui/[email protected])([email protected])
+ stripe:
+ specifier: ^13.11.0
+ version: 13.11.0
devDependencies:
- '@types/ms':
- specifier: ^0.7.31
- version: 0.7.31
- '@types/node':
- specifier: 18.11.9
- version: 18.11.9
- '@types/punycode':
- specifier: ^2.1.4
- version: 2.1.4
- '@types/react':
- specifier: ^18.2.5
- version: 18.2.14
- next:
- specifier: 14.2.0-canary.67
- version: 14.2.0-canary.67([email protected])([email protected])
- react:
- specifier: ^18.2.0
- version: 18.2.0
- tsconfig:
- specifier: workspace:*
- version: link:../tsconfig
- tsup:
- specifier: ^6.1.3
- typescript:
- specifier: ^5.1.6
- version: 5.1.6
+ '@stripe/ui-extension-tools':
+ specifier: ^0.0.1
+ version: 0.0.1(@babel/[email protected])
+
+ packages/tailwind-config:
+ dependencies:
+ '@tailwindcss/forms':
+ specifier: ^0.5.6
+ version: 0.5.6([email protected])
+ '@tailwindcss/typography':
+ specifier: ^0.5.9
+ version: 0.5.9([email protected])
+ tailwind-scrollbar-hide:
+ specifier: ^1.1.7
+ version: 1.1.7
+ tailwindcss-radix:
+ specifier: ^2.8.0
+ version: 2.8.0
+ devDependencies:
+ tailwindcss:
+ specifier: ^3.4.4
+ version: 3.4.4
- packages/widgets:
+ packages/tsconfig: {}
+
+ packages/ui:
dependencies:
'@floating-ui/react':
specifier: ^0.26.20
@@ -745,22 +696,22 @@ importers:
'@radix-ui/react-checkbox':
specifier: ^1.0.4
'@radix-ui/react-dialog':
specifier: 1.0.5
'@radix-ui/react-label':
specifier: ^2.0.2
'@radix-ui/react-navigation-menu':
specifier: ^1.1.2
'@radix-ui/react-popover':
specifier: 1.0.7
'@radix-ui/react-radio-group':
specifier: ^1.1.3
'@radix-ui/react-slot':
specifier: 1.0.1
version: 1.0.1([email protected])
@@ -769,10 +720,10 @@ importers:
'@radix-ui/react-tooltip':
specifier: ^1.0.7
'@radix-ui/react-visually-hidden':
specifier: ^1.1.0
'@react-aria/datepicker':
specifier: ^3.10.0
@@ -805,7 +756,7 @@ importers:
version: 0.7.0
cmdk:
specifier: ^0.2.0
date-fns:
specifier: ^3.6.0
version: 3.6.0
@@ -817,7 +768,7 @@ importers:
version: 8.1.7([email protected])
framer-motion:
specifier: ^10.16.16
js-cookie:
specifier: ^3.0.5
version: 3.0.5
@@ -844,13 +795,13 @@ importers:
swr:
specifier: ^2.1.5
- version: 2.1.5([email protected])
+ version: 2.2.4([email protected])
use-debounce:
specifier: ^8.0.4
version: 8.0.4([email protected])
vaul:
specifier: ^0.9.6
devDependencies:
'@dub/tailwind-config':
specifier: workspace:*
@@ -863,7 +814,7 @@ importers:
version: 3.0.6
'@types/react':
specifier: ^18.2.47
- version: 18.2.47
+ version: 18.2.48
'@types/react-dom':
specifier: ^18.2.14
version: 18.2.14
@@ -887,10 +838,62 @@ importers:
version: link:../tsconfig
tsup:
specifier: ^6.1.3
typescript:
specifier: ^5.1.6
- version: 5.6.2
+ version: 5.1.6
+
+ packages/utils:
+ dependencies:
+ '@sindresorhus/slugify':
+ specifier: ^2.2.1
+ version: 2.2.1
+ chrono-node:
+ specifier: ^2.7.5
+ version: 2.7.5
+ clsx:
+ specifier: ^2.1.1
+ version: 2.1.1
+ ms:
+ specifier: ^2.1.3
+ version: 2.1.3
+ nanoid:
+ specifier: ^5.0.1
+ version: 5.0.1
+ punycode:
+ specifier: ^2.3.0
+ version: 2.3.0
+ tailwind-merge:
+ specifier: ^2.4.0
+ version: 2.4.0
+ devDependencies:
+ '@types/ms':
+ specifier: ^0.7.31
+ version: 0.7.31
+ '@types/node':
+ specifier: 18.11.9
+ version: 18.11.9
+ '@types/punycode':
+ specifier: ^2.1.4
+ version: 2.1.4
+ '@types/react':
+ specifier: ^18.2.5
+ version: 18.2.14
+ next:
+ specifier: 14.2.0-canary.67
+ version: 14.2.0-canary.67([email protected])([email protected])
+ react:
+ specifier: ^18.2.0
+ version: 18.2.0
+ tsconfig:
+ specifier: workspace:*
+ version: link:../tsconfig
+ tsup:
+ specifier: ^6.1.3
+ typescript:
+ specifier: ^5.1.6
+ version: 5.1.6
packages:
@@ -5789,7 +5792,7 @@ packages:
react: ^16.8 || ^17.0 || ^18.0
react-dom: ^16.8 || ^17.0 || ^18.0
dependencies:
- '@babel/runtime': 7.23.1
+ '@babel/runtime': 7.23.9
'@radix-ui/primitive': 1.0.0
'@radix-ui/react-collapsible': 1.0.1([email protected])([email protected])
'@radix-ui/react-collection': 1.0.1([email protected])([email protected])
@@ -6649,7 +6652,7 @@ packages:
react: ^16.8 || ^17.0 || ^18.0
react-dom: ^16.8 || ^17.0 || ^18.0
dependencies:
- '@babel/runtime': 7.23.1
+ '@babel/runtime': 7.23.9
'@radix-ui/primitive': 1.0.0
'@radix-ui/react-collection': 1.0.2([email protected])([email protected])
'@radix-ui/react-compose-refs': 1.0.0([email protected])
@@ -7365,7 +7368,7 @@ packages:
react: ^16.8 || ^17.0 || ^18.0
react-dom: ^16.8 || ^17.0 || ^18.0
dependencies:
- '@babel/runtime': 7.23.1
+ '@babel/runtime': 7.23.9
'@radix-ui/primitive': 1.0.0
'@radix-ui/react-compose-refs': 1.0.0([email protected])
'@radix-ui/react-context': 1.0.0([email protected])
@@ -8072,7 +8075,7 @@ packages:
'@react-types/datepicker': 3.7.4([email protected])
'@react-types/dialog': 3.5.10([email protected])
'@react-types/shared': 3.23.1([email protected])
- '@swc/helpers': 0.5.11
+ '@swc/helpers': 0.5.12
react: 18.2.0
react-dom: 18.2.0([email protected])
dev: false
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index 9a9ae3ca63..11bdc3a1c9 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -2,3 +2,4 @@ packages:
- "apps/*"
- "apps/web/.react-email"
- "packages/*"
+ - "packages/embeds/*"