refactor: 템플릿에서 기본적으로 제공하고 있는 워크 플로우에 디자인과 라우팅 흡수
This commit is contained in:
+265
@@ -0,0 +1,265 @@
|
||||
import { useEffect, useState, type FormEvent } from "react";
|
||||
import { useSearchParams } from "react-router-dom";
|
||||
|
||||
import type { components } from "../../../contracts/studio/generated.ts";
|
||||
import type { DocumentPage } from "../../../contracts/studio/contract.ts";
|
||||
import type { ListDocumentsQuery } from "../../../application/studio-application.ts";
|
||||
import { useManagementCommands } from "../studio-management-server-state.ts";
|
||||
import {
|
||||
useStudioCatalogQuery,
|
||||
useStudioDocumentsQuery,
|
||||
} from "../studio-workspace-server-state.ts";
|
||||
import {
|
||||
useManagementFailureMessage,
|
||||
useTechLogCopy,
|
||||
} from "../studio-copy.ts";
|
||||
import { useStudio } from "../use-studio.ts";
|
||||
|
||||
type RecordKind = components["schemas"]["RecordKind"];
|
||||
|
||||
const STUDIO_KIND_ORDER: Record<RecordKind, number> = {
|
||||
CASE: 1,
|
||||
CONCEPT: 2,
|
||||
REFERENCE: 3,
|
||||
QUESTION: 4,
|
||||
SETUP: 5,
|
||||
PROJECT_DECISION: 6,
|
||||
};
|
||||
|
||||
export const DOCUMENT_SORTS = Object.freeze([
|
||||
"UPDATED_DESC",
|
||||
"UPDATED_ASC",
|
||||
"TITLE_ASC",
|
||||
] as const);
|
||||
|
||||
export const DOCUMENT_NEXT_ACTIONS = Object.freeze([
|
||||
"CONTINUE_EDITING",
|
||||
"VALIDATE",
|
||||
"FIX_VALIDATION",
|
||||
"CREATE_PREVIEW",
|
||||
"PUBLISH",
|
||||
"NONE",
|
||||
] as const);
|
||||
|
||||
export const DOCUMENT_PUBLICATION_STATUSES = Object.freeze([
|
||||
"NEVER_PUBLISHED",
|
||||
"PUBLISHED",
|
||||
"UNPUBLISHED",
|
||||
] as const);
|
||||
|
||||
export const DOCUMENT_STUDIO_KINDS: ReadonlyArray<RecordKind> = Object.freeze(
|
||||
(Object.keys(STUDIO_KIND_ORDER) as RecordKind[]).sort(
|
||||
(left, right) => STUDIO_KIND_ORDER[left] - STUDIO_KIND_ORDER[right],
|
||||
),
|
||||
);
|
||||
|
||||
export function useDocumentListController() {
|
||||
const copy = useTechLogCopy();
|
||||
const managementFailureMessage = useManagementFailureMessage();
|
||||
const { setRequestAnnouncement } = useStudio();
|
||||
const managementCommands = useManagementCommands();
|
||||
|
||||
const [deletingId, setDeletingId] = useState<string | null>(null);
|
||||
const [deleteError, setDeleteError] = useState("");
|
||||
const [pendingDelete, setPendingDelete] =
|
||||
useState<DocumentPage["items"][number] | null>(null);
|
||||
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const q = searchParams.get("q") ?? "";
|
||||
const kind = DOCUMENT_STUDIO_KINDS.find(
|
||||
(item) => item === searchParams.get("kind"),
|
||||
);
|
||||
const status = DOCUMENT_PUBLICATION_STATUSES.find(
|
||||
(item) => item === searchParams.get("status"),
|
||||
);
|
||||
const projectId = searchParams.get("project") ?? "";
|
||||
const sort =
|
||||
DOCUMENT_SORTS.find((item) => item === searchParams.get("sort")) ??
|
||||
"UPDATED_DESC";
|
||||
const nextAction = DOCUMENT_NEXT_ACTIONS.find(
|
||||
(item) => item === searchParams.get("next"),
|
||||
);
|
||||
const pageNumber = Math.max(1, Number(searchParams.get("page")) || 1);
|
||||
const [searchDraft, setSearchDraft] = useState(q);
|
||||
|
||||
const pageKey = [
|
||||
q,
|
||||
kind ?? "",
|
||||
status ?? "",
|
||||
projectId,
|
||||
sort,
|
||||
nextAction ?? "",
|
||||
String(pageNumber),
|
||||
].join("|");
|
||||
|
||||
const documentsQuery = useStudioDocumentsQuery({
|
||||
...(q ? { q } : {}),
|
||||
...(kind ? { kind } : {}),
|
||||
...(status ? { publicationStatus: status } : {}),
|
||||
...(projectId ? { projectId } : {}),
|
||||
...(nextAction ? { nextAction } : {}),
|
||||
sort,
|
||||
page: pageNumber,
|
||||
limit: 20,
|
||||
});
|
||||
const projectsQuery = useStudioCatalogQuery({ type: "PROJECT" });
|
||||
const projects = projectsQuery.data ?? [];
|
||||
|
||||
const [pageOverride, setPageOverride] = useState<{
|
||||
key: string;
|
||||
page: DocumentPage;
|
||||
} | null>(null);
|
||||
const page =
|
||||
pageOverride?.key === pageKey
|
||||
? pageOverride.page
|
||||
: (documentsQuery.data ?? null);
|
||||
const loading = documentsQuery.state.base === "initial-loading";
|
||||
const error =
|
||||
documentsQuery.state.base === "terminal-error"
|
||||
? copy("techLog.state.documentListLoadFailed")
|
||||
: "";
|
||||
|
||||
function go(
|
||||
next: Readonly<{
|
||||
q?: string;
|
||||
kind?: string;
|
||||
status?: string;
|
||||
project?: string;
|
||||
sort?: string;
|
||||
next?: string;
|
||||
page?: number;
|
||||
}>,
|
||||
) {
|
||||
const params = new URLSearchParams();
|
||||
const merged = {
|
||||
q,
|
||||
kind,
|
||||
status,
|
||||
project: projectId,
|
||||
sort,
|
||||
next: nextAction,
|
||||
page: pageNumber,
|
||||
...next,
|
||||
};
|
||||
if (merged.q) params.set("q", merged.q);
|
||||
if (merged.kind) params.set("kind", merged.kind);
|
||||
if (merged.status) params.set("status", merged.status);
|
||||
if (merged.project) params.set("project", merged.project);
|
||||
if (merged.sort && merged.sort !== "UPDATED_DESC") {
|
||||
params.set("sort", merged.sort);
|
||||
}
|
||||
if (merged.next) params.set("next", merged.next);
|
||||
if (merged.page && merged.page > 1) {
|
||||
params.set("page", String(merged.page));
|
||||
}
|
||||
setSearchParams(params);
|
||||
}
|
||||
|
||||
async function removeDocument(item: DocumentPage["items"][number]) {
|
||||
if (deletingId !== null) return;
|
||||
if (item.kind === "PROJECT_DECISION" && !item.project) {
|
||||
setDeleteError(
|
||||
"프로젝트에 속하지 않은 결정은 여기서 지울 수 없습니다. 먼저 프로젝트를 지정해 주세요.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setDeletingId(item.id);
|
||||
setDeleteError("");
|
||||
try {
|
||||
const deleted = await managementCommands.deleteWorkspaceDocument.submit({
|
||||
kind: item.kind,
|
||||
id: item.id,
|
||||
...(item.project ? { projectId: item.project.id } : {}),
|
||||
});
|
||||
if (!deleted.ok) {
|
||||
setDeleteError(
|
||||
managementFailureMessage(
|
||||
deleted.error,
|
||||
copy("techLog.failure.deleteFailed"),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setRequestAnnouncement(
|
||||
`작업본 ${item.title || "제목 없음"} 을(를) 삭제했습니다.`,
|
||||
);
|
||||
setPendingDelete(null);
|
||||
if (page) {
|
||||
setPageOverride({
|
||||
key: pageKey,
|
||||
page: {
|
||||
...page,
|
||||
items: page.items.filter((row) => row.id !== item.id),
|
||||
},
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
setDeletingId(null);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setSearchDraft(q);
|
||||
}, [q]);
|
||||
|
||||
function changeFilter(next: {
|
||||
kind?: ListDocumentsQuery["kind"];
|
||||
status?: ListDocumentsQuery["publicationStatus"];
|
||||
}) {
|
||||
go({
|
||||
...("kind" in next ? { kind: next.kind ?? "" } : {}),
|
||||
...("status" in next ? { status: next.status ?? "" } : {}),
|
||||
page: 1,
|
||||
});
|
||||
}
|
||||
|
||||
function submitSearch(event: FormEvent) {
|
||||
event.preventDefault();
|
||||
go({ q: searchDraft.trim(), page: 1 });
|
||||
}
|
||||
|
||||
function retry() {
|
||||
void documentsQuery.retry();
|
||||
}
|
||||
|
||||
function clearDeleteError() {
|
||||
setDeleteError("");
|
||||
}
|
||||
|
||||
const resultCount = loading
|
||||
? copy("techLog.state.documentListLoading")
|
||||
: page
|
||||
? `${page.page.totalElements}개 중 ${page.items.length}개 표시 중`
|
||||
: error
|
||||
? copy("techLog.state.documentListLoadFailed")
|
||||
: "0개";
|
||||
|
||||
return {
|
||||
changeFilter,
|
||||
clearDeleteError,
|
||||
deleteError,
|
||||
deletingId,
|
||||
error,
|
||||
go,
|
||||
kind,
|
||||
loading,
|
||||
nextAction,
|
||||
page,
|
||||
pageNumber,
|
||||
pendingDelete,
|
||||
projectId,
|
||||
projects,
|
||||
q,
|
||||
resultCount,
|
||||
retry,
|
||||
searchDraft,
|
||||
setPendingDelete,
|
||||
setSearchDraft,
|
||||
sort,
|
||||
status,
|
||||
submitSearch,
|
||||
removeDocument,
|
||||
} as const;
|
||||
}
|
||||
Reference in New Issue
Block a user