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.

Flesh out user survey

Tim Wilson8092cdd233be
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index a967b2d8c5..168d22474e 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -28,11 +28,12 @@ const updateUserSchema = z.object({
name: z.preprocess(trim, z.string().min(1).max(64)).optional(),
email: z.preprocess(trim, z.string().email()).optional(),
image: z.string().url().optional(),
+ source: z.preprocess(trim, z.string().min(1).max(32)).optional(),
});
// PUT /api/user – edit a specific user
export const PUT = withSession(async ({ req, session }) => {
- let { name, email, image } = await updateUserSchema.parseAsync(
+ let { name, email, image, source } = await updateUserSchema.parseAsync(
await req.json(),
);
try {
@@ -48,6 +49,7 @@ export const PUT = withSession(async ({ req, session }) => {
...(name && { name }),
...(email && { email }),
...(image && { image }),
+ ...(source && { source }),
},
});
return NextResponse.json(response);
diff --git a/apps/web/app/app.dub.co/(dashboard)/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/layout.tsx
index 2641550ca6..e834a687ee 100644
--- a/apps/web/app/app.dub.co/(dashboard)/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/layout.tsx
@@ -1,10 +1,10 @@
import NavTabs from "@/ui/layout/nav-tabs";
import UpgradeBanner from "@/ui/layout/upgrade-banner";
import UserDropdown from "@/ui/layout/user-dropdown";
-import UserSurveyPopupContent from "@/ui/layout/user-survey";
+import UserSurveyPopup from "@/ui/layout/user-survey";
import WorkspaceSwitcher from "@/ui/layout/workspace-switcher";
import { Divider } from "@/ui/shared/icons";
-import { Logo, MaxWidthWrapper, Popup } from "@dub/ui";
+import { Logo, MaxWidthWrapper } from "@dub/ui";
import { constructMetadata } from "@dub/utils";
import Link from "next/link";
import { ReactNode, Suspense } from "react";
@@ -16,9 +16,6 @@ export const metadata = constructMetadata();
export default function Layout({ children }: { children: ReactNode }) {
return (
<Providers>
- <Popup hiddenCookieId="hideUserSurveyPopup">
- <UserSurveyPopupContent />
- </Popup>
<div className="min-h-screen w-full bg-gray-50">
<div className="sticky left-0 right-0 top-0 z-20 border-b border-gray-200 bg-white">
<MaxWidthWrapper>
@@ -56,6 +53,7 @@ export default function Layout({ children }: { children: ReactNode }) {
</div>
{children}
</div>
+ <UserSurveyPopup />
</Providers>
);
}
diff --git a/apps/web/package.json b/apps/web/package.json
index bd43e710fb..cdc0f7f6be 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -20,6 +20,7 @@
"@prisma/client": "^5.9.1",
"@react-email/components": "^0.0.14",
"@react-email/render": "^0.0.12",
+ "@react-hook/resize-observer": "^1.2.6",
"@sindresorhus/slugify": "^2.2.1",
"@splinetool/react-spline": "^2.2.6",
"@splinetool/runtime": "^1.0.87",
diff --git a/apps/web/ui/layout/user-survey.tsx b/apps/web/ui/layout/user-survey.tsx
deleted file mode 100644
index a129ce0750..0000000000
--- a/apps/web/ui/layout/user-survey.tsx
+++ /dev/null
@@ -1,125 +0,0 @@
-"use client";
-
-import {
- Github,
- Google,
- Label,
- LinkedIn,
- Logo,
- PopupContext,
- ProductHunt,
- RadioGroup,
- RadioGroupItem,
- Twitter,
-} from "@dub/ui";
-import { cn } from "@dub/utils";
-import { Globe, X } from "lucide-react";
-import { useContext, useState } from "react";
-
-const options = [
- {
- value: "twitter",
- label: "Twitter/X",
- icon: Twitter,
- },
- {
- value: "linkedin",
- label: "LinkedIn",
- icon: LinkedIn,
- },
- {
- value: "product-hunt",
- label: "Product Hunt",
- icon: ProductHunt,
- },
- {
- value: "google",
- label: "Google",
- icon: Google,
- },
- {
- value: "github",
- label: "GitHub",
- icon: Github,
- },
- {
- value: "other",
- label: "Other",
- icon: Globe,
- },
-];
-
-export default function UserSurveyPopupContent() {
- const { hidePopup } = useContext(PopupContext);
- const [source, setSource] = useState<string | undefined>(undefined);
-
- return (
- <div className="fixed bottom-4 z-40 mx-2 rounded-lg border border-gray-200 bg-white p-4 shadow-md sm:left-4 sm:mx-auto sm:max-w-sm">
- <button
- className="absolute right-2.5 top-2.5 rounded-full p-1 transition-colors hover:bg-gray-100 active:scale-90"
- onClick={hidePopup}
- >
- <X className="h-4 w-4 text-gray-500" />
- </button>
- <div className="flex flex-col space-y-4">
- <Logo className="h-8 w-8" />
- <p className="text-sm font-medium text-gray-800">
- Where did you hear about Dub?
- </p>
- <form
- onSubmit={(e) => {
- e.preventDefault();
-
- setTimeout(() => {
- hidePopup();
- }, 5000);
- }}
- >
- <RadioGroup
- name="source"
- required
- value={source}
- onValueChange={(value) => {
- setSource(value);
- }}
- className="grid grid-cols-2 gap-4"
- >
- {options.map((option) => (
- <div
- key={option.value}
- className={cn(
- "group flex flex-col rounded-md border border-gray-200 bg-white transition-all hover:border-gray-500 hover:ring-4 hover:ring-gray-200 active:scale-[0.98]",
- {
- "border-gray-500 ring-4 ring-gray-200":
- source === option.value,
- },
- )}
- >
- <RadioGroupItem
- value={option.value}
- id={option.value}
- className="hidden"
- />
- <Label
- htmlFor={option.value}
- className="flex cursor-pointer items-center space-x-4 px-4 py-2"
- >
- <option.icon
- className={cn(
- "h-5 w-5 grayscale transition-all group-hover:grayscale-0",
- {
- "h-4 w-4": option.value === "twitter",
- "text-gray-600": option.value === "other",
- },
- )}
- />
- <p className="text-gray-600">{option.label}</p>
- </Label>
- </div>
- ))}
- </RadioGroup>
- </form>
- </div>
- </div>
- );
-}
diff --git a/apps/web/ui/layout/user-survey/index.tsx b/apps/web/ui/layout/user-survey/index.tsx
new file mode 100644
index 0000000000..e60c44c95d
--- /dev/null
+++ b/apps/web/ui/layout/user-survey/index.tsx
@@ -0,0 +1,60 @@
+"use client";
+
+import { Popup, PopupContext } from "@dub/ui";
+import useResizeObserver from "@react-hook/resize-observer";
+import { motion } from "framer-motion";
+import { X } from "lucide-react";
+import { useSession } from "next-auth/react";
+import { useContext, useRef, useState } from "react";
+import SurveyForm from "./survey-form";
+
+export default function UserSurveyPopup() {
+ const { data: session } = useSession();
+
+ // @ts-ignore
+ return session?.user.source !== null ? null : (
+ <Popup hiddenCookieId="hideUserSurveyPopup">
+ <UserSurveyPopupInner />
+ </Popup>
+ );
+}
+
+export function UserSurveyPopupInner() {
+ const { hidePopup } = useContext(PopupContext);
+
+ const contentWrapperRef = useRef<HTMLDivElement>(null);
+ const [contentHeight, setContentHeight] = useState(0);
+
+ useResizeObserver(contentWrapperRef, (target) => {
+ setContentHeight(target.borderBoxSize[0].blockSize);
+ });
+
+ return (
+ <motion.div
+ animate={{ height: contentHeight }}
+ transition={{ ease: "easeInOut", duration: 0.1 }}
+ className="fixed bottom-4 z-[1000] mx-2 overflow-hidden rounded-lg border border-gray-200 bg-white shadow-md sm:left-4 sm:mx-auto sm:max-w-sm"
+ >
+ <div className="p-4" ref={contentWrapperRef}>
+ <button
+ className="absolute right-2.5 top-2.5 rounded-full p-1 transition-colors hover:bg-gray-100 active:scale-90"
+ onClick={hidePopup}
+ >
+ <X className="h-4 w-4 text-gray-500" />
+ </button>
+ <SurveyForm
+ onSubmit={(source) => {
+ fetch("/api/user", {
+ method: "PUT",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ source }),
+ });
+ hidePopup();
+ }}
+ />
+ </div>
+ </motion.div>
+ );
+}
diff --git a/apps/web/ui/layout/user-survey/survey-form.tsx b/apps/web/ui/layout/user-survey/survey-form.tsx
new file mode 100644
index 0000000000..d2c886d921
--- /dev/null
+++ b/apps/web/ui/layout/user-survey/survey-form.tsx
@@ -0,0 +1,160 @@
+import {
+ Button,
+ Github,
+ Google,
+ Label,
+ LinkedIn,
+ Logo,
+ ProductHunt,
+ RadioGroup,
+ RadioGroupItem,
+ Twitter,
+ useMediaQuery,
+} from "@dub/ui";
+import { cn } from "@dub/utils";
+import { ChevronRight, Globe } from "lucide-react";
+import { useState } from "react";
+
+const options = [
+ {
+ value: "twitter",
+ label: "Twitter/X",
+ icon: Twitter,
+ },
+ {
+ value: "linkedin",
+ label: "LinkedIn",
+ icon: LinkedIn,
+ },
+ {
+ value: "product-hunt",
+ label: "Product Hunt",
+ icon: ProductHunt,
+ },
+ {
+ value: "google",
+ label: "Google",
+ icon: Google,
+ },
+ {
+ value: "github",
+ label: "GitHub",
+ icon: Github,
+ },
+ {
+ value: "other",
+ label: "Other",
+ icon: Globe,
+ },
+];
+
+export default function SurveyForm({
+ onSubmit,
+}: {
+ onSubmit: (source: string) => void;
+}) {
+ const { isMobile } = useMediaQuery();
+
+ const [source, setSource] = useState<string | undefined>(undefined);
+ const [otherSource, setOtherSource] = useState<string | undefined>(undefined);
+
+ return (
+ <div className="flex flex-col space-y-4">
+ <Logo className="h-8 w-8" />
+ <p className="text-sm font-medium text-gray-800">
+ Where did you hear about Dub?
+ </p>
+ <form
+ onSubmit={(e) => {
+ e.preventDefault();
+ if (source)
+ onSubmit(source === "other" ? otherSource ?? source : source);
+ }}
+ >
+ <RadioGroup
+ name="source"
+ required
+ value={source}
+ onValueChange={(value) => {
+ setSource(value);
+ }}
+ className="grid grid-cols-2 gap-3"
+ >
+ {options.map((option) => (
+ <div
+ key={option.value}
+ className={cn(
+ "group flex flex-col rounded-md border border-gray-200 bg-white transition-all active:scale-[0.98]",
+ source === option.value
+ ? "border-white ring-2 ring-gray-600"
+ : "hover:border-gray-500 hover:ring hover:ring-gray-200 active:ring-2",
+ )}
+ >
+ <RadioGroupItem
+ value={option.value}
+ id={option.value}
+ className="hidden"
+ />
+ <Label
+ htmlFor={option.value}
+ className="flex h-full cursor-pointer select-none items-center space-x-4 px-4 py-2 text-gray-600"
+ >
+ <option.icon
+ className={cn(
+ "h-5 w-5 transition-all group-hover:grayscale-0",
+ {
+ grayscale: source !== option.value,
+ "h-4 w-4": option.value === "twitter",
+ "text-gray-600": option.value === "other",
+ },
+ )}
+ />
+ <p>{option.label}</p>
+ {option.value === "other" && (
+ <div className="flex grow justify-end">
+ <ChevronRight
+ className={cn(
+ "h-4 w-4 transition-transform",
+ source === option.value && "rotate-90",
+ )}
+ />
+ </div>
+ )}
+ </Label>
+ </div>
+ ))}
+ </RadioGroup>
+ {source === "other" && (
+ <div className="mt-3">
+ <label>
+ <div className="mt-2 flex rounded-md shadow-sm">
+ <input
+ type="text"
+ required
+ maxLength={32}
+ autoFocus={!isMobile}
+ autoComplete="off"
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ placeholder="Somewhere else"
+ value={otherSource}
+ onChange={(e) => setOtherSource(e.target.value)}
+ />
+ </div>
+ </label>
+ </div>
+ )}
+ {source !== undefined && (
+ <Button
+ className="mt-4"
+ variant="primary"
+ type="submit"
+ text="Submit"
+ disabled={
+ !source.length || (source === "other" && !otherSource?.length)
+ }
+ />
+ )}
+ </form>
+ </div>
+ );
+}
diff --git a/packages/ui/src/popup.tsx b/packages/ui/src/popup.tsx
index 9f7af99888..25b8c8189b 100644
--- a/packages/ui/src/popup.tsx
+++ b/packages/ui/src/popup.tsx
@@ -31,7 +31,7 @@ export function Popup({
className="fixed inset-0"
initial={{ opacity: 0, translateY: 50 }}
animate={{ opacity: 1, translateY: 0 }}
- exit={{ y: 250 }}
+ exit={{ opacity: 0, y: "100%" }}
>
{children}
</motion.div>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3beb32fb94..191ea672d1 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -61,6 +61,9 @@ importers:
'@react-email/render':
specifier: ^0.0.12
version: 0.0.12
+ '@react-hook/resize-observer':
+ specifier: ^1.2.6
+ version: 1.2.6([email protected])
'@sindresorhus/slugify':
specifier: ^2.2.1
version: 2.2.1
@@ -5700,6 +5703,33 @@ packages:
react: 18.2.0
dev: false
+ resolution: {integrity: sha512-dy6duzl+JnAZcDbNTfmaP3xHiKtbXYOaz3G51MGVljh548Y8MWzTr+PHLOfvpypEVW9zwvl+VyKjbWKEVbV1Rg==}
+ peerDependencies:
+ react: '>=16.8'
+ dependencies:
+ react: 18.2.0
+ dev: false
+
+ resolution: {integrity: sha512-IwEphTD75liO8g+6taS+4oqz+nnroocNfWVHWz7j+N+ZO2vYrc6PV1q7GQhuahL0IOR7JccFTsFKQ/mb6iZWAg==}
+ peerDependencies:
+ react: '>=16.8'
+ dependencies:
+ react: 18.2.0
+ dev: false
+
+ resolution: {integrity: sha512-DlBXtLSW0DqYYTW3Ft1/GQFZlTdKY5VAFIC4+km6IK5NiPPDFchGbEJm1j6pSgMqPRHbUQgHJX7RaR76ic1LWA==}
+ peerDependencies:
+ react: '>=16.8'
+ dependencies:
+ '@juggle/resize-observer': 3.4.0
+ '@react-hook/latest': 1.0.3([email protected])
+ '@react-hook/passive-layout-effect': 1.2.1([email protected])
+ react: 18.2.0
+ dev: false
+
/@react-native-community/[email protected]:
resolution: {integrity: sha512-twtsv54ohcRyWVzPXL3F9VHGb4Qhn3slqqRs3wEuRzjR7cTmV2TIO2b1VhaqF4HlCgNd+cGuirvLtK2JJyaxMg==}
dependencies: