Skip to content

Commit

Permalink
fix: Fix hydration in list view caused by the spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Mar 2, 2024
1 parent 012cef2 commit 29b7c5c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { ZBookmark, ZGetBookmarksRequest } from "@/lib/types/api/bookmarks";
import { api } from "@/lib/trpc";
import TextCard from "./TextCard";
import { Slot } from "@radix-ui/react-slot";
import Masonry from 'react-masonry-css';
import resolveConfig from 'tailwindcss/resolveConfig'
import tailwindConfig from '@/tailwind.config'
import Masonry from "react-masonry-css";
import resolveConfig from "tailwindcss/resolveConfig";
import tailwindConfig from "@/tailwind.config";
import { useMemo } from "react";

function getBreakpointConfig() {
const fullConfig = resolveConfig(tailwindConfig)
const fullConfig = resolveConfig(tailwindConfig);

const breakpointColumnsObj: {[key: number]: number, default: number} = {
const breakpointColumnsObj: { [key: number]: number; default: number } = {
default: 3,
};
breakpointColumnsObj[parseInt(fullConfig.theme.screens.lg)] = 2;
Expand All @@ -22,7 +22,6 @@ function getBreakpointConfig() {
return breakpointColumnsObj;
}


function renderBookmark(bookmark: ZBookmark) {
let comp;
switch (bookmark.content.type) {
Expand Down Expand Up @@ -58,7 +57,7 @@ export default function BookmarksGrid({
return <p>No bookmarks</p>;
}
return (
<Masonry className="flex gap-4" breakpointCols={breakpointConfig} >
<Masonry className="flex gap-4" breakpointCols={breakpointConfig}>
{data.bookmarks.map((b) => renderBookmark(b))}
</Masonry>
);
Expand Down
12 changes: 8 additions & 4 deletions packages/web/app/dashboard/components/AllLists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import { api } from "@/lib/trpc";
import SidebarItem from "./SidebarItem";
import LoadingSpinner from "@/components/ui/spinner";
import NewListModal, { useNewListModal } from "./NewListModal";
import { Plus } from "lucide-react";
import Link from "next/link";
import { ZBookmarkList } from "@/lib/types/api/lists";

export default function AllLists({initialData}: {initialData: {lists: ZBookmarkList[]}}) {
const { data: lists } = api.lists.list.useQuery(undefined, {
export default function AllLists({
initialData,
}: {
initialData: { lists: ZBookmarkList[] };
}) {
let { data: lists } = api.lists.list.useQuery(undefined, {
initialData,
});

// TODO: This seems to be a bug in react query
lists ||= initialData;
const { setOpen } = useNewListModal();

return (
Expand Down
8 changes: 3 additions & 5 deletions packages/web/app/dashboard/lists/components/AllListsView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";

import { Button } from "@/components/ui/button";
import LoadingSpinner from "@/components/ui/spinner";
import { api } from "@/lib/trpc";
import { ZBookmarkList } from "@/lib/types/api/lists";
import { keepPreviousData } from "@tanstack/react-query";
Expand Down Expand Up @@ -35,14 +34,13 @@ export default function AllListsView({
initialData: ZBookmarkList[];
}) {
const { setOpen: setIsNewListModalOpen } = useNewListModal();
const { data: lists } = api.lists.list.useQuery(undefined, {
let { data: lists } = api.lists.list.useQuery(undefined, {
initialData: { lists: initialData },
placeholderData: keepPreviousData,
});

if (!lists) {
return <LoadingSpinner />;
}
// TODO: This seems to be a bug in react query
lists ||= { lists: initialData };

return (
<div className="flex flex-col flex-wrap gap-2 md:flex-row">
Expand Down
30 changes: 29 additions & 1 deletion packages/web/lib/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,42 @@ import superjson from "superjson";
import { SessionProvider } from "next-auth/react";
import { Session } from "next-auth";

function makeQueryClient() {
return new QueryClient({
defaultOptions: {
queries: {
// With SSR, we usually want to set some default staleTime
// above 0 to avoid refetching immediately on the client
staleTime: 60 * 1000,
},
},
});
}

let browserQueryClient: QueryClient | undefined = undefined;

function getQueryClient() {
if (typeof window === "undefined") {
// Server: always make a new query client
return makeQueryClient();
} else {
// Browser: make a new query client if we don't already have one
// This is very important so we don't re-make a new client if React
// supsends during the initial render. This may not be needed if we
// have a suspense boundary BELOW the creation of the query client
if (!browserQueryClient) browserQueryClient = makeQueryClient();
return browserQueryClient;
}
}

export default function Providers({
children,
session,
}: {
children: React.ReactNode;
session: Session | null;
}) {
const [queryClient] = React.useState(() => new QueryClient());
const queryClient = getQueryClient();

const [trpcClient] = useState(() =>
api.createClient({
Expand Down

0 comments on commit 29b7c5c

Please sign in to comment.