Skip to content

Commit

Permalink
perf: ⚡️ upgraded version of tRPC and @tanstack/react-query
Browse files Browse the repository at this point in the history
  • Loading branch information
growupanand committed Feb 9, 2024
1 parent b19cfa1 commit 131c0a7
Show file tree
Hide file tree
Showing 31 changed files with 215 additions and 300 deletions.
5 changes: 4 additions & 1 deletion apps/web/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ const nextConfig = {
},
// Configure `pageExtensions` to include MDX files
pageExtensions: ["js", "jsx", "mdx", "ts", "tsx"],
transpilePackages: ["@convoform/ui", "@convoform/db"],
transpilePackages: ["@convoform/ui", "@convoform/db", "@convoform/api"],
webpack: (config, { isServer }) => {
if (isServer) {
config.plugins = [...config.plugins, new PrismaPlugin()];
}

return config;
},
/** We already do linting and typechecking as separate tasks in CI */
eslint: { ignoreDuringBuilds: true },
typescript: { ignoreBuildErrors: true },
};

const withMDX = createMDX({
Expand Down
9 changes: 4 additions & 5 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
"@mdx-js/react": "^3.0.0",
"@next/mdx": "^14.1.0",
"@sentry/nextjs": "^7.91.0",
"@tanstack/react-query": "^4.36.1",
"@tremor/react": "^3.13.3",
"@trpc/client": "^10.43.6",
"@trpc/next": "^10.43.6",
"@trpc/react-query": "^10.43.6",
"@trpc/server": "^10.43.6",
"@tanstack/react-query": "^5.18.1",
"@trpc/client": "11.0.0-next-beta.264",
"@trpc/react-query": "11.0.0-next-beta.264",
"@trpc/server": "11.0.0-next-beta.264",
"@types/mdx": "^2.0.10",
"ai": "^2.2.26",
"clsx": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Props = {
export default async function ConversationDetailPage(props: Props) {
const { conversationId } = props.params;

const conversation = await api.conversation.getOne.query({
const conversation = await api.conversation.getOne({
id: conversationId,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import FormPreview from "@/components/formEditorPage/formPreview";
import NavLinks from "@/components/formEditorPage/navLinks";
import { cn } from "@/lib/utils";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";

type Props = {
params: { formId: string };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function WorkspacePage({
params: { workspaceId },
}: Readonly<Props>) {
const orgId = getOrganizationId();
const workspace = await api.workspace.getOne.query({ id: workspaceId });
const workspace = await api.workspace.getOne({ id: workspaceId });

if (!workspace) {
notFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const metadata: Metadata = {

export default async function FormViewPage({ params }: FormViewerPageProps) {
const { formId } = params;
const formData = await api.form.getOne.query({ id: formId });
const formData = await api.form.getOne({ id: formId });
if (!formData || !formData.isPublished) {
notFound();
}
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/api/form/[formId]/conversation/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function POST(
// const { isPreview } = reqPayload;
const isPreview = false;

const form = await api.form.getOneWithFields.query({
const form = await api.form.getOneWithFields({
id: params.formId,
});

Expand All @@ -39,7 +39,7 @@ export async function POST(
if (form.id !== "demo") {
// get all conversations count for current organization
const totalSubmissionsCount =
await api.conversation.getResponseCountByOrganization.query({
await api.conversation.getResponseCountByOrganization({
organizationId: form.organizationId,
});

Expand Down
39 changes: 25 additions & 14 deletions apps/web/src/app/api/trpc/[trpc]/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { type NextRequest } from "next/server";
import { getAuth } from "@clerk/nextjs/server";
import { appRouter } from "@convoform/api/src/router/root";
import { createTRPCContext } from "@convoform/api/src/trpc";
import { appRouter, createTRPCContext } from "@convoform/api";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";

// export const runtime = "edge";

/**
* This wraps the `createTRPCContext` helper and provides the required context for the tRPC API when
* handling a HTTP request (e.g. when you make requests from Client Components).
* Configure basic CORS headers
* You should extend this to match your needs
*/
function setCorsHeaders(res: Response) {
res.headers.set("Access-Control-Allow-Origin", "*");
res.headers.set("Access-Control-Request-Method", "*");
res.headers.set("Access-Control-Allow-Methods", "OPTIONS, GET, POST");
res.headers.set("Access-Control-Allow-Headers", "*");
}

const createContext = async (req: NextRequest) => {
return createTRPCContext({
headers: req.headers,
auth: getAuth(req),
export function OPTIONS() {
const response = new Response(null, {
status: 204,
});
};
setCorsHeaders(response);
return response;
}

const handler = (req: NextRequest) =>
fetchRequestHandler({
const handler = async (req: NextRequest) => {
const response = await fetchRequestHandler({
endpoint: "/api/trpc",
req,
router: appRouter,
createContext: () => createContext(req),
req,
createContext: () => createTRPCContext(),
onError:
process.env.NODE_ENV === "development"
? ({ path, error }: any) => {
Expand All @@ -32,4 +39,8 @@ const handler = (req: NextRequest) =>
: undefined,
});

setCorsHeaders(response);
return response;
};

export { handler as GET, handler as POST };
2 changes: 1 addition & 1 deletion apps/web/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Toaster } from "@convoform/ui/components/ui/toaster";

import GoogleAnalytics from "@/components/googleAnalytics";
import { cn } from "@/lib/utils";
import { TRPCReactProvider } from "@/trpc/provider";
import { TRPCReactProvider } from "@/trpc/react";
import { roboto } from "./fonts";

export const metadata: Metadata = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@convoform/ui/components/ui/sheet";
import { Menu } from "lucide-react";

import { api } from "@/trpc/client";
import { api } from "@/trpc/react";
import NavLinks from "../navLinks";
import { ConversationsCard } from "./conversationsListCard";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { montserrat } from "@/app/fonts";
import { apiClient } from "@/lib/apiClient";
import { cn } from "@/lib/utils";
import { formUpdateSchema } from "@/lib/validations/form";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";

const formSchema = formUpdateSchema;
export type FormSubmitDataSchema = z.infer<typeof formSchema>;
Expand Down Expand Up @@ -104,10 +104,12 @@ export function FormEditorCard({ form }: Readonly<Props>) {
title: "Changes saved successfully",
duration: 1500,
});
queryClient.invalidateQueries([["form"]]);
queryClient.invalidateQueries({
queryKey: [["form"]],
});
},
});
const isFormBusy = updateForm.isLoading;
const isFormBusy = updateForm.isPending;

const onSubmit = (formData: FormSubmitDataSchema) =>
updateForm.mutateAsync({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ChevronLeft, ChevronRight, Home } from "lucide-react";
import { montserrat } from "@/app/fonts";
import { LinkN } from "@/components/common/linkN";
import { cn } from "@/lib/utils";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";
import FormNameInput from "./formNameInput";

type Props = {
Expand Down
8 changes: 5 additions & 3 deletions apps/web/src/components/formEditorPage/formNameInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { toast } from "@convoform/ui/components/ui/use-toast";
import { useQueryClient } from "@tanstack/react-query";

import { cn, debounce } from "@/lib/utils";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";

type Props = {
form: Form;
Expand All @@ -23,7 +23,9 @@ export default function FormNameInput({ form, className }: Props) {
title: "Form name updated.",
duration: 1500,
});
queryClient.invalidateQueries([["form", "getOneWithWorkspace"]]);
queryClient.invalidateQueries({
queryKey: [["form", "getOneWithWorkspace"]],
});
},
onError: () => {
toast({
Expand All @@ -32,7 +34,7 @@ export default function FormNameInput({ form, className }: Props) {
});
},
});
const isUpdating = updateForm.isLoading;
const isUpdating = updateForm.isPending;

const updateWorkspace = async (name: string) =>
updateForm.mutateAsync({
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/formEditorPage/formPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@convoform/ui/components/ui/tooltip";

import { getFrontendBaseUrl } from "@/lib/url";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";
import BrowserWindow from "../common/browserWindow";
import Spinner from "../common/spinner";
import { FormViewer } from "../formSubmissionPage/formViewer";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { timeAgo } from "@/lib/utils";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";
import { DataCard } from "./dataCard";
import { DataCardSkeleton } from "./dataCardLoading";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { timeAgo } from "@/lib/utils";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";
import { DataCard } from "./dataCard";
import { DataCardSkeleton } from "./dataCardLoading";

Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/mainPage/navigationCardContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useQueryClient } from "@tanstack/react-query";
import { Loader2, Plus } from "lucide-react";

import { NavigationConfig } from "@/lib/types/navigation";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";
import BrandName from "../common/brandName";
import { NavigationLinks } from "./mainNavigation/mainNavigation";

Expand Down Expand Up @@ -43,7 +43,7 @@ export function NavigationCardContent({ orgId }: Readonly<Props>) {
variant: "destructive",
}),
});
const isCreatingWorkspace = createWorkspace.isLoading;
const isCreatingWorkspace = createWorkspace.isPending;

const handleCreateWorkspace = useCallback(async () => {
await createWorkspace.mutateAsync({
Expand Down
10 changes: 5 additions & 5 deletions apps/web/src/components/mainPage/workspace/createFormButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Loader2, Plus } from "lucide-react";
import { montserrat } from "@/app/fonts";
import { cn } from "@/lib/utils";
import { formCreateSchema } from "@/lib/validations/form";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";

type Props = {
workspace: Workspace;
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function CreateFormButton({ workspace }: Readonly<Props>) {
});
},
});
const { isLoading } = createForm;
const { isPending } = createForm;

const handleCreateForm = () =>
createForm.mutateAsync({
Expand All @@ -56,12 +56,12 @@ export default function CreateFormButton({ workspace }: Readonly<Props>) {
return (
<Button
variant="secondary"
disabled={isLoading}
disabled={isPending}
onClick={handleCreateForm}
className={cn(montserrat.className, "font-semibold")}
>
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{!isLoading && <Plus className="mr-2 h-4 w-4" />}
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{!isPending && <Plus className="mr-2 h-4 w-4" />}
New Form
</Button>
);
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/mainPage/workspace/formList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { api } from "@/trpc/client";
import { api } from "@/trpc/react";
import { FormListItem } from "./formListItem";
import FormListLoading from "./formListLoading";

Expand Down
12 changes: 8 additions & 4 deletions apps/web/src/components/mainPage/workspace/formListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useQueryClient } from "@tanstack/react-query";
import { ExternalLink, Loader2, MoreVertical, Trash } from "lucide-react";

import { LinkN } from "@/components/common/linkN";
import { api } from "@/trpc/client";
import { api } from "@/trpc/react";

type Props = {
form: Form;
Expand All @@ -29,8 +29,12 @@ export function FormListItem({ form }: Readonly<Props>) {
title: "Form deleted.",
duration: 1500,
});
queryClient.invalidateQueries([["form", "getAll"]]);
queryClient.invalidateQueries([["metrics"]]);
queryClient.invalidateQueries({
queryKey: [["form", "getAll"]],
});
queryClient.invalidateQueries({
queryKey: [["metrics"]],
});
},
onError: () => {
toast({
Expand All @@ -39,7 +43,7 @@ export function FormListItem({ form }: Readonly<Props>) {
});
},
});
const isDeleting = deleteForm.isLoading;
const isDeleting = deleteForm.isPending;

const handleDeleteForm = async () =>
deleteForm.mutateAsync({
Expand Down

0 comments on commit 131c0a7

Please sign in to comment.