'use client' import { useRef, useState, type ReactNode } from 'react' import { Mail, MessageCircle, MessageSquare, Paperclip, Phone, QrCode } from 'lucide-react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { KriSingleSelect } from '@/components/common/KriAutoComplete' import { LIST_IS_USE_LABEL } from '@/components/table-column/ListTableColumn' import { useDialog } from '@/hooks/useDialog' import { useDownloadListItemFileByHeaderMutation, useUploadListItemFileMutation } from '@/query/list' import { ListHeaderConst } from '@/const/listConst' import type { listHeader, ListSearchWithFullResult } from '@/types/list' import type { MessageSendChannel } from '@/types/message' import { type ProjectSurvey, type ResultCode, ResultCodeType } from '@/types/project' import { formatKst } from '@kri/common' import { isListFieldEditableForContact, isListFieldVisibleForContact } from '@/utils/listAccess' import { substituteVariables } from '@/lib/variableSubstitution' import { formatKoreanPhoneNumber } from '@/utils/phone' import SurveyQrDialog from '@/components/dialog/SurveyQrDialog' import SingleMessageSendDialog from '@/components/dialog/SingleMessageSendDialog' import { ctiClient } from '@/lib/cti/ctiClient' import { useCtiConnectionStatus } from '@/hooks/useCtiConnectionStatus' import { useCtiCallPhase } from '@/hooks/useCtiCallPhase' import { cn } from '@/lib/utils' import { downloadFile } from '@kri/common/browser' import { buildKkoVariableValues, buildListContactVariableValues, resolveListHeaderValue, } from '@/components/list-contact/listContactVariables' export type ListContactInfoPanelProps = { prjctIdx: number sid: string itrntPrjctCd: string userRole: number isEditing: boolean listHeaders: listHeader[] detail?: ListSearchWithFullResult surveys: ProjectSurvey[] callerTelNum?: string resultCodes: ResultCode[] formValues: Record onFieldChange: (listHderId: number, value: string) => void onSaved?: () => void } export const buildInitialValues = (listHeaders: listHeader[], detail?: ListSearchWithFullResult) => { const values: Record = {} for (const header of listHeaders) { if (header.listHderTy === ListHeaderConst.SID) continue if (!isListFieldVisibleForContact(header)) continue values[header.listHderId] = resolveListHeaderValue(header, detail) } return values } const disabledContrastClass = 'bg-white disabled:opacity-100 disabled:text-foreground disabled:bg-muted/60' // Input/textarea는 수정 불가 상태에서도 드래그 선택/복사가 가능해야 해서 disabled 대신 readOnly를 쓴다. // disabled는 공용 Input 컴포넌트의 disabled:pointer-events-none 때문에 마우스 이벤트 자체를 못 받는다. const readOnlyContrastClass = 'bg-white read-only:text-foreground read-only:bg-muted/60' const getCallButtonLabel = (isDialing: boolean, isOnCall: boolean, isEnding: boolean) => { if (isEnding) return '종료 요청 중' if (isDialing) return '거는 중' if (isOnCall) return '전화 끊기' return '전화' } const getCallButtonTitle = (isCtiReady: boolean, isDialing: boolean, isEnding: boolean) => { if (!isCtiReady) return '전화 시스템에 연결되어 있지 않습니다.' if (isDialing) return '전화를 거는 중입니다.' if (isEnding) return '통화 종료를 확인하고 있습니다.' return undefined } const FieldRow = ({ label, editable, children }: { label: string; editable?: boolean; children: ReactNode }) => (
{children}
) const ListContactInfoPanel = ({ prjctIdx, sid, itrntPrjctCd, userRole, isEditing, listHeaders, detail, surveys, callerTelNum, resultCodes, formValues, onFieldChange, onSaved, }: ListContactInfoPanelProps) => { const { showDialog } = useDialog() const uploadItemFileMutation = useUploadListItemFileMutation(prjctIdx, sid) const downloadItemFileMutation = useDownloadListItemFileByHeaderMutation(prjctIdx, sid) const fileInputRef = useRef(null) const [uploadingHeaderId, setUploadingHeaderId] = useState(null) const [sendTarget, setSendTarget] = useState<{ channel: MessageSendChannel; headerId: number } | null>(null) const [qrTargetUrl, setQrTargetUrl] = useState('') const ctiConnectionStatus = useCtiConnectionStatus() const isCtiReady = ctiConnectionStatus === 'open' const ctiCallPhase = useCtiCallPhase() const isDialing = ctiCallPhase === 'dialing' const isEnding = ctiCallPhase === 'ending' const isOnCall = ctiCallPhase === 'active' || isEnding const visibleHeaders = listHeaders .filter((header) => isListFieldVisibleForContact(header)) .sort((a, b) => { if (a.listHderTy === ListHeaderConst.MEMO) return 1 if (b.listHderTy === ListHeaderConst.MEMO) return -1 return a.listHderOrder - b.listHderOrder }) const surveyResultByIdx = new Map((detail?.surveyResults ?? []).map((result) => [result.prjctSurveyIdx, result])) const surveyUrlValues = buildListContactVariableValues(listHeaders, detail, sid) // 알림톡은 #{설문명}도 설문 URL로 치환한다. 문자·메일은 기존처럼 헤더 값만 쓴다. const kkoVariableValues = buildKkoVariableValues(surveyUrlValues, surveys) const handleStartSurvey = (survey: ProjectSurvey) => { if (!survey.surveyUrl) return window.open(substituteVariables(survey.surveyUrl, surveyUrlValues), '_blank', 'noopener,noreferrer') } const handleShowQr = (survey: ProjectSurvey) => { if (!survey.surveyUrl) return setQrTargetUrl(substituteVariables(survey.surveyUrl, surveyUrlValues)) showDialog('surveyQrDialog') } const handleSend = (channel: MessageSendChannel, headerId: number) => { setSendTarget({ channel, headerId }) showDialog('singleMessageSendDialog') } const handleCall = (telNum: string, listHderId: number) => { if (!isCtiReady || isDialing || isEnding) return if (isOnCall) { ctiClient.hangup() return } if (!telNum.trim()) { alert('전화번호가 없습니다.') return } // CTI는 ANINumber가 비어 있으면 Default 발신번호로 건다고 하지만, 의도치 않은 번호로 나가지 않도록 막는다. if (!callerTelNum?.trim()) { alert('프로젝트에 발신 번호가 등록되지 않았습니다.') return } ctiClient.makeCall({ dialNumber: telNum, prjctIdx, sid, listHderId, aniNumber: callerTelNum ?? '' }) } const handlePickFile = (listHderId: number) => { setUploadingHeaderId(listHderId) fileInputRef.current?.click() } const handleDownloadFile = async (listHderId: number, fileName: string) => { const buffer = await downloadItemFileMutation.mutateAsync(listHderId) downloadFile(new Blob([buffer], { type: 'application/octet-stream' }), fileName) } const handleFileSelected = async (e: React.ChangeEvent) => { const file = e.target.files?.[0] e.target.value = '' if (!file || uploadingHeaderId === null) return await uploadItemFileMutation.mutateAsync({ listHderId: uploadingHeaderId, file }) setUploadingHeaderId(null) alert('파일이 업로드되었습니다.') onSaved?.() } return (

리스트 정보

{visibleHeaders.map((header) => { if (header.listHderTy === ListHeaderConst.SID) return null const canEditField = isListFieldEditableForContact(header, userRole) const editable = isEditing && canEditField const value = formValues[header.listHderId] ?? '' const onChange = (next: string) => onFieldChange(header.listHderId, next) if (header.listHderTy === ListHeaderConst.MEMO) { return (