-
-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Prefetch queries with trpc (#1454)
- Loading branch information
Showing
13 changed files
with
200 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
import { cn } from "@rallly/ui"; | ||
import { dehydrate, Hydrate } from "@tanstack/react-query"; | ||
import React from "react"; | ||
|
||
import { MobileNavigation } from "@/app/[locale]/(admin)/mobile-navigation"; | ||
import { ProBadge } from "@/app/[locale]/(admin)/pro-badge"; | ||
import { Sidebar } from "@/app/[locale]/(admin)/sidebar"; | ||
import { LogoLink } from "@/app/components/logo-link"; | ||
import { PayWallDialog } from "@/components/pay-wall-dialog"; | ||
import { createSSRHelper } from "@/trpc/server/create-ssr-helper"; | ||
|
||
export default async function Layout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const helpers = await createSSRHelper(); | ||
await helpers.user.subscription.prefetch(); | ||
const dehydratedState = dehydrate(helpers.queryClient); | ||
return ( | ||
<PayWallDialog> | ||
<div className="flex flex-col pb-16 md:pb-0"> | ||
<div | ||
className={cn( | ||
"fixed inset-y-0 z-50 hidden w-72 shrink-0 flex-col gap-y-4 overflow-y-auto p-6 md:flex", | ||
)} | ||
> | ||
<div className="flex w-full items-center justify-between gap-4"> | ||
<LogoLink /> | ||
<ProBadge /> | ||
<Hydrate state={dehydratedState}> | ||
<PayWallDialog> | ||
<div className="flex flex-col pb-16 md:pb-0"> | ||
<div | ||
className={cn( | ||
"fixed inset-y-0 z-50 hidden w-72 shrink-0 flex-col gap-y-4 overflow-y-auto p-6 md:flex", | ||
)} | ||
> | ||
<div className="flex w-full items-center justify-between gap-4"> | ||
<LogoLink /> | ||
<ProBadge /> | ||
</div> | ||
<Sidebar /> | ||
</div> | ||
<div className={cn("grow space-y-4 p-3 md:ml-72 md:p-4 lg:p-6")}> | ||
<div className="max-w-5xl">{children}</div> | ||
</div> | ||
<div className="fixed bottom-0 z-20 flex h-16 w-full flex-col justify-center bg-gray-100/90 backdrop-blur-md md:hidden"> | ||
<MobileNavigation /> | ||
</div> | ||
<Sidebar /> | ||
</div> | ||
<div className={cn("grow space-y-4 p-3 md:ml-72 md:p-4 lg:p-6")}> | ||
<div className="max-w-5xl">{children}</div> | ||
</div> | ||
<div className="fixed bottom-0 z-20 flex h-16 w-full flex-col justify-center bg-gray-100/90 backdrop-blur-md md:hidden"> | ||
<MobileNavigation /> | ||
</div> | ||
</div> | ||
</PayWallDialog> | ||
</PayWallDialog> | ||
</Hydrate> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,26 @@ | ||
"use client"; | ||
import { notFound, useParams, useSearchParams } from "next/navigation"; | ||
import React from "react"; | ||
|
||
import { LegacyPollContextProvider } from "@/components/poll/poll-context-provider"; | ||
import { VisibilityProvider } from "@/components/visibility"; | ||
import { PermissionsContext } from "@/contexts/permissions"; | ||
import { trpc } from "@/trpc/client"; | ||
|
||
import Loader from "./loading"; | ||
|
||
const Prefetch = ({ children }: React.PropsWithChildren) => { | ||
const searchParams = useSearchParams(); | ||
const token = searchParams?.get("token") as string; | ||
const params = useParams<{ urlId: string }>(); | ||
const urlId = params?.urlId as string; | ||
const { data: permission } = trpc.auth.getUserPermission.useQuery( | ||
{ token }, | ||
{ | ||
enabled: !!token, | ||
}, | ||
); | ||
|
||
const { data: poll, error } = trpc.polls.get.useQuery( | ||
{ urlId }, | ||
{ | ||
retry: false, | ||
}, | ||
); | ||
|
||
const { data: participants } = trpc.polls.participants.list.useQuery({ | ||
pollId: urlId, | ||
}); | ||
|
||
const comments = trpc.polls.comments.list.useQuery({ pollId: urlId }); | ||
|
||
if (error?.data?.code === "NOT_FOUND") { | ||
notFound(); | ||
} | ||
if (!poll || !participants || !comments.isFetched) { | ||
return <Loader />; | ||
} | ||
|
||
return ( | ||
<PermissionsContext.Provider value={{ userId: permission?.userId ?? null }}> | ||
{children} | ||
</PermissionsContext.Provider> | ||
); | ||
}; | ||
|
||
export default function Layout({ children }: { children: React.ReactNode }) { | ||
import { dehydrate, Hydrate } from "@tanstack/react-query"; | ||
|
||
import { createSSRHelper } from "@/trpc/server/create-ssr-helper"; | ||
|
||
import Providers from "./providers"; | ||
|
||
export default async function Layout({ | ||
children, | ||
params, | ||
}: { | ||
params: { urlId: string }; | ||
children: React.ReactNode; | ||
}) { | ||
const trpc = await createSSRHelper(); | ||
|
||
await Promise.all([ | ||
trpc.polls.get.prefetch({ urlId: params.urlId }), | ||
trpc.polls.participants.list.prefetch({ pollId: params.urlId }), | ||
trpc.polls.comments.list.prefetch({ pollId: params.urlId }), | ||
]); | ||
return ( | ||
<Prefetch> | ||
<LegacyPollContextProvider> | ||
<VisibilityProvider>{children}</VisibilityProvider> | ||
</LegacyPollContextProvider> | ||
</Prefetch> | ||
<Hydrate state={dehydrate(trpc.queryClient)}> | ||
<Providers>{children}</Providers> | ||
</Hydrate> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use client"; | ||
|
||
import { LegacyPollContextProvider } from "@/components/poll/poll-context-provider"; | ||
import { VisibilityProvider } from "@/components/visibility"; | ||
|
||
export default function Providers({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<LegacyPollContextProvider> | ||
<VisibilityProvider>{children}</VisibilityProvider> | ||
</LegacyPollContextProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 13 additions & 1 deletion
14
apps/web/src/contexts/permissions.ts → apps/web/src/contexts/permissions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.