- 카드 팝업: card_id 오름차순 정렬 (NC26-000~008)
- 긴 봇 스크립트를 종결어미("~다."/"~요.") 경계에서 2문장씩 끊어 빈 줄 삽입 —
서버 원문·기존 줄바꿈은 유지, 90자 미만 줄은 건드리지 않음
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFZ7JWdwCaQAwNYJErL8pP
249 lines
11 KiB
TypeScript
249 lines
11 KiB
TypeScript
import { useEffect, useRef, useState } from 'react'
|
||
import type { ChatMetadata } from '../lib/api'
|
||
import { SCENARIO_QUADRANTS, type DemoCase } from '../content/cases'
|
||
import type { NegoState } from '../App'
|
||
|
||
export interface Msg {
|
||
role: 'bot' | 'user'
|
||
text: string
|
||
step?: string
|
||
meta?: ChatMetadata | null
|
||
}
|
||
|
||
// 내부 스텝 ID → 관람자용 라벨 (엔진 내부명 노출 방지)
|
||
const STEP_LABELS: Record<string, string> = {
|
||
wild_card_1pct: '1% 추가 제안',
|
||
wild_card_budget: '재원부족 제안',
|
||
가격협상_확인_버짓: '가격협상 확인',
|
||
}
|
||
const stepLabel = (s: string) => STEP_LABELS[s] ?? s
|
||
|
||
// 봇 스크립트가 한 덩어리 문단일 때 가독성을 위해 2문장씩 끊는다 (표시 전용).
|
||
// 서버가 이미 넣어둔 줄바꿈은 유지하고, 긴 줄만 종결어미("~다."/"~요.") 경계에서 나눈다.
|
||
function paragraphize(text: string): string {
|
||
return text
|
||
.split('\n')
|
||
.map((line) => {
|
||
if (line.length < 90) return line
|
||
const sentences = line.split(/(?<=[다요]\.)\s+/)
|
||
if (sentences.length < 3) return line
|
||
const chunks: string[] = []
|
||
for (let i = 0; i < sentences.length; i += 2) chunks.push(sentences.slice(i, i + 2).join(' '))
|
||
return chunks.join('\n\n')
|
||
})
|
||
.join('\n')
|
||
}
|
||
|
||
export function ChatPanel({
|
||
messages, nego, busy, started, error, demoCase, cases, finalPrice, onAnswer, onOffer, onRestart, onSelect,
|
||
}: {
|
||
messages: Msg[]
|
||
nego: NegoState
|
||
busy: boolean
|
||
started: boolean
|
||
error: string | null
|
||
demoCase: DemoCase | null
|
||
cases: DemoCase[]
|
||
finalPrice: number | null
|
||
onAnswer: (label: string) => void
|
||
onOffer: (v: number) => void
|
||
onRestart: () => void
|
||
onSelect: (c: DemoCase) => void
|
||
}) {
|
||
const [priceInput, setPriceInput] = useState('')
|
||
const scroller = useRef<HTMLDivElement>(null)
|
||
|
||
useEffect(() => {
|
||
scroller.current?.scrollTo({ top: scroller.current.scrollHeight, behavior: 'smooth' })
|
||
}, [messages, busy])
|
||
|
||
useEffect(() => setPriceInput(''), [nego.step])
|
||
|
||
const submitPrice = () => {
|
||
const v = Number(priceInput.replaceAll(',', ''))
|
||
if (!Number.isFinite(v) || v <= 0) return
|
||
onOffer(Math.round(v))
|
||
}
|
||
|
||
const savedPct = finalPrice != null && demoCase != null
|
||
? Math.round(((demoCase.item_price - finalPrice) / demoCase.item_price) * 1000) / 10
|
||
: null
|
||
|
||
return (
|
||
<section className="flex min-h-[70vh] flex-col rounded-box bg-base-100 min-[960px]:h-full min-[960px]:min-h-0">
|
||
{started && demoCase && (
|
||
<div className="flex items-center justify-between gap-3 border-b border-base-300/60 px-5 py-3">
|
||
<span className="truncate text-[15px] font-bold">{demoCase.item_name}</span>
|
||
{!nego.done && (
|
||
<button
|
||
type="button"
|
||
onClick={onRestart}
|
||
disabled={busy}
|
||
className="btn btn-ghost btn-xs text-base-content/55"
|
||
>
|
||
다시 시작
|
||
</button>
|
||
)}
|
||
</div>
|
||
)}
|
||
|
||
<div ref={scroller} className="flex-1 overflow-y-auto px-5 py-4">
|
||
{!started && (
|
||
<div className="flex h-full flex-col items-center justify-center gap-6 py-8 text-center">
|
||
<div>
|
||
<p className="text-xl font-bold">어떤 시나리오로 협상해 볼까요?</p>
|
||
<p className="mx-auto mt-2 max-w-md text-[15px] leading-relaxed text-base-content/55">
|
||
구매 엔진은 상품의 매출 규모와 경쟁 강도에 따라 가격 전략을 다르게 씁니다.
|
||
시나리오를 선택하면 해당 조건의 품목으로 협상이 시작됩니다.
|
||
</p>
|
||
</div>
|
||
{/* 사분면 다이어그램 — Y축: 매출 규모(위=높음), X축: 경쟁 강도(오른쪽=높음) */}
|
||
<div className="flex w-full max-w-sm items-stretch gap-2">
|
||
<div className="mb-7 flex flex-col text-[11px] font-medium text-base-content/45" aria-hidden>
|
||
<span className="flex flex-1 items-center justify-center [writing-mode:vertical-rl]">
|
||
매출 높음
|
||
</span>
|
||
<span className="flex flex-1 items-center justify-center [writing-mode:vertical-rl]">
|
||
매출 낮음
|
||
</span>
|
||
</div>
|
||
<div className="flex-1">
|
||
<div className="grid grid-cols-2 gap-2 border-b border-l border-base-300 pb-3 pl-3">
|
||
{[['B', 'A'], ['C', 'D']].flat().map((letter) => {
|
||
const q = SCENARIO_QUADRANTS.find((x) => x.letter === letter)
|
||
const c = q && cases.find((x) => x.scenario === q.scenario)
|
||
if (!q || !c) return null
|
||
return (
|
||
<button
|
||
key={q.scenario}
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => onSelect(c)}
|
||
aria-label={`시나리오 ${q.letter} — ${q.axis}`}
|
||
className={`cursor-pointer rounded-field px-4 py-5 transition-colors focus-visible:ring-2 focus-visible:ring-primary focus-visible:outline-none ${q.tileCls}`}
|
||
>
|
||
<span className={`text-3xl font-extrabold ${q.textCls}`}>{q.letter}</span>
|
||
<span className="mt-0.5 block text-[11px] font-medium text-base-content/45">시나리오</span>
|
||
</button>
|
||
)
|
||
})}
|
||
</div>
|
||
<div className="mt-1.5 grid grid-cols-2 pl-3 text-[11px] font-medium text-base-content/45" aria-hidden>
|
||
<span className="text-center">경쟁 낮음</span>
|
||
<span className="text-center">경쟁 높음</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<p className="text-[13px] text-base-content/45">
|
||
챗봇은 구매 담당자(MD) 역할이고, 당신은 공급사로서 가격 제안에 응답합니다.
|
||
</p>
|
||
</div>
|
||
)}
|
||
<ol className="flex flex-col gap-1">
|
||
{messages.map((m, i) => (
|
||
<li key={i} className={`msg-in chat ${m.role === 'user' ? 'chat-end' : 'chat-start'}`}>
|
||
{m.role === 'bot' && m.step && (
|
||
<div className="chat-header mb-1 flex items-center gap-1.5 text-xs font-semibold text-base-content/45">
|
||
<span>{stepLabel(m.step)}</span>
|
||
{m.meta?.card_id && (
|
||
<span className="num rounded-full bg-base-200 px-1.5 py-px font-mono text-base-content/55">
|
||
{m.meta.card_id}
|
||
</span>
|
||
)}
|
||
</div>
|
||
)}
|
||
<div
|
||
className={`chat-bubble max-w-[min(85%,34rem)] text-[15px] leading-relaxed whitespace-pre-line before:hidden ${
|
||
m.role === 'user'
|
||
? 'bg-primary text-primary-content'
|
||
: 'bg-base-200 text-base-content'
|
||
}`}
|
||
>
|
||
{m.role === 'bot' ? paragraphize(m.text) : m.text}
|
||
</div>
|
||
</li>
|
||
))}
|
||
{busy && (
|
||
<li className="chat chat-start">
|
||
<div className="chat-bubble flex items-center gap-2 bg-base-200 text-sm text-base-content/55 before:hidden">
|
||
<span className="loading loading-dots loading-sm" aria-hidden />
|
||
담당자가 검토하고 있어요
|
||
</div>
|
||
</li>
|
||
)}
|
||
</ol>
|
||
{error && (
|
||
<div className="mt-3 rounded-field bg-error/8 px-4 py-3 text-sm text-error">
|
||
{error} — 엔진 연결을 확인한 뒤 다시 시도해 주세요.
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{started && (
|
||
<div className="border-t border-base-300/60 p-4">
|
||
{nego.done ? (
|
||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||
{/* 큰 숫자는 좌측 최종 합의가 패널이 담당 — 여기서는 사건만 알린다 */}
|
||
{finalPrice != null ? (
|
||
<p className="flex items-center gap-2 text-[15px]">
|
||
<span className="flex size-5 shrink-0 items-center justify-center rounded-full bg-success" aria-hidden>
|
||
<svg viewBox="0 0 10 8" className="w-2.5">
|
||
<path d="M1 4l2.5 2.5L9 1" fill="none" stroke="white" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" />
|
||
</svg>
|
||
</span>
|
||
<span className="font-bold">협상 완료</span>
|
||
<span className="num text-base-content/60">
|
||
최종 합의가 <span className="font-semibold text-base-content">{finalPrice.toLocaleString()}원</span> · −{savedPct}%
|
||
</span>
|
||
</p>
|
||
) : (
|
||
<span className="text-sm font-bold">협상이 종료되었습니다</span>
|
||
)}
|
||
<button type="button" onClick={onRestart} className="btn btn-neutral rounded-field">
|
||
같은 케이스로 다시 협상
|
||
</button>
|
||
</div>
|
||
) : nego.mode === 'price' ? (
|
||
<form className="flex gap-2" onSubmit={(e) => { e.preventDefault(); submitPrice() }}>
|
||
<label htmlFor="price" className="sr-only">공급 제안가 (원)</label>
|
||
<input
|
||
id="price"
|
||
inputMode="numeric"
|
||
autoComplete="off"
|
||
placeholder={`제안가 입력 (기존가 ${demoCase?.item_price.toLocaleString() ?? ''}원)`}
|
||
value={priceInput}
|
||
onChange={(e) => setPriceInput(e.target.value)}
|
||
disabled={busy}
|
||
className="input num min-w-0 flex-1 bg-base-200 text-[15px] focus:outline-primary"
|
||
/>
|
||
<button
|
||
type="submit"
|
||
disabled={busy || !priceInput.trim()}
|
||
className="btn btn-primary shrink-0 rounded-field px-6"
|
||
>
|
||
제안하기
|
||
</button>
|
||
</form>
|
||
) : nego.options.length > 0 ? (
|
||
<div className="flex flex-wrap justify-end gap-2">
|
||
{nego.options.map((opt) => (
|
||
<button
|
||
key={opt}
|
||
type="button"
|
||
disabled={busy}
|
||
onClick={() => onAnswer(opt)}
|
||
className="btn rounded-field border-0 bg-base-200 px-5 font-semibold hover:bg-primary/10 hover:text-primary"
|
||
>
|
||
{opt}
|
||
</button>
|
||
))}
|
||
</div>
|
||
) : (
|
||
<p className="py-1 text-center text-sm text-base-content/45">응답을 기다리는 중…</p>
|
||
)}
|
||
</div>
|
||
)}
|
||
</section>
|
||
)
|
||
}
|