Skip to content

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

⚡️ Move polls to client-side tRPC (#2213)

Luke Vella14b718f118d3
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/polls/page.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/polls/page.tsx
index b3268720e..ebd43ff53 100644
--- a/apps/web/src/app/[locale]/(space)/(dashboard)/polls/page.tsx
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/polls/page.tsx
@@ -1,59 +1,9 @@
-import type { PollStatus } from "@rallly/database";
-import { Button } from "@rallly/ui/button";
-import { Icon } from "@rallly/ui/icon";
-import { dehydrate, HydrationBoundary } from "@tanstack/react-query";
-import { InboxIcon, PlusIcon } from "lucide-react";
import type { Metadata } from "next";
-import Link from "next/link";
-import { PollsTabbedView } from "@/app/[locale]/(space)/(dashboard)/polls/polls-tabbed-view";
-import {
- PageContainer,
- PageContent,
- PageHeader,
- PageHeaderActions,
- PageHeaderContent,
- PageTitle,
-} from "@/app/components/page-layout";
-import { SearchInput } from "@/app/components/search-input";
-import {
- EmptyState,
- EmptyStateDescription,
- EmptyStateFooter,
- EmptyStateIcon,
- EmptyStateTitle,
-} from "@/components/empty-state";
-import { MemberSelector } from "@/components/member-selector";
-import { PollsInfiniteList } from "@/features/poll/components/polls-infinite-list";
-import { loadMembers } from "@/features/space/data";
-import { Trans } from "@/i18n/client";
import { getTranslation } from "@/i18n/server";
-import { createAuthenticatedSSRHelper } from "@/trpc/server/create-ssr-helper";
-import { searchParamsSchema } from "./schema";
+import { PollsPage } from "./polls-page";
-async function loadData({
- status,
- q,
- member,
-}: {
- status?: PollStatus;
- q?: string;
- member?: string;
-}) {
- const helpers = await createAuthenticatedSSRHelper();
-
- const [members] = await Promise.all([
- loadMembers(),
- helpers.polls.infiniteChronological.prefetchInfinite({
- status,
- search: q,
- member,
- }),
- ]);
-
- return {
- members,
- dehydratedState: dehydrate(helpers.queryClient),
- };
+export default function Page() {
+ return <PollsPage />;
}
export async function generateMetadata(): Promise<Metadata> {
@@ -64,86 +14,3 @@ export async function generateMetadata(): Promise<Metadata> {
}),
};
}
-
-function PollsEmptyState() {
- return (
- <EmptyState className="p-8">
- <EmptyStateIcon>
- <InboxIcon />
- </EmptyStateIcon>
- <EmptyStateTitle>
- <Trans i18nKey="noPolls" defaults="No polls found" />
- </EmptyStateTitle>
- <EmptyStateDescription>
- <Trans
- i18nKey="noPollsDescription"
- defaults="Try adjusting your search or create a new poll"
- />
- </EmptyStateDescription>
- <EmptyStateFooter>
- <Button asChild>
- <Link href="/new">
- <Trans i18nKey="createPoll" defaults="Create Poll" />
- </Link>
- </Button>
- </EmptyStateFooter>
- </EmptyState>
- );
-}
-
-export default async function Page(props: {
- searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
-}) {
- const { t } = await getTranslation();
-
- const searchParams = await props.searchParams;
- const { status, q, member } = searchParamsSchema.parse(searchParams);
-
- const { members, dehydratedState } = await loadData({
- status,
- q,
- member,
- });
-
- return (
- <HydrationBoundary state={dehydratedState}>
- <PageContainer>
- <PageHeader>
- <PageHeaderContent>
- <PageTitle>
- <Trans i18nKey="polls" defaults="Polls" />
- </PageTitle>
- </PageHeaderContent>
- <PageHeaderActions>
- <Button variant="primary" asChild>
- <Link href="/new">
- <Icon>
- <PlusIcon />
- </Icon>
- <Trans i18nKey="newPoll" defaults="New Poll" />
- </Link>
- </Button>
- </PageHeaderActions>
- </PageHeader>
- <PageContent>
- <PollsTabbedView>
- <div className="mb-6 flex gap-x-2">
- <SearchInput
- placeholder={t("searchPollsPlaceholder", {
- defaultValue: "Search polls by title...",
- })}
- />
- <MemberSelector members={members.data} />
- </div>
- <PollsInfiniteList
- status={status}
- search={q}
- member={member}
- emptyState={<PollsEmptyState />}
- />
- </PollsTabbedView>
- </PageContent>
- </PageContainer>
- </HydrationBoundary>
- );
-}
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/polls/polls-page.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/polls/polls-page.tsx
new file mode 100644
index 000000000..4c16c09f0
--- /dev/null
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/polls/polls-page.tsx
@@ -0,0 +1,115 @@
+"use client";
+
+import { Button } from "@rallly/ui/button";
+import { Icon } from "@rallly/ui/icon";
+import { InboxIcon, PlusIcon } from "lucide-react";
+import Link from "next/link";
+import { useSearchParams } from "next/navigation";
+import React from "react";
+import {
+ PageContainer,
+ PageContent,
+ PageHeader,
+ PageHeaderActions,
+ PageHeaderContent,
+ PageSkeleton,
+ PageTitle,
+} from "@/app/components/page-layout";
+import { SearchInput } from "@/app/components/search-input";
+import {
+ EmptyState,
+ EmptyStateDescription,
+ EmptyStateFooter,
+ EmptyStateIcon,
+ EmptyStateTitle,
+} from "@/components/empty-state";
+import { MemberSelector } from "@/components/member-selector";
+import { PollsInfiniteList } from "@/features/poll/components/polls-infinite-list";
+import { Trans, useTranslation } from "@/i18n/client";
+import { trpc } from "@/trpc/client";
+import { PollsTabbedView } from "./polls-tabbed-view";
+import { searchParamsSchema } from "./schema";
+
+function PollsEmptyState() {
+ return (
+ <EmptyState className="p-8">
+ <EmptyStateIcon>
+ <InboxIcon />
+ </EmptyStateIcon>
+ <EmptyStateTitle>
+ <Trans i18nKey="noPolls" defaults="No polls found" />
+ </EmptyStateTitle>
+ <EmptyStateDescription>
+ <Trans
+ i18nKey="noPollsDescription"
+ defaults="Try adjusting your search or create a new poll"
+ />
+ </EmptyStateDescription>
+ <EmptyStateFooter>
+ <Button asChild>
+ <Link href="/new">
+ <Trans i18nKey="createPoll" defaults="Create Poll" />
+ </Link>
+ </Button>
+ </EmptyStateFooter>
+ </EmptyState>
+ );
+}
+
+function PollsPageContent() {
+ const searchParams = useSearchParams();
+ const { t } = useTranslation();
+ const [members] = trpc.space.members.useSuspenseQuery();
+
+ const { status, q, member } = searchParamsSchema.parse(
+ Object.fromEntries(searchParams.entries()),
+ );
+
+ return (
+ <PageContainer>
+ <PageHeader>
+ <PageHeaderContent>
+ <PageTitle>
+ <Trans i18nKey="polls" defaults="Polls" />
+ </PageTitle>
+ </PageHeaderContent>
+ <PageHeaderActions>
+ <Button variant="primary" asChild>
+ <Link href="/new">
+ <Icon>
+ <PlusIcon />
+ </Icon>
+ <Trans i18nKey="newPoll" defaults="New Poll" />
+ </Link>
+ </Button>
+ </PageHeaderActions>
+ </PageHeader>
+ <PageContent>
+ <PollsTabbedView>
+ <div className="mb-6 flex gap-x-2">
+ <SearchInput
+ placeholder={t("searchPollsPlaceholder", {
+ defaultValue: "Search polls by title...",
+ })}
+ />
+ <MemberSelector members={members} />
+ </div>
+ <PollsInfiniteList
+ status={status}
+ search={q}
+ member={member}
+ emptyState={<PollsEmptyState />}
+ />
+ </PollsTabbedView>
+ </PageContent>
+ </PageContainer>
+ );
+}
+
+export function PollsPage() {
+ return (
+ <React.Suspense fallback={<PageSkeleton />}>
+ <PollsPageContent />
+ </React.Suspense>
+ );
+}
diff --git a/apps/web/src/components/member-selector.tsx b/apps/web/src/components/member-selector.tsx
index a2c735161..85f5b7ada 100644
--- a/apps/web/src/components/member-selector.tsx
+++ b/apps/web/src/components/member-selector.tsx
@@ -12,11 +12,10 @@ import { UsersIcon } from "lucide-react";
import { useRouter, useSearchParams } from "next/navigation";
import { useOptimistic, useTransition } from "react";
import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";
-import type { MemberDTO } from "@/features/space/member/types";
import { Trans } from "@/i18n/client";
interface MemberSelectorProps {
- members: MemberDTO[];
+ members: { userId: string; name: string; image?: string }[];
}
export function MemberSelector({ members }: MemberSelectorProps) {
diff --git a/apps/web/src/features/poll/components/polls-infinite-list.tsx b/apps/web/src/features/poll/components/polls-infinite-list.tsx
index f072fd308..01e763728 100644
--- a/apps/web/src/features/poll/components/polls-infinite-list.tsx
+++ b/apps/web/src/features/poll/components/polls-infinite-list.tsx
@@ -240,25 +240,19 @@ export function PollsInfiniteList({
member,
emptyState,
}: PollsInfiniteListProps) {
- const {
- data,
- error,
- fetchNextPage,
- hasNextPage,
- isFetchingNextPage,
- status: queryStatus,
- } = trpc.polls.infiniteChronological.useInfiniteQuery(
- {
- status,
- search,
- member,
- },
- {
- getNextPageParam: (lastPage) => lastPage.nextCursor,
- },
- );
+ const [data, { fetchNextPage, hasNextPage, isFetchingNextPage }] =
+ trpc.polls.infiniteChronological.useSuspenseInfiniteQuery(
+ {
+ status,
+ search,
+ member,
+ },
+ {
+ getNextPageParam: (lastPage) => lastPage.nextCursor,
+ },
+ );
- const polls = data?.pages?.flatMap((page) => page.polls) ?? [];
+ const polls = data.pages.flatMap((page) => page.polls);
const loadMoreRef = React.useRef<HTMLDivElement>(null);
@@ -291,18 +285,6 @@ export function PollsInfiniteList({
};
}, [handleLoadMore]);
- if (queryStatus === "pending") {
- return <Spinner />;
- }
-
- if (queryStatus === "error") {
- return (
- <div className="py-8 text-center text-red-600">
- Error loading polls: {error?.message}
- </div>
- );
- }
-
if (polls.length === 0) {
return <>{emptyState}</>;
}
diff --git a/apps/web/src/trpc/routers/index.ts b/apps/web/src/trpc/routers/index.ts
index 961842a57..7a6727084 100644
--- a/apps/web/src/trpc/routers/index.ts
+++ b/apps/web/src/trpc/routers/index.ts
@@ -7,6 +7,7 @@ import { calendars } from "./calendars";
import { dashboard } from "./dashboard";
import { events } from "./events";
import { polls } from "./polls";
+import { space } from "./space";
import { user } from "./user";
export const appRouter = mergeRouters(
@@ -16,6 +17,7 @@ export const appRouter = mergeRouters(
dashboard,
events,
polls,
+ space,
user,
calendars,
}),
diff --git a/apps/web/src/trpc/routers/space.ts b/apps/web/src/trpc/routers/space.ts
new file mode 100644
index 000000000..3e94bb816
--- /dev/null
+++ b/apps/web/src/trpc/routers/space.ts
@@ -0,0 +1,37 @@
+import { prisma } from "@rallly/database";
+import { getActiveSpaceForUser } from "@/features/space/data";
+import { privateProcedure, router } from "../trpc";
+
+export const space = router({
+ members: privateProcedure.query(async ({ ctx }) => {
+ const activeSpace = await getActiveSpaceForUser(ctx.user.id);
+
+ if (!activeSpace) {
+ return [];
+ }
+
+ const members = await prisma.spaceMember.findMany({
+ where: {
+ spaceId: activeSpace.id,
+ },
+ select: {
+ userId: true,
+ user: {
+ select: {
+ name: true,
+ image: true,
+ },
+ },
+ },
+ orderBy: {
+ createdAt: "asc",
+ },
+ });
+
+ return members.map((member) => ({
+ userId: member.userId,
+ name: member.user.name,
+ image: member.user.image ?? undefined,
+ }));
+ }),
+});