Skip to content

Commit

Permalink
fix(bookmarks-list): resolved local storage access during ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
JaleelB committed Aug 2, 2024
1 parent 8371aed commit 290bb52
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
26 changes: 15 additions & 11 deletions app/bookmarks/components/bookmark-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@ import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { AvatarImage } from "@radix-ui/react-avatar";
import Link from "next/link";
import Cookies from "js-cookie";
import { cn, formatDate } from "@/lib/utils";
import { cn, formatDate, getLocalStorageItem } from "@/lib/utils";
import { v4 as uuidv4 } from "uuid";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Bookmark } from "../bookmark-wrapper";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import * as cheerio from "cheerio";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { BookmarksSearchBox } from "./bookmarks-search-box";
import { BookmarksDisplayMenu } from "./bookmarks-display";
import { BookmarkButton } from "./bookmark-button";
Expand Down Expand Up @@ -72,12 +69,19 @@ export default function BookmarksList({
const searchParams = useSearchParams();
const searchTerm = searchParams.get("search") || "";

const [layout, setLayout] = useState<"grid" | "rows">(() => {
return (localStorage.getItem("layout") as "grid" | "rows") || "grid";
});
const [orderBy, setOrderBy] = useState<OrderBy>(() => {
return (localStorage.getItem("orderBy") as OrderBy) || "date";
});
// const [layout, setLayout] = useState<"grid" | "rows">(() => {
// return (localStorage.getItem("layout") as "grid" | "rows") || "grid";
// });
// const [orderBy, setOrderBy] = useState<OrderBy>(() => {
// return (localStorage.getItem("orderBy") as OrderBy) || "date";
// });
const [layout, setLayout] = useState<"grid" | "rows">("grid");
const [orderBy, setOrderBy] = useState<OrderBy>("date");

useEffect(() => {
setLayout(getLocalStorageItem("layout", "grid") as "grid" | "rows");
setOrderBy(getLocalStorageItem("orderBy", "date") as OrderBy);
}, []);

useEffect(() => {
localStorage.setItem("layout", layout);
Expand Down
13 changes: 13 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ export function calculateReadTime(text: string[] | string | undefined): string {
return `${roundedMinutes} min read`;
}

export function getLocalStorageItem(key: string, defaultValue: string): string {
if (typeof window !== "undefined") {
return localStorage.getItem(key) || defaultValue;
}
return defaultValue;
}

export function setLocalStorageItem(key: string, value: string): void {
if (typeof window !== "undefined") {
localStorage.setItem(key, value);
}
}

export function formatDate(dateStr: string | null): string {
let date: Date;

Expand Down

0 comments on commit 290bb52

Please sign in to comment.