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 workspace store and persist conversions onboarding state

Tim Wilson4df59b37e027
diff --git a/apps/web/lib/actions/update-workspace-store.ts b/apps/web/lib/actions/update-workspace-store.ts
new file mode 100644
index 0000000000..6cb904be46
--- /dev/null
+++ b/apps/web/lib/actions/update-workspace-store.ts
@@ -0,0 +1,36 @@
+"use server";
+
+import { prisma } from "@dub/prisma";
+import z from "../zod";
+import { authActionClient } from "./safe-action";
+
+const schema = z.object({
+ workspaceId: z.string(),
+ key: z.string(),
+ value: z.any(),
+});
+
+// Update a workspace store item
+export const updateWorkspaceStore = authActionClient
+ .schema(schema)
+ .action(async ({ ctx, parsedInput }) => {
+ const { workspace } = ctx;
+ const { key, value } = parsedInput;
+
+ const store =
+ (workspace.store as Record<string, any> | undefined | null) ?? {};
+
+ await prisma.project.update({
+ where: {
+ id: workspace.id,
+ },
+ data: {
+ store: {
+ ...store,
+ [key]: value,
+ },
+ },
+ });
+
+ return { ok: true };
+ });
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 9ce0507abc..ddc7b88879 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -132,6 +132,7 @@ export interface WorkspaceProps extends Project {
flags?: {
[key in BetaFeatures]: boolean;
};
+ store: Record<string, any> | null;
}
export type ExpandedWorkspaceProps = WorkspaceProps & {
diff --git a/apps/web/lib/workspace/use-workspace-store.ts b/apps/web/lib/workspace/use-workspace-store.ts
new file mode 100644
index 0000000000..6be51814ff
--- /dev/null
+++ b/apps/web/lib/workspace/use-workspace-store.ts
@@ -0,0 +1,32 @@
+"use client";
+
+import { useAction } from "next-safe-action/hooks";
+import { useEffect, useState } from "react";
+import { mutate } from "swr";
+import { updateWorkspaceStore } from "../actions/update-workspace-store";
+import useWorkspace from "../swr/use-workspace";
+
+export const STORE_KEYS = {
+ conversionsOnboarding: "conversionsOnboarding",
+};
+
+export function useWorkspaceStore<T>(
+ key: string,
+): [T | undefined, (value: T) => Promise<void>, { loading: boolean }] {
+ const { id: workspaceId, slug, store, loading } = useWorkspace();
+
+ const { executeAsync } = useAction(updateWorkspaceStore);
+ const [item, setItemState] = useState<T | undefined>(store?.[key]);
+
+ useEffect(() => {
+ setItemState(store?.[key]);
+ }, [store]);
+
+ const setItem = async (value: T) => {
+ setItemState(value);
+ await executeAsync({ key, value, workspaceId: workspaceId! });
+ mutate(`/api/workspaces/${slug}`);
+ };
+
+ return [item, setItem, { loading }];
+}
diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.ts
index 0bbde40333..b025496574 100644
--- a/apps/web/lib/zod/schemas/workspaces.ts
+++ b/apps/web/lib/zod/schemas/workspaces.ts
@@ -108,6 +108,10 @@ export const WorkspaceSchema = z
.describe(
"The feature flags of the workspace, indicating which features are enabled.",
),
+ store: z
+ .record(z.any())
+ .nullable()
+ .describe("The miscellaneous key-value store of the workspace."),
})
.openapi({
title: "Workspace",
diff --git a/apps/web/ui/layout/sidebar/usage.tsx b/apps/web/ui/layout/sidebar/usage.tsx
index e8bdff01a5..099cf00e52 100644
--- a/apps/web/ui/layout/sidebar/usage.tsx
+++ b/apps/web/ui/layout/sidebar/usage.tsx
@@ -1,6 +1,10 @@
"use client";
import useWorkspace from "@/lib/swr/use-workspace";
+import {
+ STORE_KEYS,
+ useWorkspaceStore,
+} from "@/lib/workspace/use-workspace-store";
import { X } from "@/ui/shared/icons";
import ManageSubscriptionButton from "@/ui/workspaces/manage-subscription-button";
import {
@@ -93,8 +97,17 @@ function UsageInner() {
const warning = warnings.some((w) => w);
const [salesRef, setSalesRef] = useState<HTMLDivElement | null>(null);
+ const [
+ conversionsOnboarding,
+ setConversionsOnboarding,
+ { loading: loadingConversionsOnboarding },
+ ] = useWorkspaceStore<string>(STORE_KEYS.conversionsOnboarding);
+
const showConversionOnboarding =
- salesUsage === 0 && salesLimit && salesLimit > 0;
+ salesUsage === 0 &&
+ salesLimit &&
+ salesLimit > 0 &&
+ !loadingConversionsOnboarding;
return loading || usage !== undefined ? (
<>
@@ -191,8 +204,11 @@ function UsageInner() {
) : null}
</div>
</AnimatedSizeContainer>
- {showConversionOnboarding && !isMobile && (
- <ConversionOnboardingPopup reference={salesRef} />
+ {showConversionOnboarding && !isMobile && !conversionsOnboarding && (
+ <ConversionOnboardingPopup
+ reference={salesRef}
+ onDismiss={() => setConversionsOnboarding("modal-dismissed")}
+ />
)}
</>
) : null;
@@ -338,8 +354,10 @@ const formatNumber = (value: number) =>
function ConversionOnboardingPopup({
reference,
+ onDismiss,
}: {
reference: HTMLDivElement | null;
+ onDismiss: () => void;
}) {
const arrowRef = useRef<SVGSVGElement>(null);
@@ -370,8 +388,6 @@ function ConversionOnboardingPopup({
],
});
- const dismiss = () => {};
-
return (
<FloatingPortal>
<div
@@ -399,7 +415,7 @@ function ConversionOnboardingPopup({
</Link>
<button
type="button"
- onClick={dismiss}
+ onClick={onDismiss}
className="absolute right-2 top-2 rounded-md border border-neutral-200 bg-white p-1.5 shadow-sm transition-colors duration-75 hover:bg-neutral-50"
>
<X className="size-4 text-neutral-500" />
@@ -418,7 +434,7 @@ function ConversionOnboardingPopup({
variant="secondary"
className="h-7 text-xs"
text="Maybe later"
- onClick={dismiss}
+ onClick={onDismiss}
/>
<Link
href={`https://dub.co/help/article/dub-conversions`}
diff --git a/packages/prisma/schema/workspace.prisma b/packages/prisma/schema/workspace.prisma
index fcfa818c39..42a99275a6 100644
--- a/packages/prisma/schema/workspace.prisma
+++ b/packages/prisma/schema/workspace.prisma
@@ -29,7 +29,7 @@ model Project {
referralLinkId String? @unique
referredSignups Int @default(0)
- store Json @default("{}") // General key-value store for things like persisting toggles, dismissing popups, etc.
+ store Json? // General key-value store for things like persisting toggles, dismissing popups, etc.
webhookEnabled Boolean @default(false)
conversionEnabled Boolean @default(false) // TODO: remove this once Conversions goes GA