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.

Move user survey into popover button

Tim Wilson6dad912fde1b
diff --git a/apps/web/app/app.dub.co/(dashboard)/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/layout.tsx
index 30d387937a..b5361a442d 100644
--- a/apps/web/app/app.dub.co/(dashboard)/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/layout.tsx
@@ -1,7 +1,6 @@
import { MainNav } from "@/ui/layout/main-nav";
import { HelpButtonRSC } from "@/ui/layout/sidebar/help-button-rsc";
import Toolbar from "@/ui/layout/toolbar/toolbar";
-import UserSurveyPopup from "@/ui/layout/user-survey";
import { constructMetadata } from "@dub/utils";
import { ReactNode } from "react";
import Providers from "../../providers";
@@ -15,7 +14,6 @@ export default async function Layout({ children }: { children: ReactNode }) {
<div className="min-h-screen w-full bg-white">
<MainNav toolContent={<HelpButtonRSC />}>{children}</MainNav>
</div>
- <UserSurveyPopup />
{/* <ChangelogPopup /> */}
<Toolbar show={["onboarding"]} />
</Providers>
diff --git a/apps/web/ui/layout/sidebar/sidebar-nav.tsx b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
index 968672b313..0de82734e0 100644
--- a/apps/web/ui/layout/sidebar/sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
@@ -12,6 +12,7 @@ import {
useMemo,
useState,
} from "react";
+import UserSurveyButton from "../user-survey";
import { ITEMS, type NavItem as NavItemType } from "./items";
import { Usage } from "./usage";
import UserDropdown from "./user-dropdown";
@@ -97,6 +98,7 @@ export function SidebarNav({ toolContent }: { toolContent?: ReactNode }) {
</div>
</nav>
<div className="relative mt-6 flex grow flex-col justify-end">
+ <UserSurveyButton />
<Usage />
</div>
</>
diff --git a/apps/web/ui/layout/user-survey/index.tsx b/apps/web/ui/layout/user-survey/index.tsx
index a947cb99dc..13cf8bb5fa 100644
--- a/apps/web/ui/layout/user-survey/index.tsx
+++ b/apps/web/ui/layout/user-survey/index.tsx
@@ -1,11 +1,13 @@
"use client";
import { CheckCircleFill } from "@/ui/shared/icons";
-import { Popup, PopupContext, useResizeObserver } from "@dub/ui";
+import { AnimatedSizeContainer, ClientOnly, Popover } from "@dub/ui";
+import { cn } from "@dub/utils";
import { AnimatePresence, motion } from "framer-motion";
-import { X } from "lucide-react";
+import Cookies from "js-cookie";
+import { CircleHelp, X } from "lucide-react";
import { useSession } from "next-auth/react";
-import { createContext, useContext, useRef, useState } from "react";
+import { createContext, useCallback, useState } from "react";
import { toast } from "sonner";
import SurveyForm from "./survey-form";
@@ -15,89 +17,104 @@ export const UserSurveyContext = createContext<{ status: UserSurveyStatus }>({
status: "idle",
});
-export default function UserSurveyPopup() {
+const HIDDEN_COOKIE_ID = "hideUserSurveyPopup";
+
+export default function UserSurveyButton() {
const { data: session } = useSession();
+ const [hidden, setHidden] = useState(Cookies.get(HIDDEN_COOKIE_ID) === "1");
+ const [openPopover, setOpenPopover] = useState(false);
+
+ const hide = useCallback(() => {
+ setOpenPopover(false);
+ setTimeout(() => {
+ setHidden(true);
+ Cookies.set(HIDDEN_COOKIE_ID, "1");
+ }, 500);
+ }, []);
return (
session?.user &&
!session.user["source"] && (
- <Popup hiddenCookieId="hideUserSurveyPopup">
- <UserSurveyPopupInner />
- </Popup>
+ <ClientOnly>
+ <AnimatePresence initial={false}>
+ {!hidden && (
+ <motion.div exit={{ opacity: 0 }} className="p-2">
+ <Popover
+ content={<UserSurveyPopupInner hide={hide} />}
+ popoverContentClassName="mx-2"
+ openPopover={!hidden && openPopover}
+ setOpenPopover={setOpenPopover}
+ >
+ <button
+ className={cn(
+ "rounded-md p-1 text-left text-xs text-neutral-500 transition-colors duration-75",
+ "hover:text-neutral-600 data-[state=open]:text-neutral-600",
+ "outline-none focus-visible:ring-2 focus-visible:ring-black/50",
+ )}
+ >
+ <CircleHelp className="mr-1 mt-0.5 size-3" />
+ Where did you hear about Dub?
+ </button>
+ </Popover>
+ </motion.div>
+ )}
+ </AnimatePresence>
+ </ClientOnly>
)
);
}
-export function UserSurveyPopupInner() {
- const { hidePopup } = useContext(PopupContext);
+export function UserSurveyPopupInner({ hide }: { hide: () => void }) {
const { update } = useSession();
- const contentWrapperRef = useRef<HTMLDivElement>(null);
- const resizeObserverEntry = useResizeObserver(contentWrapperRef);
-
const [status, setStatus] = useState<UserSurveyStatus>("idle");
return (
- <motion.div
- initial={{ opacity: 0, translateY: 50 }}
- animate={{
- opacity: 1,
- translateY: 0,
- }}
- exit={{ opacity: 0, y: "100%" }}
- className="fixed bottom-4 z-40 mx-2 overflow-hidden rounded-lg border border-gray-200 bg-white shadow-md sm:left-4 sm:mx-auto sm:max-w-sm"
- >
- <motion.div
- animate={{
- height: resizeObserverEntry?.borderBoxSize[0].blockSize ?? "auto",
- }}
- transition={{ type: "spring", duration: 0.3 }}
- >
- <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>
- <UserSurveyContext.Provider value={{ status }}>
- <SurveyForm
- onSubmit={async (source) => {
- setStatus("loading");
- try {
- await fetch("/api/user", {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ source }),
- });
- setStatus("success");
- setTimeout(() => {
- update();
- hidePopup();
- }, 3000);
- } catch (e) {
- toast.error("Error saving response. Please try again.");
- setStatus("idle");
- }
- }}
- />
- <AnimatePresence>
- {status === "success" && (
- <motion.div
- initial={{ opacity: 0, y: 10 }}
- animate={{ opacity: 1, y: 0 }}
- className="absolute inset-0 flex flex-col items-center justify-center space-y-3 bg-white text-sm"
- >
- <CheckCircleFill className="h-8 w-8 text-green-500" />
- <p className="text-gray-500">Thank you for your response!</p>
- </motion.div>
- )}
- </AnimatePresence>
- </UserSurveyContext.Provider>
- </div>
- </motion.div>
- </motion.div>
+ <AnimatedSizeContainer height>
+ <div className="p-4">
+ <button
+ className="absolute right-2.5 top-2.5 rounded-full p-1 transition-colors hover:bg-gray-100 active:scale-90"
+ onClick={hide}
+ >
+ <X className="h-4 w-4 text-gray-500" />
+ </button>
+ <UserSurveyContext.Provider value={{ status }}>
+ <SurveyForm
+ onSubmit={async (source) => {
+ setStatus("loading");
+ try {
+ await fetch("/api/user", {
+ method: "PUT",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ source }),
+ });
+ setStatus("success");
+ setTimeout(() => {
+ update();
+ hide();
+ }, 3000);
+ } catch (e) {
+ toast.error("Error saving response. Please try again.");
+ setStatus("idle");
+ }
+ }}
+ />
+ <AnimatePresence>
+ {status === "success" && (
+ <motion.div
+ initial={{ opacity: 0, y: 10 }}
+ animate={{ opacity: 1, y: 0 }}
+ className="absolute inset-0 flex flex-col items-center justify-center space-y-3 bg-white text-sm"
+ >
+ <CheckCircleFill className="h-8 w-8 text-green-500" />
+ <p className="text-gray-500">Thank you for your response!</p>
+ </motion.div>
+ )}
+ </AnimatePresence>
+ </UserSurveyContext.Provider>
+ </div>
+ </AnimatedSizeContainer>
);
}