Skip to content

Commit a03ba4e

Browse files
committed
refactor(user): 유저 쿼리 키 정리 및 헤더 컴포넌트 구조 수정
- 유저 관련 queryKey를 userKeys로 중앙화 - HeaderSearch를 Suspense로 감싸도록 헤더 구성 변경 - Header 컴포넌트에서 불필요한 async 제거 - 로그아웃 시 유저 기본 쿼리를 null로 설정하는 방식 대신 유저 관련 query 전체 제거
1 parent a6f723e commit a03ba4e

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/features/user/api/use-my-profile.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
22

33
import { getUserProfile, updateUserProfileImage, updateUserName } from "@/entities/user";
4-
import type { UserProfileType } from "@/entities/user"; // 타입 위치에 맞게 수정
4+
import type { UserProfileType } from "@/entities/user";
55
import { showToast } from "@/shared/lib/utils/toast/show-toast";
66

77
export const userKeys = {
8-
profile: (userId: number) => ["user", "profile", userId] as const,
8+
all: ["user"] as const,
9+
basic: () => [...userKeys.all, "basic"] as const,
10+
profile: (userId: number) => [...userKeys.all, "profile", userId] as const,
911
};
1012

1113
export function useUserProfile(userId: number, initialData?: UserProfileType) {

src/features/user/api/use-user-basic.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import { useQuery } from "@tanstack/react-query";
44

55
import { getUserBasic } from "@/entities/user";
6-
7-
const userBasicKey = ["user", "basic"] as const;
6+
import { userKeys } from "@/features/user/api/use-my-profile";
87

98
async function fetchUserBasic() {
109
try {
@@ -16,7 +15,7 @@ async function fetchUserBasic() {
1615

1716
export function useUserBasic() {
1817
return useQuery({
19-
queryKey: userBasicKey,
18+
queryKey: userKeys.basic(),
2019
queryFn: fetchUserBasic,
2120
retry: false,
2221
staleTime: 1000 * 60 * 5,

src/widgets/header/header.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Suspense } from "react";
2+
13
import Image from "next/image";
24
import Link from "next/link";
35

@@ -6,7 +8,7 @@ import { Container } from "@/shared/ui";
68
import HeaderAuthSlot from "@/widgets/header/ui/header-auth-slot";
79
import HeaderSearch from "@/widgets/header/ui/header-search";
810

9-
export async function Header() {
11+
export function Header() {
1012
return (
1113
<header className="bg-background h-header sticky top-0 z-50 hidden select-none md:flex">
1214
<Container className="lg:px flex items-center justify-around gap-4 px-2 min-[960px]:px-3 lg:px-5 xl:px-7">
@@ -17,7 +19,9 @@ export async function Header() {
1719
</h1>
1820
</Link>
1921

20-
<HeaderSearch />
22+
<Suspense fallback={<div className="flex-1" aria-hidden />}>
23+
<HeaderSearch />
24+
</Suspense>
2125

2226
<HeaderAuthSlot />
2327
</Container>

src/widgets/header/ui/header-user-menu.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useQueryClient } from "@tanstack/react-query";
77
import { LogOut, Moon, Sun, User } from "lucide-react";
88
import { useTheme } from "next-themes";
99

10+
import { userKeys } from "@/features/user/api/use-my-profile";
1011
import { ROUTES } from "@/shared/config/routes";
1112
import { showToast } from "@/shared/lib/utils/toast/show-toast";
1213
import {
@@ -42,7 +43,7 @@ export default function HeaderUserMenu({ avatarUrl, avatarAlt }: HeaderUserMenuP
4243
} catch {
4344
showToast.error("로그아웃에 실패했습니다. 잠시 후 다시 시도해주세요.");
4445
} finally {
45-
queryClient.setQueryData(["user", "basic"], null);
46+
queryClient.removeQueries({ queryKey: userKeys.all, exact: false });
4647
router.refresh();
4748
router.push(ROUTES.login);
4849
}

0 commit comments

Comments
 (0)