refactor: 템플릿에서 기본적으로 제공하고 있는 워크 플로우에 디자인과 라우팅 흡수
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
import { useState, type FormEvent } from "react";
|
||||
import { z } from "zod";
|
||||
|
||||
import type {
|
||||
ProjectIndexItem,
|
||||
TopicEdit,
|
||||
} from "../../../contracts/management/contract.ts";
|
||||
import { useAppForm } from "../../../../../presentation/forms/index.ts";
|
||||
import { slugFromName } from "../components/slug-from-name.ts";
|
||||
import {
|
||||
useManagementCommands,
|
||||
useManagementTaxonomyQuery,
|
||||
} from "../studio-management-server-state.ts";
|
||||
import { useManagementFailureMessage, useTechLogCopy } from "../studio-copy.ts";
|
||||
import { useStudio } from "../use-studio.ts";
|
||||
|
||||
const TOPIC_CREATE_SCHEMA = z.object({
|
||||
name: z.string().trim().min(1),
|
||||
slug: z.string(),
|
||||
});
|
||||
|
||||
const PROJECT_CREATE_SCHEMA = z.object({
|
||||
title: z.string().trim().min(1),
|
||||
});
|
||||
|
||||
export function useTaxonomyController() {
|
||||
const managementFailureMessage = useManagementFailureMessage();
|
||||
const copy = useTechLogCopy();
|
||||
const { setRequestAnnouncement } = useStudio();
|
||||
const taxonomyQuery = useManagementTaxonomyQuery();
|
||||
const commands = useManagementCommands();
|
||||
|
||||
const topics = taxonomyQuery.data?.topics ?? null;
|
||||
const projects = taxonomyQuery.data?.projects ?? null;
|
||||
const links = new Map(
|
||||
(taxonomyQuery.data?.projectTopics ?? []).map((entry) => [
|
||||
entry.projectId,
|
||||
entry.topicIds,
|
||||
]),
|
||||
);
|
||||
const loading = taxonomyQuery.state.base === "initial-loading";
|
||||
const loadError =
|
||||
taxonomyQuery.state.base === "terminal-error"
|
||||
? copy("techLog.state.taxonomyLoadFailed")
|
||||
: "";
|
||||
|
||||
const [error, setError] = useState("");
|
||||
const [tab, setTab] =
|
||||
useState<"STRUCTURE" | "TOPIC" | "PROJECT">("STRUCTURE");
|
||||
const [pendingDelete, setPendingDelete] = useState<
|
||||
| { kind: "TOPIC"; topic: TopicEdit }
|
||||
| { kind: "PROJECT"; project: ProjectIndexItem }
|
||||
| null
|
||||
>(null);
|
||||
|
||||
const topicForm = useAppForm({
|
||||
schema: TOPIC_CREATE_SCHEMA,
|
||||
defaultValues: { name: "", slug: "" },
|
||||
allowedServerFields: ["name", "slug"] as const,
|
||||
mapToCommand(values) {
|
||||
return {
|
||||
body: {
|
||||
name: values.name.trim(),
|
||||
slug: slugFromName(values.slug || values.name),
|
||||
} as TopicEdit,
|
||||
};
|
||||
},
|
||||
submit: commands.createTopic.submit,
|
||||
});
|
||||
|
||||
const projectForm = useAppForm({
|
||||
schema: PROJECT_CREATE_SCHEMA,
|
||||
defaultValues: { title: "" },
|
||||
allowedServerFields: ["title"] as const,
|
||||
mapToCommand(values) {
|
||||
const title = values.title.trim();
|
||||
return {
|
||||
title,
|
||||
slug: slugFromName(title),
|
||||
};
|
||||
},
|
||||
submit: commands.createInitializedProject.submit,
|
||||
});
|
||||
|
||||
const pending =
|
||||
topicForm.pending ||
|
||||
projectForm.pending ||
|
||||
[
|
||||
commands.deleteTopic,
|
||||
commands.deleteProject,
|
||||
commands.publishProject,
|
||||
commands.unpublishProject,
|
||||
].some((command) => command.state.overlay.mutationPending);
|
||||
|
||||
async function reload() {
|
||||
await taxonomyQuery.retry();
|
||||
}
|
||||
|
||||
async function submitTopic(event: FormEvent<HTMLFormElement>) {
|
||||
setError("");
|
||||
const name = topicForm.values.name.trim();
|
||||
const created = await topicForm.submitForm(event);
|
||||
if (!created?.ok) return;
|
||||
setRequestAnnouncement(copy("techLog.notice.topicCreated", { name }));
|
||||
await reload();
|
||||
}
|
||||
|
||||
async function submitProject(event: FormEvent<HTMLFormElement>) {
|
||||
setError("");
|
||||
const title = projectForm.values.title.trim();
|
||||
const created = await projectForm.submitForm(event);
|
||||
if (!created?.ok) return;
|
||||
setRequestAnnouncement(copy("techLog.notice.projectCreated", { title }));
|
||||
await reload();
|
||||
}
|
||||
|
||||
async function removeTopic(topic: TopicEdit) {
|
||||
if (pending || topic.id === undefined || topic.version === undefined) return;
|
||||
setError("");
|
||||
const removed = await commands.deleteTopic.submit({
|
||||
id: topic.id,
|
||||
expectedVersion: topic.version,
|
||||
});
|
||||
if (!removed.ok) {
|
||||
setError(
|
||||
managementFailureMessage(removed.error, copy("techLog.failure.topicDeleteFailed")),
|
||||
);
|
||||
return;
|
||||
}
|
||||
setRequestAnnouncement(copy("techLog.notice.topicDeleted", { name: topic.name }));
|
||||
setPendingDelete(null);
|
||||
await reload();
|
||||
}
|
||||
|
||||
async function removeProject(project: ProjectIndexItem) {
|
||||
if (pending) return;
|
||||
setError("");
|
||||
const removed = await commands.deleteProject.submit({
|
||||
id: project.id,
|
||||
expectedVersion: project.version,
|
||||
});
|
||||
if (!removed.ok) {
|
||||
setError(
|
||||
managementFailureMessage(
|
||||
removed.error,
|
||||
copy("techLog.failure.projectDeleteFailed"),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
setRequestAnnouncement(copy("techLog.notice.projectDeleted", { name: project.name }));
|
||||
setPendingDelete(null);
|
||||
await reload();
|
||||
}
|
||||
|
||||
async function togglePublish(project: ProjectIndexItem) {
|
||||
if (pending) return;
|
||||
setError("");
|
||||
const published = project.targetVisibility !== "PRIVATE";
|
||||
const result = published
|
||||
? await commands.unpublishProject.submit({
|
||||
id: project.id,
|
||||
expectedVersion: project.version,
|
||||
})
|
||||
: await commands.publishProject.submit({
|
||||
id: project.id,
|
||||
expectedVersion: project.version,
|
||||
visibility: "PUBLIC",
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
setError(
|
||||
managementFailureMessage(
|
||||
result.error,
|
||||
published
|
||||
? copy("techLog.failure.projectUnpublishFailed")
|
||||
: copy("techLog.failure.projectPublishFailed"),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setRequestAnnouncement(
|
||||
published
|
||||
? `프로젝트 ${project.name} 을(를) 비공개로 되돌렸습니다.`
|
||||
: `프로젝트 ${project.name} 을(를) 공개했습니다.`,
|
||||
);
|
||||
await reload();
|
||||
}
|
||||
|
||||
return {
|
||||
error,
|
||||
links,
|
||||
loadError,
|
||||
loading,
|
||||
pending,
|
||||
pendingDelete,
|
||||
projectForm,
|
||||
projects,
|
||||
removeProject,
|
||||
removeTopic,
|
||||
setError,
|
||||
setPendingDelete,
|
||||
setTab,
|
||||
submitProject,
|
||||
submitTopic,
|
||||
tab,
|
||||
togglePublish,
|
||||
topicForm,
|
||||
topics,
|
||||
} as const;
|
||||
}
|
||||
Reference in New Issue
Block a user