mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-11 00:33:04 +00:00
fix: links rerender when switching category
This commit is contained in:
@@ -6,7 +6,6 @@ import LinkItem from "./LinkItem";
|
|||||||
import styles from "./links.module.scss";
|
import styles from "./links.module.scss";
|
||||||
|
|
||||||
export default function Links({ category }: { category: Category }) {
|
export default function Links({ category }: { category: Category }) {
|
||||||
console.log("render", category);
|
|
||||||
if (category === null) {
|
if (category === null) {
|
||||||
return (
|
return (
|
||||||
<div className={styles["no-category"]}>
|
<div className={styles["no-category"]}>
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ export default function CategoryItem({
|
|||||||
}, [category.id, categoryActive.id]);
|
}, [category.id, categoryActive.id]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li className={className} ref={ref}>
|
<li className={className} ref={ref} onClick={onClick}>
|
||||||
<div className={styles["content"]} onClick={onClick}>
|
<div className={styles["content"]}>
|
||||||
<span className={styles["name"]}>{category.name}</span>
|
<span className={styles["name"]}>{category.name}</span>
|
||||||
<span className={styles["links-count"]}>— {category.links.length}</span>
|
<span className={styles["links-count"]}>— {category.links.length}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { Session } from "next-auth";
|
|
||||||
import LinkTag from "next/link";
|
import LinkTag from "next/link";
|
||||||
|
|
||||||
import BlockWrapper from "../BlockWrapper/BlockWrapper";
|
import BlockWrapper from "../BlockWrapper/BlockWrapper";
|
||||||
@@ -15,14 +14,12 @@ interface SideMenuProps {
|
|||||||
favorites: Link[];
|
favorites: Link[];
|
||||||
handleSelectCategory: (category: Category) => void;
|
handleSelectCategory: (category: Category) => void;
|
||||||
categoryActive: Category;
|
categoryActive: Category;
|
||||||
session: Session;
|
|
||||||
}
|
}
|
||||||
export default function SideMenu({
|
export default function SideMenu({
|
||||||
categories,
|
categories,
|
||||||
favorites,
|
favorites,
|
||||||
handleSelectCategory,
|
handleSelectCategory,
|
||||||
categoryActive,
|
categoryActive,
|
||||||
session,
|
|
||||||
}: SideMenuProps) {
|
}: SideMenuProps) {
|
||||||
return (
|
return (
|
||||||
<div className={styles["side-menu"]}>
|
<div className={styles["side-menu"]}>
|
||||||
@@ -40,7 +37,7 @@ export default function SideMenu({
|
|||||||
<MenuControls categoryActive={categoryActive} />
|
<MenuControls categoryActive={categoryActive} />
|
||||||
</BlockWrapper>
|
</BlockWrapper>
|
||||||
<BlockWrapper>
|
<BlockWrapper>
|
||||||
<UserCard session={session} />
|
<UserCard />
|
||||||
</BlockWrapper>
|
</BlockWrapper>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
import { Session } from "next-auth";
|
import { signOut, useSession } from "next-auth/react";
|
||||||
import { signOut } from "next-auth/react";
|
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { FiLogOut } from "react-icons/fi";
|
import { FiLogOut } from "react-icons/fi";
|
||||||
|
|
||||||
import styles from "./user-card.module.scss";
|
import styles from "./user-card.module.scss";
|
||||||
|
|
||||||
export default function UserCard({ session }: { session: Session }) {
|
export default function UserCard() {
|
||||||
|
const { data } = useSession({ required: true });
|
||||||
return (
|
return (
|
||||||
<div className={styles["user-card-wrapper"]}>
|
<div className={styles["user-card-wrapper"]}>
|
||||||
<div className={styles["user-card"]}>
|
<div className={styles["user-card"]}>
|
||||||
<Image
|
<Image
|
||||||
src={session.user.image}
|
src={data.user.image}
|
||||||
width={28}
|
width={28}
|
||||||
height={28}
|
height={28}
|
||||||
alt={`${session.user.name}'s avatar`}
|
alt={`${data.user.name}'s avatar`}
|
||||||
/>
|
/>
|
||||||
{session.user.name}
|
{data.user.name}
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
onClick={() => signOut({ callbackUrl: "/signin" })}
|
onClick={() => signOut({ callbackUrl: "/signin" })}
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
/** @type {import('next').NextConfig} */
|
/** @type {import('next').NextConfig} */
|
||||||
const config = {
|
const config = {
|
||||||
webpack(config) {
|
webpack(config) {
|
||||||
config.module.rules.push({
|
config.module.rules.push({
|
||||||
test: /\.svg$/,
|
test: /\.svg$/,
|
||||||
use: ["@svgr/webpack"],
|
use: ["@svgr/webpack"],
|
||||||
});
|
});
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
},
|
},
|
||||||
images: {
|
images: {
|
||||||
domains: ["localhost", "t3.gstatic.com", "lh3.googleusercontent.com"],
|
domains: ["localhost", "t3.gstatic.com", "lh3.googleusercontent.com"],
|
||||||
formats: ["image/webp"],
|
formats: ["image/webp"],
|
||||||
},
|
},
|
||||||
|
reactStrictMode: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = config;
|
module.exports = config;
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import { useSession } from "next-auth/react";
|
import { useRouter } from "next/router";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
import Links from "../components/Links/Links";
|
import Links from "../components/Links/Links";
|
||||||
import SideMenu from "../components/SideMenu/SideMenu";
|
import SideMenu from "../components/SideMenu/SideMenu";
|
||||||
|
|
||||||
import { Category, Link } from "../types";
|
import { Category, Link } from "../types";
|
||||||
|
|
||||||
import { useRouter } from "next/router";
|
|
||||||
import { prisma } from "../utils/back";
|
import { prisma } from "../utils/back";
|
||||||
import { BuildCategory } from "../utils/front";
|
import { BuildCategory } from "../utils/front";
|
||||||
|
|
||||||
@@ -18,7 +16,6 @@ interface HomeProps {
|
|||||||
|
|
||||||
function Home({ categories, favorites, currentCategory }: HomeProps) {
|
function Home({ categories, favorites, currentCategory }: HomeProps) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { data } = useSession({ required: true });
|
|
||||||
|
|
||||||
const [categoryActive, setCategoryActive] = useState<Category | null>(
|
const [categoryActive, setCategoryActive] = useState<Category | null>(
|
||||||
currentCategory || categories?.[0]
|
currentCategory || categories?.[0]
|
||||||
@@ -26,7 +23,12 @@ function Home({ categories, favorites, currentCategory }: HomeProps) {
|
|||||||
|
|
||||||
const handleSelectCategory = (category: Category) => {
|
const handleSelectCategory = (category: Category) => {
|
||||||
setCategoryActive(category);
|
setCategoryActive(category);
|
||||||
router.push(`/?categoryId=${category.id}`);
|
const newUrl = `/?categoryId=${category.id}`;
|
||||||
|
window.history.replaceState(
|
||||||
|
{ ...window.history.state, as: newUrl, url: newUrl },
|
||||||
|
"",
|
||||||
|
newUrl
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -36,7 +38,6 @@ function Home({ categories, favorites, currentCategory }: HomeProps) {
|
|||||||
favorites={favorites}
|
favorites={favorites}
|
||||||
handleSelectCategory={handleSelectCategory}
|
handleSelectCategory={handleSelectCategory}
|
||||||
categoryActive={categoryActive}
|
categoryActive={categoryActive}
|
||||||
session={data}
|
|
||||||
/>
|
/>
|
||||||
<Links category={categoryActive} />
|
<Links category={categoryActive} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user