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.

WIP plan restrictions

Tim Wilsonf109d21de84f
diff --git a/apps/web/lib/plan-capabilities.ts b/apps/web/lib/plan-capabilities.ts
index 4f6a8ce7cb..2a96c86230 100644
--- a/apps/web/lib/plan-capabilities.ts
+++ b/apps/web/lib/plan-capabilities.ts
@@ -5,8 +5,8 @@ export const getPlanCapabilities = (
plan: WorkspaceProps["plan"] | undefined | string,
) => {
return {
- canAddFolder: plan && !["free"].includes(plan),
- canManageFolderPermissions: plan && !["free", "pro"].includes(plan), // default access level is write
- canManageCustomers: plan && !["free", "pro"].includes(plan),
+ canAddFolder: !!plan && !["free"].includes(plan),
+ canManageFolderPermissions: !!plan && !["free", "pro"].includes(plan), // default access level is write
+ canManageCustomers: !!plan && !["free", "pro"].includes(plan),
};
};
diff --git a/apps/web/lib/swr/use-customers-count.ts b/apps/web/lib/swr/use-customers-count.ts
index efcceec787..d32b3c969f 100644
--- a/apps/web/lib/swr/use-customers-count.ts
+++ b/apps/web/lib/swr/use-customers-count.ts
@@ -6,13 +6,16 @@ import useWorkspace from "./use-workspace";
export default function useCustomersCount<T = number>({
query,
+ enabled = true,
}: {
query?: z.infer<typeof getCustomersCountQuerySchema>;
+ enabled?: boolean;
} = {}) {
const { id: workspaceId } = useWorkspace();
const { data, error } = useSWR<T>(
- workspaceId &&
+ enabled &&
+ workspaceId &&
`/api/customers/count?${new URLSearchParams({ workspaceId, ...query })}`,
fetcher,
);
diff --git a/apps/web/ui/customers/customer-table/customer-table.tsx b/apps/web/ui/customers/customer-table/customer-table.tsx
index 855f78160f..77237edc42 100644
--- a/apps/web/ui/customers/customer-table/customer-table.tsx
+++ b/apps/web/ui/customers/customer-table/customer-table.tsx
@@ -1,5 +1,6 @@
"use client";
+import { getPlanCapabilities } from "@/lib/plan-capabilities";
import useCustomersCount from "@/lib/swr/use-customers-count";
import useWorkspace from "@/lib/swr/use-workspace";
import { CustomerProps } from "@/lib/types";
@@ -37,13 +38,16 @@ import { useParams, useRouter } from "next/navigation";
import { useMemo, useState } from "react";
import { toast } from "sonner";
import useSWR from "swr";
+import { EXAMPLE_CUSTOMER_DATA } from "./example-data";
import { customersColumns, useColumnVisibility } from "./use-column-visibility";
import { useCustomerFilters } from "./use-customer-filters";
export function CustomerTable() {
- const { id: workspaceId, slug: workspaceSlug } = useWorkspace();
- const { queryParams, searchParams, getQueryString } = useRouterStuff();
+ const { id: workspaceId, slug: workspaceSlug, plan } = useWorkspace();
+
+ const { canManageCustomers } = getPlanCapabilities(plan);
+ const { queryParams, searchParams, getQueryString } = useRouterStuff();
const router = useRouter();
const sortBy = searchParams.get("sortBy") || "createdAt";
@@ -56,19 +60,25 @@ export function CustomerTable() {
onRemove,
onRemoveAll,
isFiltered,
- } = useCustomerFilters({ sortBy, sortOrder });
+ } = useCustomerFilters(
+ { sortBy, sortOrder },
+ { enabled: canManageCustomers },
+ );
- const { data: customersCount, error: countError } = useCustomersCount();
+ const { data: customersCount, error: countError } = useCustomersCount({
+ enabled: canManageCustomers,
+ });
const {
data: customers,
error,
isLoading,
} = useSWR<CustomerProps[]>(
- `/api/customers${getQueryString({
- workspaceId,
- includeExpandedFields: "true",
- })}`,
+ canManageCustomers &&
+ `/api/customers${getQueryString({
+ workspaceId,
+ includeExpandedFields: "true",
+ })}`,
fetcher,
{
keepPreviousData: true,
@@ -197,7 +207,7 @@ export function CustomerTable() {
);
const { table, ...tableProps } = useTable({
- data: customers || [],
+ data: canManageCustomers ? customers || [] : EXAMPLE_CUSTOMER_DATA,
columns,
rowProps: (row) => {
const href = `/${workspaceSlug}/customers/${row.original.id}`;
@@ -272,8 +282,23 @@ export function CustomerTable() {
</div>
</AnimatedSizeContainer>
</div>
- {customers?.length !== 0 ? (
- <Table {...tableProps} table={table} />
+ {!canManageCustomers || customers?.length !== 0 ? (
+ <Table
+ {...tableProps}
+ table={table}
+ scrollWrapperClassName={
+ canManageCustomers ? undefined : "overflow-x-hidden"
+ }
+ >
+ {!canManageCustomers && (
+ <>
+ <div className="absolute inset-0 flex touch-pan-y items-center justify-center bg-gradient-to-t from-[#fff_70%] to-[#fff6]">
+ upgrade!
+ </div>
+ <div className="h-[400px]" />
+ </>
+ )}
+ </Table>
) : (
<AnimatedEmptyState
title="No customers found"
diff --git a/apps/web/ui/customers/customer-table/example-data.ts b/apps/web/ui/customers/customer-table/example-data.ts
new file mode 100644
index 0000000000..13c0133cbd
--- /dev/null
+++ b/apps/web/ui/customers/customer-table/example-data.ts
@@ -0,0 +1,43 @@
+const millisecondsPerDay = 24 * 60 * 60 * 1000;
+
+export const EXAMPLE_CUSTOMER_DATA = [
+ {
+ id: "cus_example_1",
+ externalId: "djk29fjj3I1",
+ name: "Liam White",
+ email: "[email protected]",
+ avatar: "https://avatar.vercel.sh/l.png?text=L",
+ country: "US",
+ sales: 1,
+ saleAmount: 20_00,
+ createdAt: new Date(new Date().getTime() - 14 * millisecondsPerDay),
+ link: null,
+ discount: null,
+ },
+ {
+ id: "cus_example_2",
+ externalId: "c34kd94jcz2",
+ name: "Emma Green",
+ email: "[email protected]",
+ avatar: "https://avatar.vercel.sh/e.png?text=E",
+ country: "US",
+ sales: 30,
+ saleAmount: 1200_00,
+ createdAt: new Date(new Date().getTime() - 20 * millisecondsPerDay),
+ link: null,
+ discount: null,
+ },
+ {
+ id: "cus_1JPT0T7E9C840AG88AQHXDGN4",
+ externalId: "nm58fnz9071",
+ name: "Daniel Brown",
+ email: "[email protected]",
+ avatar: "https://avatar.vercel.sh/d.png?text=D",
+ country: "US",
+ sales: 3,
+ saleAmount: 90_00,
+ createdAt: new Date(new Date().getTime() - 30 * millisecondsPerDay),
+ link: null,
+ discount: null,
+ },
+];
diff --git a/apps/web/ui/customers/customer-table/use-customer-filters.tsx b/apps/web/ui/customers/customer-table/use-customer-filters.tsx
index 92b0156a1e..faa320c0a8 100644
--- a/apps/web/ui/customers/customer-table/use-customer-filters.tsx
+++ b/apps/web/ui/customers/customer-table/use-customer-filters.tsx
@@ -5,7 +5,10 @@ import { FlagWavy } from "@dub/ui/icons";
import { COUNTRIES, nFormatter } from "@dub/utils";
import { useCallback, useMemo } from "react";
-export function useCustomerFilters(extraSearchParams: Record<string, string>) {
+export function useCustomerFilters(
+ extraSearchParams: Record<string, string>,
+ { enabled = true }: { enabled?: boolean } = {},
+) {
const { searchParamsObj, queryParams } = useRouterStuff();
const { id: workspaceId } = useWorkspace();
@@ -19,6 +22,7 @@ export function useCustomerFilters(extraSearchParams: Record<string, string>) {
query: {
groupBy: "country",
},
+ enabled,
});
const filters = useMemo(