401 lines
13 KiB
TypeScript
401 lines
13 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { RefreshCw } from 'lucide-react'
|
|
import { Button } from '@/components/ui/button'
|
|
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
|
|
import { KriDateTimePicker } from '@/components/common/KriDateTimePicker'
|
|
import CollapsibleHistorySection from '@/components/list-contact/CollapsibleHistorySection'
|
|
import { useAddListEventMutation, useListEventHistoryQuery, useReserveMutation } from '@/query/list'
|
|
import { useSendResultBySidQuery } from '@/query/message'
|
|
import { useInternalInfoQuery } from '@/query/internal'
|
|
import { clientEnv } from '@/lib/env.client'
|
|
import { ChannelType } from '@kri/common/noti-hub'
|
|
import type { SendResultInfo } from '@/types/message'
|
|
import { formatKst } from '@kri/common'
|
|
import { type ResultCode, ResultCodeType } from '@/types/project'
|
|
import { CONTACT_INTERVIEWER_ROLE, FIELD_INTERVIEWER_ROLE } from '@/const/userConst'
|
|
import { RESULT_CODE_RESERVATION, RESULT_CODE_UNUSED } from '@/const/projectConst'
|
|
import { cn } from '@/lib/utils'
|
|
import { getNearestFutureDateTime } from '@/lib/datetime'
|
|
import { useCtiLastCallHistId } from '@/hooks/useCtiLastCallHistId'
|
|
import { getCurrentPositionMeta } from '@kri/common/browser'
|
|
import type { listHeader, ListSearchWithFullResult } from '@/types/list'
|
|
import { buildListContactVariableValues } from '@/components/list-contact/listContactVariables'
|
|
import { substituteVariables } from '@/lib/surveyUrl'
|
|
import { sanitizeHtml } from '@/components/kri-editor/utils/sanitizeHtml'
|
|
|
|
const escapeHtml = (value: string) =>
|
|
value.replace(
|
|
/[&<>"']/g,
|
|
(character) =>
|
|
({
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
'"': '"',
|
|
"'": ''',
|
|
})[character] ?? character,
|
|
)
|
|
|
|
const RefreshIconButton = ({ onRefresh, isFetching }: { onRefresh: () => void; isFetching: boolean }) => (
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
aria-label="새로고침"
|
|
disabled={isFetching}
|
|
onClick={(e) => {
|
|
e.stopPropagation()
|
|
onRefresh()
|
|
}}
|
|
>
|
|
<RefreshCw className={cn('h-4 w-4', isFetching && 'animate-spin')} />
|
|
</Button>
|
|
)
|
|
|
|
type ContactEventSectionProps = {
|
|
title: string
|
|
prjctIdx: number
|
|
sid: string
|
|
resultCodeType: ResultCodeType
|
|
resultCodes: ResultCode[]
|
|
showReservation?: boolean
|
|
showInput?: boolean
|
|
submitting: boolean
|
|
onSubmit: (resultCd: number, memo: string, resveDt: string, listCallHistIdx: number | null) => Promise<void>
|
|
}
|
|
|
|
const EventHistoryList = ({
|
|
isLoading,
|
|
history,
|
|
resultCodeType,
|
|
resultCodes,
|
|
}: {
|
|
isLoading: boolean
|
|
history: ReturnType<typeof useListEventHistoryQuery>['data']
|
|
resultCodeType: ResultCodeType
|
|
resultCodes: ResultCode[]
|
|
}) => {
|
|
const filteredHistory = (history ?? [])
|
|
.filter((item) => item.eventTy === resultCodeType)
|
|
.sort((a, b) => new Date(b.regDt).getTime() - new Date(a.regDt).getTime())
|
|
|
|
const resolveLabel = (resultCd: number) =>
|
|
resultCodes.find((code) => code.resultCdTy === resultCodeType && code.resultCd === resultCd)?.resultCdLbl ??
|
|
String(resultCd)
|
|
|
|
if (isLoading) {
|
|
return <div className="rounded-md border p-4 text-center text-sm text-muted-foreground">불러오는 중...</div>
|
|
}
|
|
|
|
if (filteredHistory.length === 0) {
|
|
return <div className="rounded-md border p-4 text-center text-sm text-muted-foreground">이력이 없습니다.</div>
|
|
}
|
|
|
|
return (
|
|
<div className="max-h-60 overflow-y-auto rounded-md border">
|
|
<table className="w-full text-sm">
|
|
<thead className="sticky top-0 bg-muted">
|
|
<tr>
|
|
<th className="p-2 text-left">결과</th>
|
|
<th className="p-2 text-left">메모</th>
|
|
<th className="p-2 text-left">등록자</th>
|
|
<th className="p-2 text-left">일시</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{filteredHistory.map((item) => (
|
|
<tr key={item.listEventHistIdx} className="border-t">
|
|
<td className="p-2">
|
|
{item.resultCd}.{resolveLabel(item.resultCd)}
|
|
</td>
|
|
<td className="p-2">{item.memo || '-'}</td>
|
|
<td className="p-2">{item.regUser}</td>
|
|
<td className="p-2">{formatKst(item.regDt, 'dateHourMin')}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const ContactEventSection = ({
|
|
title,
|
|
prjctIdx,
|
|
sid,
|
|
resultCodeType,
|
|
resultCodes,
|
|
showReservation = false,
|
|
showInput = true,
|
|
submitting,
|
|
onSubmit,
|
|
}: ContactEventSectionProps) => {
|
|
const [resultCd, setResultCd] = useState('')
|
|
const [memo, setMemo] = useState('')
|
|
const [resveDt, setResveDt] = useState('')
|
|
|
|
const codes = resultCodes.filter((code) => code.resultCdTy === resultCodeType && code.resultCd !== RESULT_CODE_UNUSED)
|
|
const isReservationSelected = resultCd === String(RESULT_CODE_RESERVATION)
|
|
const isContactType = resultCodeType === ResultCodeType.CONTACT
|
|
|
|
const historyQuery = useListEventHistoryQuery(prjctIdx, sid, true)
|
|
const ctiLastCallHistId = useCtiLastCallHistId(prjctIdx, sid)
|
|
const listCallHistIdx = isContactType ? ctiLastCallHistId : null
|
|
|
|
const handleResultCdChange = (next: string) => {
|
|
setResultCd(next)
|
|
if (next === String(RESULT_CODE_RESERVATION) && !resveDt) {
|
|
setResveDt(getNearestFutureDateTime())
|
|
}
|
|
}
|
|
|
|
const handleAdd = async () => {
|
|
if (!resultCd) {
|
|
alert('결과 코드를 선택해주세요.')
|
|
return
|
|
}
|
|
if (isReservationSelected && !resveDt) {
|
|
alert('예약 날짜/시간을 선택해주세요.')
|
|
return
|
|
}
|
|
if (isContactType && !listCallHistIdx) {
|
|
if (!confirm('통화 기록이 없습니다. 계속 진행하시겠습니까?')) return
|
|
}
|
|
|
|
await onSubmit(Number(resultCd), memo, isReservationSelected ? resveDt : '', listCallHistIdx)
|
|
setMemo('')
|
|
setResveDt('')
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-3 rounded-lg border bg-white p-4">
|
|
<h2 className="text-lg font-bold">{title}</h2>
|
|
|
|
{showInput ? (
|
|
<>
|
|
<RadioGroup value={resultCd} onValueChange={handleResultCdChange} className="grid grid-cols-2 gap-2">
|
|
{codes.map((code) => (
|
|
<label key={code.resultCdUUID} className="flex items-center gap-2 text-sm font-bold cursor-pointer">
|
|
<RadioGroupItem value={String(code.resultCd)} />
|
|
{code.resultCd}.{code.resultCdLbl}
|
|
</label>
|
|
))}
|
|
</RadioGroup>
|
|
|
|
<textarea
|
|
placeholder="메모"
|
|
value={memo}
|
|
onChange={(e) => setMemo(e.target.value)}
|
|
className="flex min-h-[70px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
|
|
/>
|
|
|
|
{showReservation && isReservationSelected ? (
|
|
<KriDateTimePicker value={resveDt} onChange={setResveDt} placeholder="예약 날짜/시간" />
|
|
) : null}
|
|
|
|
<Button type="button" className="w-full" disabled={submitting} onClick={handleAdd}>
|
|
{title} 추가
|
|
</Button>
|
|
</>
|
|
) : null}
|
|
|
|
<CollapsibleHistorySection
|
|
title={`${title}(최근순)`}
|
|
showDivider={showInput}
|
|
headerExtra={<RefreshIconButton onRefresh={() => historyQuery.refetch()} isFetching={historyQuery.isFetching} />}
|
|
>
|
|
<EventHistoryList isLoading={historyQuery.isLoading} history={historyQuery.data} resultCodeType={resultCodeType} resultCodes={resultCodes} />
|
|
</CollapsibleHistorySection>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const SendResultTable = ({ isLoading, results }: { isLoading: boolean; results: SendResultInfo[] }) => {
|
|
if (isLoading) {
|
|
return <div className="rounded-md border p-4 text-center text-sm text-muted-foreground">불러오는 중...</div>
|
|
}
|
|
|
|
if (results.length === 0) {
|
|
return <div className="rounded-md border p-4 text-center text-sm text-muted-foreground">발송 이력이 없습니다.</div>
|
|
}
|
|
|
|
return (
|
|
<div className="max-h-60 overflow-y-auto rounded-md border">
|
|
<table className="w-full text-sm">
|
|
<thead className="sticky top-0 bg-muted">
|
|
<tr>
|
|
<th className="p-2 text-left">수신처</th>
|
|
<th className="p-2 text-left">결과</th>
|
|
<th className="p-2 text-left">발송일시</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{results.map((item) => (
|
|
<tr key={item.cmid} className="border-t">
|
|
<td className="p-2">{item.destEml || item.destTel || '-'}</td>
|
|
<td className="p-2">{item.resultViewLabel || item.resultLabel || item.resultCode}</td>
|
|
<td className="p-2">{formatKst(item.sendDt, 'dateHourMin')}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const SendResultHistory = ({ sid, prjctCd }: { sid: string; prjctCd: string }) => {
|
|
const [tab, setTab] = useState<'email' | 'sms'>('email')
|
|
|
|
const emailQuery = useSendResultBySidQuery(sid, prjctCd, clientEnv.REQUEST_SVC, ChannelType.EMAIL, tab === 'email')
|
|
const smsQuery = useSendResultBySidQuery(sid, prjctCd, clientEnv.REQUEST_SVC, ChannelType.SMS, tab === 'sms')
|
|
const activeQuery = tab === 'email' ? emailQuery : smsQuery
|
|
|
|
const handleTabClick = (e: React.MouseEvent, next: 'email' | 'sms') => {
|
|
e.stopPropagation()
|
|
setTab(next)
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-2 rounded-lg border bg-white p-4">
|
|
<CollapsibleHistorySection
|
|
title="발송 현황(최근순)"
|
|
showDivider={false}
|
|
headerExtra={
|
|
<div className="flex items-center gap-1">
|
|
<Button
|
|
type="button"
|
|
size="sm"
|
|
variant={tab === 'email' ? 'default' : 'outline'}
|
|
onClick={(e) => handleTabClick(e, 'email')}
|
|
>
|
|
이메일
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
size="sm"
|
|
variant={tab === 'sms' ? 'default' : 'outline'}
|
|
onClick={(e) => handleTabClick(e, 'sms')}
|
|
>
|
|
문자
|
|
</Button>
|
|
<RefreshIconButton onRefresh={() => activeQuery.refetch()} isFetching={activeQuery.isFetching} />
|
|
</div>
|
|
}
|
|
>
|
|
<SendResultTable isLoading={activeQuery.isLoading} results={activeQuery.data ?? []} />
|
|
</CollapsibleHistorySection>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export type ListContactEventPanelProps = {
|
|
prjctIdx: number
|
|
sid: string
|
|
prjctCd: string
|
|
userId: string
|
|
userRole: number
|
|
resultCodes: ResultCode[]
|
|
useFF: boolean
|
|
useContactSenario: boolean
|
|
contactSenario?: string
|
|
listHeaders: listHeader[]
|
|
detail?: ListSearchWithFullResult
|
|
}
|
|
|
|
const ListContactEventPanel = ({
|
|
prjctIdx,
|
|
sid,
|
|
prjctCd,
|
|
userId,
|
|
userRole,
|
|
resultCodes,
|
|
useFF,
|
|
useContactSenario,
|
|
contactSenario,
|
|
listHeaders,
|
|
detail,
|
|
}: ListContactEventPanelProps) => {
|
|
const addListEventMutation = useAddListEventMutation(prjctIdx, sid)
|
|
const reserveMutation = useReserveMutation(prjctIdx, sid)
|
|
const internalInfoQuery = useInternalInfoQuery()
|
|
const showContactInput = userRole !== FIELD_INTERVIEWER_ROLE
|
|
const showInterviewInput = userRole !== CONTACT_INTERVIEWER_ROLE
|
|
const contactSenarioValues = Object.fromEntries(
|
|
Object.entries(buildListContactVariableValues(listHeaders, detail, sid)).map(([key, value]) => [
|
|
key,
|
|
escapeHtml(value),
|
|
]),
|
|
)
|
|
const resolvedContactSenario = contactSenario
|
|
? sanitizeHtml(substituteVariables(contactSenario, contactSenarioValues))
|
|
: ''
|
|
|
|
const handleAddEvent =
|
|
(resultCodeType: ResultCodeType) =>
|
|
async (resultCd: number, memo: string, resveDt: string, listCallHistIdx: number | null) => {
|
|
const geoMeta = await getCurrentPositionMeta()
|
|
|
|
await addListEventMutation.mutateAsync({
|
|
prjctIdx,
|
|
sid,
|
|
resultCodeType,
|
|
resultCd,
|
|
listCallHistIdx,
|
|
meta: {
|
|
...geoMeta,
|
|
remoteAddr: internalInfoQuery.data?.clientIp,
|
|
},
|
|
memo,
|
|
})
|
|
|
|
if (resveDt) {
|
|
await reserveMutation.mutateAsync({ prjctIdx, sid, userId, resveDt })
|
|
}
|
|
|
|
alert('등록되었습니다.')
|
|
}
|
|
|
|
return (
|
|
<div className="h-[80vh] space-y-4 overflow-y-auto pr-1">
|
|
<ContactEventSection
|
|
title="컨택결과"
|
|
prjctIdx={prjctIdx}
|
|
sid={sid}
|
|
resultCodeType={ResultCodeType.CONTACT}
|
|
resultCodes={resultCodes}
|
|
showReservation
|
|
showInput={showContactInput}
|
|
submitting={addListEventMutation.isPending || reserveMutation.isPending}
|
|
onSubmit={handleAddEvent(ResultCodeType.CONTACT)}
|
|
/>
|
|
|
|
<SendResultHistory sid={sid} prjctCd={prjctCd} />
|
|
|
|
{useFF &&
|
|
<ContactEventSection
|
|
title="인터뷰 요원 확인사항"
|
|
prjctIdx={prjctIdx}
|
|
sid={sid}
|
|
resultCodeType={ResultCodeType.INTERVIEW}
|
|
resultCodes={resultCodes}
|
|
showInput={showInterviewInput}
|
|
submitting={addListEventMutation.isPending}
|
|
onSubmit={handleAddEvent(ResultCodeType.INTERVIEW)}
|
|
/>
|
|
}
|
|
|
|
|
|
{useContactSenario && resolvedContactSenario ? (
|
|
<div className="space-y-2 rounded-lg border bg-white p-4">
|
|
<h2 className="text-lg font-bold border-b pb-2">컨택 시나리오</h2>
|
|
<div className="text-base leading-relaxed" dangerouslySetInnerHTML={{ __html: resolvedContactSenario }} />
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default ListContactEventPanel
|