Files
o2o-site-AEO/solution/frontend/src/features/builder/FaqPanel.tsx
T
minaChoiandClaude Opus 5 c85c577349 이름: solution/front → solution/frontend
`backend` 옆에 `front` 가 있을 이유가 없었다. negosium 의 negodata/front 를 그대로
베꼈고 그게 왜 front 인지는 따져보지 않았다 — 근거 없이 들여온 이름이라 바로잡는다.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019uYhHQdssRubirPirrdJJC
2026-08-31 15:27:16 +09:00

215 lines
9.1 KiB
TypeScript

import {useState} from 'react';
import {useQueryClient} from '@tanstack/react-query';
import {Check, Loader2, MessageCircleQuestion, Plus, Sparkles, X} from 'lucide-react';
import {FactStatus, PUBLISHABLE_FACT_STATUSES} from '@o2o/shared';
import {getListFaqsQueryKey, useCreateFaq, useListFaqs, useTransitionFaq} from '@/api';
import {Badge} from '@/components/ui/badge';
import {Button} from '@/components/ui/button';
import {Input} from '@/components/ui/input';
import {cn} from '@/lib/utils';
import {useBuilderStore} from '@/stores/builder';
/**
* FAQ 관리 패널.
*
* ★ 왜 별도 탭인가
* FAQ 는 AI 검색이 가장 잘 인용하는 형식이다(FAQPage 구조화 데이터). 그런데 지금까지
* 관리자에는 FAQ 를 **볼 자리조차** 없었다 — COPY 잡이 만들어 둔 FAQ 가 `UNVERIFIED` 로
* 쌓여 있는데 승인할 화면이 없으니 영원히 발행되지 않았다. 실제로 8건이 대기 중인
* 사업장이 있고, FAQ 가 나가는 사업장은 한 곳뿐이었다.
*
* ★ 이 패널이 하는 일은 둘이다.
* 1) 사장님이 **직접 질문·답변을 쓴다** → 서버가 OWNER 출처로 바로 노출값(VERIFIED)에 넣는다.
* 쓴 사람이 곧 출처이자 책임 주체이기 때문이다(fact 직접 입력과 같은 규칙).
* 2) LLM 이 만들어 둔 대기 FAQ 를 **승인한다** → VERIFIED 로 전이시켜 사이트에 내보낸다.
*/
export function FaqPanel() {
const placeId = useBuilderStore((s) => s.placeId);
if (!placeId) {
return (
<div className="flex flex-1 flex-col items-center justify-center gap-2 p-6 text-center text-muted-foreground">
<MessageCircleQuestion className="size-5" />
<p className="text-xs font-semibold text-foreground">데모에서는 FAQ 를 저장할 수 없습니다</p>
<p className="text-[11px] leading-relaxed">
FAQ 는 서버(사업장)에 저장됩니다. 실제 사업장으로 들어오면 이곳에서 추가·승인할 수 있습니다.
</p>
</div>
);
}
return <FaqEditor placeId={placeId} />;
}
function FaqEditor({placeId}: {placeId: string}) {
const queryClient = useQueryClient();
const [question, setQuestion] = useState('');
const [answer, setAnswer] = useState('');
// 어떤 항목에 요청이 나가 있는지 — 버튼마다 따로 표시해야 어느 줄이 움직이는지 보인다.
const [busyId, setBusyId] = useState<string | null>(null);
const listQuery = useListFaqs(placeId);
const createFaq = useCreateFaq();
const transitionFaq = useTransitionFaq();
/** 목록을 다시 읽는다. 추가·승인 뒤 화면이 옛 상태로 남으면 사장님이 같은 걸 두 번 누른다. */
const refresh = () =>
queryClient.invalidateQueries({queryKey: getListFaqsQueryKey(placeId)});
const faqs = listQuery.data?.faqs ?? [];
const publishable = listQuery.data?.publishable ?? 0;
const pending = listQuery.data?.pending_review ?? 0;
const submit = async () => {
const q = question.trim();
const a = answer.trim();
if (!q || !a) return;
await createFaq.mutateAsync({placeId, data: {question: q, answer: a}});
setQuestion('');
setAnswer('');
await refresh();
};
const approve = async (faqId: string) => {
setBusyId(faqId);
try {
await transitionFaq.mutateAsync({placeId, faqId, data: {status: FactStatus.VERIFIED}});
await refresh();
} finally {
setBusyId(null);
}
};
const reject = async (faqId: string) => {
setBusyId(faqId);
try {
await transitionFaq.mutateAsync({placeId, faqId, data: {status: FactStatus.REJECTED}});
await refresh();
} finally {
setBusyId(null);
}
};
return (
<div className="flex-1 space-y-3 overflow-y-auto p-3.5">
<div className="flex items-center justify-between border-b border-border pb-2">
<span className="text-xs font-bold">자주 묻는 질문</span>
<span className="text-[10px] text-muted-foreground">
노출 {publishable}건{pending > 0 && ` · 대기 ${pending}건`}
</span>
</div>
{/* ── 새 FAQ 직접 추가 ───────────────────────────────── */}
<div className="space-y-2 rounded-lg border border-dashed border-border bg-muted/30 p-3">
<div className="flex items-center gap-1.5 text-xs font-bold">
<Plus className="size-3.5" /> 질문·답변 추가
</div>
<Input
value={question}
onChange={(event) => setQuestion(event.target.value)}
placeholder="질문 (예: 주차할 수 있나요?)"
className="text-xs"
/>
<textarea
value={answer}
onChange={(event) => setAnswer(event.target.value)}
placeholder="답변 (짧고 단정적으로 쓸수록 AI 답변에 그대로 인용됩니다)"
rows={3}
className="w-full resize-y rounded-md border border-input bg-transparent px-3 py-2 text-xs shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50"
/>
<Button
size="sm"
className="w-full"
disabled={!question.trim() || !answer.trim() || createFaq.isPending}
onClick={() => void submit()}
>
{createFaq.isPending ? <Loader2 className="animate-spin" /> : <Plus />}
추가하고 바로 노출
</Button>
{createFaq.isError && (
<p className="text-[11px] text-destructive">저장하지 못했습니다. 잠시 후 다시 시도해 주세요.</p>
)}
</div>
{/* ── 목록 ──────────────────────────────────────────── */}
{listQuery.isPending ? (
<div className="flex items-center justify-center gap-2 py-6 text-[11px] text-muted-foreground">
<Loader2 className="size-3.5 animate-spin" /> 불러오는 중
</div>
) : faqs.length === 0 ? (
<p className="py-6 text-center text-[11px] leading-relaxed text-muted-foreground">
아직 FAQ 가 없습니다.
<br />
손님이 실제로 자주 묻는 질문부터 채워보세요.
</p>
) : (
<div className="space-y-2">
{faqs.map((faq) => {
const faqId = String(faq.faq_id);
const isLive = PUBLISHABLE_FACT_STATUSES.includes(faq.status as FactStatus);
const isBusy = busyId === faqId;
return (
<div
key={faqId}
className={cn(
'space-y-1.5 rounded-lg border p-2.5',
isLive ? 'border-border bg-card' : 'border-warning/40 bg-warning/8',
)}
>
<div className="flex items-start justify-between gap-2">
<p className="flex-1 text-xs font-semibold break-words">{faq.question}</p>
{isLive ? (
<span className="shrink-0 text-[10px] text-muted-foreground">노출 중</span>
) : (
<Badge variant="warning" className="shrink-0 text-[10px]">
승인 대기
</Badge>
)}
</div>
<p className="text-[11px] leading-relaxed break-words text-muted-foreground">{faq.answer}</p>
{/* AI 가 쓴 문장이면 무슨 사실을 근거로 썼는지 보여준다 — 승인 판단의 재료다. */}
{faq.generated_by !== undefined && !isLive && faq.source_fact_ids?.length ? (
<p className="flex items-center gap-1 text-[10px] text-muted-foreground">
<Sparkles className="size-2.5 shrink-0" />
근거: {faq.source_fact_ids.join(' · ')}
</p>
) : null}
{!isLive && (
<div className="flex gap-1.5 pt-0.5">
<Button
size="sm"
className="h-7 flex-1 text-[11px]"
disabled={isBusy}
onClick={() => void approve(faqId)}
>
{isBusy ? <Loader2 className="animate-spin" /> : <Check />}
승인하고 노출
</Button>
<Button
size="sm"
variant="outline"
className="h-7 text-[11px]"
disabled={isBusy}
onClick={() => void reject(faqId)}
>
<X /> 버리기
</Button>
</div>
)}
</div>
);
})}
</div>
)}
<p className="text-[11px] leading-relaxed text-muted-foreground">
직접 쓴 질문·답변은 <strong className="text-foreground">사장님이 확정한 값</strong>으로 바로 노출되고,
AI 가 만들어 둔 항목은 승인해야 사이트와 AI 검색용 데이터에 나갑니다.
</p>
</div>
);
}