261021_01_ENH: 1차 마이그레이션 완료
This commit is contained in:
112
src/query/project.ts
Normal file
112
src/query/project.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
"use client";
|
||||
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { HttpError } from "@kri/common/http";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { isExternalUser } from "@/types/share";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
import { queryKeys } from "@/query/key/keys";
|
||||
import {
|
||||
isProject,
|
||||
isProjectSummary,
|
||||
isProgressDay,
|
||||
isProgressType,
|
||||
isLoi,
|
||||
isProgressScreen,
|
||||
readRows,
|
||||
type ProjectRef,
|
||||
type DateRange,
|
||||
} from "@/types/kart";
|
||||
|
||||
export function useProjectListQuery() {
|
||||
const { auth, hydrated } = useAuth();
|
||||
const user = auth.user;
|
||||
return useQuery({
|
||||
queryKey: queryKeys.project.list(user),
|
||||
queryFn: async () =>
|
||||
readRows(await nextApi.get<unknown>("/project/list"), isProject),
|
||||
enabled: hydrated && !!user && !isExternalUser(user),
|
||||
staleTime: 30_000,
|
||||
retry: retryRead,
|
||||
});
|
||||
}
|
||||
/** Title of a project from the user's project list (title bar, file names); pkey until known. */
|
||||
export function useProjectTitle(project: ProjectRef | null): string {
|
||||
const { auth } = useAuth();
|
||||
const projects = useProjectListQuery();
|
||||
if (!project) return "";
|
||||
if (isExternalUser(auth.user)) return auth.external?.projectTitle ?? project.pkey;
|
||||
return (
|
||||
projects.data?.find(
|
||||
(item) => item.projectType === project.projectType && item.pkey === project.pkey,
|
||||
)?.title ?? project.pkey
|
||||
);
|
||||
}
|
||||
export function useProjectSummaryQuery() {
|
||||
const { auth, hydrated } = useAuth();
|
||||
const user = auth.user;
|
||||
return useQuery({
|
||||
queryKey: queryKeys.project.summary(user),
|
||||
queryFn: async () =>
|
||||
readRows(
|
||||
await nextApi.get<unknown>("/project/summary"),
|
||||
isProjectSummary,
|
||||
),
|
||||
enabled: hydrated && !!user && !isExternalUser(user),
|
||||
staleTime: 30_000,
|
||||
retry: retryRead,
|
||||
});
|
||||
}
|
||||
export function useProgressQuery(project: ProjectRef, range: DateRange) {
|
||||
const { auth, hydrated } = useAuth();
|
||||
const user = auth.user;
|
||||
const path =
|
||||
"/project/" +
|
||||
project.projectType +
|
||||
"/" +
|
||||
encodeURIComponent(project.pkey) +
|
||||
"/progress/";
|
||||
const query = range.startDate ? range : undefined;
|
||||
const enabled = hydrated && !!user;
|
||||
const common = { enabled, staleTime: 30_000, retry: retryRead };
|
||||
const daily = useQuery({
|
||||
...common,
|
||||
queryKey: queryKeys.project.progressDay(user, project),
|
||||
queryFn: async () =>
|
||||
readRows(await nextApi.get<unknown>(path + "day"), isProgressDay),
|
||||
});
|
||||
const type = useQuery({
|
||||
...common,
|
||||
queryKey: queryKeys.project.progressType(user, project, range),
|
||||
queryFn: async () =>
|
||||
readRows(
|
||||
await nextApi.get<unknown>(path + "type", { query }),
|
||||
isProgressType,
|
||||
),
|
||||
});
|
||||
const loi = useQuery({
|
||||
...common,
|
||||
queryKey: queryKeys.project.progressLoi(user, project, range),
|
||||
queryFn: async () =>
|
||||
readRows(await nextApi.get<unknown>(path + "loi", { query }), isLoi),
|
||||
});
|
||||
const screen = useQuery({
|
||||
...common,
|
||||
queryKey: queryKeys.project.progressScreen(user, project, range),
|
||||
queryFn: async () =>
|
||||
readRows(
|
||||
await nextApi.get<unknown>(path + "screen", { query }),
|
||||
isProgressScreen,
|
||||
),
|
||||
});
|
||||
return { daily, type, loi, screen };
|
||||
}
|
||||
function retryRead(failures: number, error: Error): boolean {
|
||||
return (
|
||||
!(
|
||||
error instanceof HttpError &&
|
||||
error.status >= 400 &&
|
||||
error.status < 500
|
||||
) && failures < 1
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user