Files
tech-log-frontend/src/features/tech-log/presentation/studio/components/reference-fields.tsx
T

62 lines
5.2 KiB
TypeScript

import { useTechLogCopy } from "../studio-copy.ts";
import { Button, InputControl, TextAreaControl } from "../../../../../presentation/design-system/index.ts";
import type { components } from "../../../contracts/studio/generated.ts";
import { createNewItemId } from "../../../domain/studio/local-id.ts";
import { FieldNotice, type FieldIssue } from "./field-issues.tsx";
import { OrderedTextList } from "./ordered-text-list.tsx";
type ReferenceInput = components["schemas"]["ReferenceInput"];
type ReferenceRule = components["schemas"]["ReferenceRule"];
export const REFERENCE_FIELD_PATHS = ["/purpose", "/rules", "/applyWhen", "/exceptions", "/examples", "/verifiedOn"] as const;
const orderedRules = (rules: ReferenceRule[]) => rules.map((rule, order) => ({ ...rule, order }));
export function ReferenceFields({ draft, issues, onChange }: { draft: ReferenceInput; issues: readonly FieldIssue[]; onChange(draft: ReferenceInput): void }) {
const copy = useTechLogCopy();
const update = (patch: Partial<ReferenceInput>) => onChange({ ...draft, ...patch });
const moveRule = (index: number, delta: -1 | 1) => {
const target = index + delta;
if (target < 0 || target >= draft.rules.length) return;
const next = [...draft.rules];
[next[index], next[target]] = [next[target]!, next[index]!];
update({ rules: orderedRules(next) });
};
return (
<section className="studio-editor-section studio-record-authoring" aria-labelledby="studio-reference-fields-title">
<div className="studio-editor-section-heading"><p className="studio-eyebrow">REFERENCE</p><h2 id="studio-reference-fields-title">재사용할 기준</h2><p>나중에 다시 찾아도 바로 판단할 수 있도록 규칙을 짧고 독립적인 블록으로 적습니다.</p></div>
<label className="studio-document-block" data-tone="purpose">
<span className="studio-document-block__label">이 기준을 쓰는 이유</span>
<TextAreaControl aria-label="이 기준을 쓰는 이유" value={draft.purpose} placeholder="이 기준이 어떤 판단을 빠르게 만드는지 적어 주세요." onChange={(event) => update({ purpose: event.currentTarget.value })} />
<FieldNotice issues={issues} path="/purpose" />
</label>
<fieldset className="studio-rule-blocks">
<legend><span>판단 기준</span><span>{draft.rules.length}</span></legend>
<FieldNotice issues={issues} path="/rules" />
{draft.rules.map((rule, index) => (
<div className="studio-rule-block studio-ordered-item" key={rule.id}>
<span className="studio-rule-block__number" aria-hidden="true">{String(index + 1).padStart(2, "0")}</span>
<div className="studio-rule-block__writing">
<InputControl aria-label={`판단 기준 ${index + 1} 제목`} value={rule.title} placeholder="한 줄 판단 기준" onChange={(event) => update({ rules: orderedRules(draft.rules.map((candidate, at) => at === index ? { ...candidate, title: event.currentTarget.value } : candidate)) })} />
<TextAreaControl aria-label={`판단 기준 ${index + 1} 본문`} value={rule.body} placeholder="왜 이 기준을 쓰는지, 적용 범위가 무엇인지 설명하세요." onChange={(event) => update({ rules: orderedRules(draft.rules.map((candidate, at) => at === index ? { ...candidate, body: event.currentTarget.value } : candidate)) })} />
</div>
<div className="studio-item-actions"><Button visualPolicy="inherit" type="button" aria-label={copy("techLog.action.moveUp")} onClick={() => moveRule(index, -1)} disabled={index === 0}>↑</Button><Button visualPolicy="inherit" type="button" aria-label={copy("techLog.action.moveDown")} onClick={() => moveRule(index, 1)} disabled={index === draft.rules.length - 1}>↓</Button><Button visualPolicy="inherit" type="button" onClick={() => update({ rules: orderedRules(draft.rules.filter((_, at) => at !== index)) })}>{copy("techLog.action.delete")}</Button></div>
</div>
))}
<Button visualPolicy="inherit" className="studio-add-item" aria-label="판단 기준 추가" type="button" disabled={draft.rules.length >= 50} onClick={() => { if (draft.rules.length < 50) update({ rules: orderedRules([...draft.rules, { id: createNewItemId(), title: "", body: "", order: draft.rules.length }]) }); }}>{copy("techLog.action.addCriterion")}</Button>
</fieldset>
<OrderedTextList label="적용할 때" tone="fact" items={draft.applyWhen} onChange={(applyWhen) => update({ applyWhen })} />
<FieldNotice issues={issues} path="/applyWhen" />
<OrderedTextList label="예외와 주의" tone="constraint" items={draft.exceptions} onChange={(exceptions) => update({ exceptions })} />
<FieldNotice issues={issues} path="/exceptions" />
<OrderedTextList label="예시" tone="example" items={draft.examples} onChange={(examples) => update({ examples })} />
<FieldNotice issues={issues} path="/examples" />
<label className="studio-inline-property studio-inline-property--date"><span>마지막 검증일</span><InputControl aria-label="마지막 검증일" type="date" value={draft.verifiedOn ?? ""} onChange={(event) => update({ verifiedOn: event.currentTarget.value || null })} /><FieldNotice issues={issues} path="/verifiedOn" /></label>
</section>
);
}