The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add onError and onTokenExpired
Kiran K435d95b1ea51
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.tsxindex 3b35c3c346..8e71326a4d 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@@ -39,7 +39,15 @@ export default function ReferralsPageClient() {return publicToken ? (<DubDashboardtoken={publicToken}- // onTokenExpired={() => createPublicToken()} // TODO: Will fix this+ onTokenExpired={() => {+ createPublicToken();+ }}+ // containerStyles={{+ // width: "70%",+ // position: "absolute",+ // top: "60%",+ // left: "60%",+ // }}/>) : (<LoadingSpinner className="my-auto h-full w-5 text-gray-400" />diff --git a/apps/web/app/app.dub.co/(dashboard)/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/layout.tsxindex b7e306aa74..a0fc9ef0a4 100644--- a/apps/web/app/app.dub.co/(dashboard)/layout.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/layout.tsx@@ -28,7 +28,7 @@ export default async function Layout({ children }: { children: ReactNode }) {{children}</MainNav></div>- <DubWidget token="dub_embed_xxx" />+ {/* <DubWidget token="dub_embed_xxx" /> */}<Toolbar show={["onboarding"]} /></>);diff --git a/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx b/apps/web/app/app.dub.co/embed/dashboard/page-client.tsxindex 79724b579a..44d074a984 100644--- a/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx+++ b/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx@@ -18,7 +18,7 @@ import {import { Copy, MoneyBill2 } from "@dub/ui/src/icons";import { getPrettyUrl } from "@dub/utils";import { notFound } from "next/navigation";-import { useContext } from "react";+import { useContext, useEffect } from "react";import useReferralAnalytics from "../use-referral-analytics";import { useReferralLink } from "../use-referral-link";import { useReferralProgram } from "../use-referral-program";@@ -46,15 +46,23 @@ export function RewardDashboardPageClient() {token?: string;};+ useEffect(() => {+ if (linkError) {+ window.parent.postMessage(+ {+ originator: "Dub",+ event: "ERROR",+ data: linkError.info,+ },+ "*",+ );+ }+ }, [linkError]);+if (!token) {notFound();}- // Inform parent that the token has expired- if (linkError && linkError.status === 401) {- window.parent.postMessage("TOKEN_EXPIRED", "*");- }-return (<><div className="relative flex flex-col rounded-lg border border-neutral-300 bg-gradient-to-r from-neutral-50 p-4 md:p-6">@@ -141,7 +149,7 @@ export function RewardDashboardPageClient() {<ahref="https://d.to/conversions"target="_blank"- className="mt-2 flex items-center justify-center gap-2 rounded-lg bg-white p-2 transition-colors"+ className="mt-2 flex items-center justify-center gap-1 rounded-lg bg-white p-2 transition-colors"><p className="text-sm text-gray-700">Powered by</p><Wordmark className="h-4" />diff --git a/apps/web/app/app.dub.co/embed/widget/page-client.tsx b/apps/web/app/app.dub.co/embed/widget/page-client.tsxindex d01b104c5d..8ec75071d2 100644--- a/apps/web/app/app.dub.co/embed/widget/page-client.tsx+++ b/apps/web/app/app.dub.co/embed/widget/page-client.tsx@@ -10,7 +10,7 @@ import {} from "@dub/ui";import { GiftFill } from "@dub/ui/src/icons";import { currencyFormatter, getPrettyUrl } from "@dub/utils";-import { useState } from "react";+import { useEffect, useState } from "react";import { useReferralLink } from "../use-referral-link";import { useReferralProgram } from "../use-referral-program";@@ -21,7 +21,24 @@ export function EmbedWidgetPageClient() {const [selectedTab, setSelectedTab] = useState<Tab>("invite");const { program } = useReferralProgram();- const { link, isLoading: isLoadingLink } = useReferralLink();+ const {+ link,+ isLoading: isLoadingLink,+ error: linkError,+ } = useReferralLink();++ useEffect(() => {+ if (linkError) {+ window.parent.postMessage(+ {+ originator: "Dub",+ event: "ERROR",+ data: linkError.info,+ },+ "*",+ );+ }+ }, [linkError]);return (<div>@@ -133,7 +150,7 @@ export function EmbedWidgetPageClient() {<ahref="https://d.to/conversions"target="_blank"- className="mt-2 flex items-center justify-center gap-2 rounded-lg bg-white p-2 transition-colors"+ className="mt-2 flex items-center justify-center gap-1 rounded-lg bg-white p-2 transition-colors"><p className="text-sm text-gray-700">Powered by</p><Wordmark className="h-4" />diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsxindex db51353117..a1f60b5fe3 100644--- a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsx+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/[programId]/page-client.tsx@@ -8,14 +8,15 @@ import { ChartContext } from "@/ui/charts/chart-context";import TimeSeriesChart from "@/ui/charts/time-series-chart";import XAxis from "@/ui/charts/x-axis";import YAxis from "@/ui/charts/y-axis";+import { HeroBackground } from "@/ui/partners/hero-background";import { ProgramCommissionDescription } from "@/ui/partners/program-commission-description";import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";-import { MiniAreaChart } from "@dub/blocks";import {Button,buttonVariants,Check2,MaxWidthWrapper,+ MiniAreaChart,useCopyToClipboard,useRouterStuff,} from "@dub/ui";@@ -31,7 +32,6 @@ import { LinearGradient } from "@visx/gradient";import Link from "next/link";import { useParams } from "next/navigation";import { createContext, useContext, useId, useMemo } from "react";-import { HeroBackground } from "./hero-background";import { SaleTablePartner } from "./sales/sale-table";const ProgramOverviewContext = createContext<{diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.tsindex 866e9192ba..400c39bbd1 100644--- a/packages/embeds/core/src/core.ts+++ b/packages/embeds/core/src/core.ts@@ -4,8 +4,9 @@ import {DUB_POPUP_ID,WIDGET_URL,} from "./constants";+import { EmbedError } from "./error";import { createFloatingButton } from "./floating-button";-import { DubOptions, DubWidgetPlacement } from "./types";+import { DubOptions, DubWidgetPlacement, IfameMessage } from "./types";const CONTAINER_STYLES = (placement: DubWidgetPlacement,@@ -79,7 +80,7 @@ const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {iframe.src = `${iframeUrl}?token=${token}`;iframe.style.width = "100%";- iframe.style.height = "100%";+ iframe.style.height = "100vh";iframe.style.border = "none";iframe.setAttribute("credentialssupport", "");iframe.setAttribute("allow", "clipboard-write");@@ -88,11 +89,14 @@ const createIframe = (iframeUrl: string, token: string): HTMLIFrameElement => {};const renderDashboard = (- options: Pick<DubOptions, "token">,+ options: Pick<+ DubOptions,+ "token" | "containerStyles" | "onError" | "onTokenExpired"+ >,): HTMLElement | null => {console.debug("[Dub] Rendering dashboard.", options);- const { token } = options;+ const { token, containerStyles, onError, onTokenExpired } = options;const existingContainer = document.getElementById(DUB_CONTAINER_ID);@@ -110,9 +114,32 @@ const renderDashboard = (container.id = DUB_CONTAINER_ID;const iframe = createIframe(DASHBOARD_URL, token);+ Object.assign(iframe.style, {+ ...containerStyles,+ });container.appendChild(iframe);+ // Listen the message from the iframe+ window.addEventListener("message", (e) => {+ const { data, event } = e.data as IfameMessage;++ console.debug("[Dub] Iframe message", data);++ if (event === "ERROR") {+ onError?.(+ new EmbedError({+ code: data.code,+ message: data.message,+ }),+ );+ }++ if (data.code === "unauthorized") {+ onTokenExpired?.();+ }+ });+document.body.appendChild(container);return container;@@ -121,8 +148,16 @@ const renderDashboard = (const renderWidget = (options: DubOptions): HTMLElement | null => {console.debug("[Dub] Rendering widget.", options);- const { token, placement, onOpen, onClose, containerStyles, popupStyles } =- options;+ const {+ token,+ placement,+ containerStyles,+ popupStyles,+ onOpen,+ onClose,+ onError,+ onTokenExpired,+ } = options;const existingContainer = document.getElementById(DUB_CONTAINER_ID);@@ -159,6 +194,26 @@ const renderWidget = (options: DubOptions): HTMLElement | null => {popup.appendChild(iframe);container.appendChild(popup);+ // Listen the message from the iframe+ window.addEventListener("message", (e) => {+ const { data, event } = e.data as IfameMessage;++ console.debug("[Dub] Iframe message", data);++ if (event === "ERROR") {+ onError?.(+ new EmbedError({+ code: data.code,+ message: data.message,+ }),+ );+ }++ if (data.code === "unauthorized") {+ onTokenExpired?.();+ }+ });+document.body.appendChild(container);onOpen?.();diff --git a/packages/embeds/core/src/error.ts b/packages/embeds/core/src/error.tsnew file mode 100644index 0000000000..933bd4af8f--- /dev/null+++ b/packages/embeds/core/src/error.ts@@ -0,0 +1,9 @@+export class EmbedError extends Error {+ code: string;++ constructor({ code, message }: { code: string; message: string }) {+ super(message);+ this.code = code;+ this.name = "EmbedError";+ }+}diff --git a/packages/embeds/core/src/example/dashboard.html b/packages/embeds/core/src/example/dashboard.htmlindex 236da7f97e..071c019d9e 100644--- a/packages/embeds/core/src/example/dashboard.html+++ b/packages/embeds/core/src/example/dashboard.html@@ -7,7 +7,7 @@<script>document.addEventListener("DOMContentLoaded", () => {Dub.init({- token: "dub_embed_jnAD1TyhbTfCDfyAYFN5DR5ndXUXm9helYCX",+ token: "dub_embed_",type: "dashboard",});});diff --git a/packages/embeds/core/src/example/widget.html b/packages/embeds/core/src/example/widget.htmlindex f7421f6548..79e3fdd397 100644--- a/packages/embeds/core/src/example/widget.html+++ b/packages/embeds/core/src/example/widget.html@@ -7,9 +7,7 @@<script>document.addEventListener("DOMContentLoaded", () => {Dub.init({- token: "dub_embed_jnAD1TyhbTfCDfyAYFN5DR5ndXUXm9helYCX",- // trigger: "manual",- // placement: "center",+ token: "dub_embed_",});});</script>diff --git a/packages/embeds/core/src/types.ts b/packages/embeds/core/src/types.tsindex fd2117280b..a2a89b7baf 100644--- a/packages/embeds/core/src/types.ts+++ b/packages/embeds/core/src/types.ts@@ -23,6 +23,12 @@ export interface DubOptions {// The widget was closedonClose?: () => void;+ // An error occurred in the APIs+ onError?: (error: Error) => void;++ // The token expired+ onTokenExpired?: () => void;+// The placement of the widgetplacement?: DubWidgetPlacement;@@ -40,3 +46,12 @@ export type DubWidgetPlacement =| "top-right"| "top-left"| "center";++export interface IfameMessage {+ originator: "Dub";+ event: "ERROR";+ data: {+ code: string;+ message: string;+ };+}