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 docs inside the app

Kiran Kaeced07946ee
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/program-settings.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/program-settings.tsx
index c34cfbccdd..81a3c18a66 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/program-settings.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/program-settings.tsx
@@ -4,11 +4,13 @@ import { updateProgramAction } from "@/lib/actions/update-program";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { ProgramProps } from "@/lib/types";
+import { EmbedDocsSheet } from "@/ui/partners/embed-docs-sheet";
import { ProgramCommissionDescription } from "@/ui/partners/program-commission-description";
import { AnimatedSizeContainer, Button } from "@dub/ui";
-import { CircleCheckFill, LoadingSpinner } from "@dub/ui/src/icons";
+import { CircleCheckFill, Code, LoadingSpinner } from "@dub/ui/src/icons";
import { cn, pluralize } from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
+import { useState } from "react";
import {
FormProvider,
useForm,
@@ -59,6 +61,7 @@ type FormData = Pick<
function ProgramSettingsForm({ program }: { program: ProgramProps }) {
const { id: workspaceId } = useWorkspace();
+ const [isEmbedDocsOpen, setIsEmbedDocsOpen] = useState(false);
const form = useForm<FormData>({
mode: "onBlur",
@@ -107,219 +110,233 @@ function ProgramSettingsForm({ program }: { program: ProgramProps }) {
});
return (
- <form
- className="rounded-lg border border-neutral-200 bg-white"
- onSubmit={handleSubmit(async (data) => {
- await executeAsync({
- workspaceId: workspaceId || "",
- programId: program.id,
- ...data,
- commissionAmount:
- data.commissionType === "flat"
- ? data.commissionAmount * 100
- : data.commissionAmount,
- });
+ <>
+ <EmbedDocsSheet isOpen={isEmbedDocsOpen} setIsOpen={setIsEmbedDocsOpen} />
+ <form
+ className="rounded-lg border border-neutral-200 bg-white"
+ onSubmit={handleSubmit(async (data) => {
+ await executeAsync({
+ workspaceId: workspaceId || "",
+ programId: program.id,
+ ...data,
+ commissionAmount:
+ data.commissionType === "flat"
+ ? data.commissionAmount * 100
+ : data.commissionAmount,
+ });
- // Reset isDirty state
- reset(data);
- })}
- >
- <div className="flex items-center border-b border-neutral-200 p-6">
- <h2 className="text-xl font-medium text-neutral-900">Program</h2>
- </div>
+ // Reset isDirty state
+ reset(data);
+ })}
+ >
+ <div className="flex items-center justify-between border-b border-neutral-200 p-6">
+ <h2 className="text-xl font-medium text-neutral-900">Program</h2>
+ <Button
+ type="button"
+ text="Embed widget"
+ onClick={() => setIsEmbedDocsOpen(true)}
+ icon={<Code className="size-4" />}
+ className="h-8 w-fit"
+ variant="secondary"
+ />
+ </div>
- <div className="divide-y divide-neutral-200 px-6">
- <FormProvider {...form}>
- <Summary program={program} />
- </FormProvider>
+ <div className="divide-y divide-neutral-200 px-6">
+ <FormProvider {...form}>
+ <Summary program={program} />
+ </FormProvider>
- <SettingsRow
- heading="Commission"
- description="See how the affiliate will get rewarded"
- >
- <div className="-m-1">
- <AnimatedSizeContainer
- height
- transition={{ ease: "easeInOut", duration: 0.2 }}
- >
- <div className="p-1">
- <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
- {commissionTypes.map((commissionType) => (
- <label
- key={commissionType.label}
- className={cn(
- "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600 hover:bg-neutral-50",
- "transition-all duration-150",
- recurringCommission === commissionType.recurring &&
- "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",
- )}
- >
- <input
- type="radio"
- value={commissionType.label}
- className="hidden"
- checked={
- recurringCommission === commissionType.recurring
- }
- onChange={(e) => {
- if (e.target.checked) {
- setValue(
- "recurringCommission",
- commissionType.recurring,
- { shouldDirty: true },
- );
+ <SettingsRow
+ heading="Commission"
+ description="See how the affiliate will get rewarded"
+ >
+ <div className="-m-1">
+ <AnimatedSizeContainer
+ height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
+ >
+ <div className="p-1">
+ <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
+ {commissionTypes.map((commissionType) => (
+ <label
+ key={commissionType.label}
+ className={cn(
+ "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600 hover:bg-neutral-50",
+ "transition-all duration-150",
+ recurringCommission === commissionType.recurring &&
+ "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",
+ )}
+ >
+ <input
+ type="radio"
+ value={commissionType.label}
+ className="hidden"
+ checked={
+ recurringCommission === commissionType.recurring
+ }
+ onChange={(e) => {
+ if (e.target.checked) {
+ setValue(
+ "recurringCommission",
+ commissionType.recurring,
+ { shouldDirty: true },
+ );
- // If not recurring, set lifetime recurring to false
- if (!commissionType.recurring)
+ // If not recurring, set lifetime recurring to false
+ if (!commissionType.recurring)
+ setValue("isLifetimeRecurring", false, {
+ shouldDirty: true,
+ });
+ }
+ }}
+ />
+ <div className="flex grow flex-col text-sm">
+ <span className="font-medium">
+ {commissionType.label}
+ </span>
+ <span>{commissionType.description}</span>
+ </div>
+ <CircleCheckFill
+ className={cn(
+ "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",
+ recurringCommission === commissionType.recurring &&
+ "scale-100 opacity-100",
+ )}
+ />
+ </label>
+ ))}
+ </div>
+ <div
+ className={cn(
+ "transition-opacity duration-200",
+ recurringCommission ? "h-auto" : "h-0 opacity-0",
+ )}
+ aria-hidden={!recurringCommission}
+ {...{ inert: !recurringCommission ? "" : undefined }}
+ >
+ <div className="pt-6">
+ <label
+ htmlFor="duration"
+ className="pt-6 text-sm font-medium text-neutral-800"
+ >
+ Duration
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <select
+ className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ onChange={(e) => {
+ const value = parseInt(e.target.value);
+
+ if (value === 0)
+ setValue("isLifetimeRecurring", true, {
+ shouldDirty: true,
+ });
+ else if (isLifetimeRecurring)
setValue("isLifetimeRecurring", false, {
shouldDirty: true,
});
- }
- }}
- />
- <div className="flex grow flex-col text-sm">
- <span className="font-medium">
- {commissionType.label}
- </span>
- <span>{commissionType.description}</span>
- </div>
- <CircleCheckFill
- className={cn(
- "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",
- recurringCommission === commissionType.recurring &&
- "scale-100 opacity-100",
- )}
- />
- </label>
- ))}
- </div>
- <div
- className={cn(
- "transition-opacity duration-200",
- recurringCommission ? "h-auto" : "h-0 opacity-0",
- )}
- aria-hidden={!recurringCommission}
- {...{ inert: !recurringCommission ? "" : undefined }}
- >
- <div className="pt-6">
- <label
- htmlFor="duration"
- className="pt-6 text-sm font-medium text-neutral-800"
- >
- Duration
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <select
- className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- onChange={(e) => {
- const value = parseInt(e.target.value);
- if (value === 0)
- setValue("isLifetimeRecurring", true, {
- shouldDirty: true,
- });
- else if (isLifetimeRecurring)
- setValue("isLifetimeRecurring", false, {
+ setValue("recurringDuration", value, {
shouldDirty: true,
});
-
- setValue("recurringDuration", value, {
- shouldDirty: true,
- });
- }}
- value={
- (isLifetimeRecurring ? 0 : recurringDuration) ?? 1
- }
- >
- {(program.recurringInterval === "year"
- ? [1, 2]
- : [1, 3, 6, 12, 18, 24]
- ).map((v) => (
- <option value={v} key={v}>
- {v}{" "}
- {pluralize(program.recurringInterval ?? "month", v)}
- </option>
- ))}
- <option value={0}>Lifetime</option>
- </select>
+ }}
+ value={
+ (isLifetimeRecurring ? 0 : recurringDuration) ?? 1
+ }
+ >
+ {(program.recurringInterval === "year"
+ ? [1, 2]
+ : [1, 3, 6, 12, 18, 24]
+ ).map((v) => (
+ <option value={v} key={v}>
+ {v}{" "}
+ {pluralize(
+ program.recurringInterval ?? "month",
+ v,
+ )}
+ </option>
+ ))}
+ <option value={0}>Lifetime</option>
+ </select>
+ </div>
</div>
</div>
</div>
- </div>
- </AnimatedSizeContainer>
- </div>
- </SettingsRow>
+ </AnimatedSizeContainer>
+ </div>
+ </SettingsRow>
- <SettingsRow
- heading="Payout"
- description="Set how much the affiliate will get rewarded"
- >
- <div className="flex flex-col gap-6">
- <div>
- <label
- htmlFor="duration"
- className="text-sm font-medium text-neutral-800"
- >
- Payout model
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <select
- className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register("commissionType", { required: true })}
+ <SettingsRow
+ heading="Payout"
+ description="Set how much the affiliate will get rewarded"
+ >
+ <div className="flex flex-col gap-6">
+ <div>
+ <label
+ htmlFor="duration"
+ className="text-sm font-medium text-neutral-800"
>
- <option value="flat">Flat</option>
- <option value="percentage">Percentage</option>
- </select>
+ Payout model
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <select
+ className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register("commissionType", { required: true })}
+ >
+ <option value="flat">Flat</option>
+ <option value="percentage">Percentage</option>
+ </select>
+ </div>
</div>
- </div>
- <div>
- <label
- htmlFor="duration"
- className="text-sm font-medium text-neutral-800"
- >
- Amount
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- {commissionType === "flat" && (
- <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
- $
- </span>
- )}
- <input
- className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.commissionAmount &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- commissionType === "flat" ? "pl-6 pr-12" : "pr-7",
+ <div>
+ <label
+ htmlFor="duration"
+ className="text-sm font-medium text-neutral-800"
+ >
+ Amount
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ {commissionType === "flat" && (
+ <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
+ $
+ </span>
)}
- {...register("commissionAmount", {
- required: true,
- valueAsNumber: true,
- min: 0,
- max: commissionType === "flat" ? 1000 : 100,
- })}
- />
- <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
- {commissionType === "flat" ? "USD" : "%"}
- </span>
+ <input
+ className={cn(
+ "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ errors.commissionAmount &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ commissionType === "flat" ? "pl-6 pr-12" : "pr-7",
+ )}
+ {...register("commissionAmount", {
+ required: true,
+ valueAsNumber: true,
+ min: 0,
+ max: commissionType === "flat" ? 1000 : 100,
+ })}
+ />
+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
+ {commissionType === "flat" ? "USD" : "%"}
+ </span>
+ </div>
</div>
</div>
- </div>
- </SettingsRow>
- </div>
+ </SettingsRow>
+ </div>
- <div className="flex items-center justify-end rounded-b-lg border-t border-neutral-200 bg-neutral-50 px-6 py-5">
- <div>
- <Button
- text="Save changes"
- className="h-8"
- loading={isSubmitting}
- disabled={!isValid || !isDirty}
- />
+ <div className="flex items-center justify-end rounded-b-lg border-t border-neutral-200 bg-neutral-50 px-6 py-5">
+ <div>
+ <Button
+ text="Save changes"
+ className="h-8"
+ loading={isSubmitting}
+ disabled={!isValid || !isDirty}
+ />
+ </div>
</div>
- </div>
- </form>
+ </form>
+ </>
);
}
diff --git a/apps/web/ui/partners/embed-docs-sheet.tsx b/apps/web/ui/partners/embed-docs-sheet.tsx
new file mode 100644
index 0000000000..356f2a3291
--- /dev/null
+++ b/apps/web/ui/partners/embed-docs-sheet.tsx
@@ -0,0 +1,139 @@
+import useWorkspace from "@/lib/swr/use-workspace";
+import { X } from "@/ui/shared/icons";
+import { Button, Sheet, useRouterStuff } from "@dub/ui";
+import { Dispatch, SetStateAction, useEffect, useState } from "react";
+import { codeToHtml } from "shiki";
+
+interface EmbedDocsSheetProps {
+ setIsOpen: Dispatch<SetStateAction<boolean>>;
+}
+
+function EmbedDocsSheetContent({ setIsOpen }: EmbedDocsSheetProps) {
+ const { slug } = useWorkspace();
+
+ return (
+ <>
+ <div>
+ <div className="flex items-start justify-between border-b border-neutral-200 p-6">
+ <Sheet.Title className="text-xl font-semibold">
+ Embed docs
+ </Sheet.Title>
+ <Sheet.Close asChild>
+ <Button
+ variant="outline"
+ icon={<X className="size-5" />}
+ className="h-auto w-fit p-1"
+ />
+ </Sheet.Close>
+ </div>
+ <div className="p-6">
+ <div>
+ <h2 className="text-lg font-medium">React</h2>
+ <p className="text-sm text-neutral-500">
+ Embed the widget in React app
+ </p>
+ <CodeSnippet code={reactSnippet} lang="javascript" />
+ </div>
+
+ <div className="mt-4">
+ <h2 className="text-lg font-medium">HTML</h2>
+ <p className="text-sm text-neutral-500">
+ Embed the widget in non-React app
+ </p>
+ <CodeSnippet code={htmlSnippet} lang="html" />
+ </div>
+ </div>
+ </div>
+ <div className="flex grow flex-col justify-end">
+ <div className="flex items-center justify-end gap-2 border-t border-neutral-200 p-5">
+ <Button
+ type="button"
+ variant="secondary"
+ onClick={() => setIsOpen(false)}
+ text="Close"
+ className="w-fit"
+ />
+ </div>
+ </div>
+ </>
+ );
+}
+
+const reactSnippet = `import { DubWidget } from "@dub/embed-react";
+
+const App = () => {
+ const [token, setToken] = useState("");
+
+ const createToken = async () => {
+ const res = await fetch("/api/create-token");
+ const data = await res.json();
+ setToken(data.token);
+ };
+
+ useEffect(() => {
+ createToken();
+ }, []);
+
+ return <DubWidget
+ token={token}
+ onTokenExpired={createToken}
+ />`;
+
+const htmlSnippet = `<script type="module" src="https://www.dubcdn.com/analytics/embed.js"></script>
+
+<script>
+ const createToken = async () => {
+ const res = await fetch("/api/create-token");
+ const data = await res.json();
+ return data.token;
+ };
+
+ document.addEventListener("DOMContentLoaded", () => {
+ Dub.init({
+ token: await createToken(),
+ });
+ });
+</script>
+`;
+
+function CodeSnippet({ code, lang }: { code: string; lang: string }) {
+ const [highlightedCode, setHighlightedCode] = useState("");
+
+ useEffect(() => {
+ const highlight = async () => {
+ const html = await codeToHtml(code, {
+ lang,
+ theme: "min-light",
+ });
+
+ setHighlightedCode(html);
+ };
+
+ highlight();
+ }, [code, lang]);
+
+ return (
+ <div
+ dangerouslySetInnerHTML={{ __html: highlightedCode }}
+ style={{ fontSize: "14px", marginTop: "10px" }}
+ />
+ );
+}
+
+export function EmbedDocsSheet({
+ isOpen,
+ ...rest
+}: EmbedDocsSheetProps & {
+ isOpen: boolean;
+}) {
+ const { queryParams } = useRouterStuff();
+ return (
+ <Sheet
+ open={isOpen}
+ onOpenChange={rest.setIsOpen}
+ onClose={() => queryParams({ del: "partnerId" })}
+ >
+ <EmbedDocsSheetContent {...rest} />
+ </Sheet>
+ );
+}
diff --git a/packages/embeds/core/package.json b/packages/embeds/core/package.json
index 5046c5198f..211558b674 100644
--- a/packages/embeds/core/package.json
+++ b/packages/embeds/core/package.json
@@ -13,6 +13,9 @@
"dev": "tsup --watch",
"check-types": "tsc --noEmit"
},
+ "dependencies": {
+ "@floating-ui/dom": "^1.6.12"
+ },
"devDependencies": {
"@types/js-cookie": "^3.0.6",
"tsconfig": "workspace:*",
@@ -35,8 +38,5 @@
],
"publishConfig": {
"access": "public"
- },
- "dependencies": {
- "@floating-ui/dom": "^1.6.12"
}
}
diff --git a/packages/embeds/core/src/core.ts b/packages/embeds/core/src/core.ts
index 5b94b0f2a1..b8ef8905c9 100644
--- a/packages/embeds/core/src/core.ts
+++ b/packages/embeds/core/src/core.ts
@@ -348,9 +348,11 @@ export const init = (options: DubOptions): DubInitResult => {
const { container, updatePosition } = renderWidget(options, { closeWidget });
- if (!container) return null;
+ if (!container) {
+ return null;
+ }
- if (options.trigger === "floating-button")
+ if (options.trigger === "floating-button") {
createFloatingButton({
container: container,
buttonStyles: options.buttonStyles,
@@ -359,6 +361,7 @@ export const init = (options: DubOptions): DubInitResult => {
toggleWidget();
},
});
+ }
return {
openWidget,
@@ -370,7 +373,9 @@ export const init = (options: DubOptions): DubInitResult => {
.querySelectorAll(`#${DUB_CONTAINER_ID}`)
.forEach((c) => c.remove());
- if (updatePosition) window.removeEventListener("resize", updatePosition);
+ if (updatePosition) {
+ window.removeEventListener("resize", updatePosition);
+ }
},
};
};
diff --git a/packages/embeds/react/src/example/widget.tsx b/packages/embeds/react/src/example/widget.tsx
index 6dcb5d2947..c4d0fc8f48 100644
--- a/packages/embeds/react/src/example/widget.tsx
+++ b/packages/embeds/react/src/example/widget.tsx
@@ -1,8 +1,21 @@
+import { useCallback, useEffect, useState } from "react";
import ReactDom from "react-dom";
import { DubWidget } from "../widget";
const Widget = () => {
- return <DubWidget token="dub_embed_" />;
+ const [token, setToken] = useState("");
+
+ const createToken = useCallback(async () => {
+ const res = await fetch("/api/create-token");
+ const data = await res.json();
+ setToken(data.token);
+ }, []);
+
+ useEffect(() => {
+ createToken();
+ }, []);
+
+ return <DubWidget token={token} onTokenExpired={createToken} />;
};
ReactDom.render(<Widget />, document.getElementById("root"));