diff --git a/src/components/list/ListJobGate.tsx b/src/components/list/ListJobGate.tsx index 89b9e6a..3dd6a4e 100644 --- a/src/components/list/ListJobGate.tsx +++ b/src/components/list/ListJobGate.tsx @@ -2,11 +2,10 @@ import { useEffect } from 'react' import { useAtom } from 'jotai' -import { HttpError } from '@kri/common/http' import { Button } from '@/components/ui/button' import { ListJobProgress } from '@/components/list/ListJobProgress' import { useAuth } from '@/hooks/useAuth' -import { isAvailabilityUnsupported, useListJob, useListJobAvailability, useUploadResetActive } from '@/query/list-job' +import { isAvailabilityUnsupported, isListJobGone, useListJob, useListJobAvailability, useUploadResetActive } from '@/query/list-job' import { findTrackedListJob, trackListJob, untrackListJob, useTrackedListJobs } from '@/store/list-job-tracker' import { listJobLabel } from '@/const/listJobConst' import { blockerJobLabel, blockerPhaseLabel, blockerUserLabel, describeListJobAvailability } from '@/utils/listJobAvailability' @@ -45,7 +44,7 @@ export function useListJobGate({ prjctIdx, jobType, active, requestLabel, pollWh const detail = useListJob(prjctIdx, jobKey, active) const job = detail.data?.jobKey === jobKey ? detail.data : undefined const finishedJob = job && !isActiveListJob(job.jobStatus) ? job : undefined - const detailNotFound = detail.error instanceof HttpError && detail.error.status === 404 + const detailNotFound = isListJobGone(detail.error) useEffect(() => { if (!active && requestMatches && openRequest.opened) setOpenRequest(null) diff --git a/src/components/list/ListJobWatcher.tsx b/src/components/list/ListJobWatcher.tsx index 643f0c5..9984c58 100644 --- a/src/components/list/ListJobWatcher.tsx +++ b/src/components/list/ListJobWatcher.tsx @@ -4,10 +4,9 @@ import { useEffect } from 'react' import { usePathname, useRouter } from 'next/navigation' import { useAtom } from 'jotai' import { useQueries, useQueryClient } from '@tanstack/react-query' -import { HttpError } from '@kri/common/http' import { toast } from 'sonner' import { nextApi } from '@/lib/http-client' -import { listJobKeys, useMyActiveListJobs } from '@/query/list-job' +import { isListJobGone, listJobKeys, useMyActiveListJobs } from '@/query/list-job' import { listJobOpenRequestAtom } from '@/store/jotai/list-job' import { useSelectedProject } from '@/hooks/useSelectedProject' import { useDialog } from '@/hooks/useDialog' @@ -90,7 +89,7 @@ export function ListJobWatcher({ userId, prjctIdx }: { userId: string; prjctIdx: details.forEach((result, index) => { const tracked = trackedJobs[index] if (!tracked) return - if (result.error instanceof HttpError && result.error.status === 404) { + if (isListJobGone(result.error)) { untrackListJob(tracked.jobKey) return } diff --git a/src/query/auth.ts b/src/query/auth.ts index 7b45123..340344b 100644 --- a/src/query/auth.ts +++ b/src/query/auth.ts @@ -1,9 +1,12 @@ "use client"; import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { useSetAtom } from "jotai"; import { nextApi } from "@/lib/http-client"; import { useAuth } from "@/hooks/useAuth"; import { useSelectedProject } from "@/hooks/useSelectedProject"; +import { clearTrackedListJobs } from "@/store/list-job-tracker"; +import { listJobOpenRequestAtom } from "@/store/jotai/list-job"; import type { LoginUser } from "@kri/common"; export const useLoginMutation = () => { @@ -31,6 +34,7 @@ export const useLogoutMutation = () => { const qc = useQueryClient(); const { auth, logout } = useAuth(); const { clearSelectedProject } = useSelectedProject(); + const setListJobOpenRequest = useSetAtom(listJobOpenRequestAtom); return useMutation({ mutationFn: async () => { @@ -49,6 +53,8 @@ export const useLogoutMutation = () => { }, onSettled: () => { clearSelectedProject(); + clearTrackedListJobs(); + setListJobOpenRequest(null); logout(); qc.clear(); // 다음 사용자/세션에 서버상태 남지 않게 }, diff --git a/src/query/list-job.ts b/src/query/list-job.ts index 21a737b..6c2af7e 100644 --- a/src/query/list-job.ts +++ b/src/query/list-job.ts @@ -38,6 +38,14 @@ const fetchAvailability = async (prjctIdx: number, jobType: ListJobType) => /** 신규 API 미배포·롤백 상태. 반복 호출하지 않고 사전 안내만 중단한다. */ export const isAvailabilityUnsupported = (error: unknown) => error instanceof HttpError && error.status === 404 +/** + * 작업을 더 이상 조회할 수 없는 경우. 기록을 지우고 반복 조회를 멈춘다. + * - 404: 작업이 만료됐거나 내 작업이 아니다. + * - 403 PROJECT_ACCESS_DENIED: 프로젝트가 삭제됐거나 접근 권한·배정·상태가 바뀌었다. + */ +export const isListJobGone = (error: unknown) => error instanceof HttpError + && (error.status === 404 || (error.status === 403 && apiError(error).code === 'PROJECT_ACCESS_DENIED')) + export function useSubmitListJob(prjctIdx: number) { const qc = useQueryClient() const { auth } = useAuth() diff --git a/src/store/list-job-tracker.ts b/src/store/list-job-tracker.ts index 845de29..457ac54 100644 --- a/src/store/list-job-tracker.ts +++ b/src/store/list-job-tracker.ts @@ -1,7 +1,7 @@ 'use client' import { useSyncExternalStore } from 'react' -import { getLocalStorage, setLocalStorage } from '@/store/storage/secure-storage' +import { getLocalStorage, removeLocalStorage, setLocalStorage } from '@/store/storage/secure-storage' import { StorageKeys } from '@/store/storage/storage-keys' import { isListJobType, type ListJobType } from '@/types/list-job' @@ -77,6 +77,14 @@ export const untrackListJob = (jobKey: string) => { write(jobs.filter((job) => job.jobKey !== jobKey)) } +/** 로그아웃 시 다음 사용자에게 기록이 남지 않게 모두 지운다. 진행 중인 작업은 재로그인 후 my-active 조회로 다시 채워진다. */ +export const clearTrackedListJobs = () => { + if (typeof window === 'undefined') return + removeLocalStorage(StorageKeys.ListJobs) + cache = EMPTY + window.dispatchEvent(new Event(CHANGE_EVENT)) +} + export const findTrackedListJob = (jobs: TrackedListJob[], userId: string, prjctIdx: number, jobType: ListJobType) => jobs.find((job) => job.userId === userId && job.prjctIdx === prjctIdx && job.jobType === jobType)