-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.tsx
246 lines (226 loc) · 7.5 KB
/
page.tsx
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
238
239
240
241
242
243
244
245
246
import { cache, Suspense } from "react";
import { redirect } from "next/navigation";
import { HamburgerMenuIcon } from "@radix-ui/react-icons";
import { format, parseISO, startOfMonth } from "date-fns";
import { add, dinero, multiply, toDecimal } from "dinero.js";
import type { Client, Timeslot } from "~/db/getters";
import { getClients, getTimeslots } from "~/db/getters";
import { currentUser } from "~/lib/auth";
import { withUnstableCache } from "~/lib/cache";
import type { CurrencyCode } from "~/lib/currencies";
import { createConverter, currencies, formatMoney } from "~/lib/currencies";
import { Button } from "~/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "~/ui/card";
import { DashboardShell } from "../../components/dashboard-shell";
import { Calendar } from "./_components/calendar";
import { ReportTimeSheet } from "./_components/report-time-form";
import { TimeslotCard } from "./_components/timeslot-card";
export const runtime = "edge";
const getMonthMetadata = cache(
async (slots: Timeslot[], currency: CurrencyCode) => {
const billedClients = new Set(slots.map((slot) => slot.clientId)).size;
const totalHours = slots.reduce((acc, slot) => {
return acc + Number(slot.duration);
}, 0);
const convert = await createConverter();
const totalRevenue = slots.reduce(
(acc, slot) => {
const dineroObject = dinero({
amount: slot.chargeRate,
currency: currencies[slot.currency],
});
return add(
acc,
multiply(convert(dineroObject, currency), parseFloat(slot.duration)),
);
},
dinero({ amount: 0, currency: currencies[currency] }),
);
return {
billedClients,
totalHours,
totalRevenue,
};
},
);
export default async function IndexPage(props: {
searchParams: { date?: string };
}) {
const user = await currentUser();
if (!user) redirect("/login");
const date = props.searchParams.date
? parseISO(`${props.searchParams.date}T00:00:00.000Z`)
: undefined;
const clients = await withUnstableCache({
fn: getClients,
args: [user.id],
tags: ["clients"],
});
const timeslots = await withUnstableCache({
fn: getTimeslots,
args: [startOfMonth(date ?? new Date()), user.id, { mode: "month" }],
tags: ["timeslots"],
});
const { billedClients, totalHours, totalRevenue } = await getMonthMetadata(
timeslots,
user.defaultCurrency,
);
const slotsByDate = timeslots.reduce<Record<string, Timeslot[]>>(
(acc, slot) => {
const date = format(slot.date, "yyyy-MM-dd");
if (!acc[date]) acc[date] = [];
acc[date].push(slot);
return acc;
},
{},
);
const selectedDaySlots = date
? slotsByDate?.[format(date, "yyyy-MM-dd")] ?? []
: [];
return (
<DashboardShell
title="Overview"
description="Browse a summary of how your business is doing this month."
className="gap-4"
headerActions={[
<Button size="icon" variant="outline">
<HamburgerMenuIcon className="h-5 w-5" />
</Button>,
<Button className="hidden sm:inline-flex">Close month</Button>,
]}
>
<section className="flex gap-4 overflow-x-scroll md:grid md:grid-cols-2 lg:grid-cols-3">
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Total Revenue</CardTitle>
</CardHeader>
<CardContent className="w-max md:w-auto">
<div className="text-2xl font-bold">
{toDecimal(totalRevenue, formatMoney)}
</div>
<p className="text-xs text-muted-foreground">
+{toDecimal(totalRevenue, formatMoney)} since last month
</p>
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Billed time</CardTitle>
</CardHeader>
<CardContent className="w-max md:w-auto">
<div className="text-2xl font-bold">
{totalHours}
<span className="ml-1 text-lg">hours</span>
</div>
<p className="text-xs text-muted-foreground">
+{totalHours} hours from last month
</p>
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">
Active Clients
</CardTitle>
</CardHeader>
<CardContent className="w-max md:w-auto">
<div className="text-2xl font-bold">
{billedClients}
<span className="ml-1 text-lg">billed this month</span>
</div>
<p className="text-xs text-muted-foreground">
out of {clients.length} total clients
</p>
</CardContent>
</Card>
{/* <Card className="bg-muted">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">
Estimated income
</CardTitle>
</CardHeader>
<CardContent className="w-max md:w-auto">
<div className="text-2xl font-bold">
{toDecimal(
dinero({
amount: 2375000,
currency: currencies[user.defaultCurrency],
}),
formatMoney,
)}
</div>
<p className="text-xs text-muted-foreground">
after fees and taxes
</p>
</CardContent>
</Card> */}
</section>
<section className="flex flex-col gap-4 sm:grid md:grid-cols-2 lg:grid-cols-4">
<Calendar date={date} timeslots={slotsByDate} />
<SidePanel
date={date}
clients={clients}
timeslots={selectedDaySlots}
currency={user.defaultCurrency}
/>
</section>
</DashboardShell>
);
}
async function SidePanel(props: {
date?: Date;
clients: Client[];
timeslots: Timeslot[];
currency: CurrencyCode;
}) {
if (!props.date) {
return (
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-xl">No date selected</CardTitle>
</CardHeader>
<CardContent>
<p className="text-sm text-muted-foreground">
Select a date to view timeslots.
</p>
</CardContent>
</Card>
);
}
const { totalRevenue, totalHours } = await getMonthMetadata(
props.timeslots,
props.currency,
);
return (
<Card>
<CardHeader>
<CardTitle className="text-xl">
{format(props.date, "EEEE, MMMM do")}
</CardTitle>
<CardDescription>
{totalHours} hours billed for a total of{" "}
{toDecimal(totalRevenue, formatMoney)}
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-4">
{props.timeslots.length === 0 && (
<p className="text-sm text-muted-foreground">
No timeslots for this date.
</p>
)}
{props.timeslots.map((slot) => (
<TimeslotCard key={slot.id} slot={slot} />
))}
<Suspense>
<ReportTimeSheet clients={props.clients} />
</Suspense>
</CardContent>
</Card>
);
}