Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

⚡️ Move dashboard home to client-side tRPC query (#2212)

Luke Vella50404bc5d5a2
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/dashboard-home.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/dashboard-home.tsx
new file mode 100644
index 000000000..deada7dc1
--- /dev/null
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/dashboard-home.tsx
@@ -0,0 +1,153 @@
+"use client";
+
+import { Tile, TileDescription, TileGrid, TileTitle } from "@rallly/ui/tile";
+import {
+ BillingPageIcon,
+ CreatePageIcon,
+ EventPageIcon,
+ MembersPageIcon,
+ PollPageIcon,
+ SettingsPageIcon,
+} from "@/app/components/page-icons";
+import {
+ PageContainer,
+ PageContent,
+ PageHeader,
+ PageSkeleton,
+ PageTitle,
+} from "@/app/components/page-layout";
+import { HoverPrefetchLink } from "@/components/hover-prefetch-link";
+import { Trans } from "@/i18n/client";
+import { IfFeatureEnabled, useFeatureFlag } from "@/lib/feature-flags/client";
+import { trpc } from "@/trpc/client";
+import { PasswordSetupAlert } from "./password-setup-alert";
+
+export function DashboardHome() {
+ const { data: stats } = trpc.dashboard.stats.useQuery();
+ const isEmailLoginEnabled = useFeatureFlag("emailLogin");
+
+ if (!stats) {
+ return <PageSkeleton />;
+ }
+
+ return (
+ <PageContainer>
+ <PageHeader>
+ <PageTitle>
+ <Trans i18nKey="home" defaults="Home" />
+ </PageTitle>
+ </PageHeader>
+ <PageContent className="space-y-8">
+ <div className="space-y-4">
+ {stats.hasNoAccounts && isEmailLoginEnabled ? (
+ <PasswordSetupAlert />
+ ) : null}
+ </div>
+ <div className="space-y-4">
+ <h2 className="text-muted-foreground text-sm">
+ <Trans i18nKey="homeActionsTitle" defaults="Actions" />
+ </h2>
+ <TileGrid>
+ <Tile asChild>
+ <HoverPrefetchLink href="/new">
+ <CreatePageIcon />
+ <TileTitle>
+ <Trans i18nKey="create" defaults="Create" />
+ </TileTitle>
+ </HoverPrefetchLink>
+ </Tile>
+ </TileGrid>
+ </div>
+ <div className="space-y-4">
+ <h2 className="text-muted-foreground text-sm">
+ <Trans i18nKey="content" defaults="Content" />
+ </h2>
+ <TileGrid>
+ <Tile asChild>
+ <HoverPrefetchLink href="/polls">
+ <PollPageIcon />
+ <TileTitle>
+ <Trans i18nKey="polls" defaults="Polls" />
+ </TileTitle>
+ <TileDescription>
+ <Trans
+ i18nKey="openPollCount"
+ defaults="{count} open"
+ values={{ count: stats.openPollCount }}
+ />
+ </TileDescription>
+ </HoverPrefetchLink>
+ </Tile>
+
+ <Tile asChild>
+ <HoverPrefetchLink href="/events">
+ <EventPageIcon />
+ <TileTitle>
+ <Trans i18nKey="events" defaults="Events" />
+ </TileTitle>
+ <TileDescription>
+ <Trans
+ i18nKey="upcomingEventCount"
+ defaults="{count} upcoming"
+ values={{ count: stats.upcomingEventCount }}
+ />
+ </TileDescription>
+ </HoverPrefetchLink>
+ </Tile>
+ </TileGrid>
+ </div>
+ <div className="space-y-4">
+ <h2 className="text-muted-foreground text-sm">
+ <Trans i18nKey="manage" defaults="Manage" />
+ </h2>
+ <TileGrid>
+ <Tile asChild>
+ <HoverPrefetchLink href="/settings/general">
+ <SettingsPageIcon />
+ <TileTitle>
+ <Trans i18nKey="settings" defaults="Settings" />
+ </TileTitle>
+ </HoverPrefetchLink>
+ </Tile>
+
+ <Tile asChild>
+ <HoverPrefetchLink href="/settings/members">
+ <MembersPageIcon />
+ <TileTitle>
+ <Trans i18nKey="members" defaults="Members" />
+ </TileTitle>
+ <TileDescription>
+ <Trans
+ i18nKey="memberCount"
+ defaults="{count, plural, =0 {No members} one {1 member} other {# members}}"
+ values={{ count: stats.memberCount }}
+ />
+ </TileDescription>
+ </HoverPrefetchLink>
+ </Tile>
+
+ <IfFeatureEnabled feature="billing">
+ {stats.canManageBilling && (
+ <Tile asChild>
+ <HoverPrefetchLink href="/settings/billing">
+ <BillingPageIcon />
+ <TileTitle>
+ <Trans i18nKey="billing" defaults="Billing" />
+ </TileTitle>
+ <TileDescription>
+ <Trans
+ i18nKey="seatCount"
+ defaults="{count, plural, =0 {No seats} one {1 seat} other {# seats}}"
+ values={{ count: stats.seatCount }}
+ />
+ </TileDescription>
+ </HoverPrefetchLink>
+ </Tile>
+ )}
+ </IfFeatureEnabled>
+ </TileGrid>
+ </div>
+ </PageContent>
+ </PageContainer>
+ );
+}
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/page.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/page.tsx
index 23ca0f7e9..90d166522 100644
--- a/apps/web/src/app/[locale]/(space)/(dashboard)/page.tsx
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/page.tsx
@@ -1,199 +1,9 @@
-import { prisma } from "@rallly/database";
-import { Tile, TileDescription, TileGrid, TileTitle } from "@rallly/ui/tile";
import type { Metadata } from "next";
-import {
- BillingPageIcon,
- CreatePageIcon,
- EventPageIcon,
- MembersPageIcon,
- PollPageIcon,
- SettingsPageIcon,
-} from "@/app/components/page-icons";
-import {
- PageContainer,
- PageContent,
- PageHeader,
- PageTitle,
-} from "@/app/components/page-layout";
-import { requireSpace, requireUser } from "@/auth/data";
-import { HoverPrefetchLink } from "@/components/hover-prefetch-link";
-import { getUpcomingEventsCount } from "@/features/scheduled-event/data";
-import { loadMembers } from "@/features/space/data";
-import { defineAbilityForMember } from "@/features/space/member/ability";
-import { getTotalSeatsForSpace } from "@/features/space/utils";
-import { getUserHasNoAccounts } from "@/features/user/queries";
-import { Trans } from "@/i18n/client";
import { getTranslation } from "@/i18n/server";
-import { IfFeatureEnabled } from "@/lib/feature-flags/client";
-import { isFeatureEnabled } from "@/lib/feature-flags/server";
-import { PasswordSetupAlert } from "./password-setup-alert";
+import { DashboardHome } from "./dashboard-home";
-async function loadData() {
- const [space, user] = await Promise.all([requireSpace(), requireUser()]);
-
- const [
- openPollCount,
- upcomingEventCount,
- { total: memberCount },
- seatCount,
- hasNoAccounts,
- ] = await Promise.all([
- prisma.poll.count({
- where: {
- spaceId: space.id,
- status: "open",
- deleted: false,
- },
- }),
- getUpcomingEventsCount(),
- loadMembers(),
- getTotalSeatsForSpace(space.id),
- getUserHasNoAccounts(user.id),
- ]);
-
- const ability = defineAbilityForMember({ space, user });
- const canManageBilling = ability.can("manage", "Billing");
-
- return {
- openPollCount,
- upcomingEventCount,
- memberCount,
- seatCount,
- canManageBilling,
- hasNoAccounts,
- };
-}
-
-export default async function Page() {
- const {
- openPollCount,
- upcomingEventCount,
- memberCount,
- seatCount,
- canManageBilling,
- hasNoAccounts,
- } = await loadData();
-
- const isEmailLoginEnabled = isFeatureEnabled("emailLogin");
-
- return (
- <PageContainer>
- <PageHeader>
- <PageTitle>
- <Trans i18nKey="home" defaults="Home" />
- </PageTitle>
- </PageHeader>
- <PageContent className="space-y-8">
- <div className="space-y-4">
- {hasNoAccounts && isEmailLoginEnabled ? <PasswordSetupAlert /> : null}
- </div>
- <div className="space-y-4">
- <h2 className="text-muted-foreground text-sm">
- <Trans i18nKey="homeActionsTitle" defaults="Actions" />
- </h2>
- <TileGrid>
- <Tile asChild>
- <HoverPrefetchLink href="/new">
- <CreatePageIcon />
- <TileTitle>
- <Trans i18nKey="create" defaults="Create" />
- </TileTitle>
- </HoverPrefetchLink>
- </Tile>
- </TileGrid>
- </div>
- <div className="space-y-4">
- <h2 className="text-muted-foreground text-sm">
- <Trans i18nKey="content" defaults="Content" />
- </h2>
- <TileGrid>
- <Tile asChild>
- <HoverPrefetchLink href="/polls">
- <PollPageIcon />
- <TileTitle>
- <Trans i18nKey="polls" defaults="Polls" />
- </TileTitle>
- <TileDescription>
- <Trans
- i18nKey="openPollCount"
- defaults="{count} open"
- values={{ count: openPollCount }}
- />
- </TileDescription>
- </HoverPrefetchLink>
- </Tile>
-
- <Tile asChild>
- <HoverPrefetchLink href="/events">
- <EventPageIcon />
- <TileTitle>
- <Trans i18nKey="events" defaults="Events" />
- </TileTitle>
- <TileDescription>
- <Trans
- i18nKey="upcomingEventCount"
- defaults="{count} upcoming"
- values={{ count: upcomingEventCount }}
- />
- </TileDescription>
- </HoverPrefetchLink>
- </Tile>
- </TileGrid>
- </div>
- <div className="space-y-4">
- <h2 className="text-muted-foreground text-sm">
- <Trans i18nKey="manage" defaults="Manage" />
- </h2>
- <TileGrid>
- <Tile asChild>
- <HoverPrefetchLink href="/settings/general">
- <SettingsPageIcon />
- <TileTitle>
- <Trans i18nKey="settings" defaults="Settings" />
- </TileTitle>
- </HoverPrefetchLink>
- </Tile>
-
- <Tile asChild>
- <HoverPrefetchLink href="/settings/members">
- <MembersPageIcon />
- <TileTitle>
- <Trans i18nKey="members" defaults="Members" />
- </TileTitle>
- <TileDescription>
- <Trans
- i18nKey="memberCount"
- defaults="{count, plural, =0 {No members} one {1 member} other {# members}}"
- values={{ count: memberCount }}
- />
- </TileDescription>
- </HoverPrefetchLink>
- </Tile>
-
- <IfFeatureEnabled feature="billing">
- {canManageBilling && (
- <Tile asChild>
- <HoverPrefetchLink href="/settings/billing">
- <BillingPageIcon />
- <TileTitle>
- <Trans i18nKey="billing" defaults="Billing" />
- </TileTitle>
- <TileDescription>
- <Trans
- i18nKey="seatCount"
- defaults="{count, plural, =0 {No seats} one {1 seat} other {# seats}}"
- values={{ count: seatCount }}
- />
- </TileDescription>
- </HoverPrefetchLink>
- </Tile>
- )}
- </IfFeatureEnabled>
- </TileGrid>
- </div>
- </PageContent>
- </PageContainer>
- );
+export default function Page() {
+ return <DashboardHome />;
}
export async function generateMetadata(): Promise<Metadata> {
diff --git a/apps/web/src/app/api/trpc/[trpc]/route.ts b/apps/web/src/app/api/trpc/[trpc]/route.ts
index 4f8f9a93e..ff2708394 100644
--- a/apps/web/src/app/api/trpc/[trpc]/route.ts
+++ b/apps/web/src/app/api/trpc/[trpc]/route.ts
@@ -51,6 +51,7 @@ const handler = async (req: NextRequest) => {
isGuest: session.user.isGuest,
locale,
image: session.user.image ?? undefined,
+ timeZone: session.user.timeZone ?? undefined,
isLegacyGuest: isLegacyGuest ?? false,
}
: undefined;
diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts
index 947b6cf57..079cde9a4 100644
--- a/apps/web/src/lib/auth.ts
+++ b/apps/web/src/lib/auth.ts
@@ -389,6 +389,7 @@ export const getSession = cache(async () => {
name: session.user.name,
isGuest: !!session.user.isAnonymous,
image: session.user.image,
+ timeZone: session.user.timeZone ?? undefined,
},
expires: session.session.expiresAt.toISOString(),
legacy: false,
diff --git a/apps/web/src/trpc/context.ts b/apps/web/src/trpc/context.ts
index 1b444871a..337dcf465 100644
--- a/apps/web/src/trpc/context.ts
+++ b/apps/web/src/trpc/context.ts
@@ -5,6 +5,7 @@ export type User = {
isGuest: boolean;
locale?: string;
image?: string;
+ timeZone?: string;
isLegacyGuest: boolean;
};
diff --git a/apps/web/src/trpc/routers/dashboard.ts b/apps/web/src/trpc/routers/dashboard.ts
new file mode 100644
index 000000000..146764030
--- /dev/null
+++ b/apps/web/src/trpc/routers/dashboard.ts
@@ -0,0 +1,68 @@
+import type { Prisma } from "@rallly/database";
+import { prisma } from "@rallly/database";
+import { getActiveSpaceForUser } from "@/features/space/data";
+import type { MemberAbilityContext } from "@/features/space/member/ability";
+import { defineAbilityForMember } from "@/features/space/member/ability";
+import { getTotalSeatsForSpace } from "@/features/space/utils";
+import { dayjs } from "@/lib/dayjs";
+import { privateProcedure, router } from "../trpc";
+
+export const dashboard = router({
+ stats: privateProcedure.query(async ({ ctx }) => {
+ const space = await getActiveSpaceForUser(ctx.user.id);
+
+ if (!space) {
+ return null;
+ }
+
+ const now = new Date();
+ const todayStart = dayjs()
+ .tz(ctx.user.timeZone ?? "UTC")
+ .startOf("day")
+ .toDate();
+
+ const upcomingEventsWhere: Prisma.ScheduledEventWhereInput = {
+ spaceId: space.id,
+ deletedAt: null,
+ status: "confirmed",
+ OR: [
+ { allDay: false, start: { gte: now } },
+ { allDay: true, start: { gte: todayStart } },
+ ],
+ };
+
+ const [
+ openPollCount,
+ upcomingEventCount,
+ memberCount,
+ seatCount,
+ accountCount,
+ ] = await Promise.all([
+ prisma.poll.count({
+ where: {
+ spaceId: space.id,
+ status: "open",
+ deleted: false,
+ },
+ }),
+ prisma.scheduledEvent.count({ where: upcomingEventsWhere }),
+ prisma.spaceMember.count({ where: { spaceId: space.id } }),
+ getTotalSeatsForSpace(space.id),
+ prisma.account.count({ where: { userId: ctx.user.id } }),
+ ]);
+
+ const ability = defineAbilityForMember({
+ user: { id: ctx.user.id },
+ space,
+ } satisfies MemberAbilityContext);
+
+ return {
+ openPollCount,
+ upcomingEventCount,
+ memberCount,
+ seatCount,
+ hasNoAccounts: accountCount === 0,
+ canManageBilling: ability.can("manage", "Billing"),
+ };
+ }),
+});
diff --git a/apps/web/src/trpc/routers/index.ts b/apps/web/src/trpc/routers/index.ts
index f4517feac..961842a57 100644
--- a/apps/web/src/trpc/routers/index.ts
+++ b/apps/web/src/trpc/routers/index.ts
@@ -4,6 +4,7 @@ import { mergeRouters, router } from "../trpc";
import { apiKeys } from "./api-keys";
import { auth } from "./auth";
import { calendars } from "./calendars";
+import { dashboard } from "./dashboard";
import { events } from "./events";
import { polls } from "./polls";
import { user } from "./user";
@@ -12,6 +13,7 @@ export const appRouter = mergeRouters(
router({
apiKeys,
auth,
+ dashboard,
events,
polls,
user,
diff --git a/apps/web/src/trpc/server/create-ssr-helper.ts b/apps/web/src/trpc/server/create-ssr-helper.ts
index e1f51b904..5ea313f4f 100644
--- a/apps/web/src/trpc/server/create-ssr-helper.ts
+++ b/apps/web/src/trpc/server/create-ssr-helper.ts
@@ -41,6 +41,7 @@ export const createAuthenticatedSSRHelper = async () => {
isGuest: session.user.isGuest,
locale: session.user.locale ?? undefined,
image: session.user.image ?? undefined,
+ timeZone: session.user.timeZone ?? undefined,
isLegacyGuest: session.legacy && session.user.isGuest,
}
: undefined,