260714_01_ENH: 테스트 서버에서 기동 후 중간 테스트를 위한 커밋
This commit is contained in:
69
src/components/dialog/SurveyQrDialog.tsx
Normal file
69
src/components/dialog/SurveyQrDialog.tsx
Normal file
@@ -0,0 +1,69 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { toDataURL } from 'qrcode'
|
||||
import { KriDialog } from '@/components/common/KriDialog'
|
||||
import { useDialog } from '@/hooks/useDialog'
|
||||
import type { DialogId } from '@/store/jotai/dialog'
|
||||
|
||||
export type SurveyQrDialogProps = {
|
||||
dialogId?: DialogId
|
||||
open?: boolean
|
||||
onOpenChange?: (open: boolean) => void
|
||||
url: string
|
||||
}
|
||||
|
||||
const SurveyQrDialog = ({ dialogId = 'surveyQrDialog', open, onOpenChange, url }: SurveyQrDialogProps) => {
|
||||
const { isDialogOpen, showDialog, hideDialog } = useDialog()
|
||||
const [qrDataUrl, setQrDataUrl] = useState<string | null>(null)
|
||||
|
||||
const resolvedOpen = open ?? isDialogOpen(dialogId)
|
||||
|
||||
const handleOpenChange = (nextOpen: boolean) => {
|
||||
onOpenChange?.(nextOpen)
|
||||
if (nextOpen) {
|
||||
showDialog(dialogId)
|
||||
return
|
||||
}
|
||||
hideDialog(dialogId)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!resolvedOpen || !url) {
|
||||
setQrDataUrl(null)
|
||||
return
|
||||
}
|
||||
|
||||
let cancelled = false
|
||||
toDataURL(url, { width: 240 }).then((dataUrl) => {
|
||||
if (!cancelled) setQrDataUrl(dataUrl)
|
||||
})
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [resolvedOpen, url])
|
||||
|
||||
return (
|
||||
<KriDialog
|
||||
open={resolvedOpen}
|
||||
onOpenChange={handleOpenChange}
|
||||
title={<span className="text-xl">설문 QR코드</span>}
|
||||
contentClassName="sm:max-w-[360px]"
|
||||
>
|
||||
<div className="flex flex-col items-center gap-3 py-4">
|
||||
{qrDataUrl ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img src={qrDataUrl} alt="설문 QR코드" width={240} height={240} />
|
||||
) : (
|
||||
<div className="flex h-[240px] w-[240px] items-center justify-center text-sm text-muted-foreground">
|
||||
QR코드 생성 중...
|
||||
</div>
|
||||
)}
|
||||
<p className="break-all text-center text-xs text-muted-foreground">{url}</p>
|
||||
</div>
|
||||
</KriDialog>
|
||||
)
|
||||
}
|
||||
|
||||
export default SurveyQrDialog
|
||||
Reference in New Issue
Block a user