-
-
Notifications
You must be signed in to change notification settings - Fork 656
Expand file tree
/
Copy pathdata-table.tsx
More file actions
237 lines (222 loc) · 8.82 KB
/
data-table.tsx
File metadata and controls
237 lines (222 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"use client";
import { Check } from "lucide-react";
import { Fragment, useTransition } from "react";
import { Button } from "@openstatus/ui/components/ui/button";
import {
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@openstatus/ui/components/ui/table";
import { config as featureGroups, plans } from "@/data/plans";
import { getDashboardPublicUrl } from "@/lib/public-url";
import { getStripe } from "@/lib/stripe";
import { useTRPC } from "@/lib/trpc/client";
import { cn } from "@/lib/utils";
import type { WorkspacePlan } from "@openstatus/db/src/schema";
import {
getAddonPriceConfig,
getPriceConfig,
} from "@openstatus/db/src/schema/plan/utils";
import { Badge } from "@openstatus/ui/components/ui/badge";
import { useCookieState } from "@openstatus/ui/hooks/use-cookie-state";
import { useMutation, useQuery } from "@tanstack/react-query";
const BASE_URL = getDashboardPublicUrl();
export function DataTable({ restrictTo }: { restrictTo?: WorkspacePlan[] }) {
const [currency] = useCookieState("x-currency", "USD");
const trpc = useTRPC();
const [isPending, startTransition] = useTransition();
const { data: workspace } = useQuery(trpc.workspace.get.queryOptions());
const checkoutSessionMutation = useMutation(
trpc.stripeRouter.getCheckoutSession.mutationOptions({
onSuccess: async (data) => {
if (!data) return;
const stripe = await getStripe();
stripe?.redirectToCheckout({ sessionId: data.id });
},
}),
);
if (!workspace) return null;
const filteredPlans = Object.values(plans).filter((plan) =>
restrictTo ? restrictTo.includes(plan.id) : true,
);
return (
<Table className="relative table-fixed">
<TableCaption>
A list to compare the different features by plan.
</TableCaption>
<TableHeader>
<TableRow className="hover:bg-transparent">
<TableHead className="p-2 align-bottom">
Features comparison
</TableHead>
{filteredPlans.map(({ id, ...plan }) => {
const isCurrentPlan = workspace.plan === id;
const price = getPriceConfig(id, currency);
return (
<TableHead
key={id}
className={cn(
"h-auto p-2 align-bottom text-foreground",
id === "starter" ? "bg-muted/30" : "",
)}
>
<div className="flex h-full flex-col justify-between gap-1">
<div className="flex flex-1 flex-col gap-1">
<p className="font-cal text-lg">{plan.title}</p>
<p className="text-wrap font-normal text-muted-foreground text-xs">
{plan.description}
</p>
</div>
<p className="text-right">
<span className="font-mono text-lg">
{new Intl.NumberFormat(price.locale, {
style: "currency",
currency: price.currency,
}).format(price.value)}
</span>
<span className="text-muted-foreground text-sm">
/month
</span>
</p>
<Button
size="sm"
type="button"
variant={id === "starter" ? "default" : "outline"}
onClick={() => {
startTransition(async () => {
await checkoutSessionMutation.mutateAsync({
plan: id,
// TODO: move to the server as we have the current workspace
workspaceSlug: workspace.slug,
successUrl: `${BASE_URL}/settings/billing?success=true`,
cancelUrl: `${BASE_URL}/settings/billing`,
});
});
}}
disabled={isPending || isCurrentPlan}
>
{isCurrentPlan
? "Current Plan"
: isPending
? "Choosing..."
: "Choose"}
</Button>
</div>
</TableHead>
);
})}
</TableRow>
</TableHeader>
<TableBody>
{Object.entries(featureGroups).map(
([groupKey, { label, features }]) => (
<Fragment key={groupKey}>
<TableRow className="bg-muted/50">
<TableCell
colSpan={filteredPlans.length + 1}
className="font-medium"
>
{label}
</TableCell>
</TableRow>
{features.map(
({ value, label: featureLabel, monthly, badge }) => (
<TableRow key={groupKey + value}>
<TableCell>
<div className="flex items-center gap-2 text-wrap">
{featureLabel}{" "}
{badge ? (
<Badge variant="outline">{badge}</Badge>
) : null}
</div>
</TableCell>
{filteredPlans.map((plan) => {
const limitValue =
plan.limits[value as keyof typeof plan.limits];
const isAddon = value in plan.addons;
function renderContent() {
if (isAddon) {
const price = getAddonPriceConfig(
plan.id,
value as keyof typeof plan.addons,
currency,
);
if (!price) return null;
const isNumber = typeof limitValue === "number";
return (
<div>
<span>
{isNumber
? new Intl.NumberFormat("us")
.format(limitValue)
.toString()
: null}
</span>
<span>
<span className="text-muted-foreground">
{isNumber ? " + " : ""}
</span>
<span>
{new Intl.NumberFormat(price.locale, {
style: "currency",
currency: price.currency,
}).format(price.value)}
{isNumber ? "/mo./each" : "/mo."}
</span>
</span>
</div>
);
}
if (typeof limitValue === "boolean") {
return limitValue ? (
<Check className="h-4 w-4 text-foreground" />
) : (
<span className="text-muted-foreground/50">
‐
</span>
);
}
if (typeof limitValue === "number") {
return new Intl.NumberFormat("us")
.format(limitValue)
.toString();
}
// TODO: create a format function for this in @data/plans
if (value === "regions" && Array.isArray(limitValue)) {
return limitValue?.length ?? 0;
}
if (
Array.isArray(limitValue) &&
limitValue.length > 0
) {
return limitValue[0];
}
return limitValue;
}
return (
<TableCell
key={plan.id + value}
className={cn(
"font-mono",
plan.id === "starter" && "bg-muted/30",
)}
>
{renderContent()}
{monthly ? "/mo." : ""}
</TableCell>
);
})}
</TableRow>
),
)}
</Fragment>
),
)}
</TableBody>
</Table>
);
}