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 "Install Stripe Integration" button to guides

Tim Wilson78ca665a602f
diff --git a/apps/web/lib/swr/use-integrations.ts b/apps/web/lib/swr/use-integrations.ts
index 938f494c23..90cf0b7576 100644
--- a/apps/web/lib/swr/use-integrations.ts
+++ b/apps/web/lib/swr/use-integrations.ts
@@ -1,9 +1,11 @@
import { InstalledIntegrationProps } from "@/lib/types";
import { fetcher } from "@dub/utils";
-import useSWR from "swr";
+import useSWR, { SWRConfiguration } from "swr";
import useWorkspace from "./use-workspace";
-export default function useIntegrations() {
+export default function useIntegrations({
+ swrOpts,
+}: { swrOpts?: SWRConfiguration } = {}) {
const { id } = useWorkspace();
const { data: integrations, error } = useSWR<
@@ -12,6 +14,7 @@ export default function useIntegrations() {
dedupingInterval: 20000,
revalidateOnFocus: false,
keepPreviousData: true,
+ ...swrOpts,
});
return {
diff --git a/apps/web/ui/guides/guide.tsx b/apps/web/ui/guides/guide.tsx
index 9464a870da..e673fcbe60 100644
--- a/apps/web/ui/guides/guide.tsx
+++ b/apps/web/ui/guides/guide.tsx
@@ -4,6 +4,7 @@ import { BookOpen, Button, buttonVariants, ChevronLeft } from "@dub/ui";
import { cn } from "@dub/utils";
import Link from "next/link";
import { useParams, usePathname } from "next/navigation";
+import { InstallStripeIntegrationButton } from "./install-stripe-integration-button";
import { guides, IntegrationType } from "./integrations";
import { Markdown } from "./markdown";
@@ -59,6 +60,10 @@ export function Guide({ markdown }: { markdown: string }) {
</div>
<div className="space-y-6 rounded-2xl bg-white p-0 shadow-none">
+ {selectedGuide.type === "track-sale" &&
+ selectedGuide.key.startsWith("stripe") && (
+ <InstallStripeIntegrationButton />
+ )}
<Markdown>{markdown}</Markdown>
<Link
diff --git a/apps/web/ui/guides/install-stripe-integration-button.tsx b/apps/web/ui/guides/install-stripe-integration-button.tsx
new file mode 100644
index 0000000000..2fdc154e15
--- /dev/null
+++ b/apps/web/ui/guides/install-stripe-integration-button.tsx
@@ -0,0 +1,75 @@
+import useIntegrations from "@/lib/swr/use-integrations";
+import useWorkspace from "@/lib/swr/use-workspace";
+import {
+ AnimatedSizeContainer,
+ CircleCheck,
+ LoadingCircle,
+ LoadingSpinner,
+} from "@dub/ui";
+import { cn } from "@dub/utils";
+import Link from "next/link";
+import { useEffect, useState } from "react";
+
+export function InstallStripeIntegrationButton() {
+ const { slug: workspaceSlug } = useWorkspace();
+
+ const [isClicked, setIsClicked] = useState(false);
+ const [isEnabled, setIsEnabled] = useState<boolean | null>(null);
+
+ const { integrations: activeIntegrations, loading } = useIntegrations({
+ swrOpts: {
+ revalidateOnFocus: !isEnabled,
+ // Keep refreshing if the integration isn't enabled yet, most frequently if the user has clicked the button
+ refreshInterval: !isEnabled ? (isClicked ? 1000 : 5000) : undefined,
+ },
+ });
+
+ useEffect(() => {
+ if (activeIntegrations)
+ setIsEnabled(activeIntegrations.some((ai) => ai.slug === "stripe"));
+ }, [activeIntegrations]);
+
+ return (
+ <AnimatedSizeContainer
+ height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
+ >
+ <div
+ className={cn(
+ "bg-bg-subtle rounded-xl p-2 transition-colors",
+ isEnabled && "bg-green-50",
+ )}
+ >
+ {loading ? (
+ <div className="flex h-4 items-center justify-center">
+ <LoadingSpinner className="size-3.5" />
+ </div>
+ ) : isEnabled ? (
+ <div className="flex items-center justify-center gap-2 text-xs font-medium text-green-800">
+ <CircleCheck className="size-3.5" />
+ <span>Stripe integration installed</span>
+ </div>
+ ) : (
+ <>
+ <p className="text-content-default text-center text-xs font-medium">
+ Required first step
+ </p>
+ <Link
+ href={`/${workspaceSlug}/settings/integrations/stripe`}
+ target="_blank"
+ onMouseDown={(e) => {
+ if (e.button === 0 || e.button === 1) setIsClicked(true);
+ }}
+ className="text-content-inverted mt-2 flex h-10 w-full items-center justify-center gap-2 rounded-lg bg-indigo-600 px-4 text-sm font-medium transition-colors hover:bg-indigo-700"
+ >
+ {isClicked && (
+ <LoadingCircle className="size-4 opacity-60 mix-blend-screen" />
+ )}
+ Install Stripe integration
+ </Link>
+ </>
+ )}
+ </div>
+ </AnimatedSizeContainer>
+ );
+}