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.

update inline dashboard

Kiran Kdc88eab37dd5
diff --git a/packages/embeds/react/src/embed-core.ts b/packages/embeds/react/src/embed-core.ts
index c7cd739860..c72e4bd638 100644
--- a/packages/embeds/react/src/embed-core.ts
+++ b/packages/embeds/react/src/embed-core.ts
@@ -72,6 +72,7 @@ const POPUP_STYLES = (
pointerEvents: "auto",
});
+const DASHBOARD_URL = "http://localhost:8888/embed/dashboard";
const WIDGET_URL = "http://localhost:8888/embed/widget";
const DUB_CONTAINER_ID = "dub-embed-container";
const DUB_POPUP_ID = "dub-embed-popup";
@@ -91,7 +92,40 @@ const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {
return iframe;
};
+const renderDashboard = (
+ options: Pick<Options, "token">,
+): HTMLElement | null => {
+ console.debug("[Dub] Rendering dashboard.", options);
+
+ const { token } = options;
+
+ const existingContainer = document.getElementById(DUB_CONTAINER_ID);
+
+ if (existingContainer) {
+ document.body.removeChild(existingContainer);
+ return existingContainer;
+ }
+
+ if (!token) {
+ console.error("[Dub] A link token is required to embed the widget.");
+ return null;
+ }
+
+ const container = document.createElement("div");
+ container.id = DUB_CONTAINER_ID;
+
+ const iframe = createIframe(DASHBOARD_URL, token);
+
+ container.appendChild(iframe);
+
+ document.body.appendChild(container);
+
+ return container;
+};
+
const renderWidget = (options: Options): HTMLElement | null => {
+ console.debug("[Dub] Rendering widget.", options);
+
const { token, placement, onOpen, onClose, containerStyles, popupStyles } =
options;
@@ -190,11 +224,18 @@ export const toggleWidget = (): void => {
export const init = (options: Options) => {
options.trigger = options.trigger ?? "floating-button";
options.placement = options.placement ?? "bottom-right";
+ options.type = options.type ?? "widget";
- console.debug("[Dub] Initializing");
+ console.debug("[Dub] Initializing.", options);
- const container = renderWidget(options);
- if (!container) return;
+ const container =
+ options.type === "dashboard"
+ ? renderDashboard(options)
+ : renderWidget(options);
+
+ if (!container) {
+ return;
+ }
if (options.trigger === "floating-button")
createFloatingButton({
diff --git a/packages/embeds/react/src/example/html/dashboard.html b/packages/embeds/react/src/example/html/dashboard.html
new file mode 100644
index 0000000000..0e9730f93b
--- /dev/null
+++ b/packages/embeds/react/src/example/html/dashboard.html
@@ -0,0 +1,16 @@
+<html></html>
+ <head>
+ <script type="module" src="../../../dist/embed.js"></script>
+ </head>
+ <body>
+ <h1>Dub Referral Dashboard</h1>
+ <script>
+ document.addEventListener("DOMContentLoaded", () => {
+ Dub.init({
+ token: "dub_embed_jnAD1TyhbTfCDfyAYFN5DR5ndXUXm9helYCX",
+ type: "dashboard",
+ });
+ });
+ </script>
+ </body>
+</html>
\ No newline at end of file
diff --git a/packages/embeds/react/src/example/index.html b/packages/embeds/react/src/example/html/widget.html
similarity index 58%
rename from packages/embeds/react/src/example/index.html
rename to packages/embeds/react/src/example/html/widget.html
index 5fdbec666b..d9c9cca075 100644
--- a/packages/embeds/react/src/example/index.html
+++ b/packages/embeds/react/src/example/html/widget.html
@@ -1,16 +1,9 @@
<html></html>
<head>
- <script
- src="https://unpkg.com/react@17/umd/react.development.js"
- crossorigin
- ></script>
- <script
- src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
- crossorigin
- ></script>
- <script type="module" src="../../dist/embed.js"></script>
+ <script type="module" src="../../../dist/embed.js"></script>
</head>
<body>
+ <h1>Dub Referral Widget</h1>
<script>
document.addEventListener("DOMContentLoaded", () => {
Dub.init({
diff --git a/packages/embeds/react/src/example/inline-dashboard.tsx b/packages/embeds/react/src/example/react/dashboard.tsx
similarity index 81%
rename from packages/embeds/react/src/example/inline-dashboard.tsx
rename to packages/embeds/react/src/example/react/dashboard.tsx
index 8ac1830bb9..cb6c7ae5c5 100644
--- a/packages/embeds/react/src/example/inline-dashboard.tsx
+++ b/packages/embeds/react/src/example/react/dashboard.tsx
@@ -1,5 +1,5 @@
import ReactDom from "react-dom";
-import { DubDashboard } from "../dashboard";
+import { DubDashboard } from "../../dashboard";
const App = () => {
return (
diff --git a/packages/embeds/react/src/example/react/widget.tsx b/packages/embeds/react/src/example/react/widget.tsx
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/packages/embeds/react/src/types.ts b/packages/embeds/react/src/types.ts
index 0f80336db3..ca52798994 100644
--- a/packages/embeds/react/src/types.ts
+++ b/packages/embeds/react/src/types.ts
@@ -16,6 +16,9 @@ export interface Options {
// The link token
token: string;
+ // The type of embed (defaults to widget)
+ type?: "dashboard" | "widget";
+
// The trigger for the widget
trigger?: DubWidgetTrigger;
@@ -28,6 +31,7 @@ export interface Options {
// The placement of the widget
placement?: DubWidgetPlacement;
+ // The styles for the widget container
containerStyles?: Partial<CSSStyleDeclaration>;
popupStyles?: Partial<CSSStyleDeclaration>;
buttonStyles?: Partial<CSSStyleDeclaration>;