261102_03_FIX: 프로젝트 전환 시 목록 조회 쿼리 취소 문제 수정

This commit is contained in:
2026-11-02 16:19:50 +09:00
parent cbd0a559e9
commit c2a26af0dc
4 changed files with 31 additions and 6 deletions

View File

@@ -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. - 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. - 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. - 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. - 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. - 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. - Preserve public component props and shared UI contracts unless a coordinated breaking change is explicitly approved.

View File

@@ -6,9 +6,9 @@ import type { Route } from "next";
import { useStore } from "jotai"; import { useStore } from "jotai";
import { useQueryClient } from "@tanstack/react-query"; import { useQueryClient } from "@tanstack/react-query";
import { authStateAtom, useAuth } from "@/hooks/useAuth"; import { authStateAtom, useAuth } from "@/hooks/useAuth";
import { useSelectedProject } from "@/hooks/useSelectedProject"; import { selectedProjectAtom, useSelectedProject } from "@/hooks/useSelectedProject";
import { projectSelectionAtom } from "@/hooks/useProjectNavigation"; import { projectSelectionAtom } from "@/hooks/useProjectNavigation";
import { checkProjectAccess } from "@/query/project"; import { cancelPreviousProjectQueries, checkProjectAccess } from "@/query/project";
import { isProjectPage } from "@/lib/report-navigation"; import { isProjectPage } from "@/lib/report-navigation";
import { import {
announceProjectAccess, PROJECT_ACCESS_EVENT, projectAccessMessage, projectFromSearch, announceProjectAccess, PROJECT_ACCESS_EVENT, projectAccessMessage, projectFromSearch,
@@ -50,7 +50,8 @@ export function ProjectEntry({ children }: { children: ReactNode }) {
if (!candidate) throw new Error("projectType과 pkey를 올바르게 지정해 주세요."); if (!candidate) throw new Error("projectType과 pkey를 올바르게 지정해 주세요.");
await checkProjectAccess(candidate); await checkProjectAccess(candidate);
if (!current()) return; if (!current()) return;
await queryClient.cancelQueries(); if (!auth.user) return;
await cancelPreviousProjectQueries(queryClient, auth.user, store.get(selectedProjectAtom), candidate);
if (!current()) return; if (!current()) return;
select(candidate); select(candidate);
announceProjectAccess({ project: candidate, allowed: true }); announceProjectAccess({ project: candidate, allowed: true });

View File

@@ -8,7 +8,7 @@ import { toast } from "sonner";
import { useQueryClient } from "@tanstack/react-query"; import { useQueryClient } from "@tanstack/react-query";
import { authStateAtom } from "@/hooks/useAuth"; import { authStateAtom } from "@/hooks/useAuth";
import { selectedProjectAtom } from "@/hooks/useSelectedProject"; import { selectedProjectAtom } from "@/hooks/useSelectedProject";
import { checkProjectAccess } from "@/query/project"; import { cancelPreviousProjectQueries, checkProjectAccess } from "@/query/project";
import { announceProjectAccess, projectAccessMessage } from "@/lib/project-access"; import { announceProjectAccess, projectAccessMessage } from "@/lib/project-access";
import { isExternalUser } from "@/types/share"; import { isExternalUser } from "@/types/share";
import type { ProjectRef } from "@/types/kart"; import type { ProjectRef } from "@/types/kart";
@@ -37,7 +37,7 @@ export function useProjectNavigation() {
try { try {
await checkProjectAccess(project); await checkProjectAccess(project);
if (!current()) return; if (!current()) return;
await queryClient.cancelQueries(); await cancelPreviousProjectQueries(queryClient, auth.user, store.get(selectedProjectAtom), project);
if (!current()) return; if (!current()) return;
store.set(selectedProjectAtom, { userId: auth.user.userId, project: { projectType: project.projectType, pkey: project.pkey } }); store.set(selectedProjectAtom, { userId: auth.user.userId, project: { projectType: project.projectType, pkey: project.pkey } });
announceProjectAccess({ project, allowed: true }); announceProjectAccess({ project, allowed: true });

View File

@@ -1,6 +1,8 @@
"use client"; "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 { HttpError } from "@kri/common/http";
import { useAuth } from "@/hooks/useAuth"; import { useAuth } from "@/hooks/useAuth";
import { isExternalUser } from "@/types/share"; import { isExternalUser } from "@/types/share";
@@ -19,6 +21,27 @@ import {
type DateRange, type DateRange,
} from "@/types/kart"; } from "@/types/kart";
export async function cancelPreviousProjectQueries(
queryClient: QueryClient,
user: LoginUser,
selected: SelectedProject,
next: ProjectRef,
): Promise<void> {
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. */ /** Always check the server; a cached permission is not an authorization decision. */
export async function checkProjectAccess(project: ProjectRef): Promise<void> { export async function checkProjectAccess(project: ProjectRef): Promise<void> {
const result = await nextApi.get<unknown>( const result = await nextApi.get<unknown>(