261001_02_ENH: 발송 다이얼로그 미리보기 통합 및 QR·레이아웃 오류 수정

This commit is contained in:
2026-10-01 16:35:55 +09:00
parent 11f7ec1719
commit 825cf5f35f
5 changed files with 118 additions and 132 deletions

View File

@@ -0,0 +1,105 @@
'use client'
import { ChannelType, type TemplateSms } from '@kri/common/noti-hub'
import { KriEditor } from '@/components/kri-editor'
import { applyLinkPreviewClass } from '@/components/kri-editor/utils/linkPreview'
import { sanitizeEmailHtml } from '@/components/message-template/emailHtmlPolicy'
import { normalizeQrCodeForEditor, renderQrCodePreviewHtml } from '@/components/message-template/qrcode'
import TemplateAttachmentList from '@/components/message-template/TemplateAttachmentList'
import { PHONE_VERIFICATION_PREVIEW_SUFFIX, TEMPLATE_PURPOSE } from '@/const/messageTemplateConst'
import { escapeHtml, substituteVariables } from '@/lib/variableSubstitution'
import { cn } from '@/lib/utils'
import { useTemplateAttachmentImagesQuery } from '@/query/message'
import type { TemplateEmailWithAttachments, TemplateSmsWithAttachments, TemplateWithPurpose } from '@/types/message'
// 다이얼로그(max-h 90vh)의 헤더·푸터·미리보기 제목을 뺀 높이에 맞추되,
// 본문 입력칸 최소 높이(360px)가 잘리지 않도록 480px 아래로는 줄이지 않는다.
const SMS_PREVIEW_FRAME_CLASS_NAME = 'h-[clamp(480px,calc(90vh-240px),600px)]'
export type MessageSendPreviewProps = {
chnnlTy: ChannelType
template: TemplateWithPurpose | null
/** 지정하면 [$변수] 토큰을 값으로 치환한다(없는 값은 빈 문자열). 생략하면 토큰을 그대로 보여준다. */
variableValues?: Record<string, string>
}
/**
* 문자/메일 발송 다이얼로그 공용 미리보기.
* 이메일 본문은 템플릿 관리 미리보기(KriEditor)와 같은 순서로 sanitize → 링크 스타일 → QR 예시 이미지 변환을 거친다.
*/
const MessageSendPreview = ({ chnnlTy, template, variableValues }: MessageSendPreviewProps) => {
const isSms = chnnlTy === ChannelType.SMS
const attachmentNames = template
? ((template as TemplateEmailWithAttachments | TemplateSmsWithAttachments).atchFl?.split('|').filter(Boolean) ?? [])
: []
const smsAttachmentImages = useTemplateAttachmentImagesQuery(
template?.tmplatIdx ?? 0,
isSms ? attachmentNames : [],
!!template,
)
const substitute = (contents: string, escape = false) => {
if (!variableValues) return contents
const values = escape
? Object.fromEntries(Object.entries(variableValues).map(([key, val]) => [key, escapeHtml(val)]))
: variableValues
return substituteVariables(contents, values)
}
const resolvedTitle = template ? substitute(template.tmplatTitle ?? '') : ''
return (
// min-w-0: 다이얼로그 grid 칸 너비를 넘지 않도록 한다.
<div className="min-w-0 space-y-2 rounded-md border p-4">
<p className="text-lg font-semibold">미리보기</p>
{!template && <p className="text-sm text-muted-foreground">선택된 템플릿이 없습니다.</p>}
{template && (
<>
{!isSms && (
<div className="rounded border p-2 text-lg font-semibold wrap-anywhere">{resolvedTitle ? `제목: ${resolvedTitle}` : '(제목 없음)'}</div>
)}
{/* 본문이 길어도 다이얼로그가 아닌 미리보기 영역에서만 스크롤되도록 한다.
문자는 휴대폰 프레임 내부에 자체 스크롤이 있으므로 프레임 높이만 줄이고 바깥 스크롤은 두지 않는다. */}
<div className={cn('space-y-2', !isSms && 'max-h-[60vh] overflow-y-auto')}>
{isSms ? (
<div className="flex flex-col items-center">
<KriEditor
mode="sms"
value={substitute((template as TemplateSms).tmplatBody ?? '')}
onChange={() => {}}
isSmsReadOnly
smsPreviewSuffix={
template.tmplatPurpose === TEMPLATE_PURPOSE.PHONE_VERIFICATION ? PHONE_VERIFICATION_PREVIEW_SUFFIX : undefined
}
placeholder="문자 내용을 입력해 주세요"
className="max-w-80 mx-0"
smsFrameClassName={SMS_PREVIEW_FRAME_CLASS_NAME}
debounceMs={600}
smsTitle={resolvedTitle}
smsHasAttachment={attachmentNames.length > 0}
smsImages={smsAttachmentImages}
/>
</div>
) : (
<>
<div
className="kri-editor-preview rounded-md border bg-muted/30 p-3 wrap-anywhere"
dangerouslySetInnerHTML={{
__html: renderQrCodePreviewHtml(
applyLinkPreviewClass(
sanitizeEmailHtml(normalizeQrCodeForEditor(substitute(template.tmplatBody ?? '', true)) || '(내용 없음)'),
),
),
}}
/>
<TemplateAttachmentList tmplatIdx={template.tmplatIdx} atchFl={attachmentNames.join('|')} />
</>
)}
</div>
</>
)}
</div>
)
}
export default MessageSendPreview