diff --git a/packages/web/app/dashboard/bookmarks/components/AddLink.tsx b/packages/web/app/dashboard/bookmarks/components/AddLink.tsx index 6498b313..7663543f 100644 --- a/packages/web/app/dashboard/bookmarks/components/AddLink.tsx +++ b/packages/web/app/dashboard/bookmarks/components/AddLink.tsx @@ -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"; @@ -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" }); diff --git a/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx b/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx index 1360f966..a72478c1 100644 --- a/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx +++ b/packages/web/app/dashboard/bookmarks/components/BookmarkOptions.tsx @@ -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, @@ -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", @@ -26,7 +26,7 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) { }); }; const onSettled = () => { - router.refresh(); + invalidateBookmarksCache(); }; const deleteBookmarkMutator = api.bookmarks.deleteBookmark.useMutation({ onSuccess: () => { diff --git a/packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx b/packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx index 0afb42bd..44495f1f 100644 --- a/packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx +++ b/packages/web/app/dashboard/bookmarks/components/Bookmarks.tsx @@ -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 ( @@ -27,7 +29,7 @@ export default async function Bookmarks({ {bookmarks.bookmarks.length == 0 ? ( "No bookmarks" ) : ( - + )} diff --git a/packages/web/app/dashboard/bookmarks/components/BookmarksGrid.tsx b/packages/web/app/dashboard/bookmarks/components/BookmarksGrid.tsx index 1963a980..c1c8f3e0 100644 --- a/packages/web/app/dashboard/bookmarks/components/BookmarksGrid.tsx +++ b/packages/web/app/dashboard/bookmarks/components/BookmarksGrid.tsx @@ -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) { @@ -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 (
{loading && } - {bookmarks.map((b) => renderBookmark(b))} + {data.bookmarks.map((b) => renderBookmark(b))}
); } diff --git a/packages/web/app/dashboard/settings/page.tsx b/packages/web/app/dashboard/settings/page.tsx index e8799583..95637d8c 100644 --- a/packages/web/app/dashboard/settings/page.tsx +++ b/packages/web/app/dashboard/settings/page.tsx @@ -1,4 +1,3 @@ -import { Button } from "@/components/ui/button"; import ApiKeySettings from "./components/ApiKeySettings"; export default async function Settings() { return ( diff --git a/packages/web/lib/providers.tsx b/packages/web/lib/providers.tsx index e81645dd..d14d4d96 100644 --- a/packages/web/lib/providers.tsx +++ b/packages/web/lib/providers.tsx @@ -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()); @@ -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, }), ], }), diff --git a/packages/web/package.json b/packages/web/package.json index 724845f5..8ec38a0b 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -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", diff --git a/packages/web/server/api/trpc.ts b/packages/web/server/api/trpc.ts index 1f4eb775..7c4af452 100644 --- a/packages/web/server/api/trpc.ts +++ b/packages/web/server/api/trpc.ts @@ -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; @@ -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().create(); +const t = initTRPC.context().create({ + transformer: superjson, +}); export const createCallerFactory = t.createCallerFactory; // Base router and procedure helpers export const router = t.router; diff --git a/yarn.lock b/yarn.lock index 9469fc37..73e2c171 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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"