Skip to content

Commit

Permalink
ui: hydrate the react query cache in the client side components
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Feb 20, 2024
1 parent fb5b114 commit 7c04276
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 15 deletions.
5 changes: 2 additions & 3 deletions packages/web/app/dashboard/bookmarks/components/AddLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Plus } from "lucide-react";
import { useRouter } from "next/navigation";
import { useForm, SubmitErrorHandler } from "react-hook-form";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
Expand All @@ -17,14 +16,14 @@ const formSchema = z.object({
});

export default function AddLink() {
const router = useRouter();
const { setLoading } = useLoadingCard();
const invalidateBookmarksCache = api.useUtils().bookmarks.invalidate;
const bookmarkLinkMutator = api.bookmarks.bookmarkLink.useMutation({
onMutate: () => {
setLoading(true);
},
onSuccess: () => {
router.refresh();
invalidateBookmarksCache();
},
onError: () => {
toast({ description: "Something went wrong", variant: "destructive" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import { useToast } from "@/components/ui/use-toast";
import { api } from "@/lib/trpc";
import { ZBookmark, ZUpdateBookmarksRequest } from "@/lib/types/api/bookmarks";
import { useRouter } from "next/navigation";
import { ZBookmark } from "@/lib/types/api/bookmarks";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
Expand All @@ -15,9 +14,10 @@ import { Archive, MoreHorizontal, RotateCw, Star, Trash2 } from "lucide-react";

export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
const { toast } = useToast();
const router = useRouter();
const linkId = bookmark.id;

const invalidateBookmarksCache = api.useUtils().bookmarks.invalidate;

const onError = () => {
toast({
variant: "destructive",
Expand All @@ -26,7 +26,7 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
});
};
const onSettled = () => {
router.refresh();
invalidateBookmarksCache();
};
const deleteBookmarkMutator = api.bookmarks.deleteBookmark.useMutation({
onSuccess: () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export default async function Bookmarks({
redirect("/");
}

const bookmarks = await api.bookmarks.getBookmarks({
const query = {
favourited,
archived,
});
};

const bookmarks = await api.bookmarks.getBookmarks(query);

// TODO: This needs to be polished
return (
Expand All @@ -27,7 +29,7 @@ export default async function Bookmarks({
{bookmarks.bookmarks.length == 0 ? (
"No bookmarks"
) : (
<BookmarksGrid bookmarks={bookmarks.bookmarks} />
<BookmarksGrid query={query} bookmarks={bookmarks.bookmarks} />
)}
</div>
</>
Expand Down
15 changes: 12 additions & 3 deletions packages/web/app/dashboard/bookmarks/components/BookmarksGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { useLoadingCard } from "@/lib/hooks/use-loading-card";
import BookmarkCardSkeleton from "./BookmarkCardSkeleton";
import LinkCard from "./LinkCard";
import { ZBookmark } from "@/lib/types/api/bookmarks";
import { ZBookmark, ZGetBookmarksRequest } from "@/lib/types/api/bookmarks";
import { api } from "@/lib/trpc";

function renderBookmark(bookmark: ZBookmark) {
switch (bookmark.content.type) {
Expand All @@ -12,16 +13,24 @@ function renderBookmark(bookmark: ZBookmark) {
}
}

export const dynamic = "force-dynamic";

export default function BookmarksGrid({
bookmarks,
query,
bookmarks: initialBookmarks,
}: {
query: ZGetBookmarksRequest;
bookmarks: ZBookmark[];
}) {
const { data } = api.bookmarks.getBookmarks.useQuery(query, {
initialData: { bookmarks: initialBookmarks },
staleTime: Infinity,
});
const { loading } = useLoadingCard();
return (
<div className="container grid grid-cols-1 gap-4 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3">
{loading && <BookmarkCardSkeleton />}
{bookmarks.map((b) => renderBookmark(b))}
{data.bookmarks.map((b) => renderBookmark(b))}
</div>
);
}
1 change: 0 additions & 1 deletion packages/web/app/dashboard/settings/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Button } from "@/components/ui/button";
import ApiKeySettings from "./components/ApiKeySettings";
export default async function Settings() {
return (
Expand Down
2 changes: 2 additions & 0 deletions packages/web/lib/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, { useState } from "react";
import { api } from "./trpc";
import { loggerLink } from "@trpc/client";
import { httpBatchLink } from "@trpc/client";
import superjson from "superjson";

export default function Providers({ children }: { children: React.ReactNode }) {
const [queryClient] = React.useState(() => new QueryClient());
Expand All @@ -20,6 +21,7 @@ export default function Providers({ children }: { children: React.ReactNode }) {
httpBatchLink({
// TODO: Change this to be a full URL exposed as a client side setting
url: `/api/trpc`,
transformer: superjson,
}),
],
}),
Expand Down
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"react-dom": "^18",
"react-hook-form": "^7.50.1",
"server-only": "^0.0.1",
"superjson": "^2.2.1",
"tailwind-merge": "^2.2.1",
"tailwindcss-animate": "^1.0.7",
"zod": "^3.22.4",
Expand Down
5 changes: 4 additions & 1 deletion packages/web/server/api/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TRPCError, initTRPC } from "@trpc/server";
import { User } from "next-auth";
import superjson from "superjson";

export type Context = {
user: User | null;
Expand All @@ -9,7 +10,9 @@ export type Context = {
// since it's not very descriptive.
// For instance, the use of a t variable
// is common in i18n libraries.
const t = initTRPC.context<Context>().create();
const t = initTRPC.context<Context>().create({
transformer: superjson,
});
export const createCallerFactory = t.createCallerFactory;
// Base router and procedure helpers
export const router = t.router;
Expand Down
26 changes: 26 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,7 @@ __metadata:
react-dom: "npm:^18"
react-hook-form: "npm:^7.50.1"
server-only: "npm:^0.0.1"
superjson: "npm:^2.2.1"
tailwind-merge: "npm:^2.2.1"
tailwindcss: "npm:^3.3.0"
tailwindcss-animate: "npm:^1.0.7"
Expand Down Expand Up @@ -3534,6 +3535,15 @@ __metadata:
languageName: node
linkType: hard

"copy-anything@npm:^3.0.2":
version: 3.0.5
resolution: "copy-anything@npm:3.0.5"
dependencies:
is-what: "npm:^4.1.8"
checksum: 10c0/01eadd500c7e1db71d32d95a3bfaaedcb839ef891c741f6305ab0461398056133de08f2d1bf4c392b364e7bdb7ce498513896e137a7a183ac2516b065c28a4fe
languageName: node
linkType: hard

"cosmiconfig@npm:9.0.0":
version: 9.0.0
resolution: "cosmiconfig@npm:9.0.0"
Expand Down Expand Up @@ -5781,6 +5791,13 @@ __metadata:
languageName: node
linkType: hard

"is-what@npm:^4.1.8":
version: 4.1.16
resolution: "is-what@npm:4.1.16"
checksum: 10c0/611f1947776826dcf85b57cfb7bd3b3ea6f4b94a9c2f551d4a53f653cf0cb9d1e6518846648256d46ee6c91d114b6d09d2ac8a07306f7430c5900f87466aae5b
languageName: node
linkType: hard

"isarray@npm:^2.0.5":
version: 2.0.5
resolution: "isarray@npm:2.0.5"
Expand Down Expand Up @@ -8814,6 +8831,15 @@ __metadata:
languageName: node
linkType: hard

"superjson@npm:^2.2.1":
version: 2.2.1
resolution: "superjson@npm:2.2.1"
dependencies:
copy-anything: "npm:^3.0.2"
checksum: 10c0/5d8202c955170bd98ef2647f712754ac54d2d007923cfdb53a4b035304d8964b8c41d5eff41ee277896e2ac32e06abb009b571f1589416b729fe40216320cc7a
languageName: node
linkType: hard

"supports-color@npm:^5.3.0, supports-color@npm:^5.5.0":
version: 5.5.0
resolution: "supports-color@npm:5.5.0"
Expand Down

0 comments on commit 7c04276

Please sign in to comment.