diff --git a/AGENTS.md b/AGENTS.md index ea8c723..728a888 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -229,6 +229,7 @@ When implementation is complete, report in Korean: - Treat the current React version, component style, state management, routing, styling, and test approach as authoritative. - Do not perform unrelated class-to-hook, JavaScript-to-TypeScript, state-library, styling-system, or React-to-Next.js migrations. - Keep components focused and follow the repository's established split between presentation, state, data access, and side effects. +- Do not use nested ternary expressions. Use clear `if`/`else` branches, early returns, or well-named intermediate variables instead; a single, non-nested ternary is acceptable only when it remains immediately readable. - Keep effects dependency-correct and clean up subscriptions, timers, listeners, and in-flight work where applicable. - Use stable semantic keys and preserve accessibility, loading, empty, error, disabled, and success states. - Preserve public component props and shared UI contracts unless a coordinated breaking change is explicitly approved. diff --git a/src/components/project/ProjectEntry.tsx b/src/components/project/ProjectEntry.tsx index a422ede..46cf481 100644 --- a/src/components/project/ProjectEntry.tsx +++ b/src/components/project/ProjectEntry.tsx @@ -6,9 +6,9 @@ import type { Route } from "next"; import { useStore } from "jotai"; import { useQueryClient } from "@tanstack/react-query"; import { authStateAtom, useAuth } from "@/hooks/useAuth"; -import { useSelectedProject } from "@/hooks/useSelectedProject"; +import { selectedProjectAtom, useSelectedProject } from "@/hooks/useSelectedProject"; import { projectSelectionAtom } from "@/hooks/useProjectNavigation"; -import { checkProjectAccess } from "@/query/project"; +import { cancelPreviousProjectQueries, checkProjectAccess } from "@/query/project"; import { isProjectPage } from "@/lib/report-navigation"; import { announceProjectAccess, PROJECT_ACCESS_EVENT, projectAccessMessage, projectFromSearch, @@ -50,7 +50,8 @@ export function ProjectEntry({ children }: { children: ReactNode }) { if (!candidate) throw new Error("projectType과 pkey를 올바르게 지정해 주세요."); await checkProjectAccess(candidate); if (!current()) return; - await queryClient.cancelQueries(); + if (!auth.user) return; + await cancelPreviousProjectQueries(queryClient, auth.user, store.get(selectedProjectAtom), candidate); if (!current()) return; select(candidate); announceProjectAccess({ project: candidate, allowed: true }); diff --git a/src/hooks/useProjectNavigation.ts b/src/hooks/useProjectNavigation.ts index 5f25267..0cf31c1 100644 --- a/src/hooks/useProjectNavigation.ts +++ b/src/hooks/useProjectNavigation.ts @@ -8,7 +8,7 @@ import { toast } from "sonner"; import { useQueryClient } from "@tanstack/react-query"; import { authStateAtom } from "@/hooks/useAuth"; import { selectedProjectAtom } from "@/hooks/useSelectedProject"; -import { checkProjectAccess } from "@/query/project"; +import { cancelPreviousProjectQueries, checkProjectAccess } from "@/query/project"; import { announceProjectAccess, projectAccessMessage } from "@/lib/project-access"; import { isExternalUser } from "@/types/share"; import type { ProjectRef } from "@/types/kart"; @@ -37,7 +37,7 @@ export function useProjectNavigation() { try { await checkProjectAccess(project); if (!current()) return; - await queryClient.cancelQueries(); + await cancelPreviousProjectQueries(queryClient, auth.user, store.get(selectedProjectAtom), project); if (!current()) return; store.set(selectedProjectAtom, { userId: auth.user.userId, project: { projectType: project.projectType, pkey: project.pkey } }); announceProjectAccess({ project, allowed: true }); diff --git a/src/query/project.ts b/src/query/project.ts index 5c7e517..081bdd3 100644 --- a/src/query/project.ts +++ b/src/query/project.ts @@ -1,6 +1,8 @@ "use client"; -import { useQuery } from "@tanstack/react-query"; +import { useQuery, type QueryClient } from "@tanstack/react-query"; +import type { LoginUser } from "@kri/common"; +import type { SelectedProject } from "@/hooks/useSelectedProject"; import { HttpError } from "@kri/common/http"; import { useAuth } from "@/hooks/useAuth"; import { isExternalUser } from "@/types/share"; @@ -19,6 +21,27 @@ import { type DateRange, } from "@/types/kart"; +export async function cancelPreviousProjectQueries( + queryClient: QueryClient, + user: LoginUser, + selected: SelectedProject, + next: ProjectRef, +): Promise { + if (!selected || selected.userId !== user.userId) return; + const previous = selected.project; + if (previous.projectType === next.projectType && previous.pkey === next.pkey) return; + + await queryClient.cancelQueries({ + predicate: ({ queryKey }) => { + if (queryKey[0] !== "kart" || queryKey[1] !== user.userId || + queryKey[2] !== user.userRole || queryKey[3] !== user.userTeam) return false; + // Project-scoped keys put type/pkey at positions 4/5 or 5/6. + return (queryKey[4] === previous.projectType && queryKey[5] === previous.pkey) || + (queryKey[5] === previous.projectType && queryKey[6] === previous.pkey); + }, + }); +} + /** Always check the server; a cached permission is not an authorization decision. */ export async function checkProjectAccess(project: ProjectRef): Promise { const result = await nextApi.get(